diff --git a/1.4/Assemblies/RJW_Menstruation.dll b/1.4/Assemblies/RJW_Menstruation.dll index 6a5f83d..eb6d422 100644 Binary files a/1.4/Assemblies/RJW_Menstruation.dll and b/1.4/Assemblies/RJW_Menstruation.dll differ diff --git a/1.4/MilkModule/Assemblies/MilkModule.dll b/1.4/MilkModule/Assemblies/MilkModule.dll index 7337849..78e3a88 100644 Binary files a/1.4/MilkModule/Assemblies/MilkModule.dll and b/1.4/MilkModule/Assemblies/MilkModule.dll differ diff --git a/1.4/source/RJW_Menstruation/RJW_Menstruation/Patch/Biotech_Patch.cs b/1.4/source/RJW_Menstruation/RJW_Menstruation/Patch/Biotech_Patch.cs index 7bbdefb..b1e4f91 100644 --- a/1.4/source/RJW_Menstruation/RJW_Menstruation/Patch/Biotech_Patch.cs +++ b/1.4/source/RJW_Menstruation/RJW_Menstruation/Patch/Biotech_Patch.cs @@ -3,6 +3,7 @@ using System.Linq; using RimWorld; using Verse; using System.Collections.Generic; +using System.Reflection; namespace RJW_Menstruation { @@ -108,4 +109,61 @@ namespace RJW_Menstruation comp.TakeLoosePregnancy(); } } + + [HarmonyPatch(typeof(PregnancyUtility), nameof(PregnancyUtility.TryTerminatePregnancy))] + public class TryTerminatePregnancy_Patch + { + private static Hediff GetEarliestPregnancy(Pawn pawn) + { + Hediff Earliest_Pregnancy = PregnancyUtility.GetPregnancyHediff(pawn); + foreach (HediffComp_Menstruation comp in pawn.GetMenstruationComps()) + { + Hediff pregnancy = comp.Pregnancy; + if (pregnancy == null) continue; + if (Earliest_Pregnancy == null || Earliest_Pregnancy.Severity > pregnancy.Severity) Earliest_Pregnancy = pregnancy; + } + + return Earliest_Pregnancy; + } + + private static readonly MethodInfo GetPregnancyHediff = AccessTools.Method(typeof(PregnancyUtility), nameof(PregnancyUtility.GetPregnancyHediff), new System.Type[] { typeof(Pawn) }); + + // Also called for Recipe_TerminatePregnancy.ApplyOnPawn + public static IEnumerable Transpiler(IEnumerable instructions) + { + if (GetPregnancyHediff == null) throw new System.InvalidOperationException("GetPregnancyHediff not found"); + foreach (CodeInstruction instruction in instructions) + { + if (instruction.Calls(GetPregnancyHediff)) + yield return CodeInstruction.Call(typeof(TryTerminatePregnancy_Patch), nameof(TryTerminatePregnancy_Patch.GetEarliestPregnancy)); + else yield return instruction; + } + } + + public static void Postfix(bool __result, Pawn pawn) + { + if (__result) + foreach (HediffComp_Menstruation comp in pawn.GetMenstruationComps()) + _ = comp.Pregnancy; // get_Pregnancy will remove the hediff attached to the comp that doesn't have it anymore + } + } + + [HarmonyPatch(typeof(Recipe_TerminatePregnancy), nameof(Recipe_TerminatePregnancy.AvailableOnNow))] + public class TerminatePregnancy_AvailableOnNow_Patch + { + public static void Postfix(ref bool __result, Thing thing) + { + if (!ModsConfig.BiotechActive || !(thing is Pawn pawn)) return; + __result |= pawn.GetMenstruationComps().Any(comp => comp.Pregnancy != null); + } + } + + [HarmonyPatch(typeof(Recipe_TerminatePregnancy), nameof(Recipe_TerminatePregnancy.ApplyOnPawn))] + public class TerminatePregnancy_ApplyOnPawn_Patch + { + public static IEnumerable Transpiler(IEnumerable instructions) + { + return TryTerminatePregnancy_Patch.Transpiler(instructions); + } + } } \ No newline at end of file diff --git a/changelogs.txt b/changelogs.txt index 45b5b39..daa6265 100644 --- a/changelogs.txt +++ b/changelogs.txt @@ -1,5 +1,6 @@ Version 1.0.8.4 - Newborns should now be baseliners if there are no xenotypes to inherit. + - The Biotech terminate pregnancy recipe can now terminate a menstruation pregnancy, too. Version 1.0.8.3 - Compatibility update for RJW 5.3.0.9