forked from gro-ove/actools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LuaHelper.cs
65 lines (53 loc) · 2.23 KB
/
LuaHelper.cs
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
using System;
using System.Linq;
using AcTools.Processes;
using AcTools.Utils.Helpers;
using FirstFloor.ModernUI.Helpers;
using JetBrains.Annotations;
using MoonSharp.Interpreter;
namespace AcManager.Tools.Miscellaneous {
public static class LuaHelper {
public static bool CompareStrings(string a, string b) {
return string.Equals(a, b, StringComparison.InvariantCulture);
}
public static bool CompareStringsIgnoringCase(string a, string b) {
return string.Equals(a, b, StringComparison.InvariantCultureIgnoreCase);
}
public static double GetNumberValue(string s) {
return FlexibleParser.TryParseDouble(s) ?? double.NaN;
}
public static void Log(params object[] str) {
Logging.Write("Lua: " + str.Select(x => $"“{x}”").JoinToString(", "));
}
private static bool _registered;
public static object ToMoonSharp<T>() where T : struct {
return Enum.GetValues(typeof(T)).OfType<T>().ToDictionary(x => x.ToString(), x => (int)(object)x);
}
[CanBeNull]
public static Script GetExtended() {
try {
if (!_registered) {
UserData.RegisterAssembly();
_registered = true;
}
var state = new Script();
state.Globals["log"] = (Action<object[]>)Log;
state.Globals["numutils"] = new Table(state) {
["numvalue"] = (Func<string, double>)GetNumberValue
};
state.Globals["strutils"] = new Table(state) {
["equals"] = (Func<string, string, bool>)CompareStrings,
["equals_i"] = (Func<string, string, bool>)CompareStringsIgnoringCase
};
state.Globals[@"SessionType"] = ToMoonSharp<Game.SessionType>();
return state;
} catch (Exception e) {
Logging.Warning("Can’t initialize: " + e);
return null;
}
}
public static bool AsBool(this DynValue value) {
return (value.Type != DataType.String || value.String != @"0") && value.CastToBool();
}
}
}