Added ModulLoader
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\FireBird2D.Interfaces\Firebird2D.DataypesAndInterfaces.csproj" />
|
||||
<ProjectReference Include="..\Firebird2D.Logger\Firebird2D.Logger.csproj" />
|
||||
<ProjectReference Include="..\Firebird2D.Requirement\Firebird2D.Requirement.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="Modules\Moduls.xml">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,99 @@
|
||||
using Firebird2D.Datatypes;
|
||||
using Firebird2D.Interfaces;
|
||||
using Firebird2D.Logger;
|
||||
using Firebird2D.Requirement;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Reflection;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace Firebird2D.ModulLoading
|
||||
{
|
||||
public struct Modul
|
||||
{
|
||||
public ModuleType ModuleType { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string RelativePath { get; set; }
|
||||
}
|
||||
|
||||
|
||||
[XmlRoot("Moduls")]
|
||||
public class ModulList
|
||||
{
|
||||
[XmlArrayItem("Modul")]
|
||||
public List<Modul> mods = [];
|
||||
}
|
||||
|
||||
public class ModulLoader
|
||||
{
|
||||
private readonly IGame gameInstance;
|
||||
|
||||
private ModulList? modulList = null;
|
||||
|
||||
|
||||
public ModulLoader(IGame gameInstance)
|
||||
{
|
||||
this.gameInstance = gameInstance;
|
||||
}
|
||||
|
||||
public void LoadModules()
|
||||
{
|
||||
DeserialiseModulList();
|
||||
if (modulList == null) return;
|
||||
List<Modul> eventModul = modulList.mods.Where(m => m.ModuleType == ModuleType.EventSystem).ToList();
|
||||
|
||||
if (eventModul.Count > 1) GameLogger.GetInstance(0).Log("ModuleLoader", LogType.Warning, $"More then one EventModul defined first modul ist loaded");
|
||||
|
||||
if (eventModul.Count != 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
Assembly eventAssembly = Assembly.Load(eventModul[0].RelativePath);
|
||||
|
||||
GameLogger.GetInstance(0).Log("ModuleLoader", LogType.Info, $"F{eventModul[0].Name} loaded.");
|
||||
|
||||
LinkEventAssembly(eventAssembly);
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
GameLogger.GetInstance(0).Log("ModuleLoader", LogType.Warning, $"Failed to load {eventModul[0].Name}, with exception {ex.Message}");
|
||||
}
|
||||
}
|
||||
else
|
||||
GameLogger.GetInstance(0).Log("ModuleLoader", LogType.Warning, $"More then one EventModul defined first modul ist loaded");
|
||||
|
||||
|
||||
|
||||
|
||||
if (modulList.mods.Count == 0) return;
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void LinkEventAssembly(Assembly assembly)
|
||||
{
|
||||
Type? type = assembly.GetTypes().FirstOrDefault(t => typeof(IEventBus).IsAssignableFrom(t) && !t.IsInterface);
|
||||
Require.NotNull(type,"type",0);
|
||||
|
||||
IEventBus? eventBus = Activator.CreateInstance(type) as IEventBus;
|
||||
|
||||
Require.NotNull(eventBus,"eventBus",0);
|
||||
gameInstance.EventBus = eventBus;
|
||||
}
|
||||
|
||||
private void DeserialiseModulList()
|
||||
{
|
||||
try
|
||||
{
|
||||
using (FileStream file = new FileStream("Modules/Moduls.xml", FileMode.Open))
|
||||
{
|
||||
XmlSerializer xmlSerializer = new XmlSerializer(typeof(ModulList));
|
||||
modulList = (ModulList?)xmlSerializer.Deserialize(file);
|
||||
}
|
||||
}
|
||||
catch(Exception ex) {
|
||||
modulList = null;
|
||||
GameLogger.GetInstance(0).Log("ModulLoader",LogType.Warning, $"Modul file not existing or invalid: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<Moduls>
|
||||
<!--
|
||||
<Modul>
|
||||
<ModuleType>Plugin</ModuleType>
|
||||
<Name>Logger</Name>
|
||||
<RelativePath>Modules/Logger</RelativePath>
|
||||
</Modul>
|
||||
-->
|
||||
</Moduls>
|
||||
Reference in New Issue
Block a user