From d18360ec33798a607728c03a351ebf27098d9016 Mon Sep 17 00:00:00 2001 From: Robert Paciorek Date: Tue, 24 Jun 2025 17:00:38 +0000 Subject: [PATCH] plugins system with sample (reflection based) --- plugins/sample_plugin/Plugin.cs | 36 +++++++++++++++++++++++ plugins/sample_plugin/SamplePlugin.csproj | 17 +++++++++++ src/Program.cs | 21 ++++++++++++- 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 plugins/sample_plugin/Plugin.cs create mode 100644 plugins/sample_plugin/SamplePlugin.csproj diff --git a/plugins/sample_plugin/Plugin.cs b/plugins/sample_plugin/Plugin.cs new file mode 100644 index 0000000..5c0325a --- /dev/null +++ b/plugins/sample_plugin/Plugin.cs @@ -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}"); + } +} diff --git a/plugins/sample_plugin/SamplePlugin.csproj b/plugins/sample_plugin/SamplePlugin.csproj new file mode 100644 index 0000000..56ee8bf --- /dev/null +++ b/plugins/sample_plugin/SamplePlugin.csproj @@ -0,0 +1,17 @@ + + + net6.0 + SamplePlugin + enable + + + + + + + + + ../../src/bin/Debug/net6.0/sodoffmmo.dll + + + diff --git a/src/Program.cs b/src/Program.cs index 6878f22..75ed5d0 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -1,11 +1,30 @@ using sodoffmmo; using sodoffmmo.Core; using System.Net; +using System.IO; +using System.Reflection; Configuration.Initialize(); -Server server; +string pluginsDir = Path.GetFullPath("plugins"); +Console.WriteLine($"Loading plugins from '{pluginsDir}' ..."); +List 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 == "*") { server = new( IPAddress.IPv6Any,