完善Lua的支持并服务器保持lua实例
This commit is contained in:
parent
48dbe8785c
commit
9f16542f51
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
12
Server/LuaEngine/LuaScript.cs
Normal file
12
Server/LuaEngine/LuaScript.cs
Normal file
@ -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
|
||||
{
|
||||
}
|
||||
}
|
@ -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();
|
||||
|
@ -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<Client> Clients { get; private set; }
|
||||
public DateTime StartTime { get; private set; }
|
||||
|
||||
public LuaEngine LuaEngine { get; private set; }
|
||||
|
||||
public List<Client> Clients { get; private set; }
|
||||
public Timer PingTimer;
|
||||
|
||||
public Server()
|
||||
{
|
||||
Clients = new List<Client>();
|
||||
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<Client>();
|
||||
|
||||
PingTimer = new Timer(1000 * ServerApp.Config.PingTime); // 1 Minute default
|
||||
PingTimer.Elapsed += PingClients;
|
||||
|
@ -314,6 +314,7 @@
|
||||
<Compile Include="Json\QuestJson.cs" />
|
||||
<Compile Include="Logger.cs" />
|
||||
<Compile Include="LuaEngine\LuaEngine.cs" />
|
||||
<Compile Include="LuaEngine\LuaScript.cs" />
|
||||
<Compile Include="Models\BattleStats.cs" />
|
||||
<Compile Include="Models\BlockInfo.cs" />
|
||||
<Compile Include="Models\CharacterAdditionalStruct.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user