-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTASerializableDataExtension.cs
334 lines (294 loc) · 14.5 KB
/
TASerializableDataExtension.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
using ColossalFramework;
using ColossalFramework.IO;
using HarmonyLib;
using ICities;
using System;
using System.IO;
using System.Threading;
using UnityEngine;
using static TreeAnarchy.TAMod;
using static TreeManager;
using PrefabData = PrefabCollection<TreeInfo>.PrefabData;
namespace TreeAnarchy {
public class TASerializableDataExtension : ISerializableDataExtension {
private enum Format : uint {
Version4 = 4,
Version5,
Version6,
Version7,
}
private const string TREE_ANARCHY_KEY = @"TreeAnarchy";
private class Data : IDataContainer {
private const ushort fireDamageBurningMask = unchecked((ushort)~(TreeInstance.Flags.Burning | TreeInstance.Flags.FireDamage));
private static EncodedArray.UShort m_encodedArray;
private static FastList<PrefabData> m_simulationPrefabs;
private void UpdateTreeLimit(int newSize) {
TreeScaleFactor = newSize / DefaultTreeLimit;
SaveSettings();
}
private void BeginDeserializeInfos(DataSerializer serializer) {
// Set read array.
m_encodedArray = EncodedArray.UShort.BeginRead(serializer);
// Get simulationPrefabs fastlist.
m_simulationPrefabs = AccessTools.Field(typeof(PrefabCollection<TreeInfo>), "m_simulationPrefabs").GetValue(null) as FastList<PrefabCollection<TreeInfo>.PrefabData>;
}
private ushort DeserializeInfo() {
// Read prefab index.
uint prefabIndex = m_encodedArray.Read();
// Check for new index.
if ((int)prefabIndex >= m_simulationPrefabs.m_size) {
int simPrefabsLength = 0;
if (m_simulationPrefabs.m_buffer != null) {
simPrefabsLength = m_simulationPrefabs.m_buffer.Length;
}
// Expand simulation prefab fastlist length if required.
if ((int)prefabIndex >= simPrefabsLength) {
int capacity = Mathf.Max(Mathf.Max((int)(prefabIndex + 1), 32), simPrefabsLength << 1);
m_simulationPrefabs.SetCapacity(capacity);
}
m_simulationPrefabs.m_size = (int)(prefabIndex + 1);
}
// Update simulation prefab reference count.
m_simulationPrefabs.m_buffer[prefabIndex].m_refcount++;
return (ushort)prefabIndex;
}
private void EndDeserializeInfos(DataSerializer serializer) {
// Close off array reading.
m_encodedArray.EndRead();
m_encodedArray = null;
// Read prefab names.
int numEncodedNames = (int)serializer.ReadUInt16();
PrefabData item = default;
for (int i = 0; i < numEncodedNames; ++i) {
// Check for existing info reference.
if (i < m_simulationPrefabs.m_size) {
// Existing info reference - populate the name, but only if it hasn't already been populated (don't overwrite).
string prefabName = serializer.ReadUniqueString();
if (m_simulationPrefabs.m_buffer[i].m_name == null) {
m_simulationPrefabs.m_buffer[i].m_name = prefabName;
}
continue;
}
// New reference.
item.m_name = serializer.ReadUniqueString();
item.m_refcount = 0;
item.m_prefab = null;
item.m_replaced = false;
m_simulationPrefabs.Add(item);
}
}
public void Deserialize(DataSerializer s) {
TreeManager treeManager = Singleton<TreeManager>.instance;
int savedBufferSize = s.ReadInt32(); // Read in Max limit
if (savedBufferSize <= MAX_TREE_COUNT) {
TALog("Invalid extended tree buffer size detected; aborting");
return;
}
uint newBufferSize = (uint)Math.Max(savedBufferSize, MaxTreeLimit);
Array32<TreeInstance> newTreeArray = new Array32<TreeInstance>(newBufferSize);
TreeInstance[] newTreeBuffer = newTreeArray.m_buffer;
// Initialize Array32 by creating zero (null) item and resetting unused count to zero (unused count will be recalculated after data population).
newTreeArray.CreateItem(out uint _);
newTreeArray.ClearUnused();
EncodedArray.UShort uShort = EncodedArray.UShort.BeginRead(s);
for (int i = DefaultTreeLimit; i < savedBufferSize; ++i) {
TreeInstance.Flags flag = (TreeInstance.Flags)uShort.Read();
flag &= ~(TreeInstance.Flags.FireDamage | TreeInstance.Flags.Burning);
newTreeBuffer[i].m_flags = (ushort)flag;
}
uShort.EndRead();
// Tree prefab indexes.
BeginDeserializeInfos(s);
for (int i = DefaultTreeLimit; i < savedBufferSize; ++i) {
if (newTreeBuffer[i].m_flags != 0) {
newTreeBuffer[i].m_infoIndex = DeserializeInfo();
}
}
EndDeserializeInfos(s);
EncodedArray.Short @short = EncodedArray.Short.BeginRead(s);
for (int i = DefaultTreeLimit; i < savedBufferSize; i++) {
if (newTreeBuffer[i].m_flags != 0) {
newTreeBuffer[i].m_posX = @short.Read();
} else {
newTreeBuffer[i].m_posX = 0;
}
}
@short.EndRead();
EncodedArray.Short @short1 = EncodedArray.Short.BeginRead(s);
for (int i = DefaultTreeLimit; i < savedBufferSize; i++) {
if (newTreeBuffer[i].m_flags != 0) {
newTreeBuffer[i].m_posZ = @short1.Read();
} else {
newTreeBuffer[i].m_posZ = 0;
}
}
@short1.EndRead();
EncodedArray.UShort uShort1 = EncodedArray.UShort.BeginRead(s);
for (int i = 1; i < savedBufferSize; i++) {
if ((newTreeBuffer[i].m_flags & (ushort)TreeInstance.Flags.FixedHeight) != 0) {
newTreeBuffer[i].m_posY = uShort1.Read();
}
}
uShort1.EndRead();
FastList<BurningTree> burningTrees = Singleton<TreeManager>.instance.m_burningTrees;
burningTrees.Clear();
BurningTree burningTree = default;
int burningListSize = (int)s.ReadUInt24();
treeManager.m_burningTrees.EnsureCapacity(burningListSize);
for (int i = 0; i < burningListSize; ++i) {
burningTree.m_treeIndex = s.ReadUInt24();
burningTree.m_fireIntensity = (byte)s.ReadUInt8();
burningTree.m_fireDamage = (byte)s.ReadUInt8();
uint treeIndex = burningTree.m_treeIndex;
if (treeIndex != 0u) {
burningTrees.Add(burningTree);
newTreeBuffer[treeIndex].m_flags |= 64;
if (burningTree.m_fireIntensity != 0) {
newTreeBuffer[treeIndex].m_flags |= 128;
}
}
}
// copy over default tree buffer to new buffer
TreeInstance[] oldTreeBuffer = treeManager.m_trees.m_buffer;
for(int i = 1; i < DefaultTreeLimit; ++i) {
ushort flags = oldTreeBuffer[i].m_flags;
if(flags != 0) {
newTreeBuffer[i].m_flags = flags;
newTreeBuffer[i].m_infoIndex = oldTreeBuffer[i].m_infoIndex;
newTreeBuffer[i].m_posX = oldTreeBuffer[i].m_posX;
newTreeBuffer[i].m_posZ = oldTreeBuffer[i].m_posZ;
}
}
// Assign new array and create new updated tree array.
treeManager.m_trees = newTreeArray;
int arraySize = newTreeArray.m_buffer.Length;
treeManager.m_updatedTrees = new ulong[arraySize >> 6];
}
public void AfterDeserialize(DataSerializer s) { }
public void Serialize(DataSerializer s) {
int treeLimit = MaxTreeLimit;
TreeManager treeManager = Singleton<TreeManager>.instance;
TreeInstance[] buffer = treeManager.m_trees.m_buffer;
TAManager.ExtraTreeInfo[] extraInfos = TAManager.m_extraTreeInfos;
// Important to save treelimit as it is an adjustable variable on every load
s.WriteInt32(treeLimit);
/* Apparently, the trees could be located anywhere in the buffer
* even if there's only 1 tree in the buffer. I'm assuming this is
* due to performance concerns.
* So have to iterate through the entire buffer.
*/
EncodedArray.UShort uShort = EncodedArray.UShort.BeginWrite(s);
for (int i = DefaultTreeLimit; i < treeLimit; ++i) {
uShort.Write(buffer[i].m_flags);
}
uShort.EndWrite();
try {
PrefabCollection<TreeInfo>.BeginSerialize(s);
for (int i = DefaultTreeLimit; i < treeLimit; ++i) {
if (buffer[i].m_flags != 0) {
PrefabCollection<TreeInfo>.Serialize(buffer[i].m_infoIndex);
}
}
} finally {
PrefabCollection<TreeInfo>.EndSerialize(s);
}
EncodedArray.Short @short = EncodedArray.Short.BeginWrite(s);
for (int i = DefaultTreeLimit; i < treeLimit; i++) {
if (buffer[i].m_flags != 0) {
@short.Write(buffer[i].m_posX);
}
}
@short.EndWrite();
@short = EncodedArray.Short.BeginWrite(s);
for (int i = DefaultTreeLimit; i < treeLimit; i++) {
if (buffer[i].m_flags != 0) {
@short.Write(buffer[i].m_posZ);
}
}
@short.EndWrite();
uShort = EncodedArray.UShort.BeginWrite(s);
for (int i = 1; i < treeLimit; i++) {
if ((buffer[i].m_flags & (ushort)TreeInstance.Flags.FixedHeight) != 0) {
uShort.Write(buffer[i].m_posY);
}
}
uShort.EndWrite();
EncodedArray.Float @float = EncodedArray.Float.BeginWrite(s);
for (int i = 1; i < treeLimit; i++) {
if (buffer[i].m_flags != 0) {
@float.Write(extraInfos[i].m_extraScale);
}
}
@float.EndWrite();
// Burning trees.
FastList<BurningTree> burningTrees = treeManager.m_burningTrees;
uint burningTreesSize = (uint)burningTrees.m_size;
BurningTree[] burningTreeBuffer = burningTrees.m_buffer;
s.WriteUInt24(burningTreesSize);
for (int i = 0; i < burningTreesSize; ++i) {
s.WriteUInt24(burningTreeBuffer[i].m_treeIndex);
s.WriteUInt8(burningTreeBuffer[i].m_fireIntensity);
s.WriteUInt8(burningTreeBuffer[i].m_fireDamage);
}
}
}
public void OnCreated(ISerializableData s) { }
public void OnReleased() { }
public void OnLoadData() { }
private static void ClearBurningTrees() {
Singleton<TreeManager>.instance.m_burningTrees.Clear();
}
public static void IntegratedDeserialize(TreeInstance[] trees) {
try {
if (Singleton<SimulationManager>.instance.m_serializableDataStorage.TryGetValue(TREE_ANARCHY_KEY, out byte[] data)) {
if (data is null) {
TALog("No extra trees to load");
return;
}
using (MemoryStream stream = new MemoryStream(data)) {
DataSerializer.Deserialize<Data>(stream, DataSerializer.Mode.Memory);
}
} else {
//for (int i = DefaultTreeLimit; i < trees.Length; i++) {
// trees[i].m_flags = 0;
//}
}
} catch (Exception e) {
Debug.LogException(e);
}
}
public void OnSaveData() {
try {
byte[] data;
using (var stream = new MemoryStream()) {
DataSerializer.Serialize(stream, DataSerializer.Mode.Memory, (uint)Format.Version7, new Data());
data = stream.ToArray();
}
SaveData(TREE_ANARCHY_KEY, data);
TALog($"Saved {data.Length} bytes of data");
} catch (Exception e) {
Debug.LogException(e);
}
}
private void SaveData(string id, byte[] data) {
SimulationManager smInstance = Singleton<SimulationManager>.instance;
while (!Monitor.TryEnter(smInstance.m_serializableDataStorage, SimulationManager.SYNCHRONIZE_TIMEOUT)) { }
try {
smInstance.m_serializableDataStorage[id] = data;
} finally {
Monitor.Exit(smInstance.m_serializableDataStorage);
}
}
private void EraseData(string id) {
SimulationManager smInstance = Singleton<SimulationManager>.instance;
while (!Monitor.TryEnter(smInstance.m_serializableDataStorage, SimulationManager.SYNCHRONIZE_TIMEOUT)) { }
try {
if (smInstance.m_serializableDataStorage.ContainsKey(id)) {
smInstance.m_serializableDataStorage.Remove(id);
}
} finally {
Monitor.Exit(smInstance.m_serializableDataStorage);
}
}
}
}