-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathItemTypeOrderedSet.cs
More file actions
231 lines (191 loc) · 5.12 KB
/
ItemTypeOrderedSet.cs
File metadata and controls
231 lines (191 loc) · 5.12 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
using System.Collections.Generic;
using System.Linq;
using Terraria;
using Terraria.ModLoader;
using Terraria.ModLoader.Config;
using Terraria.ModLoader.IO;
namespace MagicStorage
{
public class ItemTypeOrderedSet
{
public static ItemTypeOrderedSet Empty => new("Empty");
private const string Suffix = "~v2";
private const string Suffix3 = "~v3";
private readonly string _name;
private List<Item> _items = new();
private List<ItemDefinition> _unloadedItems = new();
private HashSet<int> _set = new();
private bool _itemListDirty;
private HashSet<int> _pendingAdditions = new();
private HashSet<int> _pendingRemovals = new();
public int Count
{
get
{
if (_itemListDirty)
{
UpdateCollections();
_itemListDirty = false;
}
return _items.Count;
}
}
public IEnumerable<Item> Items
{
get
{
if (_itemListDirty)
{
UpdateCollections();
_itemListDirty = false;
}
return _items;
}
}
public int? MemoryLimit { get; init; }
public ItemTypeOrderedSet(string name)
{
_name = name;
}
private void UpdateCollections() {
foreach (int type in _pendingRemovals)
{
_pendingAdditions.Remove(type);
_set.Remove(type);
_items.RemoveAll(item => item.type == type);
}
_pendingRemovals.Clear();
foreach (int type in _pendingAdditions)
{
if (MemoryLimit is { } limit)
{
while (_set.Count >= limit)
{
int toRemove = _set.First();
_set.Remove(toRemove);
_items.RemoveAll(item => item.type == toRemove);
}
}
if (_set.Add(type))
{
// This is the first time we've seen this type, or it was previously removed
_items.Add(new Item(type));
}
else
{
// Move the item to the end of the list
List<Item> matches = _items.Where(item => item.type == type).ToList();
_items.RemoveAll(item => item.type == type);
_items.AddRange(matches);
}
}
_pendingAdditions.Clear();
}
public IEnumerable<int> Get()
{
if (_itemListDirty)
{
UpdateCollections();
_itemListDirty = false;
}
return [.. _set];
}
public bool Add(Item item) => Add(item.type);
public bool Add(int type)
{
bool wasNotPresent = !Contains(type);
_pendingRemovals.Remove(type);
_pendingAdditions.Add(type);
if (wasNotPresent)
_itemListDirty = true;
return wasNotPresent;
}
public bool Contains(int type) => !_pendingRemovals.Contains(type) && (_set.Contains(type) || _pendingAdditions.Contains(type));
public bool Contains(Item item) => Contains(item.type);
public bool Remove(Item item) => Remove(item.type);
public bool Remove(int type)
{
bool wasPresent = Contains(type);
_pendingAdditions.Remove(type);
_pendingRemovals.Add(type);
if (wasPresent)
_itemListDirty = true;
return wasPresent;
}
public void Clear()
{
_set.Clear();
_items.Clear();
_unloadedItems.Clear();
_pendingAdditions.Clear();
_pendingRemovals.Clear();
_itemListDirty = false;
}
public ItemTypeOrderedSet Clone() {
if (_itemListDirty) {
UpdateCollections();
_itemListDirty = false;
}
var clone = new ItemTypeOrderedSet(_name);
// Only the loaded IDs are relevant
foreach (int id in _set) {
// Delay generating the item collection until it's requested
clone._pendingAdditions.Add(id);
}
clone._itemListDirty = true;
return clone;
}
public void Save(TagCompound c)
{
HashSet<int> typeSet = new(_set);
// Resolve the set to what it would be if the pending changes were applied
if (_itemListDirty)
{
HashSet<int> additions = new(_pendingAdditions);
typeSet.UnionWith(additions);
typeSet.ExceptWith(_pendingRemovals);
}
List<ItemDefinition> list = typeSet.Select(x => new ItemDefinition(x)).TakeLastIfLimitExists(MemoryLimit).ToList();
if (MemoryLimit is int limit && list.Count < limit)
list.AddRange(_unloadedItems.TakeLast(limit - list.Count));
c.Add(_name + Suffix3, list);
}
public void Load(TagCompound tag)
{
if (tag.GetList<TagCompound>(_name) is { Count: > 0 } listV1)
{
_items = listV1
.Select(Utility.SafelyLoadItem)
.Where(static i => !i.IsAir)
.TakeLastIfLimitExists(MemoryLimit)
.ToList();
_pendingAdditions = new HashSet<int>(_items.Select(static i => i.type));
_itemListDirty = true;
}
else if (tag.GetList<int>(_name + Suffix) is { Count: > 0 } listV2)
{
_items = listV2
.Where(static x => x < ItemLoader.ItemCount) // Unable to reliably restore invalid IDs; just ignore them
.Select(static x => new Item(x))
.Where(static x => !x.IsAir) // Filters out deprecated items
.TakeLastIfLimitExists(MemoryLimit)
.ToList();
_pendingAdditions = new HashSet<int>(_items.Select(static i => i.type));
_itemListDirty = true;
}
else if (tag.GetList<ItemDefinition>(_name + Suffix3) is { Count: > 0 } listV3)
{
foreach (var def in listV3.TakeLastIfLimitExists(MemoryLimit))
{
if (!def.IsUnloaded)
_pendingAdditions.Add(def.Type);
else
_unloadedItems.Add(def);
}
_itemListDirty = true;
}
else
Clear();
}
}
}