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. 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;
public static void Reset()
{
ReportThrows = false;
}
}
}