update: 更新到godot4.4.1,并大量重构代码
This commit is contained in:
40
Scripts/Mods/LuaModManager.cs
Normal file
40
Scripts/Mods/LuaModManager.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Godot;
|
||||
using NLua;
|
||||
|
||||
class LuaMod : IDisposable {
|
||||
private readonly Lua lua = new();
|
||||
|
||||
public LuaMod(string path, string? package_path = null) {
|
||||
lua.LoadCLRPackage();
|
||||
lua.State.Encoding = System.Text.Encoding.UTF8;
|
||||
lua["package.path"] = package_path ?? Path.GetDirectoryName(path) + "/?.lua;";
|
||||
lua.RegisterFunction("print", new Action<object[]>(GD.Print).Method);
|
||||
// Action bar = () => { GD.Print("bar"); };
|
||||
// lua.RegisterFunction("bar", bar.Target, bar.Method);
|
||||
lua.DoFile(path);
|
||||
}
|
||||
|
||||
public void BindFunc(ModManager mod) {
|
||||
foreach (var api in mod.GetAPIs()) {
|
||||
lua.RegisterFunction(api.Key, api.Value.Target, api.Value.Method);
|
||||
}
|
||||
foreach (var hook in mod.GetHooks()) {
|
||||
if (lua[hook.Key] is LuaFunction func) {
|
||||
mod.AddHook(hook.Key, (args) => {
|
||||
var rets = func.Call(args).FirstOrDefault();
|
||||
if (rets is bool b) return b;
|
||||
return false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
lua?.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user