Skip to content

Commit

Permalink
Merge pull request #410 from MSchmoecker/feat/conditional-initalization
Browse files Browse the repository at this point in the history
feat: only init managers when they are first accessed
  • Loading branch information
MSchmoecker authored Sep 22, 2023
2 parents eb6d5d4 + 4a18b78 commit e28127b
Show file tree
Hide file tree
Showing 22 changed files with 275 additions and 151 deletions.
43 changes: 2 additions & 41 deletions JotunnLib/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,59 +38,20 @@ public class Main : BaseUnityPlugin
internal static Harmony Harmony;
internal static GameObject RootObject;

private List<IManager> Managers;

private void Awake()
{
// Set instance
Instance = this;

// Harmony patches
Harmony = new Harmony(ModGuid);
Harmony.PatchAll(typeof(ModCompatibility));

// Initialize Logger
Jotunn.Logger.Init();

// Root Container for GameObjects in the DontDestroyOnLoad scene
RootObject = new GameObject("_JotunnRoot");
DontDestroyOnLoad(RootObject);

// Create and initialize all managers
Managers = new List<IManager>
{
LocalizationManager.Instance,
CommandManager.Instance,
InputManager.Instance,
SkillManager.Instance,
PrefabManager.Instance,
ItemManager.Instance,
PieceManager.Instance,
CreatureManager.Instance,
ZoneManager.Instance,
MockManager.Instance,
KitbashManager.Instance,
GUIManager.Instance,
KeyHintManager.Instance,
NetworkManager.Instance,
SynchronizationManager.Instance,
RenderManager.Instance,
MinimapManager.Instance,
UndoManager.Instance
};
foreach (IManager manager in Managers)
{
Logger.LogInfo("Initializing " + manager.GetType().Name);
manager.Init();
}

ModQuery.Init();
Jotunn.Logger.Init();
ModCompatibility.Init();

// Flip the "modded" switch of Valheim
Game.isModded = true;

Logger.LogInfo("Jötunn v" + Version + " loaded successfully");
}

private void Start()
Expand All @@ -107,7 +68,7 @@ private void Start()
InitializePatches();
#pragma warning restore CS0612 // Method is obsolete

LocalizationManager.Instance.LoadingAutomaticLocalizations();
AutomaticLocalizationsLoading.Init();
}

private void OnApplicationQuit()
Expand Down
8 changes: 7 additions & 1 deletion JotunnLib/Managers/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ public class CommandManager : IManager
/// <summary>
/// Hide .ctor
/// </summary>
private CommandManager() {}
private CommandManager() { }

static CommandManager()
{
((IManager)Instance).Init();
}

/// <summary>
/// Internal Action delegate to add custom entities into vanilla command's option list
Expand All @@ -42,6 +47,7 @@ private CommandManager() {}
/// </summary>
void IManager.Init()
{
Logger.LogInfo("Initializing CommandManager");
AddConsoleCommand(new ClearCommand());

Main.Harmony.PatchAll(typeof(Patches));
Expand Down
7 changes: 7 additions & 0 deletions JotunnLib/Managers/CreatureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public class CreatureManager : IManager
/// </summary>
private CreatureManager() { }

static CreatureManager()
{
((IManager)Instance).Init();
}

/// <summary>
/// Unity "character" layer ID.
/// </summary>
Expand Down Expand Up @@ -64,6 +69,8 @@ private CreatureManager() { }
/// </summary>
void IManager.Init()
{
Logger.LogInfo("Initializing CreatureManager");

SpawnListContainer = new GameObject("Creatures");
SpawnListContainer.transform.parent = Main.RootObject.transform;
SpawnListContainer.SetActive(false);
Expand Down
74 changes: 52 additions & 22 deletions JotunnLib/Managers/GUIManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public class GUIManager : IManager
/// </summary>
private GUIManager() { }

static GUIManager()
{
((IManager)Instance).Init();
}

/// <summary>
/// Event that gets fired every time the Unity scene changed and a new PixelFix
/// object was created. Subscribe to this event to create your custom GUI objects
Expand Down Expand Up @@ -286,6 +291,8 @@ private static void TextInput_IsVisible(ref bool __result)
/// </summary>
void IManager.Init()
{
Logger.LogInfo("Initializing GUIManager");

// Cache headless state
Headless = SystemInfo.graphicsDeviceType == GraphicsDeviceType.Null;

Expand All @@ -295,6 +302,7 @@ void IManager.Init()
return;
}

Instance.TryCreateGUI();
Main.Harmony.PatchAll(typeof(Patches));
SceneManager.sceneLoaded += InitialLoad;
}
Expand All @@ -312,11 +320,18 @@ private static class Patches
[HarmonyPatch(typeof(InventoryGui), nameof(InventoryGui.Update)), HarmonyTranspiler, HarmonyWrapSafe]
private static IEnumerable<CodeInstruction> InventoryGui_Update(IEnumerable<CodeInstruction> instructions) => BlockUseTranspiler(instructions);

[HarmonyPatch(typeof(FejdStartup), nameof(FejdStartup.SetupGui)), HarmonyPostfix]
private static void FejdStartup_SetupGui(FejdStartup __instance) => Instance.FejdStartup_SetupGui(__instance);
[HarmonyPatch(typeof(FejdStartup), nameof(FejdStartup.SetupGui))]
[HarmonyPatch(typeof(Game), nameof(Game.Start))]
[HarmonyPostfix]
private static void CreateCustomGUI()
{
bool created = Instance.TryCreateGUI();

[HarmonyPatch(typeof(Game), nameof(Game.Start)), HarmonyPostfix]
private static void Game_Start(Game __instance) => Instance.Game_Start(__instance);
if (!created)
{
Logger.LogError("Could not create custom GUI");
}
}
}

