update: 更新到godot4.4.1,并大量重构代码
This commit is contained in:
69
Scripts/Mods/GlobalModManager.cs
Normal file
69
Scripts/Mods/GlobalModManager.cs
Normal file
@ -0,0 +1,69 @@
|
||||
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user