89 lines
2.6 KiB
C#
89 lines
2.6 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. 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;
|
||
|
|
|
||
|
|
public static void Reset()
|
||
|
|
{
|
||
|
|
ReportThrows = false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|