-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathESerializeableExtension.cs
More file actions
384 lines (368 loc) · 18.5 KB
/
ESerializeableExtension.cs
File metadata and controls
384 lines (368 loc) · 18.5 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
using ColossalFramework;
using ColossalFramework.IO;
using ICities;
using System;
using System.IO;
using System.Threading;
using static EManagersLib.EPropManager;
namespace EManagersLib {
public sealed class ESerializableData : ISerializableDataExtension {
private const string EMANAGER_PROP_KEY = @"EManagers/PropAnarchy";
private const string PROPSNAPPINGID = "PropSnapping";
private const string PROPPRECISIONID = "PropPrecision";
private const string PROPPAINTERID = "PropPainter";
private enum Format : uint {
Version1 = 1,
Version2 = 2,
Version3 = 3,
Version4 = 4
}
private sealed class Data : IDataContainer {
private void EnsureCapacity(int maxLimit, out Array32<EPropInstance> newArray, out EPropInstance[] propBuffer) {
if (maxLimit > MAX_PROP_LIMIT) {
EPropInstance[] oldBuffer = m_props.m_buffer;
newArray = new Array32<EPropInstance>((uint)maxLimit);
newArray.CreateItem(out uint _);
newArray.ClearUnused();
propBuffer = newArray.m_buffer;
for (int i = 1; i < DEFAULT_PROP_LIMIT; i++) {
if (oldBuffer[i].m_flags != 0) {
propBuffer[i].m_flags = oldBuffer[i].m_flags;
propBuffer[i].m_infoIndex = oldBuffer[i].m_infoIndex;
propBuffer[i].m_posX = oldBuffer[i].m_posX;
propBuffer[i].m_posZ = oldBuffer[i].m_posZ;
propBuffer[i].m_angle = oldBuffer[i].m_angle;
}
}
return;
}
newArray = m_props;
propBuffer = m_props.m_buffer;
}
private void RepackBuffer(int maxLimit, int propCount, Format version, Array32<EPropInstance> existingPropBuffer) {
if (maxLimit > MAX_PROP_LIMIT) {
if (propCount > MAX_PROP_LIMIT) {
m_props = existingPropBuffer;
//UpdatePropLimit(maxLimit);
/* UpdatePropLimit first so PropScaleFactor is updated for next statement */
m_updatedProps = new ulong[MAX_UPDATEDPROP_LIMIT];
return; /* Just return with existing buffers */
}
/* Pack the result into old buffer as we are sure there are enough space to fit in buffer */
EPropInstance[] existingBuffer = existingPropBuffer.m_buffer;
EPropInstance[] oldBuffer = m_props.m_buffer;
/* make sure to fill in 1~262144 props first */
for (int i = 1; i < DEFAULT_PROP_LIMIT; i++) {
if (existingBuffer[i].m_flags != 0) {
oldBuffer[i].m_posY = existingBuffer[i].m_posY;
oldBuffer[i].m_scale = existingBuffer[i].m_scale;
oldBuffer[i].m_color = existingBuffer[i].m_color;
}
}
for (uint i = DEFAULT_PROP_LIMIT, offsetIndex = 1; i < existingBuffer.Length; i++) {
if (existingBuffer[i].m_flags != 0) {
while (oldBuffer[offsetIndex].m_flags != 0) { offsetIndex++; } /* Find available slot in old buffer */
oldBuffer[offsetIndex].m_flags = existingBuffer[i].m_flags;
oldBuffer[offsetIndex].m_infoIndex = existingBuffer[i].m_infoIndex;
oldBuffer[offsetIndex].m_posX = existingBuffer[i].m_posX;
oldBuffer[offsetIndex].m_posZ = existingBuffer[i].m_posZ;
oldBuffer[offsetIndex].m_angle = existingBuffer[i].m_angle;
oldBuffer[offsetIndex].m_posY = existingBuffer[i].m_posY;
oldBuffer[offsetIndex].m_scale = existingBuffer[i].m_scale;
oldBuffer[offsetIndex].m_color = existingBuffer[i].m_color;
}
}
}
}
public void Deserialize(DataSerializer s) {
int maxLen = s.ReadInt32(); // Read in Max limit
int propCount = 0;
EnsureCapacity(maxLen, out Array32<EPropInstance> newBuffer, out EPropInstance[] props);
EncodedArray.UShort uShort = EncodedArray.UShort.BeginRead(s);
for (int i = DEFAULT_PROP_LIMIT; i < maxLen; i++) {
props[i].m_flags = uShort.Read();
}
uShort.EndRead();
PrefabCollection<PropInfo>.BeginDeserialize(s);
for (int i = 1; i < maxLen; i++) {
if (props[i].m_flags != 0) {
props[i].m_infoIndex = (ushort)PrefabCollection<PropInfo>.Deserialize(true);
propCount++;
}
}
PrefabCollection<PropInfo>.EndDeserialize(s);
EncodedArray.Short @short = EncodedArray.Short.BeginRead(s);
for (int i = DEFAULT_PROP_LIMIT; i < maxLen; i++) {
if (props[i].m_flags != 0) {
props[i].m_posX = @short.Read();
} else {
props[i].m_posX = 0;
}
}
@short.EndRead();
EncodedArray.Short @short1 = EncodedArray.Short.BeginRead(s);
for (int i = DEFAULT_PROP_LIMIT; i < maxLen; i++) {
if (props[i].m_flags != 0) {
props[i].m_posZ = @short1.Read();
} else {
props[i].m_posZ = 0;
}
}
@short1.EndRead();
EncodedArray.UShort uShort2 = EncodedArray.UShort.BeginRead(s);
for (int i = DEFAULT_PROP_LIMIT; i < maxLen; i++) {
if (props[i].m_flags != 0) {
props[i].m_angle = uShort2.Read();
} else {
props[i].m_angle = 0;
}
}
uShort2.EndRead();
EncodedArray.UShort uShortPosY = EncodedArray.UShort.BeginRead(s);
for (int i = 1; i < maxLen; i++) {
if ((props[i].m_flags & EPropInstance.FIXEDHEIGHTFLAG) != 0) {
props[i].m_posY = uShortPosY.Read();
} else {
props[i].m_posY = 0;
}
}
uShortPosY.EndRead();
if (s.version >= (uint)Format.Version4) {
EncodedArray.Float @floatPreciseX = EncodedArray.Float.BeginRead(s);
for (int i = 1; i < maxLen; i++) {
if (props[i].m_flags != 0) {
props[i].m_preciseX = floatPreciseX.Read();
} else {
props[i].m_preciseX = 0f;
}
}
floatPreciseX.EndRead();
EncodedArray.Float @floatPreciseZ = EncodedArray.Float.BeginRead(s);
for (int i = 1; i < maxLen; i++) {
if (props[i].m_flags != 0) {
props[i].m_preciseZ = floatPreciseZ.Read();
} else {
props[i].m_preciseZ = 0f;
}
}
floatPreciseZ.EndRead();
} else {
EncodedArray.Float @floatPreciseX = EncodedArray.Float.BeginRead(s);
for (int i = 1; i < maxLen; i++) {
if (props[i].m_flags != 0) {
props[i].m_preciseX = (props[i].m_posX + floatPreciseX.Read()) * 0.263671875f;
} else {
props[i].m_preciseX = 0f;
}
}
floatPreciseX.EndRead();
EncodedArray.Float @floatPreciseZ = EncodedArray.Float.BeginRead(s);
for (int i = 1; i < maxLen; i++) {
if (props[i].m_flags != 0) {
props[i].m_preciseZ = (props[i].m_posZ + floatPreciseZ.Read()) * 0.263671875f;
} else {
props[i].m_preciseZ = 0f;
}
}
floatPreciseZ.EndRead();
}
EncodedArray.Float @float = EncodedArray.Float.BeginRead(s);
for (int i = 1; i < maxLen; i++) {
if (props[i].m_flags != 0) {
props[i].m_scale = @float.Read();
} else {
props[i].m_scale = 0f;
}
}
@float.EndRead();
EncodedArray.Float @float1 = EncodedArray.Float.BeginRead(s);
for (int i = 1; i < maxLen; i++) {
if (props[i].m_flags != 0) {
props[i].m_color.r = @float1.Read();
props[i].m_color.g = @float1.Read();
props[i].m_color.b = @float1.Read();
props[i].m_color.a = @float1.Read();
}
}
@float1.EndRead();
/* Now Resize / Repack buffer if necessary */
RepackBuffer(maxLen, propCount, (Format)s.version, newBuffer);
}
public void AfterDeserialize(DataSerializer s) { }
public void Serialize(DataSerializer s) {
int propLimit = MAX_PROP_LIMIT;
EPropInstance[] buffer = m_props.m_buffer;
// Important to save proplimit as it is an adjustable variable on every load
s.WriteInt32(propLimit);
EUtils.ELog($"Saving limit: {propLimit}");
EncodedArray.UShort uShort = EncodedArray.UShort.BeginWrite(s);
for (int i = DEFAULT_PROP_LIMIT; i < propLimit; i++) {
uShort.Write(buffer[i].m_flags);
}
uShort.EndWrite();
try {
PrefabCollection<PropInfo>.BeginSerialize(s);
for (int i = 1; i < propLimit; i++) {
if (buffer[i].m_flags != 0) {
PrefabCollection<PropInfo>.Serialize(buffer[i].m_infoIndex);
}
}
} finally {
PrefabCollection<PropInfo>.EndSerialize(s);
}
EncodedArray.Short @short = EncodedArray.Short.BeginWrite(s);
for (int i = DEFAULT_PROP_LIMIT; i < propLimit; i++) {
if (buffer[i].m_flags != 0) {
@short.Write(buffer[i].m_posX);
}
}
@short.EndWrite();
EncodedArray.Short @short1 = EncodedArray.Short.BeginWrite(s);
for (int i = DEFAULT_PROP_LIMIT; i < propLimit; i++) {
if (buffer[i].m_flags != 0) {
@short1.Write(buffer[i].m_posZ);
}
}
@short1.EndWrite();
EncodedArray.UShort uShort1 = EncodedArray.UShort.BeginWrite(s);
for (int i = DEFAULT_PROP_LIMIT; i < propLimit; i++) {
if (buffer[i].m_flags != 0) {
uShort1.Write(buffer[i].m_angle);
}
}
uShort1.EndWrite();
EncodedArray.UShort uShort2 = EncodedArray.UShort.BeginWrite(s);
for (int i = 1; i < propLimit; i++) {
if ((buffer[i].m_flags & EPropInstance.FIXEDHEIGHTFLAG) != 0) {
uShort2.Write(buffer[i].m_posY);
}
}
uShort2.EndWrite();
EncodedArray.Float @floatPreciseX = EncodedArray.Float.BeginWrite(s);
for (int i = 1; i < propLimit; i++) {
if (buffer[i].m_flags != 0) {
@floatPreciseX.Write(buffer[i].m_preciseX);
}
}
floatPreciseX.EndWrite();
EncodedArray.Float @floatPreciseZ = EncodedArray.Float.BeginWrite(s);
for (int i = 1; i < propLimit; i++) {
if (buffer[i].m_flags != 0) {
@floatPreciseZ.Write(buffer[i].m_preciseZ);
}
}
floatPreciseZ.EndWrite();
EncodedArray.Float @float = EncodedArray.Float.BeginWrite(s);
for (int i = 1; i < propLimit; i++) {
if (buffer[i].m_flags != 0) {
@float.Write(buffer[i].m_scale);
}
}
@float.EndWrite();
EncodedArray.Float @float1 = EncodedArray.Float.BeginWrite(s);
for (int i = 1; i < propLimit; i++) {
if (buffer[i].m_flags != 0) {
@float1.Write(buffer[i].m_color.r);
@float1.Write(buffer[i].m_color.g);
@float1.Write(buffer[i].m_color.b);
@float1.Write(buffer[i].m_color.a);
}
}
@float1.EndWrite();
}
}
public static void IntegratedPropDeserialize(EPropInstance[] _) {
if (MAX_PROP_LIMIT <= DEFAULT_PROP_LIMIT) return;
try {
if (Singleton<SimulationManager>.instance.m_serializableDataStorage.TryGetValue(EMANAGER_PROP_KEY, out byte[] data)) {
if (data is null) {
EUtils.ELog("No extra props to load");
return;
}
using (MemoryStream stream = new MemoryStream(data)) {
DataSerializer.Deserialize<Data>(stream, DataSerializer.Mode.Memory);
}
}
EUtils.ProcessQueues();
} catch (Exception e) {
UnityEngine.Debug.LogException(e);
}
}
private static Type PropSnappingLegacyHandler(string _) => typeof(PropSnapping.Data);
private static Type PropPrecisionLegacyHandler(string _) => typeof(PropPrecision.Data);
private static Type PropPainterLegacyHandler(string _) => typeof(PropPainter.PropPainterDataContainer);
public void OnLoadData() {
/* Try load old prop snapping data */
if (ToolManager.instance.m_properties.m_mode == ItemClass.Availability.Game) {
SimulationManager smInstance = Singleton<SimulationManager>.instance;
if (smInstance.m_serializableDataStorage.TryGetValue(PROPSNAPPINGID, out byte[] data)) {
EUtils.ELog("Found Prop Snapping data, loading...");
using (MemoryStream ms = new MemoryStream(data)) {
var s = DataSerializer.Deserialize<PropSnapping.Data>(ms, DataSerializer.Mode.Memory, PropSnappingLegacyHandler);
}
EUtils.ELog("Loaded " + (data.Length / 1024) + "kb of Prop Snapping data");
EraseData(PROPSNAPPINGID);
}
if (smInstance.m_serializableDataStorage.TryGetValue(PROPPRECISIONID, out data)) {
EUtils.ELog("Found Prop Precision data, loading...");
using (MemoryStream ms = new MemoryStream(data)) {
var s = DataSerializer.Deserialize<PropPrecision.Data>(ms, DataSerializer.Mode.Memory, PropPrecisionLegacyHandler);
}
EUtils.ELog("Loaded " + (data.Length / 1024) + "kb of Prop Precision data");
EraseData(PROPPRECISIONID);
}
if (smInstance.m_serializableDataStorage.TryGetValue(PROPPAINTERID, out data)) {
EUtils.ELog("Found Prop Painter data, loading...");
using (MemoryStream ms = new MemoryStream(data)) {
var s = DataSerializer.Deserialize<PropPainter.PropPainterDataContainer>(ms, DataSerializer.Mode.Memory, PropPainterLegacyHandler);
}
EUtils.ELog("Loaded " + (data.Length / 1024) + "kb of old Prop Painter data");
EraseData(PROPPAINTERID);
}
}
}
public void OnSaveData() {
if (MAX_PROP_LIMIT <= DEFAULT_PROP_LIMIT) return;
try {
byte[] data;
using (var stream = new MemoryStream()) {
DataSerializer.Serialize(stream, DataSerializer.Mode.Memory, (uint)Format.Version4, new Data());
data = stream.ToArray();
}
SaveData(EMANAGER_PROP_KEY, data);
EUtils.ELog($"Saved {data.Length} bytes of data");
} catch (Exception e) {
UnityEngine.Debug.LogException(e);
}
#if ENABLEEIGHTYONE
// Process 81 tiles data
EGameAreaManager.Serialize();
EDistrictManager.Serialize();
EElectricityManager.Serialize();
EWaterManager.Serialize();
#endif
}
internal static 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 static 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);
}
}
public void OnCreated(ISerializableData serializedData) { }
public void OnReleased() { }
}
}