using System; using System.Collections.Generic; using Verse; namespace BlaxLaws { /// /// One-time logging. Every hook sits in try/catch and logs through HookGuard, which logs here, so a /// broken hook logs once per game instead of every call. The flags reset in /// GameComponent_BlaxLaws.FinalizeInit, so a failure in a later game logs again. /// public static class LawsLog { private static readonly HashSet logged = new HashSet(); /// Logs "[Blax Laws] where failed" with the exception, once per key until the next reset. public static void Once(string where, Exception e) { try { lock (logged) { if (!logged.Add(where)) return; } Log.Error("[Blax Laws] " + where + " failed (logged once per game): " + e); } catch { // Logging must never throw into a hook. } } /// A one-time message (stand-downs, hints), not an error. public static void MessageOnce(string key, string text) { try { lock (logged) { if (!logged.Add("msg:" + key)) return; } Log.Message("[Blax Laws] " + text); } catch { } } /// A one-time warning, for data the game hands over that Laws can't use (a broken def, a saved name it can't read). public static void WarningOnce(string key, string text) { try { lock (logged) { if (!logged.Add("warn:" + key)) return; } Log.Warning("[Blax Laws] " + text); } catch { } } public static void ResetOnce() { lock (logged) logged.Clear(); } } /// /// Switches the test mod flips to force failure paths or to measure the game without Laws. Not game /// state: nothing here is saved and the test runner resets every field after each test. /// public static class LawsTestHooks { /// CaseFiler.Report throws, to test that a hook's HookGuard keeps vanilla running. public static bool ReportThrows; /// Hook P's gizmo postfix throws inside its guard, to test that the gizmo bar keeps vanilla's gizmos. public static bool CourtGizmoThrows; /// The court place switch throws while it is built, to test the guard around it in hook P. public static bool CourtSwitchThrows; /// GameComponent_BlaxLaws skips its tick, as if the mod were absent (the performance test's baseline). public static bool TickSuspended; /// Every Laws patch is removed (RemovePatches) until RestorePatches or Reset. public static bool PatchesRemoved { get; private set; } /// Removes every Harmony patch of Laws, as if the mod were absent. RestorePatches puts them back. public static void RemovePatches() { if (PatchesRemoved) return; BlaxLawsStartup.Harmony.UnpatchAll(BlaxLawsStartup.HarmonyId); PatchesRemoved = true; } /// Applies the patches again after RemovePatches. Returns the patch classes that failed. public static List RestorePatches() { var failed = new List(); if (!PatchesRemoved) return failed; BlaxLawsStartup.PatchAll(failed); PatchesRemoved = false; return failed; } public static void Reset() { ReportThrows = false; CourtGizmoThrows = false; CourtSwitchThrows = false; TickSuspended = false; RestorePatches(); } } }