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;