diff --git a/Core/Utils/Watermark.cs b/Core/Utils/Watermark.cs index 934a285d..882303ed 100644 --- a/Core/Utils/Watermark.cs +++ b/Core/Utils/Watermark.cs @@ -68,7 +68,7 @@ public class Watermark { sptConfig = _configServer.GetConfig(ConfigTypes.CORE); } - public void Initialize() + public virtual void Initialize() { var description = _watermarkLocale.GetDescription(); var warning = _watermarkLocale.GetWarning(); diff --git a/ExampleOverrideMod/ExampleOverrideMod.csproj b/ExampleOverrideMod/ExampleOverrideMod.csproj new file mode 100644 index 00000000..3cebe147 --- /dev/null +++ b/ExampleOverrideMod/ExampleOverrideMod.csproj @@ -0,0 +1,13 @@ + + + + net9.0 + enable + enable + + + + + + + diff --git a/ExampleOverrideMod/WatermarkOverride.cs b/ExampleOverrideMod/WatermarkOverride.cs new file mode 100644 index 00000000..5f220ed1 --- /dev/null +++ b/ExampleOverrideMod/WatermarkOverride.cs @@ -0,0 +1,26 @@ +using Core.Annotations; +using Core.Models.Utils; +using Core.Servers; +using Core.Services; +using Core.Utils; + +namespace ExampleOverrideMod; + +[Injectable(InjectableTypeOverride = typeof(Watermark))] +public class WatermarkOverride : Watermark +{ + public WatermarkOverride( + ILogger logger, + ConfigServer configServer, + LocalisationService localisationService, + WatermarkLocale watermarkLocale + ) : base(logger, configServer, localisationService, watermarkLocale) + { + } + + public override void Initialize() + { + Console.WriteLine("This is a watermark mod override!"); + base.Initialize(); + } +} diff --git a/Server/HarmonyBootstrapper.cs b/Server/HarmonyBootstrapper.cs new file mode 100644 index 00000000..db8d20c3 --- /dev/null +++ b/Server/HarmonyBootstrapper.cs @@ -0,0 +1,24 @@ +using System.Reflection; + +namespace Server; + +public class HarmonyBootstrapper +{ + public static void LoadAllPatches(List assemblies) + { + /* TODO: Benched idea until someone can figure out how to make Harmony work on net9.0 runtime if even possible? + var hamony = new Harmony("SPT"); + foreach (var assembly in assemblies) + { + try + { + hamony.PatchAll(assembly); + } + catch (Exception e) + { + Console.WriteLine(e); + } + } + */ + } +} diff --git a/Server/ModDllLoader.cs b/Server/ModDllLoader.cs new file mode 100644 index 00000000..f718e6a4 --- /dev/null +++ b/Server/ModDllLoader.cs @@ -0,0 +1,26 @@ +using System.Reflection; + +namespace Server; + +public class ModDllLoader +{ + private const string ModPath = "./user/mods/"; + public static List LoadAllMods() + { + if (!Directory.Exists(ModPath)) + Directory.CreateDirectory(ModPath); + var mods = new List(); + foreach (var file in Directory.GetFiles(ModPath, "*.dll", SearchOption.AllDirectories)) + { + try + { + mods.Add(Assembly.LoadFile(Path.GetFullPath(file))); + } + catch (Exception e) + { + Console.WriteLine(e); + } + } + return mods; + } +} diff --git a/Server/Program.cs b/Server/Program.cs index 8b146f57..a74760ec 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -1,6 +1,6 @@ -using Core.Annotations; +using System.Reflection; +using Core.Annotations; using Core.Context; -using Core.Servers; using Core.Utils; namespace Server; @@ -9,12 +9,13 @@ public static class Program { public static void Main(string[] args) { + var assemblies = ModDllLoader.LoadAllMods(); + HarmonyBootstrapper.LoadAllPatches(assemblies); var builder = WebApplication.CreateBuilder(args); RegisterSptComponents(builder.Services); - - // TODO: deal with modding overriding services here! - + RegisterModOverrideComponents(builder.Services, assemblies); + try { @@ -33,6 +34,13 @@ public static class Program } } + private static void RegisterModOverrideComponents(IServiceCollection builderServices, List assemblies) + { + // We get all the services from this assembly first, since mods will override them later + RegisterComponents(builderServices, assemblies.SelectMany(a => a.GetTypes()) + .Where(type => Attribute.IsDefined(type, typeof(Injectable)))); + } + private static void RegisterComponents(IServiceCollection builderServices, IEnumerable types) { var groupedTypes = types.Select(t => diff --git a/Server/user/mods/TestMod/ExampleOverrideMod.dll b/Server/user/mods/TestMod/ExampleOverrideMod.dll new file mode 100644 index 00000000..9b58eb5f Binary files /dev/null and b/Server/user/mods/TestMod/ExampleOverrideMod.dll differ diff --git a/server-csharp.sln b/server-csharp.sln index f248ddd5..17fa6f4d 100644 --- a/server-csharp.sln +++ b/server-csharp.sln @@ -6,6 +6,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core", "Core\Core.csproj", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "UnitTests\UnitTests.csproj", "{6C0681F9-4013-4579-82DA-0A9297984FD3}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleOverrideMod", "ExampleOverrideMod\ExampleOverrideMod.csproj", "{0B9E34E4-04E1-4644-B508-2A53D198F059}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -24,5 +26,9 @@ Global {6C0681F9-4013-4579-82DA-0A9297984FD3}.Debug|Any CPU.Build.0 = Debug|Any CPU {6C0681F9-4013-4579-82DA-0A9297984FD3}.Release|Any CPU.ActiveCfg = Release|Any CPU {6C0681F9-4013-4579-82DA-0A9297984FD3}.Release|Any CPU.Build.0 = Release|Any CPU + {0B9E34E4-04E1-4644-B508-2A53D198F059}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0B9E34E4-04E1-4644-B508-2A53D198F059}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0B9E34E4-04E1-4644-B508-2A53D198F059}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0B9E34E4-04E1-4644-B508-2A53D198F059}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal