From 19ddf5feaad68a9baeef6b1dfdecc2d05616b1ea Mon Sep 17 00:00:00 2001 From: meiskam Date: Fri, 12 Feb 2021 13:34:30 -0500 Subject: [PATCH 1/2] Add Rules button to Health Overview, closes #5 --- ...World_HealthCardUtility_DrawOverviewTab.cs | 31 +++++++++++++++++++ ...awn_FoodRestrictionTracker_Configurable.cs | 18 ----------- ...strictionTracker_CurrentFoodRestriction.cs | 2 +- Source/PawnRules.csproj | 2 +- 4 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 Source/Patch/RimWorld_HealthCardUtility_DrawOverviewTab.cs delete mode 100644 Source/Patch/RimWorld_Pawn_FoodRestrictionTracker_Configurable.cs diff --git a/Source/Patch/RimWorld_HealthCardUtility_DrawOverviewTab.cs b/Source/Patch/RimWorld_HealthCardUtility_DrawOverviewTab.cs new file mode 100644 index 0000000..f3309fb --- /dev/null +++ b/Source/Patch/RimWorld_HealthCardUtility_DrawOverviewTab.cs @@ -0,0 +1,31 @@ +using HarmonyLib; +using PawnRules.Data; +using PawnRules.Interface; +using RimWorld; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using Verse; + +namespace PawnRules.Patch +{ + [HarmonyPatch(typeof(HealthCardUtility), "DrawOverviewTab")] + internal static class RimWorld_HealthCardUtility_DrawOverviewTab + { + private static void Postfix(ref float __result, UnityEngine.Rect leftRect, Pawn pawn, float curY) + { + if (!Registry.IsActive) { return; } + + FloatMenu overviewWindowStack = Find.WindowStack.FloatMenu; + + if (overviewWindowStack == null) { return; } + + List overviewWindowStackOptions = (List) typeof(FloatMenu).GetField("options", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(overviewWindowStack); + if (overviewWindowStackOptions?.Count > 0 && overviewWindowStackOptions.Last().Label.Equals("ManageFoodRestrictions".Translate())) + { + overviewWindowStack.Close(false); + Dialog_Rules.Open(pawn); + } + } + } +} diff --git a/Source/Patch/RimWorld_Pawn_FoodRestrictionTracker_Configurable.cs b/Source/Patch/RimWorld_Pawn_FoodRestrictionTracker_Configurable.cs deleted file mode 100644 index 565fcfc..0000000 --- a/Source/Patch/RimWorld_Pawn_FoodRestrictionTracker_Configurable.cs +++ /dev/null @@ -1,18 +0,0 @@ -using HarmonyLib; -using PawnRules.Data; -using RimWorld; - -namespace PawnRules.Patch -{ - [HarmonyPatch(typeof(Pawn_FoodRestrictionTracker), "Configurable", MethodType.Getter)] - internal static class RimWorld_Pawn_FoodRestrictionTracker_Configurable - { - private static bool Prefix(ref bool __result) - { - if (!Registry.IsActive) { return true; } - - __result = false; - return false; - } - } -} diff --git a/Source/Patch/RimWorld_Pawn_FoodRestrictionTracker_CurrentFoodRestriction.cs b/Source/Patch/RimWorld_Pawn_FoodRestrictionTracker_CurrentFoodRestriction.cs index 7f6b650..9d5346c 100644 --- a/Source/Patch/RimWorld_Pawn_FoodRestrictionTracker_CurrentFoodRestriction.cs +++ b/Source/Patch/RimWorld_Pawn_FoodRestrictionTracker_CurrentFoodRestriction.cs @@ -11,7 +11,7 @@ private static bool Prefix(ref FoodRestriction __result) { if (!Registry.IsActive) { return true; } - __result = new FoodRestriction(-1, Lang.Get("FoodRestrictionsOverridden")); + __result = new FoodRestriction(-1, Lang.Get("Gizmo.EditRulesLabel")); return false; } } diff --git a/Source/PawnRules.csproj b/Source/PawnRules.csproj index f0c3352..abd3393 100644 --- a/Source/PawnRules.csproj +++ b/Source/PawnRules.csproj @@ -52,6 +52,7 @@ + @@ -94,7 +95,6 @@ - From ac69460520ce3f319de1b6dd3536e1b984009fd3 Mon Sep 17 00:00:00 2001 From: meiskam Date: Fri, 12 Feb 2021 14:25:50 -0500 Subject: [PATCH 2/2] Fixed a few NullReferenceExceptions when CurrentMap is null (caravaning with every settlement abandoned) --- Source/Interface/Dialog_Rules.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Source/Interface/Dialog_Rules.cs b/Source/Interface/Dialog_Rules.cs index 89d2269..300df2b 100644 --- a/Source/Interface/Dialog_Rules.cs +++ b/Source/Interface/Dialog_Rules.cs @@ -136,7 +136,7 @@ private void UpdateSelected() UpdateTemplate(); } - private IEnumerable GetOtherPawnsOfType(bool byKind) => _type == null ? Find.CurrentMap.mapPawns.AllPawns.Where(pawn => (pawn != _pawn) && (pawn.GetTargetType() == _preset.Type) && (!byKind || (pawn.kindDef == _pawn.kindDef))) : Find.CurrentMap.mapPawns.AllPawns.Where(pawn => pawn.GetTargetType() == _type); + private IEnumerable GetOtherPawnsOfType(bool byKind) => _type == null ? Find.CurrentMap?.mapPawns.AllPawns.Where(pawn => (pawn != _pawn) && (pawn.GetTargetType() == _preset.Type) && (!byKind || (pawn.kindDef == _pawn.kindDef))) : Find.CurrentMap?.mapPawns.AllPawns.Where(pawn => pawn.GetTargetType() == _type); private string GetPresetNameDefinite() { @@ -149,9 +149,10 @@ private List GetAssignmentOptions() var options = new List(); var otherPawnsOfType = GetOtherPawnsOfType(false); - if (GetOtherPawnsOfType(false).Any()) { options.Add(new FloatMenuOption(Lang.Get("Dialog_Rules.AssignAll", _preset.Type.LabelPlural.ToLower()), () => AssignAll(false))); } + if (GetOtherPawnsOfType(false)?.Any() == true) { options.Add(new FloatMenuOption(Lang.Get("Dialog_Rules.AssignAll", _preset.Type.LabelPlural.ToLower()), () => AssignAll(false))); } if ((_type == null) && _pawn.RaceProps.Animal && otherPawnsOfType.Any(kind => kind.kindDef == _pawn.kindDef)) { options.Add(new FloatMenuOption(Lang.Get("Dialog_Rules.AssignAll", _pawn.kindDef.GetLabelPlural().ToLower()), () => AssignAll(true))); } - options.AddRange(Find.CurrentMap.mapPawns.AllPawns.Where(pawn => ((_type != null) || (pawn != _pawn)) && (pawn.GetTargetType() == _preset.Type)).Select(pawn => new FloatMenuOption(Lang.Get("Dialog_Rules.AssignSpecific", pawn.Name.ToString().Italic()), () => AssignSpecific(pawn)))); + var toAdd = Find.CurrentMap?.mapPawns.AllPawns.Where(pawn => ((_type != null) || (pawn != _pawn)) && (pawn.GetTargetType() == _preset.Type)).Select(pawn => new FloatMenuOption(Lang.Get("Dialog_Rules.AssignSpecific", pawn.Name.ToString().Italic()), () => AssignSpecific(pawn))); + if (toAdd != null) { options.AddRange(toAdd); } if ((_type == null) && _preset.Selected.IsPreset) { options.Add(new FloatMenuOption(Lang.Get("Dialog_Rules.AssignDefault", _preset.Type.LabelPlural.ToLower()), () => Dialog_Alert.Open(Lang.Get("Dialog_Rules.AssignDefaultConfirm", _preset.Type.LabelPlural.ToLower(), _preset.Selected.Name.Bold()), Dialog_Alert.Buttons.YesNo, () => Registry.SetDefaultRules(_preset.Selected)))); } return options;