-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
ConditionManager.cs
298 lines (243 loc) · 9.53 KB
/
ConditionManager.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace QoLBar;
public interface IDisplayPriority
{
public int DisplayPriority { get; }
}
public interface ICondition : IDisplayPriority
{
public string ID { get; }
public string ConditionName { get; }
public bool Check(dynamic arg);
}
public interface IDrawableCondition
{
public string GetTooltip(CndCfg cndCfg);
public string GetSelectableTooltip(CndCfg cndCfg);
public void Draw(CndCfg cndCfg);
}
public interface IArgCondition
{
public dynamic GetDefaultArg(CndCfg cndCfg);
}
public interface IOnImportCondition
{
public void OnImport(CndCfg cndCfg);
}
public interface IConditionCategory : IDisplayPriority
{
public string CategoryName { get; }
}
public interface IConditionSetPreset
{
public string Name { get; }
public CndSetCfg Generate();
}
public static class ConditionManager
{
public enum BinaryOperator
{
AND,
OR,
EQUALS,
XOR
}
private static readonly Dictionary<string, ICondition> conditions = new();
private static readonly Dictionary<ICondition, IConditionCategory> categoryMap = new();
private static readonly Dictionary<(ICondition, dynamic), bool> conditionCache = new();
private static readonly Dictionary<CndSetCfg, (bool prev, float time)> conditionSetCache = new();
private static readonly Dictionary<CndSetCfg, List<bool>> debugSteps = new();
private static readonly HashSet<CndSetCfg> lockedSets = new();
private static float lastConditionCache = 0;
public static List<(IConditionCategory category, List<ICondition> conditions)> ConditionCategories { get; private set; } = new();
public static List<IConditionSetPreset> Presets { get; } = new();
public static void Initialize()
{
foreach (var t in Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsAssignableTo(typeof(IConditionCategory)) && !t.IsInterface))
{
var category = (IConditionCategory)Activator.CreateInstance(t);
if (category == null) continue;
var list = new List<ICondition>();
ConditionCategories.Add((category, list));
if (!t.IsAssignableTo(typeof(ICondition))) continue;
list.Add((ICondition)category);
}
foreach (var t in Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsAssignableTo(typeof(ICondition)) && !t.IsInterface))
{
var condition = (ICondition)Activator.CreateInstance(t);
if (condition == null) continue;
conditions.Add(condition.ID, condition);
var categoryType = t.GetCustomAttributes().FirstOrDefault(attr => attr.GetType().IsAssignableTo(typeof(IConditionCategory)))?.GetType();
if (categoryType == null)
{
if (t.IsAssignableTo(typeof(IConditionCategory)))
categoryMap.Add(condition, (IConditionCategory)condition);
continue;
}
var (category, list) = ConditionCategories.FirstOrDefault(tuple => tuple.category.GetType() == categoryType);
if (category == null) continue;
list.Add(condition);
categoryMap.Add(condition, category);
}
ConditionCategories = ConditionCategories.OrderBy(t => t.category.DisplayPriority).ToList();
for (int i = 0; i < ConditionCategories.Count; i++)
{
var (category, list) = ConditionCategories[i];
ConditionCategories[i] = (category, list.OrderBy(c => c.DisplayPriority).ToList());
}
foreach (var t in Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsAssignableTo(typeof(IConditionSetPreset)) && !t.IsInterface))
{
var preset = (IConditionSetPreset)Activator.CreateInstance(t);
if (preset == null) continue;
Presets.Add(preset);
}
}
public static ICondition GetCondition(string id) => conditions.TryGetValue(id, out var condition) ? condition : null;
public static IConditionCategory GetConditionCategory(ICondition condition) => categoryMap[condition];
public static IConditionCategory GetConditionCategory(string id) => GetConditionCategory(GetCondition(id));
public static bool CheckCondition(string id, dynamic arg = null, bool negate = false)
{
var condition = GetCondition(id);
return condition != null && (!negate ? CheckCondition(condition, arg) : !CheckCondition(condition, arg));
}
private static bool CheckCondition(ICondition condition, dynamic arg)
{
if (conditionCache.TryGetValue((condition, arg), out bool cache)) // ReSharper / Rider hates this being a var for some reason
return cache;
try
{
cache = condition.Check(arg);
}
catch
{
cache = false;
}
conditionCache[(condition, arg)] = cache;
return cache;
}
private static bool CheckUnaryCondition(bool negate, ICondition condition, dynamic arg)
{
try
{
return !negate ? condition.Check(arg) : !condition.Check(arg);
}
catch
{
return false;
}
}
private static bool CheckBinaryCondition(bool prev, BinaryOperator op, bool negate, ICondition condition, dynamic arg)
{
return op switch
{
BinaryOperator.AND => prev && CheckUnaryCondition(negate, condition, arg),
BinaryOperator.OR => prev || CheckUnaryCondition(negate, condition, arg),
BinaryOperator.EQUALS => prev == CheckUnaryCondition(negate, condition, arg),
BinaryOperator.XOR => prev ^ CheckUnaryCondition(negate, condition, arg),
_ => prev
};
}
public static bool CheckConditionSet(int i) => i >= 0 && i < QoLBar.Config.CndSetCfgs.Count && CheckConditionSet(QoLBar.Config.CndSetCfgs[i]);
public static bool CheckConditionSet(CndSetCfg set)
{
if (lockedSets.Contains(set))
return conditionSetCache.TryGetValue(set, out var c) && c.prev;
if (conditionSetCache.TryGetValue(set, out var cache) && QoLBar.RunTime <= cache.time + (QoLBar.Config.NoConditionCache ? 0 : 0.1f))
return cache.prev;
lockedSets.Add(set);
var first = true;
var prev = true;
var steps = new List<bool>();
foreach (var cnd in set.Conditions)
{
var condition = GetCondition(cnd.ID);
if (condition == null) continue;
if (first)
{
prev = CheckUnaryCondition(cnd.Negate, condition, cnd.Arg);
first = false;
}
else
{
prev = CheckBinaryCondition(prev, cnd.Operator, cnd.Negate, condition, cnd.Arg);
}
steps.Add(prev);
}
lockedSets.Remove(set);
conditionSetCache[set] = (prev, QoLBar.RunTime);
debugSteps[set] = steps;
return prev;
}
public static List<bool> GetDebugSteps(CndSetCfg set) => debugSteps.TryGetValue(set, out var steps) ? steps : null;
public static void UpdateCache()
{
if (QoLBar.Config.NoConditionCache)
{
conditionCache.Clear();
return;
}
if (QoLBar.RunTime < lastConditionCache + 0.1f) return;
conditionCache.Clear();
lastConditionCache = QoLBar.RunTime;
}
public static void SwapConditionSet(int from, int to)
{
var set = QoLBar.Config.CndSetCfgs[from];
foreach (var bar in QoLBar.Config.BarCfgs)
{
if (bar.ConditionSet == from)
bar.ConditionSet = to;
else if (bar.ConditionSet == to)
bar.ConditionSet = from;
}
foreach (var condition in from s in QoLBar.Config.CndSetCfgs from condition in s.Conditions where condition.ID == Conditions.ConditionSetCondition.constID select condition)
{
if (condition.Arg == from)
condition.Arg = to;
else if (condition.Arg == to)
condition.Arg = from;
}
QoLBar.Config.CndSetCfgs.RemoveAt(from);
QoLBar.Config.CndSetCfgs.Insert(to, set);
QoLBar.Config.Save();
IPC.MovedConditionSetProvider.SendMessage(from, to);
}
public static void RemoveConditionSet(int i)
{
foreach (var bar in QoLBar.Config.BarCfgs)
{
if (bar.ConditionSet > i)
bar.ConditionSet -= 1;
else if (bar.ConditionSet == i)
bar.ConditionSet = -1;
}
foreach (var s in QoLBar.Config.CndSetCfgs)
{
for (int j = s.Conditions.Count - 1; j >= 0; j--)
{
var cond = s.Conditions[j];
if (cond.ID != Conditions.ConditionSetCondition.constID) continue;
if (cond.Arg > i)
cond.Arg -= 1;
else if (cond.Arg == i)
s.Conditions.RemoveAt(j);
}
}
QoLBar.Config.CndSetCfgs.RemoveAt(i);
QoLBar.Config.Save();
IPC.RemovedConditionSetProvider.SendMessage(i);
}
public static void ShiftCondition(CndSetCfg set, CndCfg cndCfg, bool increment)
{
var i = set.Conditions.IndexOf(cndCfg);
if (!increment ? i <= 0 : i >= (set.Conditions.Count - 1)) return;
var j = (increment ? i + 1 : i - 1);
var condition = set.Conditions[i];
set.Conditions.RemoveAt(i);
set.Conditions.Insert(j, condition);
QoLBar.Config.Save();
}
}