using System; using HarmonyLib; using Verse; namespace BlaxDefaults { /// /// 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. /// [StaticConstructorOnStartup] public static class BlaxDefaultsStartup { public const string HarmonyId = "blax.defaults"; public static readonly Harmony Harmony = new Harmony(HarmonyId); /// Patch classes applied and failed at startup, for the smoke test. 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" : "")); } } }