-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGmicFilterParser.cs
402 lines (347 loc) · 15.8 KB
/
GmicFilterParser.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace GmicAnimate
{
public class Filter
{
public string FriendlyName { get; set; }
public string GmicCommand { get; set; }
public List<Parameter> Parameters { get; set; } = new List<Parameter>();
}
public class Parameter
{
public string Name { get; set; }
public string Type { get; set; }
public dynamic DefaultValue { get; set; }
public dynamic MinValue { get; set; }
public dynamic MaxValue { get; set; }
public List<string> Choices { get; set; }
public List<dynamic> Values { get; set; } = new List<dynamic>(); // To store multiple values if needed
public Dictionary<string, object> Properties { get; set; } = new Dictionary<string, object>(); // Additional properties
}
internal class GmicFilterParser
{
//int currentLineNum = 0; // Used in debugging
private static readonly HashSet<string> ExcludedFilters = new HashSet<string>
{
"_none_", // For non-functional "filters"
"gui_download_all_data" // Specific filter commands that should be ignored
};
public string ParseFiltersToJSON(string[] lines)
{
List<Filter> filters = new List<Filter>();
Filter currentFilter = null;
try
{
foreach (var line in lines)
{
if (line.StartsWith("#@gui") && !IsExcludedLine(line))
{
if (line.Contains(':') && !line.Trim().StartsWith("#@gui :"))
{
// New filter definition
string trimmedLine = Regex.Replace(line.Substring(6), @"<[^>]+>", String.Empty).Trim();
int colonIndex = trimmedLine.IndexOf(':');
string filterName = trimmedLine.Substring(0, colonIndex).Trim();
string[] commandParts = trimmedLine.Substring(colonIndex + 1).Split(',');
string command = commandParts[0].Trim();
if (ExcludedFilters.Contains(command) || command == "_none_")
{
currentFilter = null; // Reset current filter on special commands or non-processing filters
continue;
}
currentFilter = new Filter
{
FriendlyName = filterName,
GmicCommand = command
};
filters.Add(currentFilter);
}
else if (line.Trim().StartsWith("#@gui :") && currentFilter != null)
{
// Parameter of the current filter
string paramLine = line.Substring(6).Trim();
Parameter param = ParseParameter(paramLine);
if (param != null)
{
SplitIfRequired(param, currentFilter.Parameters); // Instead of directly adding, split if required
}
}
}
else if (line.StartsWith("#@gui _") || line.StartsWith("#@gui :_="))
{
// Handle transitions for special GUI elements or reset filter context
currentFilter = null; // Reset on new sections or specific non-parameter GUI elements
}
}
}
catch (Exception e)
{
return e.Message;
}
// Write the JSON to a file
string outputJsonFileName = "FiltersParameterList.json";
File.WriteAllText(outputJsonFileName, JsonConvert.SerializeObject(filters, Newtonsoft.Json.Formatting.Indented));
return "Successfully parsed filter file to JSON as FiltersParameterList.json";
}
private bool IsExcludedLine(string line)
{
return line.Contains("gui_ja") || line.Contains("gui_ca") || line.Contains("gui_fr") ||
line.Contains("gui_es") || line.Contains("gui_de"); // Extend with other language codes as needed
}
private Parameter ParseParameter(string paramLine)
{
paramLine = paramLine.Trim();
if (paramLine.StartsWith(":_="))
{
return null; // Skip GUI structural elements
}
int equalsIndex = paramLine.IndexOf('=');
if (equalsIndex == -1) return null; // Properly formed parameter lines have an '='
string name = paramLine.Substring(1, equalsIndex - 1).Trim();
string value = paramLine.Substring(equalsIndex + 1).Trim();
int startArgs = value.IndexOfAny(new char[] { '(', '{', '[' });
if (startArgs == -1) return null; // Malformed parameter definition
char openBracket = value[startArgs];
char closeBracket = (openBracket == '(' ? ')' : openBracket == '{' ? '}' : ']');
int endArgs = FindMatchingBracket(value, startArgs, openBracket, closeBracket);
if (endArgs == -1) return null; // Handle unmatched brackets
string type = value.Substring(0, startArgs).Trim();
string args = value.Substring(startArgs + 1, endArgs - startArgs - 1).Trim();
// Track if it has a tilde or underscore character, to add it back later
bool hasTilde = false;
bool hasUnderscore = false;
if (type.StartsWith("~"))
{
hasTilde = true;
type = type.Substring(1); // Remove tilde if present
}
if (type.StartsWith("_"))
{
hasUnderscore = true;
type = type.Substring(1); // Remove underscore if present
}
if (type.Equals("link") || type.Equals("separator") || type.Equals("note"))
{
return null; // Skip non-parameter GUI elements
}
Parameter param = new Parameter
{
Name = name,
Type = type
};
// Special handling for point type
if (type == "point")
{
param.DefaultValue = args; // Set the entire argument string as DefaultValue
param.Properties["OriginalType"] = "point"; // Store the original complex type
}
else if (type == "choice" || type == "file" || type == "file_in" || type == "file_out")
{
param = HandleChoiceType(param, args);
}
else if (type == "text")
{
param.Properties["TextString"] = args.Trim('"'); // Store the text string for text type parameters
}
else
{
param = HandleStandardType(param, args);
if (name.Contains("(%)"))
{
param.Properties["IsPercentage"] = true; // Mark parameter as percentage
}
}
// Add back tilde or underscore back to type if it was removed
if (hasTilde)
{
param.Type = "~" + param.Type;
}
if (hasUnderscore)
{
param.Type = "_" + param.Type;
}
return param;
}
private int FindMatchingBracket(string value, int startIndex, char openBracket, char closeBracket)
{
int depth = 0;
for (int i = startIndex; i < value.Length; i++)
{
if (value[i] == openBracket) depth++;
if (value[i] == closeBracket) depth--;
if (depth == 0) return i;
}
return -1; // No matching bracket found
}
private Parameter HandleChoiceType(Parameter param, string args)
{
// This regex will match commas only if they're outside of quotes
var choices = Regex.Split(args, ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)")
.Select(s => s.Trim().Trim('"')) // Trim spaces and quotes
.ToList();
if (choices.Count > 0)
{
if (int.TryParse(choices[0], out int defaultIndex))
{
param.DefaultValue = defaultIndex;
choices.RemoveAt(0); // Remove the default index from choices list
}
else
{
param.DefaultValue = 0; // Default to the first choice if parsing index fails
}
}
param.Choices = choices;
return param;
}
private Parameter HandleStandardType(Parameter param, string args)
{
string[] parts = args.Split(',');
switch (param.Type)
{
case "int":
case "float":
if (parts.Length == 3)
{
param.DefaultValue = Convert.ToDouble(parts[0]);
param.MinValue = Convert.ToDouble(parts[1]);
param.MaxValue = Convert.ToDouble(parts[2]);
}
else if (parts.Length == 1)
{
param.DefaultValue = Convert.ToDouble(parts[0]);
}
break;
case "bool":
param.DefaultValue = parts[0] == "1" || parts[0].ToLower() == "true";
break;
case "color":
// Directly assign the string for color parameters
if (parts.Length >= 1)
{
param.DefaultValue = parts[0].Trim('"');
}
break;
}
return param;
}
private void SplitIfRequired(Parameter param, List<Parameter> parameters)
{
string cleanType = param.Type.TrimStart('~').TrimStart('_');
if (cleanType == "color")
{
SplitColorHexToRGBA(param, parameters);
}
else if (cleanType == "point")
{
SplitPointParameters(param, parameters);
}
else
{
parameters.Add(param);
}
}
private void SplitColorHexToRGBA(Parameter param, List<Parameter> parameters)
{
if (param.DefaultValue is string hexColor)
{
// Check hexColor length to determine if it's RGB (#RRGGBB) or RGBA (#RRGGBBAA)
if (hexColor.StartsWith("#")) hexColor = hexColor.Substring(1); // Remove the # if present
string originalType = param.Type;
if (hexColor.Length == 6 || hexColor.Length == 8)
{
List<int> rgbaValues = new List<int>
{
Convert.ToInt32(hexColor.Substring(0, 2), 16), // Red
Convert.ToInt32(hexColor.Substring(2, 2), 16), // Green
Convert.ToInt32(hexColor.Substring(4, 2), 16) // Blue
};
if (hexColor.Length == 8)
{
rgbaValues.Add(Convert.ToInt32(hexColor.Substring(6, 2), 16)); // Alpha
}
// Clear the original hex value as we've split it into components
param.DefaultValue = null;
param.Values.Clear(); // Make sure to clear existing values before adding new
// Add parameters for each color component
for (int i = 0; i < rgbaValues.Count; i++)
{
Parameter colorComponentParam = new Parameter
{
Name = $"{param.Name}_{(i == 0 ? "Red" : i == 1 ? "Green" : i == 2 ? "Blue" : "Alpha")}",
Type = originalType,
DefaultValue = rgbaValues[i],
MinValue = 0,
MaxValue = 255
};
param.Values.Add(colorComponentParam);
parameters.Add(colorComponentParam); // Optionally add each as separate parameters if needed
}
}
}
}
private void SplitPointParameters(Parameter param, List<Parameter> parameters)
{
if (param.DefaultValue is string pointValues)
{
var values = pointValues.Split(new char[] { ',' })
.Select(v => v.Trim().TrimEnd('%'))
.ToList();
int count = values.Count;
// Clear existing values if any
param.Values.Clear();
// Handling X and Y, which should be available in every point parameter as basics
if (count > 0) AddSubParameter(param, parameters, "_X", "float", values[0], GUIOnly: false);
if (count > 1) AddSubParameter(param, parameters, "_Y", "float", values[1], GUIOnly: false);
// Optional parameters with safe checking for existence - Need to check if these are only used by the GUI and should be left out
if (count > 2) AddSubParameter(param, parameters, "_Removable", "int", values[2], GUIOnly: true);
if (count > 3) AddSubParameter(param, parameters, "_Burst", "int", values[3], GUIOnly: true);
if (count > 4) AddSubParameter(param, parameters, "_Red", "int", values[4], GUIOnly: true);
if (count > 5) AddSubParameter(param, parameters, "_Green", "int", values[5], GUIOnly: true);
if (count > 6) AddSubParameter(param, parameters, "_Blue", "int", values[6], GUIOnly: true);
if (count > 7) AddSubParameter(param, parameters, "_Alpha", "int", values[7], GUIOnly: true);
if (count > 8) AddSubParameter(param, parameters, "_Radius", "float", values[8], GUIOnly: true);
}
// Store original type for later reference if needed
param.Properties["OriginalType"] = "point";
}
private void AddSubParameter(Parameter parentParam, List<Parameter> parameters, string suffix, string type, string value, bool GUIOnly)
{
var newParam = new Parameter
{
Name = $"{parentParam.Name}{suffix}",
Type = type,
};
switch (type)
{
case "int":
newParam.DefaultValue = Convert.ToInt32(value, CultureInfo.InvariantCulture);
if (suffix == "_Removable" || suffix == "_Burst")
{
// Set 'Trinary' specific properties
newParam.Properties.Add("TypeDetail", "Trinary");
newParam.MinValue = -1;
newParam.MaxValue = 1;
}
break;
case "float":
newParam.DefaultValue = Convert.ToDouble(value, CultureInfo.InvariantCulture);
break;
default:
newParam.DefaultValue = value;
break;
}
if (!GUIOnly)
{
parameters.Add(newParam);
parentParam.Values.Add(newParam);
}
}
}
}