From 818a343295569e5b51a3cf6b49bcfae2c2d04d94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Schm=C3=B6cker?= Date: Mon, 25 Sep 2023 22:35:32 +0200 Subject: [PATCH] feat: log a warning if a mod accesses a manager too early --- JotunnLib/Main.cs | 12 ++++++++++++ JotunnLib/Managers/CommandManager.cs | 2 +- JotunnLib/Managers/CreatureManager.cs | 2 +- JotunnLib/Managers/GUIManager.cs | 2 +- JotunnLib/Managers/InputManager.cs | 2 +- JotunnLib/Managers/ItemManager.cs | 2 +- JotunnLib/Managers/KeyHintManager.cs | 2 +- JotunnLib/Managers/KitbashManager.cs | 2 +- JotunnLib/Managers/LocalizationManager.cs | 2 +- JotunnLib/Managers/MinimapManager.cs | 2 +- JotunnLib/Managers/MockSystem/MockManager.cs | 2 +- JotunnLib/Managers/NetworkManager.cs | 2 +- JotunnLib/Managers/PieceManager.cs | 2 +- JotunnLib/Managers/PrefabManager.cs | 2 +- JotunnLib/Managers/RenderManager.cs | 2 +- JotunnLib/Managers/SkillManager.cs | 2 +- JotunnLib/Managers/SynchronizationManager.cs | 2 +- JotunnLib/Managers/UndoManager.cs | 2 +- JotunnLib/Managers/ZoneManager.cs | 2 +- 19 files changed, 30 insertions(+), 18 deletions(-) diff --git a/JotunnLib/Main.cs b/JotunnLib/Main.cs index 21a7975b7..13ae1e657 100644 --- a/JotunnLib/Main.cs +++ b/JotunnLib/Main.cs @@ -131,5 +131,17 @@ private void InitializePatches() tuple.Item1.Invoke(null, null); } } + + internal static void LogManagerInit(string manager) + { + Jotunn.Logger.LogInfo($"Initializing {manager}"); + + if (!Instance) + { + string message = $"{manager} was accessed before Jotunn Awake, this can cause unexpected behaviour. " + + "Please make sure to add `[BepInDependency(Jotunn.Main.ModGuid)]` next to your BaseUnityPlugin"; + Jotunn.Logger.LogWarning(BepInExUtils.GetSourceModMetadata(), message); + } + } } } diff --git a/JotunnLib/Managers/CommandManager.cs b/JotunnLib/Managers/CommandManager.cs index 4723e2a04..3bbd59437 100644 --- a/JotunnLib/Managers/CommandManager.cs +++ b/JotunnLib/Managers/CommandManager.cs @@ -47,7 +47,7 @@ static CommandManager() /// void IManager.Init() { - Logger.LogInfo("Initializing CommandManager"); + Main.LogManagerInit("CommandManager"); AddConsoleCommand(new ClearCommand()); Main.Harmony.PatchAll(typeof(Patches)); diff --git a/JotunnLib/Managers/CreatureManager.cs b/JotunnLib/Managers/CreatureManager.cs index 1ca847f91..568922dc9 100644 --- a/JotunnLib/Managers/CreatureManager.cs +++ b/JotunnLib/Managers/CreatureManager.cs @@ -69,7 +69,7 @@ static CreatureManager() /// void IManager.Init() { - Logger.LogInfo("Initializing CreatureManager"); + Main.LogManagerInit("CreatureManager"); SpawnListContainer = new GameObject("Creatures"); SpawnListContainer.transform.parent = Main.RootObject.transform; diff --git a/JotunnLib/Managers/GUIManager.cs b/JotunnLib/Managers/GUIManager.cs index d1af089ed..f514d538c 100644 --- a/JotunnLib/Managers/GUIManager.cs +++ b/JotunnLib/Managers/GUIManager.cs @@ -291,7 +291,7 @@ private static void TextInput_IsVisible(ref bool __result) /// void IManager.Init() { - Logger.LogInfo("Initializing GUIManager"); + Main.LogManagerInit("GUIManager"); // Cache headless state Headless = SystemInfo.graphicsDeviceType == GraphicsDeviceType.Null; diff --git a/JotunnLib/Managers/InputManager.cs b/JotunnLib/Managers/InputManager.cs index db23c838d..739cecf1d 100644 --- a/JotunnLib/Managers/InputManager.cs +++ b/JotunnLib/Managers/InputManager.cs @@ -218,7 +218,7 @@ static InputManager() /// void IManager.Init() { - Logger.LogInfo("Initializing InputManager"); + Main.LogManagerInit("InputManager"); // Dont init on a dedicated server if (!GUIManager.IsHeadless()) diff --git a/JotunnLib/Managers/ItemManager.cs b/JotunnLib/Managers/ItemManager.cs index ec06c69f1..90404859b 100644 --- a/JotunnLib/Managers/ItemManager.cs +++ b/JotunnLib/Managers/ItemManager.cs @@ -73,7 +73,7 @@ static ItemManager() /// void IManager.Init() { - Logger.LogInfo("Initializing ItemManager"); + Main.LogManagerInit("ItemManager"); Main.Harmony.PatchAll(typeof(Patches)); } diff --git a/JotunnLib/Managers/KeyHintManager.cs b/JotunnLib/Managers/KeyHintManager.cs index f61fd8880..9c834b9f4 100644 --- a/JotunnLib/Managers/KeyHintManager.cs +++ b/JotunnLib/Managers/KeyHintManager.cs @@ -68,7 +68,7 @@ static KeyHintManager() /// void IManager.Init() { - Logger.LogInfo("Initializing KeyHintManager"); + Main.LogManagerInit("KeyHintManager"); // Dont init on a headless server if (!GUIManager.IsHeadless()) diff --git a/JotunnLib/Managers/KitbashManager.cs b/JotunnLib/Managers/KitbashManager.cs index 7edfc0483..09eb75aac 100644 --- a/JotunnLib/Managers/KitbashManager.cs +++ b/JotunnLib/Managers/KitbashManager.cs @@ -39,7 +39,7 @@ static KitbashManager() /// void IManager.Init() { - Logger.LogInfo("Initializing KitbashManager"); + Main.LogManagerInit("KitbashManager"); ItemManager.OnKitbashItemsAvailable += ApplyKitbashes; } diff --git a/JotunnLib/Managers/LocalizationManager.cs b/JotunnLib/Managers/LocalizationManager.cs index 9f5f2d6a7..cc2b468a2 100644 --- a/JotunnLib/Managers/LocalizationManager.cs +++ b/JotunnLib/Managers/LocalizationManager.cs @@ -91,7 +91,7 @@ static LocalizationManager() /// void IManager.Init() { - Logger.LogInfo("Initializing LocalizationManager"); + Main.LogManagerInit("LocalizationManager"); Main.Harmony.PatchAll(typeof(Patches)); diff --git a/JotunnLib/Managers/MinimapManager.cs b/JotunnLib/Managers/MinimapManager.cs index fa40dd60a..64e175d70 100644 --- a/JotunnLib/Managers/MinimapManager.cs +++ b/JotunnLib/Managers/MinimapManager.cs @@ -103,7 +103,7 @@ static MinimapManager() /// void IManager.Init() { - Logger.LogInfo("Initializing MinimapManager"); + Main.LogManagerInit("MinimapManager"); // Dont init on a headless server if (GUIManager.IsHeadless()) diff --git a/JotunnLib/Managers/MockSystem/MockManager.cs b/JotunnLib/Managers/MockSystem/MockManager.cs index ddd1a812d..561de003c 100644 --- a/JotunnLib/Managers/MockSystem/MockManager.cs +++ b/JotunnLib/Managers/MockSystem/MockManager.cs @@ -56,7 +56,7 @@ private MockManager() /// void IManager.Init() { - Logger.LogInfo("Initializing MockManager"); + Main.LogManagerInit("MockManager"); MockPrefabContainer = new GameObject("MockPrefabs"); MockPrefabContainer.transform.parent = Main.RootObject.transform; diff --git a/JotunnLib/Managers/NetworkManager.cs b/JotunnLib/Managers/NetworkManager.cs index c66ddf947..56f650358 100644 --- a/JotunnLib/Managers/NetworkManager.cs +++ b/JotunnLib/Managers/NetworkManager.cs @@ -50,7 +50,7 @@ static NetworkManager() /// void IManager.Init() { - Logger.LogInfo("Initializing NetworkManager"); + Main.LogManagerInit("NetworkManager"); Main.Harmony.PatchAll(typeof(Patches)); } diff --git a/JotunnLib/Managers/PieceManager.cs b/JotunnLib/Managers/PieceManager.cs index 7fa4782e5..3cd936e2b 100644 --- a/JotunnLib/Managers/PieceManager.cs +++ b/JotunnLib/Managers/PieceManager.cs @@ -91,7 +91,7 @@ public static class PieceCategorySettings /// void IManager.Init() { - Logger.LogInfo("Initializing PieceManager"); + Main.LogManagerInit("PieceManager"); Main.Harmony.PatchAll(typeof(Patches)); } diff --git a/JotunnLib/Managers/PrefabManager.cs b/JotunnLib/Managers/PrefabManager.cs index 887bf5273..2eed30de3 100644 --- a/JotunnLib/Managers/PrefabManager.cs +++ b/JotunnLib/Managers/PrefabManager.cs @@ -61,7 +61,7 @@ static PrefabManager() /// void IManager.Init() { - Logger.LogInfo("Initializing PrefabManager"); + Main.LogManagerInit("PrefabManager"); PrefabContainer = new GameObject("Prefabs"); PrefabContainer.transform.parent = Main.RootObject.transform; diff --git a/JotunnLib/Managers/RenderManager.cs b/JotunnLib/Managers/RenderManager.cs index 3c5c621b7..fe66044ca 100644 --- a/JotunnLib/Managers/RenderManager.cs +++ b/JotunnLib/Managers/RenderManager.cs @@ -52,7 +52,7 @@ static RenderManager() /// void IManager.Init() { - Logger.LogInfo("Initializing RenderManager"); + Main.LogManagerInit("RenderManager"); if (GUIManager.IsHeadless()) { diff --git a/JotunnLib/Managers/SkillManager.cs b/JotunnLib/Managers/SkillManager.cs index 77c19bc28..792f1f993 100644 --- a/JotunnLib/Managers/SkillManager.cs +++ b/JotunnLib/Managers/SkillManager.cs @@ -36,7 +36,7 @@ static SkillManager() /// void IManager.Init() { - Logger.LogInfo("Initializing SkillManager"); + Main.LogManagerInit("SkillManager"); Main.Harmony.PatchAll(typeof(Patches)); } diff --git a/JotunnLib/Managers/SynchronizationManager.cs b/JotunnLib/Managers/SynchronizationManager.cs index 20ea32829..abd47f444 100644 --- a/JotunnLib/Managers/SynchronizationManager.cs +++ b/JotunnLib/Managers/SynchronizationManager.cs @@ -62,7 +62,7 @@ private SynchronizationManager() { } /// void IManager.Init() { - Logger.LogInfo("Initializing SynchronizationManager"); + Main.LogManagerInit("SynchronizationManager"); // Register RPCs and the admin watchdog ConfigRPC = NetworkManager.Instance.AddRPC( diff --git a/JotunnLib/Managers/UndoManager.cs b/JotunnLib/Managers/UndoManager.cs index 5b034df84..12a2baff7 100644 --- a/JotunnLib/Managers/UndoManager.cs +++ b/JotunnLib/Managers/UndoManager.cs @@ -72,7 +72,7 @@ static UndoManager() /// void IManager.Init() { - Logger.LogInfo("Initializing UndoManager"); + Main.LogManagerInit("UndoManager"); Main.Harmony.PatchAll(typeof(Patches)); } diff --git a/JotunnLib/Managers/ZoneManager.cs b/JotunnLib/Managers/ZoneManager.cs index c4b8b3929..31e7e29f6 100644 --- a/JotunnLib/Managers/ZoneManager.cs +++ b/JotunnLib/Managers/ZoneManager.cs @@ -64,7 +64,7 @@ static ZoneManager() /// void IManager.Init() { - Logger.LogInfo("Initializing ZoneManager"); + Main.LogManagerInit("ZoneManager"); LocationContainer = new GameObject("Locations"); LocationContainer.transform.parent = Main.RootObject.transform;