Fix error when trying to terminate a menstruation pregnancy

This commit is contained in:
lutepickle 2023-01-11 07:32:50 -08:00
parent da7284b909
commit 71575c671e
3 changed files with 32 additions and 15 deletions

Binary file not shown.

View File

@ -4,6 +4,8 @@ using RimWorld;
using Verse;
using System.Collections.Generic;
using System.Reflection;
using System;
using System.Reflection.Emit;
namespace RJW_Menstruation
{
@ -120,9 +122,22 @@ namespace RJW_Menstruation
}
}
[HarmonyPatch(typeof(PregnancyUtility), nameof(PregnancyUtility.TryTerminatePregnancy))]
public class TryTerminatePregnancy_Patch
[HarmonyDebug]
[HarmonyPatch]
public class TerminatePregnancy_Patch
{
public static IEnumerable<MethodBase> TargetMethods()
{
yield return AccessTools.Method(typeof(PregnancyUtility), nameof(PregnancyUtility.TryTerminatePregnancy));
yield return AccessTools.Method(typeof(Recipe_TerminatePregnancy), nameof(Recipe_TerminatePregnancy.ApplyOnPawn));
}
private static PregnancyAttitude? GetAttitude(Hediff pregnancy)
{
if (pregnancy is Hediff_Pregnant preg) return preg.Attitude;
else return null;
}
private static Hediff GetEarliestPregnancy(Pawn pawn)
{
Hediff Earliest_Pregnancy = PregnancyUtility.GetPregnancyHediff(pawn);
@ -136,20 +151,30 @@ namespace RJW_Menstruation
return Earliest_Pregnancy;
}
private static readonly MethodInfo GetPregnancyHediff = AccessTools.Method(typeof(PregnancyUtility), nameof(PregnancyUtility.GetPregnancyHediff), new System.Type[] { typeof(Pawn) });
private static readonly MethodInfo GetPregnancyHediff = AccessTools.Method(typeof(PregnancyUtility), nameof(PregnancyUtility.GetPregnancyHediff), new Type[] { typeof(Pawn) });
private static readonly MethodInfo Get_Attitude = AccessTools.DeclaredPropertyGetter(typeof(Hediff_Pregnant), nameof(Hediff_Pregnant.Attitude));
// Also called for Recipe_TerminatePregnancy.ApplyOnPawn
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
if (GetPregnancyHediff == null || GetPregnancyHediff.ReturnType != typeof(Hediff)) throw new System.InvalidOperationException("GetPregnancyHediff not found");
if (GetPregnancyHediff == null || GetPregnancyHediff.ReturnType != typeof(Hediff)) throw new InvalidOperationException("GetPregnancyHediff not found");
if (Get_Attitude == null || Nullable.GetUnderlyingType(Get_Attitude.ReturnType) != typeof(PregnancyAttitude)) throw new InvalidOperationException("get_Attitude not found");
foreach (CodeInstruction instruction in instructions)
{
if (instruction.Calls(GetPregnancyHediff))
yield return CodeInstruction.Call(typeof(TryTerminatePregnancy_Patch), nameof(TryTerminatePregnancy_Patch.GetEarliestPregnancy));
yield return CodeInstruction.Call(typeof(TerminatePregnancy_Patch), nameof(TerminatePregnancy_Patch.GetEarliestPregnancy));
// Menstruation pregnancies don't have an attitude, so skip the cast to Hediff_Pregnant and call a version that handles it
else if (instruction.opcode == OpCodes.Castclass && (Type)instruction.operand == typeof(Hediff_Pregnant))
yield return new CodeInstruction(OpCodes.Nop);
else if (instruction.Calls(Get_Attitude))
yield return CodeInstruction.Call(typeof(TerminatePregnancy_Patch), nameof(TerminatePregnancy_Patch.GetAttitude));
else yield return instruction;
}
}
}
[HarmonyPatch(typeof(PregnancyUtility), nameof(PregnancyUtility.TryTerminatePregnancy))]
public class PregnancyUtility_TryTerminatePregnancy_Patch
{
public static void Postfix(bool __result, Pawn pawn)
{
if (__result)
@ -168,15 +193,6 @@ namespace RJW_Menstruation
}
}
[HarmonyPatch(typeof(Recipe_TerminatePregnancy), nameof(Recipe_TerminatePregnancy.ApplyOnPawn))]
public class TerminatePregnancy_ApplyOnPawn_Patch
{
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
return TryTerminatePregnancy_Patch.Transpiler(instructions);
}
}
[HarmonyPatch(typeof(Pawn_GeneTracker), "Notify_GenesChanged")]
public class Notify_GenesChanged_Patch
{

View File

@ -1,5 +1,6 @@
Version 1.0.8.6
- Updated Traditional Chinese translation by Hydrogen.
- Fix error when trying to terminate a non-Biotech pregnancy.
- Added several menstruation-related genes.
- Added experimental support for twins and hybrids with Biotech pregnancies, disabled by default.