Skip to content

Commit

Permalink
isolate mod component creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzyhau committed Mar 9, 2023
1 parent 959db0f commit 874a3de
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
6 changes: 5 additions & 1 deletion Source/Hat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ public void InitalizeAssemblies()
{
mod.InitializeAssembly();
}
foreach (var mod in Mods)
{
mod.InitializeComponents();
}
Logger.Log("HAT", "Assembly initialization completed!");
}

Expand All @@ -214,7 +218,7 @@ public void InitalizeComponents()
{
foreach(var mod in Mods)
{
mod.InitializeComponents();
mod.InjectComponents();
}
Logger.Log("HAT", "Component initialization completed!");
}
Expand Down
23 changes: 15 additions & 8 deletions Source/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down

0 comments on commit 874a3de

Please sign in to comment.