117 lines
3.8 KiB
C#
117 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Verse;
|
|
|
|
namespace BlaxLaws
|
|
{
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
public static class LawsLog
|
|
{
|
|
private static readonly HashSet<string> logged = new HashSet<string>();
|
|
|
|
/// <summary>Logs "[Blax Laws] where failed" with the exception, once per key until the next reset.</summary>
|
|
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.
|
|
}
|
|
}
|
|
|
|
/// <summary>A one-time message (stand-downs, hints), not an error.</summary>
|
|
public static void MessageOnce(string key, string text)
|
|
{
|
|
try
|
|
{
|
|
lock (logged)
|
|
{
|
|
if (!logged.Add("msg:" + key))
|
|
return;
|
|
}
|
|
Log.Message("[Blax Laws] " + text);
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
|
|
/// <summary>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).</summary>
|
|
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();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
public static class LawsTestHooks
|
|
{
|
|
/// <summary>CaseFiler.Report throws, to test that a hook's HookGuard keeps vanilla running.</summary>
|
|
public static bool ReportThrows;
|
|
|
|
/// <summary>GameComponent_BlaxLaws skips its tick, as if the mod were absent (the performance test's baseline).</summary>
|
|
public static bool TickSuspended;
|
|
|
|
/// <summary>Every Laws patch is removed (RemovePatches) until RestorePatches or Reset.</summary>
|
|
public static bool PatchesRemoved { get; private set; }
|
|
|
|
/// <summary>Removes every Harmony patch of Laws, as if the mod were absent. RestorePatches puts them back.</summary>
|
|
public static void RemovePatches()
|
|
{
|
|
if (PatchesRemoved)
|
|
return;
|
|
BlaxLawsStartup.Harmony.UnpatchAll(BlaxLawsStartup.HarmonyId);
|
|
PatchesRemoved = true;
|
|
}
|
|
|
|
/// <summary>Applies the patches again after RemovePatches. Returns the patch classes that failed.</summary>
|
|
public static List<string> RestorePatches()
|
|
{
|
|
var failed = new List<string>();
|
|
if (!PatchesRemoved)
|
|
return failed;
|
|
BlaxLawsStartup.PatchAll(failed);
|
|
PatchesRemoved = false;
|
|
return failed;
|
|
}
|
|
|
|
public static void Reset()
|
|
{
|
|
ReportThrows = false;
|
|
TickSuspended = false;
|
|
RestorePatches();
|
|
}
|
|
}
|
|
}
|