-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathUndertaleLists.cs
566 lines (503 loc) · 19.5 KB
/
UndertaleLists.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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Reflection;
using UndertaleModLib.Models;
namespace UndertaleModLib
{
/// <summary>
/// Basic observable list type, usable for data models to be presented in graphical interfaces.
/// </summary>
public class UndertaleObservableList<T> : ObservableCollection<T>
{
// Reference to the private internal list inside Collection<T>
private readonly List<T> internalList;
// Internal list field (name "items" is guaranteed not to change due to Collection<T> being serializable)
private static readonly FieldInfo itemsField = typeof(Collection<T>).GetField("items", BindingFlags.NonPublic | BindingFlags.Instance);
/// <summary>
/// Creates a new observable list, which is empty and has default capacity.
/// </summary>
public UndertaleObservableList()
{
// Get ahold of internal list
internalList = (List<T>)itemsField.GetValue(this);
}
/// <summary>
/// Creates a new observable list, which is empty and has the specified initial capacity.
/// </summary>
public UndertaleObservableList(int capacity)
{
// Get ahold of internal list
internalList = (List<T>)itemsField.GetValue(this);
// Set capacity directly
internalList.Capacity = capacity;
}
/// <summary>
/// Sets the capacity of the internal list of this collection.
/// </summary>
public void SetCapacity(int capacity)
{
internalList.Capacity = capacity;
}
/// <summary>
/// Sets the capacity of the internal list of this collection.
/// </summary>
public void SetCapacity(uint capacity) => SetCapacity((int)capacity);
/// <summary>
/// Adds an item to the internal list of this collection (that is, without notifying any observers).
/// </summary>
/// <param name="item">Item to add.</param>
public void InternalAdd(T item)
{
internalList.Add(item);
}
}
/// <summary>
/// Simple list object, serialized as a 32-bit count followed immediately by all objects in the list.
/// </summary>
public class UndertaleSimpleList<T> : UndertaleObservableList<T>, UndertaleObject where T : UndertaleObject, new()
{
/// <inheritdoc />
public void Serialize(UndertaleWriter writer)
{
// Write list count
int count = Count;
writer.Write((uint)count);
// Write actual objects
int i = 0;
try
{
for (; i < count; i++)
{
writer.WriteUndertaleObject(this[i]);
}
}
catch (UndertaleSerializationException e)
{
throw new UndertaleSerializationException($"{e.Message}\nwhile writing item {i + 1} of {count} in a list of {typeof(T).FullName}", e);
}
}
/// <inheritdoc />
public void Unserialize(UndertaleReader reader)
{
// Read count and set list with that capacity
uint count = reader.ReadUInt32();
Clear();
SetCapacity(count);
// Read actual objects
uint i = 0;
try
{
for (; i < count; i++)
{
InternalAdd(reader.ReadUndertaleObject<T>());
}
}
catch (UndertaleSerializationException e)
{
throw new UndertaleSerializationException($"{e.Message}\nwhile reading item {i + 1} of {count} in a list of {typeof(T).FullName}", e);
}
}
/// <inheritdoc cref="UndertaleObject.UnserializeChildObjectCount(UndertaleReader)"/>
public static uint UnserializeChildObjectCount(UndertaleReader reader)
{
// Read base object count; short-circuit if there's no objects
uint count = reader.ReadUInt32();
if (count == 0)
{
return 0;
}
// If the objects are resource refs, the size is known as 4 bytes
Type t = typeof(T);
if (t.IsAssignableTo(typeof(UndertaleResourceRef)))
{
// UndertaleResourceById<T, ChunkT> = 4 bytes
reader.Position += count * 4;
return count;
}
// If objects have a static child object size/count, simply multiply to determine the number of total objects
if (t.IsAssignableTo(typeof(IStaticChildObjectsSize)))
{
uint subSize = reader.GetStaticChildObjectsSize(t);
uint subCount = 0;
if (t.IsAssignableTo(typeof(IStaticChildObjCount)))
{
subCount = reader.GetStaticChildCount(t);
}
reader.Position += count * subSize;
return count + (count * subCount);
}
// Determine object counts recursively for all objects
Func<UndertaleReader, uint> unserializeFunc = reader.GetUnserializeCountFunc(t);
uint totalCount = 0;
uint i = 0;
try
{
for (; i < count; i++)
{
totalCount += 1 + unserializeFunc(reader);
}
}
catch (UndertaleSerializationException e)
{
throw new UndertaleSerializationException($"{e.Message}\nwhile reading child object count of item {i + 1} of {count} in a list of {t.FullName}", e);
}
return totalCount;
}
}
/// <summary>
/// Simple list object, specifically for strings, serialized as a count followed immediately by all strings in the list.
/// </summary>
public class UndertaleSimpleListString : UndertaleObservableList<UndertaleString>, UndertaleObject
{
/// <inheritdoc />
public void Serialize(UndertaleWriter writer)
{
// Write count
int count = Count;
writer.Write((uint)count);
// Write all of the strings
int i = 0;
try
{
for (; i < count; i++)
{
writer.WriteUndertaleString(this[i]);
}
}
catch (UndertaleSerializationException e)
{
throw new UndertaleSerializationException($"{e.Message}\nwhile writing item {i + 1} of {count} in a string-list", e);
}
}
/// <inheritdoc />
public void Unserialize(UndertaleReader reader)
{
// Read count and set list with that capacity
uint count = reader.ReadUInt32();
Clear();
SetCapacity(count);
// Read all of the strings
uint i = 0;
try
{
for (; i < count; i++)
{
InternalAdd(reader.ReadUndertaleString());
}
}
catch (UndertaleSerializationException e)
{
throw new UndertaleSerializationException($"{e.Message}\nwhile reading item {i + 1} of {count} in a string-list", e);
}
}
/// <inheritdoc cref="UndertaleObject.UnserializeChildObjectCount(UndertaleReader)"/>
public static uint UnserializeChildObjectCount(UndertaleReader reader)
{
uint count = reader.ReadUInt32();
reader.Position += count * 4;
return 0;
}
}
/// <summary>
/// Simple list object, serialized as a 16-bit count followed immediately by all objects in the list.
/// </summary>
public class UndertaleSimpleListShort<T> : UndertaleObservableList<T>, UndertaleObject where T : UndertaleObject, new()
{
private void EnsureShortCount()
{
if (Count > Int16.MaxValue)
throw new InvalidOperationException("Count of short SimpleList exceeds maximum number allowed.");
}
/// <inheritdoc cref="Collection{T}.Add(T)"/>
public new void Add(T item)
{
base.Add(item);
EnsureShortCount();
}
/// <inheritdoc />
public void Serialize(UndertaleWriter writer)
{
// Write list count
int count = Count;
writer.Write((ushort)count);
// Write actual objects
int i = 0;
try
{
for (; i < count; i++)
{
writer.WriteUndertaleObject(this[i]);
}
}
catch (UndertaleSerializationException e)
{
throw new UndertaleSerializationException($"{e.Message}\nwhile writing item {i + 1} of {count} in a list of {typeof(T).FullName}", e);
}
}
/// <inheritdoc />
public void Unserialize(UndertaleReader reader)
{
// Read count and set list with that capacity
ushort count = reader.ReadUInt16();
Clear();
SetCapacity(count);
// Read actual objects
ushort i = 0;
try
{
for (; i < count; i++)
{
InternalAdd(reader.ReadUndertaleObject<T>());
}
}
catch (UndertaleSerializationException e)
{
throw new UndertaleSerializationException($"{e.Message}\nwhile reading item {i + 1} of {count} in a list of {typeof(T).FullName}", e);
}
}
/// <inheritdoc cref="UndertaleObject.UnserializeChildObjectCount(UndertaleReader)"/>
public static uint UnserializeChildObjectCount(UndertaleReader reader)
{
// Read base object count; short-circuit if there's no objects
ushort count = reader.ReadUInt16();
if (count == 0)
{
return 0;
}
// If objects have a static child object size/count, simply multiply to determine the number of total objects
Type t = typeof(T);
if (t.IsAssignableTo(typeof(IStaticChildObjectsSize)))
{
uint subSize = reader.GetStaticChildObjectsSize(t);
uint subCount = 0;
if (t.IsAssignableTo(typeof(IStaticChildObjCount)))
{
subCount = reader.GetStaticChildCount(t);
}
reader.Position += count * subSize;
return count + (count * subCount);
}
// Determine object counts recursively for all objects
Func<UndertaleReader, uint> unserializeFunc = reader.GetUnserializeCountFunc(t);
uint totalCount = 0;
uint i = 0;
try
{
for (; i < count; i++)
{
totalCount += 1 + unserializeFunc(reader);
}
}
catch (UndertaleSerializationException e)
{
throw new UndertaleSerializationException($"{e.Message}\nwhile reading child object count of item {i + 1} of {count} in a list of {t.FullName}", e);
}
return totalCount;
}
}
/// <summary>
/// List of objects that is preceded by an array of pointers, pointing to each object in order.
/// </summary>
public class UndertalePointerList<T> : UndertaleObservableList<T>, UndertaleObject where T : UndertaleObject, new()
{
/// <inheritdoc />
public void Serialize(UndertaleWriter writer)
{
// Write count
int count = Count;
writer.Write((uint)count);
// Write all pointers
foreach (T obj in this)
{
writer.WriteUndertaleObjectPointer<T>(obj);
}
// Write blobs, if necessary for the given type
Type t = typeof(T);
if (t.IsAssignableTo(typeof(UndertaleObjectWithBlobs)))
{
int i = 0;
try
{
for (; i < count; i++)
{
((UndertaleObjectWithBlobs)this[i]).SerializeBlobBefore(writer);
}
}
catch (UndertaleSerializationException e)
{
throw new UndertaleSerializationException($"{e.Message}\nwhile writing blob for item {i + 1} of {count} in a list of {t.FullName}", e);
}
}
// Write actual objects
int j = 0;
try
{
for (; j < count; j++)
{
T obj = this[j];
// Serialize pre-padding, if this is a type that requires it
if (t.IsAssignableTo(typeof(PrePaddedObject)))
{
((PrePaddedObject)obj).SerializePrePadding(writer);
}
// Write object
writer.WriteUndertaleObject(obj);
// Serialize padding, if this is a type that requires it
if (t.IsAssignableTo(typeof(PaddedObject)))
{
// The last object does NOT get padding (TODO: at least in AUDO)
if (j != count - 1)
{
((PaddedObject)obj).SerializePadding(writer);
}
}
}
}
catch (UndertaleSerializationException e)
{
throw new UndertaleSerializationException($"{e.Message}\nwhile writing item {j + 1} of {count} in a list of {t.FullName}", e);
}
}
/// <inheritdoc />
public void Unserialize(UndertaleReader reader)
{
// Read count and set list with that capacity
uint count = reader.ReadUInt32();
Clear();
SetCapacity(count);
// Read in all object pointers
Type t = typeof(T);
uint i = 0;
try
{
for (; i < count; i++)
{
InternalAdd(reader.ReadUndertaleObjectPointer<T>());
}
}
catch (UndertaleSerializationException e)
{
throw new UndertaleSerializationException($"{e.Message}\nwhile reading pointer to item {i + 1} of {count} in a list of {t.FullName}", e);
}
// Advance to start of first object (particularly, if blobs exist)
if (count > 0)
{
uint pos = reader.GetAddressForUndertaleObject(this[0]);
if (reader.AbsPosition != pos)
{
long skip = pos - reader.AbsPosition;
if (skip > 0)
{
reader.AbsPosition += skip;
}
else
{
throw new IOException("First list item starts inside pointer list");
}
}
}
// Read in actual objects
uint j = 0;
try
{
for (; j < count; j++)
{
T obj = this[(int)j];
// Unserialize pre-padding, if this is a type that requires it
if (t.IsAssignableTo(typeof(PrePaddedObject)))
{
((PrePaddedObject)obj).UnserializePrePadding(reader);
}
// Read object
reader.ReadUndertaleObject(obj);
// Unserialize padding, if this is a type that requires it
if (t.IsAssignableTo(typeof(PaddedObject)))
{
// The last object does NOT get padding (TODO: at least in AUDO)
if (j != count - 1)
{
((PaddedObject)obj).UnserializePadding(reader);
}
}
}
}
catch (UndertaleSerializationException e)
{
throw new UndertaleSerializationException($"{e.Message}\nwhile reading item {j + 1} of {count} in a list of {t.FullName}", e);
}
}
/// <inheritdoc cref="UndertaleObject.UnserializeChildObjectCount(UndertaleReader)"/>
public static uint UnserializeChildObjectCount(UndertaleReader reader)
{
// Read base object count; short-circuit if there's no objects
uint count = reader.ReadUInt32();
if (count == 0)
{
return 0;
}
// If objects have a static child object size/count, simply multiply to determine the number of total objects
Type t = typeof(T);
if (t.IsAssignableTo(typeof(IStaticChildObjectsSize)))
{
uint subSize = reader.GetStaticChildObjectsSize(t);
uint subCount = 0;
if (t.IsAssignableTo(typeof(IStaticChildObjCount)))
{
subCount = reader.GetStaticChildCount(t);
}
reader.Position += (count * 4) + (count * subSize);
return count + (count * subCount);
}
// Read pointers of all objects
uint[] pointers = reader.ListPtrsPool.Rent((int)count);
for (uint i = 0; i < count; i++)
{
pointers[i] = reader.ReadUInt32();
}
// Advance to start of first object (particularly, if blobs exist)
uint pos = pointers[0];
if (reader.AbsPosition != pos)
{
long skip = pos - reader.AbsPosition;
if (skip > 0)
{
reader.AbsPosition += skip;
}
else
{
throw new IOException("First list item starts inside pointer list");
}
}
// Determine object counts recursively for all objects
var unserializeFunc = reader.GetUnserializeCountFunc(t);
uint totalCount = 0;
uint j = 0;
try
{
for (; j < count; j++)
{
reader.AbsPosition = pointers[j];
totalCount += 1 + unserializeFunc(reader);
}
}
catch (UndertaleSerializationException e)
{
throw new UndertaleSerializationException($"{e.Message}\nwhile reading child object count of item {j + 1} of {count} in a list of {t.FullName}", e);
}
finally
{
reader.ListPtrsPool.Return(pointers);
}
return totalCount;
}
}
/// <summary>
/// Shorthand for <see cref="UndertaleSimpleList{T}"/> containing <see cref="UndertaleResourceById{T, ChunkT}"/>.
/// </summary>
public class UndertaleSimpleResourcesList<T, ChunkT> : UndertaleSimpleList<UndertaleResourceById<T, ChunkT>> where T : UndertaleResource, new() where ChunkT : UndertaleListChunk<T>
{
// TODO: Allow direct access to Resource elements?
}
}