-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLuaExecutor.cs
More file actions
115 lines (99 loc) · 3.62 KB
/
LuaExecutor.cs
File metadata and controls
115 lines (99 loc) · 3.62 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System.Text;
namespace GTerm
{
internal class LuaExecutor
{
private readonly CommandCollector Collector;
private static readonly string GTermLuaDir = "lua/gterm";
internal LuaExecutor(CommandCollector collector)
{
this.Collector = collector;
}
public async Task<ExecuteLuaResult> ExecuteLuaAsync(string luaCode, int? customCollectionWindowMs = null, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(luaCode))
{
return new ExecuteLuaResult
{
Success = false,
Error = "Lua code cannot be empty"
};
}
if (!GmodInterop.TryGetGmodPath(out string gmodPath, false))
{
return new ExecuteLuaResult
{
Success = false,
Error = "Could not find Garry's Mod installation path"
};
}
string gtermDir = Path.Combine(gmodPath, "garrysmod", GTermLuaDir);
string fileName = $"{Guid.NewGuid()}.lua";
string filePath = Path.Combine(gtermDir, fileName);
try
{
if (!Directory.Exists(gtermDir))
{
LocalLogger.WriteLine($"Creating directory: {gtermDir}");
Directory.CreateDirectory(gtermDir);
}
LocalLogger.WriteLine($"Writing Lua code to: {filePath}");
await File.WriteAllTextAsync(filePath, luaCode, cancellationToken);
string command = $"lua_openscript_cl gterm/{fileName}";
LocalLogger.WriteLine($"Executing command: {command}");
CommandResult result = await this.Collector.ExecuteCommandAsync(command, customCollectionWindowMs, cancellationToken);
try
{
if (File.Exists(filePath))
{
LocalLogger.WriteLine($"Cleaning up: {filePath}");
File.Delete(filePath);
}
}
catch (Exception ex)
{
LocalLogger.WriteLine($"Warning: Failed to delete temp file: {ex.Message}");
}
if (!result.Success)
{
return new ExecuteLuaResult
{
Success = false,
Error = result.Error ?? "Command execution failed"
};
}
return new ExecuteLuaResult
{
Success = true,
FileName = fileName,
Output = result.Output,
CollectionDurationMs = result.CollectionDurationMs
};
}
catch (Exception ex)
{
try
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
catch { }
return new ExecuteLuaResult
{
Success = false,
Error = $"Exception: {ex.Message}"
};
}
}
}
internal class ExecuteLuaResult
{
public bool Success { get; set; }
public string? FileName { get; set; }
public List<OutputLine> Output { get; set; } = [];
public double CollectionDurationMs { get; set; }
public string? Error { get; set; }
}
}