mirror of
https://github.com/SoDOff-Project/sodoff-mmo.git
synced 2025-10-11 08:18:49 -07:00
plugins system with sample (reflection based)
This commit is contained in:
parent
4278b8565b
commit
d18360ec33
36
plugins/sample_plugin/Plugin.cs
Normal file
36
plugins/sample_plugin/Plugin.cs
Normal 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}");
|
||||
}
|
||||
}
|
17
plugins/sample_plugin/SamplePlugin.csproj
Normal file
17
plugins/sample_plugin/SamplePlugin.csproj
Normal 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>
|
@ -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<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 == "*") {
|
||||
server = new(
|
||||
IPAddress.IPv6Any,
|
||||
|
Loading…
x
Reference in New Issue
Block a user