plugins system with sample (reflection based)

This commit is contained in:
Robert Paciorek 2025-06-24 17:00:38 +00:00
parent 4278b8565b
commit d18360ec33
3 changed files with 73 additions and 1 deletions

View File

@ -0,0 +1,36 @@
using System;
using HarmonyLib;
namespace SoDOff_MMO_Plugin {
// all classes from SoDOff_MMO_Plugin namespace will be created on dll load
public class SamplePlugin1 {
public SamplePlugin1() {
Console.WriteLine("SamplePlugin1 constructor");
var harmony = new Harmony("SamplePlugin1");
harmony.PatchAll();
}
}
public class SamplePlugin2 {
public SamplePlugin2() {
Console.WriteLine("SamplePlugin2 constructor");
}
}
}
namespace Internal {
// classes from other namespace will be NOT created on dll load
public class SamplePlugin3 {
public SamplePlugin3() {
Console.WriteLine("SamplePlugin3 constructor");
}
}
}
[HarmonyPatch("ChatMessageHandler", "Chat")]
public class ChatMessageHandler_Chat {
[HarmonyPrefix]
public static void Chat(sodoffmmo.Core.Client client, string message) {
Console.WriteLine($"Chat message: {message}");
}
}

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AssemblyName>SamplePlugin</AssemblyName>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Lib.Harmony" Version="2.3.6" />
</ItemGroup>
<ItemGroup>
<Reference Include="sodoffmmo">
<HintPath>../../src/bin/Debug/net6.0/sodoffmmo.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

View File

@ -1,11 +1,30 @@
using sodoffmmo; using sodoffmmo;
using sodoffmmo.Core; using sodoffmmo.Core;
using System.Net; using System.Net;
using System.IO;
using System.Reflection;
Configuration.Initialize(); Configuration.Initialize();
Server server; string pluginsDir = Path.GetFullPath("plugins");
Console.WriteLine($"Loading plugins from '{pluginsDir}' ...");
List<dynamic> plugins = new();
try {
foreach (var dllFile in Directory.GetFiles(pluginsDir, "*.dll")) {
var dll = Assembly.LoadFrom(dllFile);
foreach(Type type in dll.GetExportedTypes()) {
if (type.Namespace == "SoDOff_MMO_Plugin") {
Console.WriteLine($" * {type} ({Path.GetFileName(dllFile)})");
plugins.Add(Activator.CreateInstance(type));
}
}
}
} catch (System.IO.DirectoryNotFoundException) {
Console.WriteLine(" - Skipped: plugins directory do not exist");
}
Console.WriteLine($"Starting MMO server ...");
Server server;
if (String.IsNullOrEmpty(Configuration.ServerConfiguration.ListenIP) || Configuration.ServerConfiguration.ListenIP == "*") { if (String.IsNullOrEmpty(Configuration.ServerConfiguration.ListenIP) || Configuration.ServerConfiguration.ListenIP == "*") {
server = new( server = new(
IPAddress.IPv6Any, IPAddress.IPv6Any,