using System; using System.Collections.Generic; using HarmonyLib; using RimWorld; using Verse; namespace BlaxCastes { /// A pawn's faction, guest status and host faction, taken before a status change. internal struct JoinSnapshot { public bool valid; public Faction faction; public bool hasGuest; public GuestStatus status; public Faction host; public static JoinSnapshot Of(Pawn pawn) { Pawn_GuestTracker guest = pawn.guest; return new JoinSnapshot { valid = true, faction = pawn.Faction, hasGuest = guest != null, status = guest != null ? guest.guestStatusInt : GuestStatus.Guest, host = guest?.HostFaction, }; } public JoinSide Side => JoinDefaults.Classify(faction, hasGuest, status, host); } /// /// Shared bodies of the join hooks. The prefixes snapshot the pawn, the postfixes compare. Only a /// change into player colonist, player slave or player prisoner queues a join, so the /// SetGuestStatus(null) that vanilla runs on existing members (inside SetFaction, before a trade) /// queues nothing. A change out of the player side is noted, and the membership ends on the next /// tick if the pawn is still gone by then. Nothing is applied here: SetFaction and SetGuestStatus /// reset medical care, areas, work and slave mode right after, so joins wait for the next tick. /// internal static class JoinHooks { private static readonly HashSet logged = new HashSet(); /// Logs a hook failure once per hook, so a Castes bug can never break a trade or a capture. public static void LogOnce(string where, Exception e) { try { if (logged.Add(where)) Log.Error("[Blax Castes] " + where + " failed (logged once): " + e); } catch { } } private static bool Watching(Pawn pawn) => pawn != null && Current.Game != null && Scribe.mode == LoadSaveMode.Inactive && pawn.RaceProps != null && pawn.RaceProps.Humanlike; public static JoinSnapshot Before(Pawn pawn) { try { return Watching(pawn) ? JoinSnapshot.Of(pawn) : default; } catch (Exception e) { LogOnce("Join snapshot", e); return default; } } public static void After(Pawn pawn, JoinSnapshot before) { if (!before.valid) return; try { JoinSide was = before.Side; JoinSide now = JoinSnapshot.Of(pawn).Side; if (was == now) return; if (now != JoinSide.None) JoinDefaults.Enqueue(pawn, JoinScopes.ReasonFor(pawn) ?? JoinDefaults.StatusFallback); else JoinDefaults.NoteLeft(pawn); } catch (Exception e) { LogOnce("Join check", e); } } } [HarmonyPatch(typeof(Pawn), nameof(Pawn.SetFaction), new[] { typeof(Faction), typeof(Pawn) })] internal static class Patch_Pawn_SetFaction { public static void Prefix(Pawn __instance, out JoinSnapshot __state) => __state = JoinHooks.Before(__instance); public static void Postfix(Pawn __instance, JoinSnapshot __state) => JoinHooks.After(__instance, __state); } [HarmonyPatch(typeof(Pawn_GuestTracker), nameof(Pawn_GuestTracker.SetGuestStatus))] internal static class Patch_GuestTracker_SetGuestStatus { public static void Prefix(Pawn ___pawn, out JoinSnapshot __state) => __state = JoinHooks.Before(___pawn); public static void Postfix(Pawn ___pawn, JoinSnapshot __state) => JoinHooks.After(___pawn, __state); } /// /// Buying: the pawn's status changes inside Pawn.PreTraded, so a "bought" scope covers it. The /// finalizer pops the scope even when PreTraded throws. Blax Slaves and Trade as Slave or Colonist /// only set guest.joinStatus before this call, so the final status (slave or not) picks the row. /// [HarmonyPatch(typeof(Pawn), nameof(Pawn.PreTraded))] internal static class Patch_Pawn_PreTraded { public static void Prefix(Pawn __instance, TradeAction action, out int __state) { __state = 0; try { if (action == TradeAction.PlayerBuys) __state = JoinScopes.Push(JoinScopes.Bought, __instance); } catch (Exception e) { JoinHooks.LogOnce("Buy scope", e); } } public static Exception Finalizer(Exception __exception, int __state) { try { JoinScopes.Pop(__state); } catch (Exception e) { JoinHooks.LogOnce("Buy scope", e); } return __exception; } } }