Sample mod project and modding bootstrap

This commit is contained in:
Alex
2025-01-10 23:42:41 +00:00
parent b62cf73086
commit ba78a15613
8 changed files with 109 additions and 6 deletions
+1 -1
View File
@@ -68,7 +68,7 @@ public class Watermark {
sptConfig = _configServer.GetConfig<CoreConfig>(ConfigTypes.CORE);
}
public void Initialize()
public virtual void Initialize()
{
var description = _watermarkLocale.GetDescription();
var warning = _watermarkLocale.GetWarning();
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
</ItemGroup>
</Project>
+26
View File
@@ -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();
}
}
+24
View File
@@ -0,0 +1,24 @@
using System.Reflection;
namespace Server;
public class HarmonyBootstrapper
{
public static void LoadAllPatches(List<Assembly> 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);
}
}
*/
}
}
+26
View File
@@ -0,0 +1,26 @@
using System.Reflection;
namespace Server;
public class ModDllLoader
{
private const string ModPath = "./user/mods/";
public static List<Assembly> LoadAllMods()
{
if (!Directory.Exists(ModPath))
Directory.CreateDirectory(ModPath);
var mods = new List<Assembly>();
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;
}
}
+13 -5
View File
@@ -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<Assembly> 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<Type> types)
{
var groupedTypes = types.Select(t =>
Binary file not shown.
+6
View File
@@ -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