Added settings for romance patches

Added in-game explanation for DefExtension_ModifyMtb effect
This commit is contained in:
amevarashi 2023-06-25 18:54:32 +05:00
parent 77dd1a23d6
commit e622b7a391
12 changed files with 189 additions and 35 deletions

View file

@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using Verse;
namespace RJWSexperience.Ideology.Patches
@ -60,6 +61,8 @@ namespace RJWSexperience.Ideology.Patches
[HarmonyPatch(typeof(RelationsUtility), "Incestuous")]
public static class Rimworld_Patch_IncestuousManualRomance
{
public static bool Prepare() => RsiMod.Prefs.patchIncestuousManualRomance;
/// <summary>
/// Override incestuous check in the manual romance
/// </summary>
@ -97,6 +100,8 @@ namespace RJWSexperience.Ideology.Patches
[HarmonyPatch(typeof(Pawn_RelationsTracker), nameof(Pawn_RelationsTracker.SecondaryRomanceChanceFactor))]
public static class Rimworld_Patch_SecondaryRomanceChanceFactor
{
public static bool Prepare() => RsiMod.Prefs.patchRomanceChanceFactor;
/// <summary>
/// <para>
/// Replace
@ -124,9 +129,6 @@ namespace RJWSexperience.Ideology.Patches
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
@ -142,41 +144,103 @@ namespace RJWSexperience.Ideology.Patches
// 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)
IEnumerator<CodeInstruction> enumerator = instructions.GetEnumerator();
bool endOfInstructions = enumerator.MoveNext();
// Find and replace GetRelations
while (!endOfInstructions)
{
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;
}
var instruction = enumerator.Current;
if (instruction.Calls(getRelationsInfo))
{
yield return new CodeInstruction(OpCodes.Call, helperInfo);
skipping = true;
continue;
enumerator.MoveNext(); // skip original method call
break;
}
yield return instruction;
endOfInstructions = enumerator.MoveNext();
}
if (endOfInstructions)
{
Log.Error("[RSI] Failed to transpile Pawn_RelationsTracker.SecondaryRomanceChanceFactor: PawnRelationUtility.GetRelations call not found");
yield break;
}
// Skip everything until Mul
while (!endOfInstructions)
{
if (enumerator.Current.opcode == OpCodes.Mul)
{
enumerator.MoveNext(); // skip Mul
yield return enumerator.Current; // return next op, it should be "store result"
break;
}
endOfInstructions = enumerator.MoveNext();
}
if (endOfInstructions)
{
Log.Error("[RSI] Failed to transpile Pawn_RelationsTracker.SecondaryRomanceChanceFactor: Mul not found. This error means half of SecondaryRomanceChanceFactor was erased. Very not good");
yield break;
}
// Skip the rest of the loop
while (!endOfInstructions)
{
if (enumerator.Current.opcode == OpCodes.Endfinally)
{
// Endfinally will be skipped by the next MoveNext()
break;
}
endOfInstructions = enumerator.MoveNext();
}
if (endOfInstructions)
{
Log.Error("[RSI] Failed to transpile Pawn_RelationsTracker.SecondaryRomanceChanceFactor: Endfinally not found. This error means half of SecondaryRomanceChanceFactor was erased. Very not good");
yield break;
}
// Return the rest of the method
while (enumerator.MoveNext())
{
yield return enumerator.Current;
}
}
[HarmonyPatch(typeof(Precept), nameof(Precept.GetTip))]
public static class Rimworld_Patch_PreceptTip
{
public static void Postfix(ref string __result, Precept __instance)
{
if (__instance.def.modExtensions.NullOrEmpty())
{
return;
}
bool tipChanged = false;
StringBuilder tipBuilder = new StringBuilder(__result);
tipBuilder.AppendLine();
tipBuilder.AppendInNewLine((Keyed.ModTitle + ":").Colorize(ColoredText.TipSectionTitleColor));
for (int i = 0; i < __instance.def.modExtensions.Count; i++)
{
if (__instance.def.modExtensions[i] is IPreceptTipPostfix tipPostfix)
{
tipBuilder.AppendInNewLine(" - " + tipPostfix.GetTip());
tipChanged = true;
}
}
if (tipChanged)
{
__result = tipBuilder.ToString();
}
}
}
}