dev: 更新代码,并准备LuaMod

This commit is contained in:
ZZY
2025-06-14 14:38:28 +08:00
parent 3fa39fc71e
commit 13450ea09a
20 changed files with 486 additions and 397 deletions

View File

@ -1,67 +1,70 @@
using Godot;
using NLua;
using System;
using System.Text;
using System.Collections.Generic;
using System.Collections.ObjectModel;
public class GlobalModManager
{
public readonly Lua lua = new();
private readonly string modsPath;
public class GlobalModManager(string modPath = "user://mods") {
public static GlobalModManager Instance { get; } = new();
private readonly Dictionary<string, LuaModManager> _mods = [];
public ReadOnlyDictionary<string, LuaModManager> Mods => new(_mods);
private readonly Dictionary<string, string> _modList = [];
public ReadOnlyDictionary<string, string> ModList => new(_modList);
private readonly string modPath = modPath;
private static readonly Logging.Logger logger = Logging.GetLogger("GlobalMod");
public GlobalModManager(string modsPath = "user://Mods/") {
this.modsPath = modsPath;
lua.State.Encoding = Encoding.UTF8;
// lua.State.SetHook(KeraLua.LuaHookMask.Count, 1000000);
InitSandBox();
// RegisterAPI();
}
public void LoadAllMods()
{
if (!DirAccess.DirExistsAbsolute(modsPath))
public void SearchAllMods(bool isFlush = false) {
if (isFlush) {
_modList.Clear();
}
if (!DirAccess.DirExistsAbsolute(modPath))
return;
using var dir = DirAccess.Open(modsPath);
using var dir = DirAccess.Open(modPath);
if (dir == null) return;
dir.ListDirBegin();
string fileName = dir.GetNext();
while (!string.IsNullOrEmpty(fileName))
{
if (fileName.EndsWith(".lua"))
{
LoadMod(fileName);
while (!string.IsNullOrEmpty(fileName)) {
string fullPath = $"{modPath}/{fileName}";
const string entryFileName = "main.lua";
if (dir.CurrentIsDir() && !fileName.StartsWith('.') &&
FileAccess.FileExists($"{fullPath}/{entryFileName}")) {
_modList.Add($"{fullPath}/{entryFileName}", fullPath);
}
fileName = dir.GetNext();
}
}
private void InitSandBox() {
}
private void LoadMod(string filePath)
{
try
{
string luaCode = LoadFile(filePath);
lua.DoString(luaCode);
GD.Print($"成功加载Mod: {filePath}");
public void LoadAllMods(bool isFlush = false) {
if (isFlush) {
_mods.Clear();
}
catch (Exception e)
{
GD.PrintErr($"加载Mod {filePath} 失败: {e.Message}");
foreach (var mod in _modList) {
try {
LoadMod(mod.Key, mod.Value);
}
catch (System.Exception e) {
logger.Exception("Failed to load mod {0}", e, mod.Key);
}
}
}
private static void SaveFile(string path, string content)
{
public void ClearAllMods() {
_mods.Clear();
}
private void LoadMod(string filePath, string modPath) {
string _filePath = ProjectSettings.GlobalizePath(filePath);
string _modPath = ProjectSettings.GlobalizePath(modPath);
_mods.Add(filePath, new LuaModManager(_filePath, _modPath));
}
private static void SaveFile(string path, string content) {
using var file = FileAccess.Open(path, FileAccess.ModeFlags.Write);
file.StoreString(content);
}
private static string LoadFile(string path)
{
private static string LoadFile(string path) {
using var file = FileAccess.Open(path, FileAccess.ModeFlags.Read);
string content = file.GetAsText();
return content;

View File

@ -1,19 +1,19 @@
using System;
using System.IO;
using System.Linq;
using Godot;
using NLua;
class LuaMod : IDisposable {
public class LuaModManager : IDisposable {
private readonly Lua lua = new();
private static readonly Logging.Logger logger = Logging.GetLogger("LuaMod");
public LuaMod(string path, string? package_path = null) {
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[]>(GD.Print).Method);
// Action bar = () => { GD.Print("bar"); };
// lua.RegisterFunction("bar", bar.Target, bar.Method);
lua.RegisterFunction("print", new Action<object[]>(Godot.GD.Print).Method);
logger.Info("Loading Lua Mod: " + path);
lua.DoFile(path);
}
@ -36,5 +36,9 @@ class LuaMod : IDisposable {
lua?.Dispose();
GC.SuppressFinalize(this);
}
public static void WriteComment(Delegate @delegate) {
// TODO
}
}

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;
class ModManager {
public class ModManager {
private readonly Dictionary<string, Delegate> _api = [];
private readonly Dictionary<string, Delegate> _hooks = [];
public delegate bool OnCallback(params object[] args);