45 lines
1.3 KiB
C#
45 lines
1.3 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using NLua;
|
|
|
|
public class LuaModManager : IDisposable {
|
|
private readonly Lua lua = new();
|
|
private static readonly Logging.Logger logger = Logging.GetLogger("LuaMod");
|
|
|
|
public LuaModManager(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[]>(Godot.GD.Print).Method);
|
|
|
|
logger.Info("Loading Lua Mod: " + path);
|
|
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);
|
|
}
|
|
|
|
public static void WriteComment(Delegate @delegate) {
|
|
// TODO
|
|
}
|
|
}
|
|
|