rimworld-blax-laws/Source/BlaxLaws/Sentences/EscapeHook.cs

47 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using HarmonyLib;
using RimWorld;
using Verse;
namespace BlaxLaws
{
/// <summary>
/// The escape hook: a postfix on GuestUtility.Notify_PrisonerEscaped (RimWorld/GuestUtility.cs:72-82, a
/// loop over the colonists, not inlinable). The game calls it in exactly one place: Pawn.ExitMap, for a
/// prisoner of the colony that was not released and doesn't leave into a caravan or a transporter
/// (Verse/Pawn.cs:2557-2589). It returns at once unless the pawn has a jailed case; then it notes the tick of
/// the escape on that case (LawCase.escapedTick). A pawn that leaves custody another way (sold, given away,
/// kidnapped, lost with a map) never gets the note, and the world pawn GC may discard an escapee before the
/// next sentence sweep (RimWorld.Planet/WorldPawnGC.cs:28-63, 153-177), so only this note tells an escape
/// apart at the sweep. It runs inside ExitMap while the pawn is still spawned; the sweep clears the note
/// only once it sees the pawn back on a map.
/// </summary>
[HarmonyPatch(typeof(GuestUtility), nameof(GuestUtility.Notify_PrisonerEscaped))]
public static class Patch_GuestUtility_NotifyPrisonerEscaped
{
public static void Postfix(Pawn prisoner)
{
try
{
if (prisoner == null)
return;
GameComponent_BlaxLaws comp = GameComponent_BlaxLaws.Instance;
if (comp == null)
return;
IReadOnlyList<LawCase> list = comp.CasesOf(prisoner);
for (int i = 0; i < list.Count; i++)
{
LawCase c = list[i];
if (!c.IsClosed && c.status == CaseStatus.Jailed && c.escapedTick < 0)
c.escapedTick = Find.TickManager?.TicksGame ?? 0;
}
}
catch (Exception e)
{
HookGuard.Fail("escape hook, GuestUtility.Notify_PrisonerEscaped", e);
}
}
}
}