rimworld-blax-defaults/Source/BlaxDefaults/HookGuard.cs

33 lines
931 B
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using Verse;
namespace BlaxDefaults
{
/// <summary>
/// Every hook body sits in try/catch and reports here, so a Defaults bug never breaks a construction, a
/// designation or a bill. Each hook logs its first failure only.
/// <code>
/// static void Postfix(...)
/// {
/// try { ... }
/// catch (Exception e) { HookGuard.Report("MakeNewZone", e); }
/// }
/// </code>
/// </summary>
public static class HookGuard
{
private static readonly HashSet<string> reported = new HashSet<string>();
public static void Report(string hook, Exception e)
{
lock (reported)
{
if (!reported.Add(hook))
return;
}
Log.Error("[Blax Defaults] " + hook + " failed; later failures of this hook are not logged: " + e);
}
}
}