-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaScript.cs
More file actions
73 lines (61 loc) · 2.16 KB
/
LuaScript.cs
File metadata and controls
73 lines (61 loc) · 2.16 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
using Microsoft.Xna.Framework;
using MoonSharp.Interpreter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Script = MoonSharp.Interpreter.Script;
#nullable enable
namespace HatsPlusPlus;
internal class LuaScript {
internal Script value = null!;
internal DynValue state = null!;
internal static LuaScript New(string path) {
var luaScript = new LuaScript();
//HACK: !!! VERY IMPORANT !!! Ensure lua scripts are properly sandboxed. We dont want malware in our hats!
luaScript.value = new Script(CoreModules.Preset_Complete);
luaScript.value.Globals["PATH_DELIMETER"] = "\\";
LuaUtils.LoadApi(luaScript);
//TODO: what if lua script has errors?
luaScript.state = luaScript.value.DoFile(path);
return luaScript;
}
public static implicit operator Script(LuaScript input) {
return input.value;
}
internal void ProtectedCall(string functionName, params object[] args) {
try {
var functionTable = state.Table.Get(functionName);
if (functionTable.Function is var fn && fn is not null) {
fn.Call(args);
}
} catch (ScriptRuntimeException e) {
LuaLogger.Error($"{e.DecoratedMessage ?? e.Message}");
}
}
internal void TryProtectedCall(string functionName, params object[] args) {
try {
var functionTable = state.Table.Get(functionName);
if (functionTable.Function is var fn && fn is not null) {
fn.Call(args);
} else {
LuaLogger.Warn($"Attempted to call a missing function {functionName}");
}
} catch (ScriptRuntimeException e) {
LuaLogger.Error($"{e.DecoratedMessage ?? e.Message}");
}
}
internal void SetImagesPath(string path) {
value.Globals["imagesPath"] = path;
}
internal void Select() {
ProtectedCall("select");
}
internal void Spawn() {
ProtectedCall("spawn");
}
internal void Update(params object[] args) {
ProtectedCall("update", args);
}
}