70 lines
1.7 KiB
C#
70 lines
1.7 KiB
C#
using Godot;
|
|
using NLua;
|
|
using System;
|
|
using System.Text;
|
|
|
|
public class GlobalModManager
|
|
{
|
|
public readonly Lua lua = new();
|
|
private readonly string modsPath;
|
|
|
|
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))
|
|
return;
|
|
|
|
using var dir = DirAccess.Open(modsPath);
|
|
if (dir == null) return;
|
|
|
|
dir.ListDirBegin();
|
|
string fileName = dir.GetNext();
|
|
while (!string.IsNullOrEmpty(fileName))
|
|
{
|
|
if (fileName.EndsWith(".lua"))
|
|
{
|
|
LoadMod(fileName);
|
|
}
|
|
fileName = dir.GetNext();
|
|
}
|
|
}
|
|
|
|
private void InitSandBox() {
|
|
}
|
|
|
|
private void LoadMod(string filePath)
|
|
{
|
|
try
|
|
{
|
|
string luaCode = LoadFile(filePath);
|
|
lua.DoString(luaCode);
|
|
GD.Print($"成功加载Mod: {filePath}");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
GD.PrintErr($"加载Mod {filePath} 失败: {e.Message}");
|
|
}
|
|
}
|
|
|
|
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)
|
|
{
|
|
using var file = FileAccess.Open(path, FileAccess.ModeFlags.Read);
|
|
string content = file.GetAsText();
|
|
return content;
|
|
}
|
|
}
|