From 874a3de24a980424cc9170aef872411f350deb50 Mon Sep 17 00:00:00 2001 From: Krzyhau Date: Thu, 9 Mar 2023 19:24:35 +0100 Subject: [PATCH] isolate mod component creation --- Source/Hat.cs | 6 +++++- Source/Mod.cs | 23 +++++++++++++++-------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Source/Hat.cs b/Source/Hat.cs index d91cc15..7c85dfd 100644 --- a/Source/Hat.cs +++ b/Source/Hat.cs @@ -198,6 +198,10 @@ public void InitalizeAssemblies() { mod.InitializeAssembly(); } + foreach (var mod in Mods) + { + mod.InitializeComponents(); + } Logger.Log("HAT", "Assembly initialization completed!"); } @@ -214,7 +218,7 @@ public void InitalizeComponents() { foreach(var mod in Mods) { - mod.InitializeComponents(); + mod.InjectComponents(); } Logger.Log("HAT", "Component initialization completed!"); } diff --git a/Source/Mod.cs b/Source/Mod.cs index f407921..3c21bae 100644 --- a/Source/Mod.cs +++ b/Source/Mod.cs @@ -90,8 +90,21 @@ public void InitializeAssets() } } - // injects custom components of this mod into the game + public void InitializeComponents() + { + if (RawAssembly == null || Assembly == null) return; + + foreach (Type type in Assembly.GetExportedTypes()) + { + if (!typeof(IGameComponent).IsAssignableFrom(type) || !type.IsPublic) continue; + var gameComponent = (IGameComponent)Activator.CreateInstance(type, new object[] { ModLoader.Game }); + Components.Add(gameComponent); + } + } + + // injects custom components of this mod into the game + public void InjectComponents() { // add game components foreach (var component in Components) @@ -102,17 +115,11 @@ public void InitializeComponents() } } + // loads mod's assemblies public void InitializeAssembly() { if (RawAssembly == null) return; Assembly = Assembly.Load(RawAssembly); - - foreach (Type type in Assembly.GetExportedTypes()) - { - if (!typeof(IGameComponent).IsAssignableFrom(type) || !type.IsPublic) continue; - var gameComponent = (IGameComponent)Activator.CreateInstance(type, new object[] { ModLoader.Game }); - Components.Add(gameComponent); - } } public void Dispose()