forked from IlliumIv/Stashie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilterParser.cs
455 lines (392 loc) · 19.1 KB
/
FilterParser.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
using System;
using System.Collections.Generic;
using System.Linq;
using ExileCore;
using SharpDX;
namespace Stashie
{
public class FilterParser
{
private const char SYMBOL_COMMANDSDIVIDE = ',';
private const char SYMBOL_COMMAND_FILTER_OR = '|';
private const char SYMBOL_NAMEDIVIDE = ':';
private const char SYMBOL_SUBMENUNAME = ':';
private const char SYMBOL_NOT = '!';
private const string COMMENTSYMBOL = "#";
private const string COMMENTSYMBOLALT = "//";
//String compare
private const string PARAMETER_CLASSNAME = "classname";
private const string PARAMETER_BASENAME = "basename";
private const string PARAMETER_PATH = "path";
private const string PARAMETER_NAME = "name";
private const string PARAMETER_DESCRIPTION = "desc";
private const string PARAMETER_CLUSTERJEWELBASE = "clusterjewelbase";
//Number compare
private const string PARAMETER_QUALITY = "itemquality";
private const string PARAMETER_RARITY = "rarity";
private const string PARAMETER_ILVL = "ilvl";
private const string PARAMETER_MapTier = "tier";
private const string PARAMETER_NUMBER_OF_SOCKETS = "numberofsockets";
private const string PARAMETER_LARGEST_LINK_SIZE = "numberoflinks";
private const string PARAMETER_VEILED = "veiled";
private const string PARAMETER_FRACTUREDMODS = "fractured";
private const string PARAMETER_SKILLGEMLEVEL = "skillgemlevel";
private const string PARAMTER_CLUSTERJEWELPASSIVES = "clusterjewelpassives";
private const string PARAMETER_METAMORPHREWARDSAMOUNT = "sampleStats";
private const string PARAMETER_METAMORPHGOODREWARDSAMOUNT = "goodrewards";
private const string PARAMETER_METAMORPHBADREWARDSAMOUNT = "badrewards";
private const string PARAMETER_DELIRIUMREWARDSAMOUNT = "deliriumrewards";
//Boolean
private const string PARAMETER_IDENTIFIED = "identified";
private const string PARAMETER_ISCORRUPTED = "corrupted";
private const string PARAMETER_ISINFLUENCED = "influenced";
private const string PARAMETER_ISELDER = "Elder";
private const string PARAMETER_ISSHAPER = "Shaper";
private const string PARAMETER_ISCRUSADER = "Crusader";
private const string PARAMETER_ISHUNTER = "Hunter";
private const string PARAMETER_ISREDEEMER = "Redeemer";
private const string PARAMETER_ISWARLORD = "Warlord";
private const string PARAMETER_ISSYNTHESISED = "Synthesised";
private const string PARAMETER_ISBLIGHTEDMAP = "blightedMap";
private const string PARAMETER_ISELDERGUARDIANMAP = "elderGuardianMap";
private const string PARAMETER_AFFINITYACTIVE = "affinity";
private const string PARAMETER_IGNOREAFFINITY = "shifting";
//Operations
private const string OPERATION_NONEQUALITY = "!=";
private const string OPERATION_LESSEQUAL = "<=";
private const string OPERATION_BIGGERQUAL = ">=";
private const string OPERATION_EQUALITY = "=";
private const string OPERATION_BIGGER = ">";
private const string OPERATION_LESS = "<";
private const string OPERATION_CONTAINS = "^";
private const string OPERATION_NOTCONTAINS = "!^";
private const string OPERATION_PLUS = "+"; //maybe not needed yet
private static readonly string[] Operations =
{
OPERATION_NONEQUALITY,
OPERATION_LESSEQUAL,
OPERATION_BIGGERQUAL,
OPERATION_NOTCONTAINS,
OPERATION_EQUALITY,
OPERATION_BIGGER,
OPERATION_LESS,
OPERATION_CONTAINS,
OPERATION_PLUS
};
public static List<CustomFilter> Parse(string[] filtersLines)
{
var allFilters = new List<CustomFilter>();
for (var i = 0; i < filtersLines.Length; ++i)
{
var filterLine = filtersLines[i];
filterLine = filterLine.Replace("\t", "");
if (filterLine.StartsWith(COMMENTSYMBOL)) continue;
if (filterLine.StartsWith(COMMENTSYMBOLALT)) continue;
if (filterLine.Replace(" ", "").Length == 0) continue;
var nameIndex = filterLine.IndexOf(SYMBOL_NAMEDIVIDE);
if (nameIndex == -1)
{
DebugWindow.LogMsg("Filter parser: Can't find filter name in line: " + (i + 1), 5);
continue;
}
var newFilter = new CustomFilter {Name = filterLine.Substring(0, nameIndex).Trim(), Index = i + 1};
var filterCommandsLine = filterLine.Substring(nameIndex + 1);
var submenuIndex = filterCommandsLine.IndexOf(SYMBOL_SUBMENUNAME);
if (submenuIndex != -1)
{
newFilter.SubmenuName = filterCommandsLine.Substring(submenuIndex + 1);
filterCommandsLine = filterCommandsLine.Substring(0, submenuIndex);
}
var filterCommands = filterCommandsLine.Split(SYMBOL_COMMANDSDIVIDE);
newFilter.Commands = filterCommandsLine;
var filterErrorParse = false;
foreach (var command in filterCommands)
{
if (string.IsNullOrEmpty(command.Replace(" ", ""))) continue;
if (command.Contains(SYMBOL_COMMAND_FILTER_OR))
{
var orFilterCommands = command.Split(SYMBOL_COMMAND_FILTER_OR);
var newOrFilter = new BaseFilter {BAny = true};
newFilter.Filters.Add(newOrFilter);
foreach (var t in orFilterCommands)
{
if (ProcessCommand(newOrFilter, t)) continue;
filterErrorParse = true;
break;
}
if (filterErrorParse) break;
}
else
{
if (ProcessCommand(newFilter, command)) continue;
filterErrorParse = true;
break;
}
}
if (!filterErrorParse)
{
allFilters.Add(newFilter);
}
else
{
DebugWindow.LogMsg($"Line: {i + 1}", 5, Color.Red);
}
}
return allFilters;
}
private static bool ProcessCommand(BaseFilter newFilter, string command)
{
command = command.Trim();
if (command.Contains(PARAMETER_AFFINITYACTIVE))
{
var affinityActiveCommand = new AffinityActiveFilter { BAffinityActive = command[0] != SYMBOL_NOT };
newFilter.Filters.Add(affinityActiveCommand);
return true;
}
if (command.Contains(PARAMETER_IGNOREAFFINITY))
{
var affinityIgnoreCommand = new AffinityIgnoreFilter { BAffinityIgnore = command[0] != SYMBOL_NOT };
newFilter.Filters.Add(affinityIgnoreCommand);
return true;
}
if (command.Contains(PARAMETER_IDENTIFIED))
{
var identCommand = new IdentifiedItemFilter {BIdentified = command[0] != SYMBOL_NOT};
newFilter.Filters.Add(identCommand);
return true;
}
if (command.Contains(PARAMETER_ISCORRUPTED))
{
var corruptedCommand = new CorruptedItemFilter {BCorrupted = command[0] != SYMBOL_NOT};
newFilter.Filters.Add(corruptedCommand);
return true;
}
if (command.Contains(PARAMETER_ISELDER))
{
var elderCommand = new ElderItemFiler {isElder = command[0] != SYMBOL_NOT};
newFilter.Filters.Add(elderCommand);
return true;
}
if (command.Contains(PARAMETER_ISSHAPER))
{
var shaperCommand = new ShaperItemFilter {isShaper = command[0] != SYMBOL_NOT};
newFilter.Filters.Add(shaperCommand);
return true;
}
if (command.Contains(PARAMETER_ISCRUSADER))
{
var crusaderCommand = new CrusaderItemFilter {isCrusader = command[0] != SYMBOL_NOT};
newFilter.Filters.Add(crusaderCommand);
return true;
}
if (command.Contains(PARAMETER_ISHUNTER))
{
var hunterCommand = new HunterItemFilter {isHunter = command[0] != SYMBOL_NOT};
newFilter.Filters.Add(hunterCommand);
return true;
}
if (command.Contains(PARAMETER_ISREDEEMER))
{
var redeemerCommand = new RedeemerItemFilter {isRedeemer = command[0] != SYMBOL_NOT};
newFilter.Filters.Add(redeemerCommand);
return true;
}
if (command.Contains(PARAMETER_ISWARLORD))
{
var warordCommand = new WarlordItemFilter {isWarlord = command[0] != SYMBOL_NOT};
newFilter.Filters.Add(warordCommand);
return true;
}
if (command.Contains(PARAMETER_ISINFLUENCED))
{
var influencedCommand = new AnyInfluenceItemFilter {isInfluenced = command[0] != SYMBOL_NOT};
newFilter.Filters.Add(influencedCommand);
return true;
}
if (command.Contains(PARAMETER_ISBLIGHTEDMAP))
{
var blightedMapCommand = new BlightedMapFilter {isBlightMap = command[0] != SYMBOL_NOT};
newFilter.Filters.Add(blightedMapCommand);
return true;
}
if (command.Contains(PARAMETER_ISELDERGUARDIANMAP))
{
var elderGuardianMapCommand = new ElderGuardianMapFilter
{isElderGuardianMap = command[0] != SYMBOL_NOT};
newFilter.Filters.Add(elderGuardianMapCommand);
return true;
}
string parameter;
string operation;
string value;
if (!ParseCommand(command, out parameter, out operation, out value))
{
DebugWindow.LogMsg($"Unknown operation: {command}", 5, Color.Red);
return false;
}
var stringComp = new FilterParameterCompare {CompareString = value};
switch (parameter.ToLower())
{
case PARAMETER_CLASSNAME:
stringComp.StringParameter = data => data.ClassName;
break;
case PARAMETER_BASENAME:
stringComp.StringParameter = data => data.BaseName;
break;
case PARAMETER_NAME:
stringComp.StringParameter = data => data.Name;
break;
case PARAMETER_PATH:
stringComp.StringParameter = data => data.Path;
break;
case PARAMETER_DESCRIPTION:
stringComp.StringParameter = data => data.Description;
break;
case PARAMETER_RARITY:
stringComp.StringParameter = data => data.Rarity.ToString();
break;
case PARAMETER_QUALITY:
stringComp.IntParameter = data => data.ItemQuality;
stringComp.CompareInt = int.Parse(value);
stringComp.StringParameter = data => data.ItemQuality.ToString();
break;
case PARAMETER_MapTier:
stringComp.IntParameter = data => data.MapTier;
stringComp.CompareInt = int.Parse(value);
stringComp.StringParameter = data => data.MapTier.ToString();
break;
case PARAMETER_ILVL:
stringComp.IntParameter = data => data.ItemLevel;
stringComp.CompareInt = int.Parse(value);
stringComp.StringParameter = data => data.ItemLevel.ToString();
break;
case PARAMETER_NUMBER_OF_SOCKETS:
stringComp.IntParameter = data => data.NumberOfSockets;
stringComp.CompareInt = int.Parse(value);
stringComp.StringParameter = data => data.NumberOfSockets.ToString();
break;
case PARAMETER_LARGEST_LINK_SIZE:
stringComp.IntParameter = data => data.LargestLinkSize;
stringComp.CompareInt = int.Parse(value);
stringComp.StringParameter = data => data.LargestLinkSize.ToString();
break;
case PARAMETER_VEILED:
stringComp.IntParameter = data => data.Veiled;
stringComp.CompareInt = int.Parse(value);
stringComp.StringParameter = data => data.Veiled.ToString();
break;
case PARAMETER_FRACTUREDMODS:
stringComp.IntParameter = data => data.Fractured;
stringComp.CompareInt = int.Parse(value);
stringComp.StringParameter = data => data.Fractured.ToString();
break;
case PARAMETER_DELIRIUMREWARDSAMOUNT:
stringComp.IntParameter = data => data.DeliriumStacks;
stringComp.CompareInt = int.Parse(value);
stringComp.StringParameter = data => data.DeliriumStacks.ToString();
break;
case PARAMETER_SKILLGEMLEVEL:
stringComp.IntParameter = data => data.SkillGemLevel;
stringComp.CompareInt = int.Parse(value);
stringComp.StringParameter = data => data.SkillGemLevel.ToString();
break;
case PARAMETER_METAMORPHREWARDSAMOUNT:
stringComp.IntParameter = data => data.MetamorphSampleRewardsAmount;
stringComp.CompareInt = int.Parse(value);
stringComp.StringParameter = data => data.MetamorphSampleRewardsAmount.ToString();
break;
case PARAMETER_METAMORPHGOODREWARDSAMOUNT:
stringComp.IntParameter = data => data.MetamorphSampleGoodRewardsAmount;
stringComp.CompareInt = int.Parse(value);
stringComp.StringParameter = data => data.MetamorphSampleGoodRewardsAmount.ToString();
break;
case PARAMETER_METAMORPHBADREWARDSAMOUNT:
stringComp.IntParameter = data => data.MetamorphSampleBadRewardsAmount;
stringComp.CompareInt = int.Parse(value);
stringComp.StringParameter = data => data.MetamorphSampleBadRewardsAmount.ToString();
break;
default:
DebugWindow.LogMsg($"Filter parser: Parameter is not defined in code: {parameter}", 10);
return false;
}
switch (operation.ToLower())
{
case OPERATION_EQUALITY:
stringComp.CompDeleg = data => stringComp.StringParameter(data).Equals(stringComp.CompareString);
break;
case OPERATION_NONEQUALITY:
stringComp.CompDeleg = data => !stringComp.StringParameter(data).Equals(stringComp.CompareString);
break;
case OPERATION_CONTAINS:
stringComp.CompDeleg = data => stringComp.StringParameter(data).Contains(stringComp.CompareString);
break;
case OPERATION_NOTCONTAINS:
stringComp.CompDeleg = data => !stringComp.StringParameter(data).Contains(stringComp.CompareString);
break;
case OPERATION_BIGGER:
if (stringComp.IntParameter == null)
{
DebugWindow.LogMsg(
$"Filter parser error: Can't compare string parameter with {OPERATION_BIGGER} (numerical) operation. Statement: {command}",
10);
return false;
}
stringComp.CompDeleg = data => stringComp.IntParameter(data) > stringComp.CompareInt;
break;
case OPERATION_LESS:
if (stringComp.IntParameter == null)
{
DebugWindow.LogMsg(
$"Filter parser error: Can't compare string parameter with {OPERATION_LESS} (numerical) operation. Statement: {command}",
10);
return false;
}
stringComp.CompDeleg = data => stringComp.IntParameter(data) < stringComp.CompareInt;
break;
case OPERATION_LESSEQUAL:
if (stringComp.IntParameter == null)
{
DebugWindow.LogMsg(
$"Filter parser error: Can't compare string parameter with {OPERATION_LESSEQUAL} (numerical) operation. Statement: {command}",
10);
return false;
}
stringComp.CompDeleg = data => stringComp.IntParameter(data) <= stringComp.CompareInt;
break;
case OPERATION_BIGGERQUAL:
if (stringComp.IntParameter == null)
{
DebugWindow.LogMsg(
$"Filter parser error: Can't compare string parameter with {OPERATION_BIGGERQUAL} (numerical) operation. Statement: {command}",
10);
return false;
}
stringComp.CompDeleg = data => stringComp.IntParameter(data) >= stringComp.CompareInt;
break;
default:
DebugWindow.LogMsg($"Filter parser: Operation is not defined in code: {operation}", 10);
return false;
}
newFilter.Filters.Add(stringComp);
return true;
}
private static bool ParseCommand(string command, out string parameter, out string operation, out string value)
{
parameter = "";
operation = "";
value = "";
var operationIndex = -1;
foreach (var t in Operations)
{
operationIndex = command.IndexOf(t, StringComparison.Ordinal);
if (operationIndex == -1) continue;
operation = t;
break;
}
if (operationIndex == -1) return false;
parameter = command.Substring(0, operationIndex).Trim();
value = command.Substring(operationIndex + operation.Length).Trim();
return true;
}
}
}