-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDropTableAlteration.cs
265 lines (236 loc) · 11.2 KB
/
DropTableAlteration.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
using SideLoader;
using SideLoader.Model;
using SideLoader.SLPacks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using UnityEngine;
// For renaming fields [XmlElement(ElementName = "Members")] or [XmlElement("Members")]
// https://docs.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlelementattribute
// Ignore field when reading/writíng the xml [XmlIgnore]
// https://docs.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlattributes.xmlignore
// Migration
// 1. minor release:
// - add migrations
// - set [XmlElement("OldFieldName"), DefaultValue(null), EditorBrowsable(EditorBrowsableState.Never)]
// - or [XmlArray("OldListName"), DefaultValue(null), EditorBrowsable(EditorBrowsableState.Never)]
// ^- define xml element ^- default values are not exported ^- hides from SL editor
// - append _DEPRECATED to C# field
// - make dummy classes when structure is about to change
// 2. 1st major release: no changes
// 3. 2nd major release: drop migrations, set [XmlIgnore]
// 3. 3rd major release: drop fields
namespace Nm1fiOutward.Drops
{
[SL_Serialized]
public class DropTableAlteration : ContentTemplate
{
public enum ItemSourceType
{
/// <summary>This alteration is used in merchant inventories.</summary>
Merchant,
/// <summary>This alteration is used in loot containers, chests, junk piles and so on.</summary>
LootContainer,
/// <summary>This alteration is used in bandit and monster drops.</summary>
EnemyOrMonster,
/// <summary>This alteration is used in Gatherables.</summary>
//Gatherable
};
// Abstract/Interface
public override ITemplateCategory PackCategory => SLPackManager.GetCategoryInstance<DropTableAlterationCategory>();
public override string DefaultTemplateName => "Untitled DropTableAlteration";
public string UID => $"{SerializedSLPackName}.{SerializedFilename}";
// User defined
[XmlElement("TargetType")]
public ItemSourceType TargetType;
[XmlElement("MatchAny", typeof(AnyConstraint))]
[XmlElement("MatchAll", typeof(AllConstraint))]
public ConstraintGroup Matcher = new AllConstraint();
[XmlArray("Alterations")]
[XmlArrayItem("AddGuaranteedDrops", typeof(AddGuaranteedDrops))]
[XmlArrayItem("ModifyGuaranteedDrops", typeof(ModifyGuaranteedDrops))]
[XmlArrayItem("RemoveGuaranteedDrops", typeof(RemoveGuaranteedDrops))]
[XmlArrayItem("AddGuaranteedFromDropTable", typeof(AddGuaranteedFromDropTable))]
[XmlArrayItem("AddChanceDrops", typeof(AddChanceDrops))]
[XmlArrayItem("ModifyChanceDrops", typeof(ModifyChanceDrops))]
[XmlArrayItem("RemoveChanceDrops", typeof(RemoveChanceDrops))]
[XmlArrayItem("AdditionalDropTablesByUID", typeof(AdditionalDropTablesByUID))]
public List<DropableAlteration> Alterations = new List<DropableAlteration>();
public bool HasAlterations => HasGuaranteedDropperAlterations || HasChanceDropperAlterations;
public bool HasGuaranteedDropperAlterations => GuaranteedDropperAlterations.Any();
public bool HasChanceDropperAlterations => ChanceDropperAlterations.Any();
public bool HasAdditionalDrops => AdditionalDropTableAlterations.Any();
public override void ApplyActualTemplate()
{
ClearCaches();
// run migrations here
if (Alterations.Count == 0)
{
DropsPlugin.LogWarning($"No Alterations configured for DropTableAlteration '{UID}'!");
return;
}
if (Matcher != null)
{
var ownerUIDHint = ((Func<OwnerUIDConstraint.OwnerHint>)delegate () {
switch (TargetType)
{
case ItemSourceType.Merchant: return OwnerUIDConstraint.OwnerHint.Merchant;
case ItemSourceType.LootContainer: return OwnerUIDConstraint.OwnerHint.TreasureChest;
default: return OwnerUIDConstraint.OwnerHint.None;
}
})();
foreach (var constraint in Matcher.IterateTree())
if (constraint is OwnerUIDConstraint ownerUIDConstraint)
ownerUIDConstraint.Hint = ownerUIDHint;
Matcher.ApplyActualTemplate();
}
if (Alterations != null)
foreach (var alteration in Alterations)
alteration.ApplyActualTemplate();
SLPackManager.AddLateApplyListener(OnLateApply);
DropsPlugin.Log($"Registered DropTableAlteration '{UID}'");
}
private void OnLateApply(object[] args)
{
var errorMessage = new StringBuilder();
{
if (Matcher != null && Matcher.Validate() is IList<string> errors)
foreach (var error in errors)
errorMessage.Append($"\n - {error}");
}
foreach (var (i, alteration) in Alterations.Select((alteration, i) => (i, alteration)))
{
if (alteration.Validate() is IList<string> errors)
foreach (var error in errors)
errorMessage.Append($"\n - Alterations[{i}]: {error}");
}
if (errorMessage.Length > 0)
{
DropsPlugin.LogWarning($"Following errors were reported when validating fields for {UID}:{errorMessage}");
return;
}
}
// Internal
IList<(GuaranteedDropperAlteration, int)> m_guaranteedAlterationsCache;
IList<(ChanceDropperAlteration, int)> m_chanceAlterationsCache;
IList<(AdditionalDropperAlteration, int)> m_additionalAlterationsCache;
private void ClearCaches()
{
m_guaranteedAlterationsCache = null;
m_chanceAlterationsCache = null;
m_additionalAlterationsCache = null;
}
private void UpdateCaches()
{
m_guaranteedAlterationsCache = new List<(GuaranteedDropperAlteration, int)>();
m_chanceAlterationsCache = new List<(ChanceDropperAlteration, int)>();
m_additionalAlterationsCache = new List<(AdditionalDropperAlteration, int)>();
if (Alterations != null)
{
int i = 0;
foreach (var alteration in Alterations)
{
if (alteration is GuaranteedDropperAlteration guaranteedAlteration)
m_guaranteedAlterationsCache.Add((guaranteedAlteration, i));
else if (alteration is ChanceDropperAlteration chanceAlteration)
m_chanceAlterationsCache.Add((chanceAlteration, i));
else if (alteration is AdditionalDropperAlteration additionalAlteration)
m_additionalAlterationsCache.Add((additionalAlteration, i));
else
DropsPlugin.LogError($"Unknown alteration type {alteration.GetType().Name} at index {i} in {UID}");
i++;
}
}
}
private IList<(GuaranteedDropperAlteration, int)> GuaranteedDropperAlterations {
get {
if (m_guaranteedAlterationsCache == null)
UpdateCaches();
return m_guaranteedAlterationsCache;
}
}
private IList<(ChanceDropperAlteration, int)> ChanceDropperAlterations {
get {
if (m_chanceAlterationsCache == null)
UpdateCaches();
return m_chanceAlterationsCache;
}
}
private IList<(AdditionalDropperAlteration, int)> AdditionalDropTableAlterations {
get {
if (m_additionalAlterationsCache == null)
UpdateCaches();
return m_additionalAlterationsCache;
}
}
public bool TryUpdate(DropableWrapper dropable)
{
var guaranteedAlterations = GuaranteedDropperAlterations;
var chanceAlterations = ChanceDropperAlterations;
if ((guaranteedAlterations.Any() || chanceAlterations.Any()) && Matcher != null && Matcher.IsMatch(dropable))
{
if (guaranteedAlterations.Any())
{
if (dropable.GuaranteedDroppers is List<GuaranteedDropWrapper> droppers)
{
foreach (var (alteration, i) in guaranteedAlterations)
{
var found = false;
foreach (var dropper in droppers)
if (alteration.TryApply(dropable, dropper))
{
found = true;
if (alteration.ModifyFirstMatchOnly)
break;
}
if (!found)
DropsPlugin.LogWarning($"DropTableAlteration '{UID}' was unable to find ItemDropper for Alterations[{i}] {alteration.GetType().Name} for {dropable}");
}
}
}
if (chanceAlterations.Any())
{
if (dropable.ChanceDroppers is List<DropTableWrapper> droppers)
{
foreach (var (alteration, i) in chanceAlterations)
{
var found = false;
foreach (var dropper in droppers)
if (alteration.TryApply(dropable, dropper))
{
found = true;
if (alteration.ModifyFirstMatchOnly)
break;
}
if (!found)
DropsPlugin.LogWarning($"DropTableAlteration '{UID}' was unable to find ItemDropper for Alterations[{i}] {alteration.GetType().Name} for {dropable}");
}
}
}
}
return dropable.HasChanges;
}
public bool TryGenerateAdditionalItems(Dropable dropable, Transform container)
{
var alterations = AdditionalDropTableAlterations;
if (alterations.Any() && Matcher != null && Matcher.IsMatch(dropable))
{
foreach (var (alteration, _) in alterations)
alteration.GenerateAdditionalItems(dropable, container);
return true;
}
return false;
}
public void AddAdditionalDroppersTo(DropableWrapper dropable)
{
var alterations = AdditionalDropTableAlterations;
if (alterations.Any() && Matcher != null && Matcher.IsMatch(dropable))
{
foreach (var (alteration, _) in alterations)
alteration.AddAdditionalDroppers(dropable);
}
}
}
}