-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDropTableMatcher.cs
280 lines (226 loc) · 9.17 KB
/
DropTableMatcher.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
using SideLoader;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
namespace Nm1fiOutward.Drops
{
[SL_Serialized]
public abstract class DropTableMatcher : ITemplateListItem
{
public abstract bool IsMatch(Dropable dropable);
public virtual void ApplyActualTemplate() { }
public virtual IList<string> Validate() => null;
}
// Container matchers
public abstract class ConstraintGroup : DropTableMatcher
{
[XmlElement("Any", typeof(AnyConstraint))]
[XmlElement("All", typeof(AllConstraint))]
[XmlElement("DropableName", typeof(DropableNameConstraint))]
[XmlElement("MerchantDropNameName", typeof(MerchantDropNameConstraint))]
[XmlElement("OwnerUID", typeof(OwnerUIDConstraint))]
[XmlElement("Scene", typeof(SceneConstraint))]
[XmlElement("Region", typeof(RegionConstraint))]
public readonly List<DropTableMatcher> Constraints = new List<DropTableMatcher>();
protected abstract string Name { get; }
public override string ToString()
=> $"{Name}({string.Join(", ", Constraints)})";
public override void ApplyActualTemplate()
{
foreach (var constraint in Constraints)
constraint.ApplyActualTemplate();
}
public override IList<string> Validate()
{
var errors = new List<string>();
var i = 0;
foreach (var constraint in Constraints)
{
if (constraint.Validate() is IList<string> constraintrErrors && constraintrErrors.Any())
foreach (var error in constraintrErrors)
errors.Add($"Match{Name}[{i}]: {error}");
i++;
}
return errors.Any() ? errors : null;
}
public IEnumerable<DropTableMatcher> IterateTree()
{
foreach (var child in Constraints)
{
yield return child;
if (child is ConstraintGroup group)
foreach (var grantChild in group.IterateTree())
yield return grantChild;
}
yield break;
}
}
public class AnyConstraint : ConstraintGroup
{
protected override string Name => "Any";
public override bool IsMatch(Dropable dropable)
=> !Constraints.Any() || Constraints.Any(constraint => constraint.IsMatch(dropable));
}
public class AllConstraint : ConstraintGroup
{
protected override string Name => "All";
public override bool IsMatch(Dropable dropable)
=> Constraints.Any() && Constraints.TrueForAll(constraint => constraint.IsMatch(dropable));
}
// Leaf constraints
public abstract class LeafConstraint : DropTableMatcher { }
public abstract class SingleValueXMLConstraint : LeafConstraint // , IXmlSerializable
{
protected abstract string XmlData { get; set; }
protected virtual bool RequiresValue => true;
public override IList<string> Validate()
{
if (RequiresValue)
{
var val = XmlData.Trim();
XmlData = val;
if (string.IsNullOrEmpty(val))
return new[] { $"{GetType().Name} should not be empty! Leave matcher list empty to match any." };
}
return null;
}
#region IXmlSerializable
public XmlSchema GetSchema() => null;
public void ReadXml(XmlReader reader) => XmlData = reader.ReadElementContentAsString();
public void WriteXml(XmlWriter writer) => writer.WriteString(XmlData);
#endregion
}
public class DropableNameConstraint : SingleValueXMLConstraint, IXmlSerializable
{
public string DropableName = "";
public override string ToString() => $"name={DropableName}";
protected override string XmlData { get => DropableName; set => DropableName = value; }
public override bool IsMatch(Dropable dropable)
=> dropable != null
&& dropable.name is string name
&& !string.IsNullOrEmpty(name)
&& name.Contains(DropableName);
}
public class MerchantDropNameConstraint : SingleValueXMLConstraint, IXmlSerializable
{
public string MerchantDropName = "";
public override string ToString() => $"name={MerchantDropName}";
protected override string XmlData { get => MerchantDropName; set => MerchantDropName = value; }
public override bool IsMatch(Dropable dropable)
=> dropable != null
&& dropable.UID is string uid
&& !string.IsNullOrEmpty(uid)
&& uid.StartsWith("MerchantDrop_")
&& uid.Contains(MerchantDropName);
}
public class OwnerUIDConstraint : SingleValueXMLConstraint, IXmlSerializable
{
public enum OwnerHint
{
None,
TreasureChest,
Merchant,
}
public string UID = "";
// Class containing these constraints, should set this hint, if it knows target types
[EditorBrowsable(EditorBrowsableState.Never)]
public OwnerHint Hint = OwnerHint.None;
public override string ToString() => $"holder-uid={UID}";
protected override string XmlData { get => UID; set => UID = value; }
public override bool IsMatch(Dropable dropable)
{
if (dropable == null)
return false;
if ((Hint == OwnerHint.None || Hint == OwnerHint.TreasureChest)
&& dropable.transform.parent.parent.GetComponent<TreasureChest>() is TreasureChest chest)
return UID == chest.HolderUID;
else if ((Hint == OwnerHint.None || Hint == OwnerHint.Merchant)
&& dropable.transform.parent.GetComponent<Merchant>() is Merchant merchant)
return UID == merchant.HolderUID;
return false;
}
}
public class SceneConstraint : SingleValueXMLConstraint, IXmlSerializable
{
public string SceneName;
public SceneConstraint() : base()
{
if (AreaManager.Instance.CurrentArea is Area currentArea)
{
SceneName = SceneManagerHelper.ActiveSceneName;
}
}
public override string ToString() => $"scene={SceneName}";
protected override string XmlData { get => SceneName; set => SceneName = value; }
public override bool IsMatch(Dropable dropable)
=> dropable != null
&& dropable.gameObject.scene.name is string scene
&& !string.IsNullOrEmpty(scene)
&& scene.Contains(SceneName);
}
public class RegionConstraint : SingleValueXMLConstraint, IXmlSerializable
{
public enum Regions
{
Abrassar,
AntiquePlateau,
Caldera,
Chersonese,
EnmerkarForest,
HallowedMarsh,
};
private static readonly Dictionary<string, Regions> areaFamiliesToRegion = new Dictionary<string, Regions> {
{ "Levant", Regions.Abrassar },
{ "Harmattan", Regions.AntiquePlateau },
{ "Sirocco", Regions.Caldera },
{ "Cierzo", Regions.Chersonese },
{ "Berg", Regions.EnmerkarForest },
{ "Monsoon", Regions.HallowedMarsh }
};
private static Dictionary<string, Regions?> sceneToRegionCache = new Dictionary<string, Regions?>();
private static Regions? RegionFromScene(string scene)
{
{
if (sceneToRegionCache.TryGetValue(scene, out var region))
return region;
}
foreach (var areaFamily in AreaManager.AreaFamilies)
foreach (var keyword in areaFamily.FamilyKeywords)
if (scene.Contains(keyword))
{
if (areaFamiliesToRegion.TryGetValue(areaFamily.FamilyName, out var region))
{
sceneToRegionCache.Add(scene, region);
return region;
}
break;
}
sceneToRegionCache.Add(scene, null);
return null;
}
public Regions Region;
public RegionConstraint() : base()
{
var region = RegionFromScene(SceneManagerHelper.ActiveSceneName);
Region = region ?? Regions.Chersonese;
}
public override string ToString() => $"region={Region}";
protected override string XmlData {
get => Region.ToString();
set {
if (Enum.TryParse(value, out Regions region))
Region = region;
}
}
public override bool IsMatch(Dropable dropable)
=> dropable?.gameObject?.scene.name is string scene
&& !string.IsNullOrEmpty(scene)
&& RegionFromScene(scene) is var region
&& region != null
&& region == Region;
}
}