/// <summary>
Expand Down Expand Up @@ -459,32 +474,47 @@ internal void InitialLoad(Scene scene, LoadSceneMode loadMode)
}
}

private void FejdStartup_SetupGui(FejdStartup self)
private bool TryCreateGUI()
{
GameObject root = SceneManager.GetActiveScene().GetRootGameObjects().FirstOrDefault(x => x.name == "GuiRoot");
Transform gui = root?.transform.Find("GUI");
if (!gui)
GUIInStart = SceneManager.GetActiveScene().name == "start";
ResetInputBlock();

if (CustomGUIFront && CustomGUIBack)
{
Logger.LogError("GuiRoot GUI not found, not creating custom GUI");
return;
return true;
}
GUIInStart = true;
CreateCustomGUI(gui);
ResetInputBlock();

Transform gui = FindGUIRoot();

if (gui)
{
CreateCustomGUI(gui);
return true;
}

return false;
}

private void Game_Start(Game self)
private static Transform FindGUIRoot()
{
GameObject root = SceneManager.GetActiveScene().GetRootGameObjects().FirstOrDefault(x => x.name == "_GameMain");
Transform gui = root?.transform.Find("LoadingGUI");
if (!gui)
var rootGameObjects = SceneManager.GetActiveScene().GetRootGameObjects();

foreach (var rootObject in rootGameObjects)
{
Logger.LogError("_GameMain LoadingGUI not found, not creating custom GUI");
return;
var name = rootObject.name;

if (name == "GuiRoot")
{
return rootObject.transform.Find("GUI");
}

if (name == "_GameMain")
{
return rootObject.transform.Find("LoadingGUI");
}
}
GUIInStart = false;
CreateCustomGUI(gui);
ResetInputBlock();

return null;
}

/// <summary>
Expand Down
9 changes: 8 additions & 1 deletion JotunnLib/Managers/InputManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,20 @@ public static GamepadButton GetGamepadButton(KeyCode key)
/// <summary>
/// Hide .ctor
/// </summary>
private InputManager() {}
private InputManager() { }

static InputManager()
{
((IManager)Instance).Init();
}

/// <summary>
/// Initialize the manager
/// </summary>
void IManager.Init()
{
Logger.LogInfo("Initializing InputManager");

// Dont init on a dedicated server
if (!GUIManager.IsHeadless())
{
Expand Down
6 changes: 6 additions & 0 deletions JotunnLib/Managers/ItemManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public class ItemManager : IManager
/// </summary>
private ItemManager() { }

static ItemManager()
{
((IManager)Instance).Init();
}

/// <summary>
/// Event that gets fired after the vanilla items are in memory and available for cloning.
/// Your code will execute every time a new ObjectDB is copied (on every menu start).
Expand Down Expand Up @@ -68,6 +73,7 @@ private ItemManager() { }
/// </summary>
void IManager.Init()
{
Logger.LogInfo("Initializing ItemManager");
Main.Harmony.PatchAll(typeof(Patches));
}

Expand Down
7 changes: 7 additions & 0 deletions JotunnLib/Managers/KeyHintManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public class KeyHintManager : IManager
/// </summary>
private KeyHintManager() { }

static KeyHintManager()
{
((IManager)Instance).Init();
}

/// <summary>
/// Internal Dictionary holding the references to the custom key hints added to the manager
/// </summary>
Expand Down Expand Up @@ -63,6 +68,8 @@ private KeyHintManager() { }
/// </summary>
void IManager.Init()
{
Logger.LogInfo("Initializing KeyHintManager");

// Dont init on a headless server
if (!GUIManager.IsHeadless())
{
Expand Down
8 changes: 7 additions & 1 deletion JotunnLib/Managers/KitbashManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ public class KitbashManager : IManager
/// <summary>
/// Hide .ctor
/// </summary>
private KitbashManager() {}
private KitbashManager() { }

static KitbashManager()
{
((IManager)Instance).Init();
}

/// <summary>
/// Internal list of objects to which Kitbashing should be applied.
Expand All @@ -34,6 +39,7 @@ private KitbashManager() {}
/// </summary>
void IManager.Init()
{
Logger.LogInfo("Initializing KitbashManager");
ItemManager.OnKitbashItemsAvailable += ApplyKitbashes;
}

Expand Down
Loading

0 comments on commit e28127b

Please sign in to comment.