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

50 lines
1.7 KiB
C#
Raw Normal View History

using System;
using HarmonyLib;
using Verse;
namespace BlaxDefaults
{
/// <summary>
/// Reads the defaults file and applies the Harmony patches after every Mod class exists and the defs are
/// loaded. Patches go one class at a time, so a single broken target can't take the rest down.
/// </summary>
[StaticConstructorOnStartup]
public static class BlaxDefaultsStartup
{
public const string HarmonyId = "blax.defaults";
public static readonly Harmony Harmony = new Harmony(HarmonyId);
/// <summary>Patch classes applied and failed at startup, for the smoke test.</summary>
public static int PatchesApplied, PatchesFailed;
static BlaxDefaultsStartup()
{
try
{
DefaultsLibrary.LoadAtStartup();
}
catch (Exception e)
{
Log.Error("[Blax Defaults] Reading the defaults file failed: " + e);
}
foreach (Type type in typeof(BlaxDefaultsStartup).Assembly.GetTypes())
{
if (!type.IsDefined(typeof(HarmonyPatch), false))
continue;
try
{
Harmony.CreateClassProcessor(type).Patch();
PatchesApplied++;
}
catch (Exception e)
{
PatchesFailed++;
Log.Error("[Blax Defaults] Patch " + type.Name + " failed: " + e);
}
}
Log.Message("[Blax Defaults] " + PatchesApplied + " patches applied" + (PatchesFailed > 0 ? ", " + PatchesFailed + " failed" : ""));
}
}
}