using HarmonyLib; using RimWorld; using rjw; using RJWSexperience.Ideology.HistoryEvents; using RJWSexperience.Ideology.Precepts; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Reflection.Emit; using Verse; namespace RJWSexperience.Ideology.Patches { [HarmonyPatch(typeof(MarriageCeremonyUtility), nameof(MarriageCeremonyUtility.Married))] public static class Rimworld_Patch_Marriage { public static void Postfix(Pawn firstPawn, Pawn secondPawn) { RsiHistoryEventDefOf.RSI_NonIncestuosMarriage.RecordEventWithPartner(firstPawn, secondPawn); RsiHistoryEventDefOf.RSI_NonIncestuosMarriage.RecordEventWithPartner(secondPawn, firstPawn); } } [HarmonyPatch(typeof(RitualOutcomeEffectWorker_FromQuality), "GiveMemoryToPawn")] public static class Rimworld_Patch_RitualOutcome_DontGiveMemoryToAnimals { public static bool Prefix(Pawn pawn) { return !pawn.IsAnimal(); } } [HarmonyPatch(typeof(IdeoFoundation), nameof(IdeoFoundation.CanAdd))] public static class Rimworld_Patch_IdeoFoundation { public static void Postfix(PreceptDef precept, ref IdeoFoundation __instance, ref AcceptanceReport __result) { DefExtension_MultipleMemesRequired extension = precept.GetModExtension(); if (extension == null) return; if (extension.requiredAllMemes.NullOrEmpty()) return; for (int i = 0; i < extension.requiredAllMemes.Count; i++) { if (!__instance.ideo.memes.Contains(extension.requiredAllMemes[i])) { List report = new List(); foreach (MemeDef meme in extension.requiredAllMemes) report.Add(meme.LabelCap); __result = new AcceptanceReport("RequiresMeme".Translate() + ": " + report.ToCommaList()); return; } } } } [HarmonyPatch(typeof(RelationsUtility), "Incestuous")] public static class Rimworld_Patch_IncestuousManualRomance { /// /// Override incestuous check in the manual romance /// /// Pawn to try do romance /// Target for romance /// Result of the original method /// Run original method implementation public static bool Prefix(Pawn one, Pawn two, ref bool __result) { __result = RsiIncestuous(one, two); return false; } /// /// Check if Ideology allows romance attempt /// /// Pawn to try do romance /// Target for romance /// Forbid romance option public static bool RsiIncestuous(Pawn one, Pawn two) { PreceptDef incestuousPrecept = one.Ideo?.PreceptsListForReading.Select(precept => precept.def).FirstOrFallback(def => def.issue == VariousDefOf.Incestuos); var allowManualRomanceOnlyFor = incestuousPrecept?.GetModExtension()?.allowManualRomanceOnlyFor; BloodRelationDegree relationDegree = RelationHelpers.GetBloodRelationDegree(one, two); if (allowManualRomanceOnlyFor == null) { return relationDegree < BloodRelationDegree.NotRelated; } return !allowManualRomanceOnlyFor.Contains(relationDegree); } } [HarmonyPatch(typeof(Pawn_RelationsTracker), nameof(Pawn_RelationsTracker.SecondaryRomanceChanceFactor))] public static class Rimworld_Patch_SecondaryRomanceChanceFactor { /// /// /// Replace /// float num = 1f; /// foreach (PawnRelationDef pawnRelationDef in this.pawn.GetRelations(otherPawn)) /// { /// num *= pawnRelationDef.romanceChanceFactor; /// } /// /// with /// /// float num = 1f; /// num = RomanceChanceFactorHelpers.GetRomanceChanceFactor(this.pawn, otherPawn) /// /// /// Original method body /// Modified method body public static IEnumerable Transpiler(IEnumerable instructions) { MethodInfo getRelationsInfo = AccessTools.Method( typeof(PawnRelationUtility), nameof(PawnRelationUtility.GetRelations), new[] { typeof(Pawn), typeof(Pawn) }); MethodInfo helperInfo = AccessTools.Method( typeof(RomanceChanceFactorHelpers), nameof(RomanceChanceFactorHelpers.GetRomanceChanceFactor), new[] { typeof(Pawn), typeof(Pawn) }); bool skipping = false; bool yieldNext = false; bool finished = false; // Original IL looks something like this: // Load this.pawn // Load otherPawn // Call GetRelations // Start loop // ... // Mul num and romanceChanceFactor // Store result // ... // Endfinaly // Idea is to substitute GetRelations call with a method with the same signature, // store results in the same place original loop does and remove everything else until Endfinaly foreach (CodeInstruction instruction in instructions) { if (finished) { yield return instruction; continue; } if (skipping) { if (yieldNext) { yield return instruction; yieldNext = false; } else if (instruction.opcode == OpCodes.Mul) { yieldNext = true; } else if (instruction.opcode == OpCodes.Endfinally) { finished = true; } continue; } if (instruction.Calls(getRelationsInfo)) { yield return new CodeInstruction(OpCodes.Call, helperInfo); skipping = true; continue; } yield return instruction; } } } }