rimworld-blax-laws/Source/BlaxLaws/Detection/Hook_EF_Interactions.cs

85 lines
3.9 KiB
C#

using System;
using System.Runtime.CompilerServices;
using HarmonyLib;
using RimWorld;
using Verse;
namespace BlaxLaws
{
/// <summary>
/// Hook E: a social interaction happens (category Interaction). Postfix on
/// Pawn_InteractionsTracker.TryInteractWith (RimWorld/Pawn_InteractionsTracker.cs:176), about 90 lines,
/// which returns true at :268 once the interaction took place. The offender is the initiator (the
/// tracker's pawn), the victim the recipient. Insults in a spree come through here (RimWorld/JobDriver_Insult.cs:34),
/// and so does the jailbreaker's talk: JobGiver_InducePrisonerToEscape gives a SparkJailbreak job
/// (RimWorld/JobGiver_InducePrisonerToEscape.cs:15-16) whose driver, JobDriver_SingleInteraction (Core
/// Defs/JobDefs/Jobs_Misc.xml:558-559), calls Toils_Interpersonal.Interact (RimWorld/Toils_Interpersonal.cs:292-301).
/// </summary>
[HarmonyPatch(typeof(Pawn_InteractionsTracker), nameof(Pawn_InteractionsTracker.TryInteractWith))]
public static class Patch_Pawn_InteractionsTracker_TryInteractWith
{
public const string Where = "hook E, Pawn_InteractionsTracker.TryInteractWith";
public static void Postfix(Pawn ___pawn, Pawn recipient, InteractionDef intDef, bool __result)
{
if (!Watch.Interaction || !__result)
return;
File(___pawn, recipient, intDef, __result);
}
// The work, off the hot path: a method with a try block is never inlined, so the flag check above
// stays a static field read the patched method can inline.
[MethodImpl(MethodImplOptions.NoInlining)]
private static void File(Pawn ___pawn, Pawn recipient, InteractionDef intDef, bool __result)
{
try
{
if (intDef == null || !Watch.MayMatch(ActCategories.Interaction, intDef))
return;
CaseFiler.Report(new ActEvent(ActCategories.Interaction, intDef, ___pawn) { victim = recipient });
}
catch (Exception e)
{
HookGuard.Fail(Where, e);
}
}
}
/// <summary>
/// Hook F: a social fight starts (Special/StartedSocialFight). Postfix on
/// Pawn_InteractionsTracker.StartSocialFight (RimWorld/Pawn_InteractionsTracker.cs:373): a message, two
/// TryStartMentalState calls and a tale. CheckSocialFightStart calls it on the recipient of an
/// interaction (:217, :355-371), so the offender is the tracker's pawn, the one who swings first, and the
/// provoker (otherPawn) is the victim. It files only when the fight really started for the offender.
/// </summary>
[HarmonyPatch(typeof(Pawn_InteractionsTracker), nameof(Pawn_InteractionsTracker.StartSocialFight))]
public static class Patch_Pawn_InteractionsTracker_StartSocialFight
{
public const string Where = "hook F, Pawn_InteractionsTracker.StartSocialFight";
public static void Postfix(Pawn ___pawn, Pawn otherPawn)
{
if (!Watch.StartedSocialFight)
return;
File(___pawn, otherPawn);
}
// The work, off the hot path: a method with a try block is never inlined, so the flag check above
// stays a static field read the patched method can inline.
[MethodImpl(MethodImplOptions.NoInlining)]
private static void File(Pawn ___pawn, Pawn otherPawn)
{
try
{
if (___pawn == null || ___pawn.MentalStateDef != MentalStateDefOf.SocialFighting || BlaxLawsActDefOf.StartedSocialFight == null)
return;
CaseFiler.Report(new ActEvent(ActCategories.Special, BlaxLawsActDefOf.StartedSocialFight, ___pawn) { victim = otherPawn });
}
catch (Exception e)
{
HookGuard.Fail(Where, e);
}
}
}
}