|
14 | 14 | /// <summary>
|
15 | 15 | /// Utility for interacting with grid-server Lua.
|
16 | 16 | /// </summary>
|
17 |
| -public class LuaUtility : ILuaUtility |
| 17 | +public partial class LuaUtility : ILuaUtility |
18 | 18 | {
|
19 | 19 | private static readonly Assembly _assembly = Assembly.GetExecutingAssembly();
|
20 | 20 | private const string _luaVmResource = "Grid.Bot.Lua.LuaVMTemplate.lua";
|
21 | 21 |
|
22 |
| - private static string FixFormatString(string input) |
| 22 | + [GeneratedRegex(@"{{(\d{1,2})}}", RegexOptions.IgnoreCase | RegexOptions.Compiled)] |
| 23 | + private static partial Regex FormatPartRegex(); |
| 24 | + |
| 25 | + |
| 26 | + private static readonly string _LuaVM; |
| 27 | + |
| 28 | + static LuaUtility() |
23 | 29 | {
|
24 |
| - //language=regex |
25 |
| - const string partRegex = @"{{(\d{1,2})}}"; |
| 30 | + using var stream = _assembly.GetManifestResourceStream(_luaVmResource); |
| 31 | + using var reader = new StreamReader(stream); |
26 | 32 |
|
| 33 | + _LuaVM = FixFormatString(reader.ReadToEnd()); |
| 34 | + } |
| 35 | + |
| 36 | + private static string FixFormatString(string input) |
| 37 | + { |
27 | 38 | input = input.Replace("{", "{{");
|
28 | 39 | input = input.Replace("}", "}}");
|
29 | 40 |
|
30 |
| - input = Regex.Replace(input, partRegex, (m) => { return $"{{{m.Groups[1]}}}"; }); |
| 41 | + input = FormatPartRegex().Replace(input, (m) => { return $"{{{m.Groups[1]}}}"; }); |
31 | 42 |
|
32 | 43 | return input;
|
33 | 44 | }
|
34 | 45 |
|
35 | 46 | /// <inheritdoc cref="ILuaUtility.LuaVMTemplate"/>
|
36 |
| - public string LuaVMTemplate |
37 |
| - { |
38 |
| - get { |
39 |
| - using var stream = _assembly.GetManifestResourceStream(_luaVmResource); |
40 |
| - using var reader = new StreamReader(stream); |
41 |
| - |
42 |
| - return FixFormatString(reader.ReadToEnd()); |
43 |
| - } |
44 |
| - } |
| 47 | + public string LuaVMTemplate => _LuaVM; |
45 | 48 |
|
46 | 49 | /// <inheritdoc cref="ILuaUtility.ParseResult(IEnumerable{LuaValue})"/>
|
47 | 50 | public (string result, ReturnMetadata metadata) ParseResult(IEnumerable<LuaValue> result)
|
48 | 51 | {
|
| 52 | + if (result.Count() == 1) |
| 53 | + { |
| 54 | + // Legacy case, where LuaVM is not enabled. |
| 55 | + |
| 56 | + var mockMetadata = new ReturnMetadata(); |
| 57 | + mockMetadata.Success = true; |
| 58 | + |
| 59 | + return ((string)Lua.ConvertLua(result.First()), mockMetadata); |
| 60 | + } |
| 61 | + |
49 | 62 | return (
|
50 |
| - (string)Lua.ConvertLua(result.FirstOrDefault()), |
| 63 | + (string)Lua.ConvertLua(result.FirstOrDefault()), |
51 | 64 | JsonConvert.DeserializeObject<ReturnMetadata>((string)Lua.ConvertLua(result.ElementAtOrDefault(1)))
|
52 | 65 | );
|
53 | 66 | }
|
|
0 commit comments