-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathItemFilter.cs
More file actions
274 lines (235 loc) · 9.34 KB
/
ItemFilter.cs
File metadata and controls
274 lines (235 loc) · 9.34 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
using ExileCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Dynamic.Core.Exceptions;
using System.Linq.Expressions;
namespace ItemFilterLibrary;
public class ItemQuery<T> where T : ItemData
{
public string Query { get; set; }
public string RawQuery { get; set; }
public Func<T, bool> CompiledQuery { get; set; }
public int InitialLine { get; set; }
public bool FailedToCompile => Error != null;
public string Error { get; set; }
public override string ToString()
{
return $"InitialLine({InitialLine}) Query({Query.Replace("\n", "")}) RawQuery({RawQuery.Replace("\n", "")}) Failed?({FailedToCompile})";
}
public bool Matches(T item)
{
return Matches(item, false);
}
public bool Matches(T item, bool enableDebug)
{
if (item?.Entity?.IsValid != true || FailedToCompile)
return false;
try
{
if (CompiledQuery(item))
{
if (enableDebug)
DebugWindow.LogMsg($"[ItemQueryProcessor] {RawQuery} matched item {item.BaseName}", 10);
return true;
}
}
catch (Exception ex)
{
DebugWindow.LogError($"[ItemQueryProcessor] Evaluation error for query {RawQuery}. Item {item?.BaseName ?? "null"}\n{ex}");
return false;
}
return false;
}
}
public class ItemQuery : ItemQuery<ItemData>
{
private static readonly ParsingConfig ParsingConfig = new ParsingConfig()
{
AllowNewToEvaluateAnyType = true,
ResolveTypesBySimpleName = true,
CustomTypeProvider = new CustomDynamicLinqCustomTypeProvider(),
};
public static ItemQuery Load(string query) => Load(query, query, 0);
public static ItemQuery<T> Load<T>(string query) where T : ItemData
{
return Load<T>(query, query, 0);
}
public static ItemQuery Load(string query, string rawQuery, int line)
{
var generic = Load<ItemData>(query, rawQuery, line);
return new ItemQuery
{
RawQuery = generic.RawQuery,
CompiledQuery = generic.CompiledQuery,
InitialLine = generic.InitialLine,
Query = generic.Query,
Error = generic.Error,
};
}
public static ItemQuery<T> Load<T>(string query, string rawQuery, int line) where T : ItemData
{
try
{
var lambda = ParseItemDataLambda<T>(query);
var compiledLambda = lambda.Compile();
return new ItemQuery<T>
{
Query = query,
RawQuery = rawQuery,
CompiledQuery = compiledLambda,
InitialLine = line
};
}
catch (Exception ex)
{
var exMessage = ex is ParseException parseEx
? $"{parseEx.Message} (at index {parseEx.Position})"
: ex.ToString();
DebugWindow.LogError($"[ItemQueryProcessor] Error processing query ({rawQuery}) on Line # {line}: {exMessage}", 15);
return new ItemQuery<T>
{
Query = query,
RawQuery = rawQuery,
CompiledQuery = null,
InitialLine = line,
Error = exMessage, // to use with stashie to output the same number of inputs and match up the syntax style correctly
};
}
}
private static Expression<Func<T, bool>> ParseItemDataLambda<T>(string expression) where T : ItemData
{
return DynamicExpressionParser.ParseLambda<T, bool>(ParsingConfig, false, expression);
}
}
public class ItemFilter : ItemFilter<ItemData>
{
public ItemFilter(ItemFilter<ItemData> genericFilter) : base(genericFilter.Queries.ToList())
{
}
public ItemFilter(List<ItemQuery> queries) : base(queries.Cast<ItemQuery<ItemData>>().ToList())
{
}
public ItemFilter(List<(ItemQuery, bool isNegative)> queries) : base(queries.Select(x => ((ItemQuery<ItemData>)x.Item1, x.isNegative)).ToList())
{
}
private static List<(ItemQuery<T>, bool isNegative)> GetQueries<T>(string filterFilePath, string[] rawLines) where T : ItemData
{
var compiledQueries = new List<(ItemQuery<T>, bool isNegative)>();
var lines = SplitQueries(rawLines);
foreach (var (query, rawQuery, initialLine, isNegative) in lines)
{
compiledQueries.Add((ItemQuery.Load<T>(query, rawQuery, initialLine), isNegative));
}
DebugWindow.LogMsg($@"[ItemQueryProcessor] Processed {filterFilePath.Split("\\").LastOrDefault()} with {compiledQueries.Count} queries", 2);
return compiledQueries;
}
private static List<(string section, string rawSection, int sectionStartLine, bool isNegative)> SplitQueries(string[] rawLines)
{
string section = null;
string rawSection = null;
bool isNegative = false;
var sectionStartLine = 0;
var lines = new List<(string section, string, int sectionStartLine, bool isNegative)>();
foreach (var (line, index) in rawLines.Append("").Select((value, i) => (value, i)))
{
if (!string.IsNullOrWhiteSpace(line))
{
var lineWithoutComment = line.IndexOf("//", StringComparison.Ordinal) is var commentIndex and not -1
? line[..commentIndex]
: line;
lineWithoutComment = lineWithoutComment.Trim();
if (section == null)
{
if (!string.IsNullOrWhiteSpace(lineWithoutComment))
{
sectionStartLine = index + 1; // Set at the start of each section
if (lineWithoutComment[0] == '^')
{
lineWithoutComment = lineWithoutComment[1..];
isNegative = true;
}
}
else
{
//skip comment lines at the beginning of a section
continue;
}
}
section += $"{lineWithoutComment}\n";
rawSection += $"{line}\n";
}
else
{
if (!string.IsNullOrWhiteSpace(section))
{
lines.Add((section, rawSection.TrimEnd('\n'), sectionStartLine, isNegative));
}
section = null;
rawSection = null;
isNegative = false;
}
}
return lines;
}
public static ItemFilter LoadFromPath(string filterFilePath) => new ItemFilter(LoadFromPath<ItemData>(filterFilePath));
public static ItemFilter<T> LoadFromPath<T>(string filterFilePath) where T : ItemData
{
return new ItemFilter<T>(GetQueries<T>(filterFilePath, File.ReadAllLines(filterFilePath)));
}
public static ItemFilter LoadFromList(string filterName, IEnumerable<string> list) => new ItemFilter(LoadFromList<ItemData>(filterName, list));
public static ItemFilter<T> LoadFromList<T>(string filterName, IEnumerable<string> list) where T : ItemData
{
var compiledQueries = list.Select((query, i) => ItemQuery.Load<T>(query, query, i + 1)).ToList();
DebugWindow.LogMsg($@"[ItemQueryProcessor] Processed {filterName.Split("\\").LastOrDefault()} with {compiledQueries.Count} queries", 2);
return new ItemFilter<T>(compiledQueries);
}
public static ItemFilter LoadFromString(string @string) => new ItemFilter(LoadFromString<ItemData>(@string));
public static ItemFilter<T> LoadFromString<T>(string @string) where T : ItemData
{
return new ItemFilter<T>(GetQueries<T>("memory", @string.ReplaceLineEndings("\n").Split("\n")));
}
}
public class ItemFilter<T> where T : ItemData
{
private readonly List<(ItemQuery<T> Query, bool IsNegative)> _queries;
public IReadOnlyCollection<(ItemQuery<T> Query, bool IsNegative)> Queries => _queries;
public ItemFilter(List<ItemQuery<T>> queries)
{
_queries = queries.Select(x=>(x, false)).ToList();
}
public ItemFilter(List<(ItemQuery<T>, bool isNegative)> queries)
{
_queries = queries;
}
public bool Matches(T item)
{
return Matches(item, false);
}
public bool Matches(T item, bool enableDebug)
{
if (item?.Entity?.IsValid != true)
return false;
foreach (var (query, isNegative) in _queries)
{
try
{
if (!query.FailedToCompile && query.CompiledQuery(item))
{
if (enableDebug)
DebugWindow.LogMsg($"[ItemQueryProcessor] Matches an Item\nLine # {query.InitialLine}\nItem({item.BaseName})\n{query.RawQuery}", 10);
return !isNegative;
}
}
catch (Exception ex)
{
// huge issue when the amount of catching starts creeping up
// 4500 lines that produce an error on one item take 50ms per Tick() vs handling the error taking 0.2ms
DebugWindow.LogError($"Evaluation Error! Line # {query.InitialLine} Entry: '{query.RawQuery}' Item {item?.BaseName ?? "null"}\n{ex}");
}
}
return false;
}
}