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 1/3] 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); + } + } } } From 55554df7e2ca5a08ff30d04d368ee4cd4a4d0b13 Mon Sep 17 00:00:00 2001 From: callavico <131923070+callavico@users.noreply.github.com> Date: Wed, 26 Apr 2023 20:56:55 -0400 Subject: [PATCH 2/3] Remove hediffs during Pawn Generation Calls RemoveAllSexChangeThoughts via a notify method that we call after PawnGenerator creates genes --- Source/Genes/Gender/Gene_FemaleOnly.cs | 10 ++++++++- Source/Genes/Gender/Gene_MaleOnly.cs | 8 ++++++- Source/Genes/Patch_AddNotifyOnGeneration.cs | 23 +++++++++++++++++++++ Source/Genes/RJW_Gene.cs | 8 +++++++ Source/Rjw-Genes.csproj | 1 + 5 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 Source/Genes/Patch_AddNotifyOnGeneration.cs diff --git a/Source/Genes/Gender/Gene_FemaleOnly.cs b/Source/Genes/Gender/Gene_FemaleOnly.cs index 0cfbe0d..110370f 100644 --- a/Source/Genes/Gender/Gene_FemaleOnly.cs +++ b/Source/Genes/Gender/Gene_FemaleOnly.cs @@ -4,7 +4,7 @@ using rjw; namespace RJW_Genes { - public class Gene_FemaleOnly : Gene + public class Gene_FemaleOnly : RJW_Gene { public override void PostMake() { @@ -24,6 +24,8 @@ namespace RJW_Genes private void AdjustPawnToFemale() { + Log.Message($"Gene_FemaleOnly AdjustPawnToFemale | {pawn} | {pawn.gender}"); + // Here we really use the Gender.Female and not our helper IsFemale(pawn) if (pawn.gender == Gender.Female) return; @@ -38,5 +40,11 @@ namespace RJW_Genes } } + public override void Notify_OnPawnGeneration() + { + base.Notify_OnPawnGeneration(); + // If this is Pawn generation, then we can assume that the pawn was never any gender other than female, so they shouldn't have sex change thoughts. + GenderUtility.RemoveAllSexChangeThoughts(pawn); + } } } diff --git a/Source/Genes/Gender/Gene_MaleOnly.cs b/Source/Genes/Gender/Gene_MaleOnly.cs index 51806cf..eba7ce5 100644 --- a/Source/Genes/Gender/Gene_MaleOnly.cs +++ b/Source/Genes/Gender/Gene_MaleOnly.cs @@ -4,7 +4,7 @@ using rjw; namespace RJW_Genes { - public class Gene_MaleOnly : Gene + public class Gene_MaleOnly : RJW_Gene { public override void PostMake() { @@ -38,5 +38,11 @@ namespace RJW_Genes } } + public override void Notify_OnPawnGeneration() + { + base.Notify_OnPawnGeneration(); + // If this is Pawn generation, then we can assume that the pawn was never any gender other than male, so they shouldn't have sex change thoughts. + GenderUtility.RemoveAllSexChangeThoughts(pawn); + } } } diff --git a/Source/Genes/Patch_AddNotifyOnGeneration.cs b/Source/Genes/Patch_AddNotifyOnGeneration.cs new file mode 100644 index 0000000..3395a81 --- /dev/null +++ b/Source/Genes/Patch_AddNotifyOnGeneration.cs @@ -0,0 +1,23 @@ +using HarmonyLib; +using System.Linq; +using Verse; + +namespace RJW_Genes.Genes +{ + [HarmonyPatch] + public static class Patch_AddNotifyOnGeneration + { + [HarmonyPatch(typeof(PawnGenerator), "GenerateGenes")] + [HarmonyPostfix] + public static void PawnGenerator_GenerateGenes_Postfix(Pawn pawn) + { + if (pawn.genes == null) return; + + foreach(var gene in pawn.genes.GenesListForReading) + { + if (gene is RJW_Gene rjwGene) + rjwGene.Notify_OnPawnGeneration(); + } + } + } +} diff --git a/Source/Genes/RJW_Gene.cs b/Source/Genes/RJW_Gene.cs index d03440c..dfa05bf 100644 --- a/Source/Genes/RJW_Gene.cs +++ b/Source/Genes/RJW_Gene.cs @@ -12,5 +12,13 @@ namespace RJW_Genes if (GenitaliaUtility.PawnStillNeedsGenitalia(pawn)) Sexualizer.sexualize_pawn(pawn); } + + /// + /// Executed via PawnGenerator.GenerateGenes at Pawn generation + /// Allows for execution of code that should only happen during PawnGeneration + /// + public virtual void Notify_OnPawnGeneration() + { + } } } diff --git a/Source/Rjw-Genes.csproj b/Source/Rjw-Genes.csproj index 68a3e34..d85935b 100644 --- a/Source/Rjw-Genes.csproj +++ b/Source/Rjw-Genes.csproj @@ -131,6 +131,7 @@ + From 7f83731a6947bf98dad65d4b92c33dc6a16f92ae Mon Sep 17 00:00:00 2001 From: callavico <131923070+callavico@users.noreply.github.com> Date: Fri, 28 Apr 2023 01:43:30 -0400 Subject: [PATCH 3/3] Remove unnecessary debug log --- Source/Genes/Gender/Gene_FemaleOnly.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Source/Genes/Gender/Gene_FemaleOnly.cs b/Source/Genes/Gender/Gene_FemaleOnly.cs index 110370f..bc8ac20 100644 --- a/Source/Genes/Gender/Gene_FemaleOnly.cs +++ b/Source/Genes/Gender/Gene_FemaleOnly.cs @@ -24,8 +24,6 @@ namespace RJW_Genes private void AdjustPawnToFemale() { - Log.Message($"Gene_FemaleOnly AdjustPawnToFemale | {pawn} | {pawn.gender}"); - // Here we really use the Gender.Female and not our helper IsFemale(pawn) if (pawn.gender == Gender.Female) return;