From 9f16542f517f3d473b4eaa886ef683fd4cf84721 Mon Sep 17 00:00:00 2001 From: Longfeng Qin Date: Wed, 11 Dec 2024 09:11:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84Lua=E7=9A=84=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=B9=B6=E6=9C=8D=E5=8A=A1=E5=99=A8=E4=BF=9D=E6=8C=81?= =?UTF-8?q?lua=E5=AE=9E=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/LuaEngine/LuaEngine.cs | 75 +++++++++++++++++++++++++++++++++-- Server/LuaEngine/LuaScript.cs | 12 ++++++ Server/Program.cs | 26 +++--------- Server/Server.cs | 17 ++++---- Server/Server.csproj | 1 + 5 files changed, 99 insertions(+), 32 deletions(-) create mode 100644 Server/LuaEngine/LuaScript.cs diff --git a/Server/LuaEngine/LuaEngine.cs b/Server/LuaEngine/LuaEngine.cs index 5e8348f..1f2a4be 100644 --- a/Server/LuaEngine/LuaEngine.cs +++ b/Server/LuaEngine/LuaEngine.cs @@ -1,13 +1,13 @@ using NLua; using System; -namespace PSO2SERVER.LuaEngine +namespace PSO2SERVER.LuaScriptEngine { - public class LuaScriptEngine + public class LuaEngine { private Lua lua; - public LuaScriptEngine() + public LuaEngine() { // 初始化 LuaEngine 解释器 lua = new Lua(); @@ -55,16 +55,83 @@ namespace PSO2SERVER.LuaEngine } // 使用 MethodBase 注册方法 - public void RegisterFunction(string luaFunctionName, object obj, string methodName) + public void RegisterFunction(object obj, string luaFunctionName, string methodName) { var methodInfo = obj.GetType().GetMethod(methodName); lua.RegisterFunction(luaFunctionName, obj, methodInfo); } + // 注册委托函数到 Lua + public void RegisterFunction(string luaFunctionName, Delegate function) + { + // 使用反射将委托作为方法注册 + var method = function.Method; + lua.RegisterFunction(luaFunctionName, null, method); + } + + // 清理 Lua 环境 + public void Close() + { + lua.Close(); + } + // 清理 LuaEngine 解释器 public void Dispose() { lua.Dispose(); } + + // 示例方法 + public int AddNumbers(int a, int b) + { + return a + b; + } + + // 示例方法 + public void print(string text, params object[] args) + { + Logger.Write(text, args); + } + + public void printobj(object obj) + { + Logger.WriteObj(obj); + } + + // 注册函数 + public void RegisterExampleFunction() + { + // 使用反射将方法注册到 Lua + RegisterFunction(this, "AddNumbers", "AddNumbers"); + } + + public void RegisterLogger() + { + // 使用反射将方法注册到 Lua + RegisterFunction(this, "print", "print"); + RegisterFunction(this, "printobj", "printobj"); + } + + public void LusTest() + { + var engine = new LuaEngine(); + engine.RegisterLogger(); + // 注册 C# 函数到 Lua + engine.RegisterExampleFunction(); + + // Lua 脚本 + string luaScript = @" + -- 调用 C# 注册的函数 + result = AddNumbers(10, 20) + print('Result from C#: ' .. result) + printobj(result) + "; + + // 执行 Lua 脚本 + engine.ExecuteScript(luaScript); + + // 关闭 Lua 环境 + engine.Close(); + } } } diff --git a/Server/LuaEngine/LuaScript.cs b/Server/LuaEngine/LuaScript.cs new file mode 100644 index 0000000..3c8b67c --- /dev/null +++ b/Server/LuaEngine/LuaScript.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PSO2SERVER.LuaScriptEngine +{ + internal class LuaScript + { + } +} diff --git a/Server/Program.cs b/Server/Program.cs index 0b92a6c..ec7eeb4 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -19,6 +19,7 @@ using PSO2SERVER.Zone; using PSO2SERVER.Json; using PSO2SERVER.Models; using NLua; +using PSO2SERVER.LuaScriptEngine; namespace PSO2SERVER { @@ -32,7 +33,7 @@ namespace PSO2SERVER public const string ServerAuthor = "Sancaros (https://github.com/Sancaros/PSO2SERVER)"; public const string ServerCopyright = "(C) 2024 Sancaros."; public const string ServerLicense = "All licenced under AGPL."; - public const string ServerVersion = "v0.1.3"; + public const string ServerVersion = "v0.1.4"; public const string ServerVersionName = "Sancaros"; public const int ServerShipProtNums = 10; @@ -96,26 +97,6 @@ namespace PSO2SERVER Console.CancelKeyPress += Exit; AppDomain.CurrentDomain.ProcessExit += Exit; - //JsonTest.JsonReadTest(); - - //using (var state = new LuaEngine()) - //{ - // //Retrieving LuaEngine functions: - // state.DoString(@"function ScriptFunc (val1, val2) - // if val1 > val2 then - // return val1 + 1 - // else - // return val2 - 1 - // end - // end - // "); - // var scriptFunc = state["ScriptFunc"] as LuaFunction; - // var funcRes = scriptFunc.Call(3, 5).First(); - // Console.WriteLine($"Func result:{funcRes}"); - - // Console.ReadKey(); - //} - try { for (var i = 0; i < args.Length; i++) @@ -233,6 +214,9 @@ namespace PSO2SERVER Server = new Server(); + Server.LuaEngine.RegisterLogger(); + //Server.LuaEngine.LusTest(); + await InitializeConfigurationAsync(); await InitializeDatabaseAsync(); diff --git a/Server/Server.cs b/Server/Server.cs index de4ac3e..b1ec6df 100644 --- a/Server/Server.cs +++ b/Server/Server.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; using System.Timers; - +using PSO2SERVER.LuaScriptEngine; using PSO2SERVER.Network; using PSO2SERVER.Protocol.Packets; @@ -10,22 +10,25 @@ namespace PSO2SERVER public class Server { public static Server Instance { get; private set; } - public string _name { get; private set; } - private readonly SocketServer _server; - public List Clients { get; private set; } public DateTime StartTime { get; private set; } + + public LuaEngine LuaEngine { get; private set; } + + public List Clients { get; private set; } public Timer PingTimer; public Server() { - Clients = new List(); + Instance = this; _name = ServerApp.ServerBlockBalanceName; _server = new SocketServer(ServerApp.ServerBlockBalanceProt); - _server.NewClient += HandleNewClient; - Instance = this; StartTime = DateTime.Now; + LuaEngine = new LuaEngine(); + + _server.NewClient += HandleNewClient; + Clients = new List(); PingTimer = new Timer(1000 * ServerApp.Config.PingTime); // 1 Minute default PingTimer.Elapsed += PingClients; diff --git a/Server/Server.csproj b/Server/Server.csproj index 20425b8..9ba2479 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -314,6 +314,7 @@ +