-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
286 lines (239 loc) · 8.85 KB
/
Copy pathProgram.cs
File metadata and controls
286 lines (239 loc) · 8.85 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Globalization;
namespace zoneToTrigger;
class Program
{
public static string filepath = string.Empty;
public static string triggersType = string.Empty;
public static Dictionary<string, StringBuilder> triggers = [];
public static List<Region> checkpoints = [];
public static List<Region> cancels = [];
static void Main(string[] args)
{
if (args.Length < 2)
{
string helpMessage = @"Provide the name of a configuration file followed by a path to the .json file
Example 1: ./zoneToTrigger rj test.json
Example 2: zoneToTrigger.exe df.ini ./zones/test.json
.ini file must be present in the executable directory to be loaded, providing the extension is not required";
Console.WriteLine(helpMessage);
return;
}
triggersType = Directory.EnumerateFiles(Path.Combine(AppContext.BaseDirectory, "config"))
.FirstOrDefault(f => Path.GetFileName(f).StartsWith(args[0], StringComparison.OrdinalIgnoreCase));
if (!File.Exists(triggersType))
{
Console.WriteLine($"Configuration file {args[0]} doesn't exist");
return;
}
filepath = args[^1];
triggers = ParseIni(triggersType);
if (!File.Exists(filepath) || !filepath.EndsWith(".json"))
{
Console.WriteLine($"File can't be loaded, exiting: {filepath}");
return;
}
try
{
string json = File.ReadAllText(filepath);
using var doc = JsonDocument.Parse(json);
var segment = doc.RootElement
.GetProperty("tracks")
.GetProperty("main")
.GetProperty("zones")
.GetProperty("segments")[0];
var checkpointsJson = segment.GetProperty("checkpoints").GetRawText();
if (!string.IsNullOrEmpty(checkpointsJson))
{
var checkpointsArray = JsonDocument.Parse(checkpointsJson).RootElement.EnumerateArray();
foreach (var checkpoint in checkpointsArray)
{
var regionsJson = checkpoint.GetProperty("regions").GetRawText();
var regions = JsonSerializer.Deserialize<List<Region>>(regionsJson)!;
checkpoints.AddRange(regions);
}
checkpoints.RemoveAt(0);
}
else
{
Console.WriteLine("No checkpoints detected!");
Environment.Exit(1);
}
// Extract and deserialize cancels
string cancelsJson = string.Empty;
try
{
cancelsJson = segment.GetProperty("cancel").GetRawText();
}
catch { }
if (!string.IsNullOrEmpty(cancelsJson))
{
var cancelsArray = JsonDocument.Parse(cancelsJson).RootElement.EnumerateArray();
foreach (var cancel in cancelsArray)
{
var regionsJson = cancel.GetProperty("regions").GetRawText();
var regions = JsonSerializer.Deserialize<List<Region>>(regionsJson)!;
cancels.AddRange(regions);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
Console.WriteLine($"Stack Trace: {ex.StackTrace}");
return;
}
int i = 0;
string guid = Guid.NewGuid().ToString("N")[..10];
foreach (var checkpoint in checkpoints)
{
checkpoint.targetname = $"checkpoint_{i + 1}_{guid}";
checkpoint.zoneToTrigger = $"created_by_zoneToTrigger";
checkpoint.GetValues();
i++;
}
i = 0;
foreach (var cancel in cancels)
{
cancel.targetname = $"cancel_{i + 1}_{guid}";
cancel.zoneToTrigger = $"created_by_zoneToTrigger";
cancel.GetValues();
i++;
}
WriteToFile(guid);
}
public static void WriteToFile(string guid)
{
string cfgFilePath = filepath[..^5] + ".cfg";
string logicOutput = "add:\n{\n";
logicOutput += "\"classname\" \"logic_auto\"\n";
logicOutput += "\"spawnflags\" \"0\"\n";
logicOutput += $"\"targetname\" \"{guid}\"\n";
logicOutput += "\"zoneToTrigger\" \"created_by_zoneToTrigger\"\n";
logicOutput += FormattableString.Invariant($"\"origin\" \"{checkpoints[0].origin[0]} {checkpoints[0].origin[1]} {checkpoints[0].origin[2]}\"\n");
using StreamWriter writer = new(cfgFilePath);
if (checkpoints.Count > 0)
{
if (!triggers.ContainsKey("checkpoint"))
{
Console.Error.WriteLine($"WARNING: Checkpoint zones are not defined.");
}
else
{
foreach (var checkpoint in checkpoints)
{
writer.Write(checkpoint.AddTrigger("checkpoint"));
logicOutput += checkpoint.AddLogicOutput();
}
}
}
if (cancels.Count > 0)
{
if (!triggers.ContainsKey("cancel"))
{
Console.Error.WriteLine($"WARNING: Cancel zones are not defined.");
}
else
{
foreach (var cancel in cancels)
{
writer.Write(cancel.AddTrigger("cancel"));
logicOutput += cancel.AddLogicOutput();
}
}
}
logicOutput += "}";
writer.Write(logicOutput);
}
public static Dictionary<string, StringBuilder> ParseIni(string filepath)
{
string[] lines = File.ReadAllLines(filepath);
var sections = new Dictionary<string, StringBuilder>();
string current = null;
foreach (var line in lines)
{
if (line.StartsWith('[') && line.EndsWith(']'))
{
current = line.Trim('[', ']').ToLower();
sections[current] = new StringBuilder();
}
else if (current != null)
{
sections[current].AppendLine(line);
}
}
return sections;
}
}
public class Region
{
[JsonPropertyName("points")]
public List<List<double>> Points { get; set; }
[JsonPropertyName("bottom")]
public double Bottom { get; set; }
[JsonPropertyName("height")]
public double Height { get; set; }
//Helpers
public List<double> origin = [];
public string mins = string.Empty;
public string maxs = string.Empty;
public string targetname = string.Empty;
public string zoneToTrigger = string.Empty;
public void GetValues()
{
if (Points.Count != 4)
{
Console.WriteLine("Region doesn't have exactly 4 points, exiting");
Environment.Exit(1);
}
GetOrigin();
GetMinsAndMaxs();
}
public void GetOrigin()
{
List<double> point1 = Points[0];
List<double> point2 = Points[2];
double midX = Math.Round((point1[0] + point2[0]) / 2, 3);
double midY = Math.Round((point1[1] + point2[1]) / 2, 3);
double midZ = Math.Round(Bottom + (Height / 2), 3);
origin = [midX, midY, midZ];
}
public void GetMinsAndMaxs()
{
List<double> point1 = Points[0];
List<double> point2 = Points[2];
double halfSizeX = Math.Round(Math.Abs(point1[0] - point2[0]) / 2, 3);
double halfSizeY = Math.Round(Math.Abs(point1[1] - point2[1]) / 2, 3);
double halfSizeZ = Math.Round(Height / 2, 3);
maxs = FormattableString.Invariant($"maxs {halfSizeX} {halfSizeY} {halfSizeZ}");
mins = FormattableString.Invariant($"mins {-halfSizeX} {-halfSizeY} {-halfSizeZ}");
}
public string AddTrigger(string section)
{
if (!Program.triggers.TryGetValue(section, out var value))
{
Console.Error.WriteLine($"WARNING: trigger '{section}' is not defined.");
return "";
}
string trigger = value.ToString();
string s = FormattableString.Invariant($"\"origin\" \"{origin![0]} {origin[1]} {origin[2]}\"\n");
s += $"\"targetname\" \"{targetname}\"\n";
s += $"\"zoneToTrigger\" \"{zoneToTrigger}\"\n";
int index = trigger.LastIndexOf('}');
if (index != -1)
{
trigger = trigger.Insert(index, s);
}
return trigger;
}
public string AddLogicOutput()
{
string s = "";
s += $"\"OnMapSpawn\" \"{targetname},AddOutput,solid 2,0.5,1\"\n";
s += $"\"OnMapSpawn\" \"{targetname},AddOutput,{mins},1,1\"\n";
s += $"\"OnMapSpawn\" \"{targetname},AddOutput,{maxs},1,1\"\n";
return s;
}
}