rjw-sexperience-ideology/Source/IdeologyAddon/Patches/Rimworld_Patch.cs

184 lines
5.4 KiB
C#

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<DefExtension_MultipleMemesRequired>();
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<string> report = new List<string>();
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
{
/// <summary>
/// Override incestuous check in the manual romance
/// </summary>
/// <param name="one">Pawn to try do romance</param>
/// <param name="two">Target for romance</param>
/// <param name="__result">Result of the original method</param>
/// <returns>Run original method implementation</returns>
public static bool Prefix(Pawn one, Pawn two, ref bool __result)
{
__result = RsiIncestuous(one, two);
return false;
}
/// <summary>
/// Check if Ideology allows romance attempt
/// </summary>
/// <param name="one">Pawn to try do romance</param>
/// <param name="two">Target for romance</param>
/// <returns>Forbid romance option</returns>
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<DefExtension_Incest>()?.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
{
/// <summary>
/// <para>
/// Replace
/// float num = 1f;
/// foreach (PawnRelationDef pawnRelationDef in this.pawn.GetRelations(otherPawn))
/// {
/// num *= pawnRelationDef.romanceChanceFactor;
/// }
/// </para>
/// <para>with</para>
/// <para>
/// float num = 1f;
/// num = RomanceChanceFactorHelpers.GetRomanceChanceFactor(this.pawn, otherPawn)
/// </para>
/// </summary>
/// <param name="instructions">Original method body</param>
/// <returns>Modified method body</returns>
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> 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;
}
}
}
}