-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaLoader.cs
More file actions
85 lines (72 loc) · 2.1 KB
/
LuaLoader.cs
File metadata and controls
85 lines (72 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using NLua;
namespace CSharpLuaIntegration;
/**
* LuaLoader class for managing Lua instances
*/
class LuaLoader : IDisposable
{
private readonly Lua lua;
public Lua LuaInstance => lua;
public LuaLoader()
{
lua = new Lua();
lua.LoadCLRPackage();
}
public void LoadLuaScript(string luaScriptPath) {
if (!File.Exists(luaScriptPath))
{
MessageBox.Show("Lua script file not found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
lua.DoFile(luaScriptPath);
}
public void ExecuteLuaScript(string luaScriptPath)
{
try
{
lua.LoadCLRPackage();
LoadLuaScript(luaScriptPath);
if (!ValidateLuaScriptStructure(luaScriptPath))
{
MessageBox.Show("Invalid Lua script structure.",
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
LuaFunction mainFunction = lua.GetFunction("Main");
object[] result = mainFunction.Call();
bool success = Convert.ToBoolean(result[0]);
MessageBox.Show(success ? "Plugins executed." : "Plugins failed to execute.",
"Execution Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Error executing Lua script: {ex.Message}",
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private bool ValidateLuaScriptStructure(string luaScriptPath)
{
if (!File.Exists(luaScriptPath))
{
return false;
}
lua.DoFile(luaScriptPath);
return lua.GetFunction("Main") != null;
}
public string? GetPluginName()
{
return lua["PluginName"] as string;
}
public string? GetPluginDescription()
{
return lua["PluginDescription"] as string;
}
public void Dispose()
{
lua?.Dispose();
}
}