From 8c51c3fa5943ad8688d8bf139425af9fe53a1d7c Mon Sep 17 00:00:00 2001 From: callavico <131923070+callavico@users.noreply.github.com> Date: Wed, 26 Apr 2023 20:54:49 -0400 Subject: [PATCH] Add a helper that removes all hediffs associated with rjw sex changes --- Source/Genes/Gender/GenderUtility.cs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/Source/Genes/Gender/GenderUtility.cs b/Source/Genes/Gender/GenderUtility.cs index ea0e1d6..3297a0b 100644 --- a/Source/Genes/Gender/GenderUtility.cs +++ b/Source/Genes/Gender/GenderUtility.cs @@ -1,9 +1,10 @@ using Verse; using rjw; using RimWorld; -using System.Collections; using System.Linq; using System; +using System.Collections.Generic; +using HarmonyLib; namespace RJW_Genes { @@ -69,5 +70,29 @@ namespace RJW_Genes // Force Redraw at the spot pawn.Drawer.renderer.graphics.SetAllGraphicsDirty(); } + + // Fetch these once at load time because they don't change inside RJW + private static readonly List wasSexThoughts = Traverse.Create(typeof(GenderHelper)).Field("old_sex_list").GetValue>(); + private static readonly List sexChangeThoughts = Traverse.Create(typeof(GenderHelper)).Field("SexChangeThoughts").GetValue>(); + + public static void RemoveAllSexChangeThoughts(Pawn pawn) + { + // Shouldn't ever be true in the normal case, but this stops someone from calling this with an incorrect setup + if (pawn?.health == null) + return; + + if(wasSexThoughts == null || sexChangeThoughts == null || !wasSexThoughts.Any() || !sexChangeThoughts.Any()) + { + Log.Error($"Couldn't get values from RJW.\nold_sex_list: {wasSexThoughts.ToStringSafeEnumerable()}\nSexChangeThoughts: {sexChangeThoughts.ToStringSafeEnumerable()}"); + return; + } + + foreach(var def in wasSexThoughts.Concat(sexChangeThoughts)) + { + var hediff = pawn.health.hediffSet.GetFirstHediffOfDef(def); + if (hediff != null) + pawn.health.RemoveHediff(hediff); + } + } } }