forked from UnderminersTeam/UndertaleModTool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUndertaleIO.cs
More file actions
1041 lines (902 loc) · 36.9 KB
/
UndertaleIO.cs
File metadata and controls
1041 lines (902 loc) · 36.9 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
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using UndertaleModLib.Compiler;
using UndertaleModLib.Models;
using UndertaleModLib.Util;
namespace UndertaleModLib
{
/**
* TODO: This is not the cleanest implementation, but I was focusing on clean interface.
* Could probably use some refactoring or a complete rewrite.
*/
public interface UndertaleResourceRef : UndertaleObject
{
object Resource { get; set; }
void PostUnserialize(UndertaleReader reader);
int SerializeById(UndertaleWriter writer);
}
public class UndertaleResourceById<T, ChunkT> : UndertaleResourceRef, IStaticChildObjectsSize, IDisposable where T : UndertaleResource, new() where ChunkT : UndertaleListChunk<T>
{
/// <inheritdoc cref="IStaticChildObjectsSize.ChildObjectsSize" />
public static readonly uint ChildObjectsSize = 4;
public int CachedId { get; set; } = -1;
public T Resource { get; set; }
object UndertaleResourceRef.Resource { get => Resource; set => Resource = (T)value; }
public UndertaleResourceById()
{
this.CachedId = -1;
}
public UndertaleResourceById(int id = -1)
{
this.CachedId = id;
}
public UndertaleResourceById(T res)
{
this.Resource = res;
}
public UndertaleResourceById(T res, int id = -1)
{
this.Resource = res;
this.CachedId = id;
}
private static ChunkT FindListChunk(UndertaleData data)
{
if (data.FORM.ChunksTypeDict.TryGetValue(typeof(ChunkT), out UndertaleChunk chunk))
{
return (ChunkT)chunk;
}
return null;
}
public int SerializeById(UndertaleWriter writer)
{
ChunkT chunk = FindListChunk(writer.undertaleData);
if (chunk != null)
{
if (Resource != null)
{
CachedId = chunk.IndexDict[Resource];
if (CachedId < 0)
throw new IOException("Unregistered object");
}
else
{
int newCachedId;
if (typeof(ChunkT) == typeof(UndertaleChunkAGRP))
newCachedId = 0;
else
newCachedId = -1;
if (CachedId > 0 || (typeof(ChunkT) != typeof(UndertaleChunkAGRP) && CachedId == 0))
{
if (chunk.List.Count > CachedId && chunk.List[CachedId] is not null)
{
int firstNullOccurrence = chunk.List.IndexOf(default);
if (firstNullOccurrence != -1)
newCachedId = firstNullOccurrence;
}
else
{
newCachedId = CachedId;
}
}
CachedId = newCachedId;
}
}
return CachedId;
}
public void UnserializeById(UndertaleReader reader, int id)
{
if (id < -1)
throw new IOException("Invalid value for resource ID (" + typeof(ChunkT).Name + "): " + id);
CachedId = id;
reader.RequestResourceUpdate(this);
}
public void PostUnserialize(UndertaleReader reader)
{
IList<T> list = FindListChunk(reader.undertaleData)?.List;
if (list != null)
{
if (typeof(ChunkT) == typeof(UndertaleChunkAGRP) && CachedId == reader.undertaleData.GetBuiltinSoundGroupID() && list.Count == 0) // I won't even ask why this works like that
{
Resource = default;
return;
}
if (CachedId >= list.Count)
{
reader.SubmitWarning("Invalid value for resource ID of type " + typeof(ChunkT).Name + ": " + CachedId + " (there are only " + list.Count + ")");
return;
}
Resource = CachedId >= 0 ? list[CachedId] : default;
if (Resource == null && CachedId >= 0)
{
// Naturally this can only happen with 2024.11 data files.
// FIXME: Is this a good idea?
if (reader.undertaleData.IsGameMaker2())
{
if (!reader.undertaleData.IsVersionAtLeast(2024, 11))
reader.undertaleData.SetGMS2Version(2024, 11);
}
else
{
reader.SubmitWarning("ID reference to null object found on file built with GMS pre-2!");
}
}
}
}
/// <inheritdoc/>
public override string ToString()
{
return (Resource?.ToString() ?? "(null)") + GetMarkerSuffix();
}
/// <inheritdoc/>
public void Dispose()
{
GC.SuppressFinalize(this);
Resource = default;
}
public string GetMarkerSuffix()
{
return "@" + CachedId;
}
public void Serialize(UndertaleWriter writer)
{
writer.Write(SerializeById(writer));
}
public void Unserialize(UndertaleReader reader)
{
UnserializeById(reader, reader.ReadInt32());
}
}
public class UndertaleReader : AdaptiveBinaryReader
{
/// <summary>
/// Function to delegate warning messages to.
/// </summary>
/// <param name="warning">Warning message.</param>
/// <param name="isImportant">Whether the warning is deemed important (that is, may risk data loss when re-saving).</param>
public delegate void WarningHandlerDelegate(string warning, bool isImportant);
/// <summary>
/// Function to delegate informational messages to.
/// </summary>
/// <param name="message">Informational message.</param>
public delegate void MessageHandlerDelegate(string message);
private readonly WarningHandlerDelegate _warningHandler;
private readonly MessageHandlerDelegate _messageHandler;
public bool ReadOnlyGEN8 { get; set; }
/// <summary>
/// The detected absolute path of the data file, if a FileStream is passed in, or null otherwise (by default).
/// Can also be manually changed.
/// </summary>
public string FilePath { get; set; } = null;
/// <summary>
/// The detected absolute path of the directory containing the data file, if a FileStream is passed in, or null otherwise (by default).
/// Can also be manually changed.
/// </summary>
public string Directory { get; set; } = null;
internal readonly record struct BytecodeInformation(uint InstructionCount, UndertaleCode RootEntry);
internal Dictionary<uint, BytecodeInformation> BytecodeAddresses;
internal ArrayPool<uint> ListPtrsPool = ArrayPool<uint>.Create(100000, 17);
internal string LastChunkName;
internal List<string> AllChunkNames;
internal bool Bytecode14OrLower = false;
public UndertaleReader(Stream input,
WarningHandlerDelegate warningHandler = null, MessageHandlerDelegate messageHandler = null,
bool onlyGeneralInfo = false) : base(input)
{
_warningHandler = warningHandler;
_messageHandler = messageHandler;
ReadOnlyGEN8 = onlyGeneralInfo;
if (input is FileStream fs)
{
FilePath = fs.Name;
Directory = Path.GetDirectoryName(FilePath);
}
FillUnserializeCountDictionaries();
}
// TODO: This would be more useful if it reported location like the exceptions did
public void SubmitWarning(string warning, bool isImportant = true)
{
if (_warningHandler != null)
_warningHandler.Invoke(warning, isImportant);
else
throw new IOException(warning);
}
public void SubmitMessage(string message)
{
if (_messageHandler != null)
_messageHandler.Invoke(message);
else
Debug.WriteLine(message);
}
public UndertaleChunk ReadUndertaleChunk()
{
return UndertaleChunk.Unserialize(this);
}
public (uint, UndertaleChunk) CountChunkChildObjects()
{
return UndertaleChunk.CountChunkChildObjects(this);
}
private readonly List<UndertaleResourceRef> _resourceRefsToResolve = new(256);
internal UndertaleData undertaleData;
public UndertaleData ReadUndertaleData()
{
// Create new data context
UndertaleData data = new();
undertaleData = data;
// Ensure root chunk is called "FORM"
string name = ReadChars(4);
if (name != "FORM")
throw new IOException($"Root chunk is \"{name}\", not FORM");
uint length = ReadUInt32();
data.FORM = new UndertaleChunkFORM
{
Length = length
};
DebugUtil.Assert(data.FORM.Name == name);
// Perform object counting pass on file
long startPos = Position;
uint poolSize = 0;
if (!ProcessObjectCountingErrors()) // process an exception from "FillUnserializeCountDictionaries()"
{
try
{
if (!ReadOnlyGEN8)
{
poolSize = data.FORM.UnserializeObjectCount(this);
}
}
catch (Exception e)
{
countUnserializeExc = e;
Debug.WriteLine(e);
SwitchReaderType(false);
}
}
ListPtrsPool = null;
// Initialize object pools
InitializePools(poolSize);
// Read all of the main data
Position = startPos;
EnsureLengthOperation lenReader = EnsureLengthFromHere(data.FORM.Length);
data.FORM.UnserializeChunk(this);
lenReader.ToHere();
// Resolve resource IDs
SubmitMessage("Resolving resource IDs...");
foreach (UndertaleResourceRef res in _resourceRefsToResolve)
{
res.PostUnserialize(this);
}
_resourceRefsToResolve.Clear();
// Skip extra processing if it's not a regular data file (e.g., audio group files)
if (data.FORM.GEN8 is not null)
{
// Initialize GML builtins
data.BuiltinList = new BuiltinList(data);
// Initialize game-specific data resolver
try
{
Decompiler.GameSpecificResolver.Initialize(data);
}
catch (Exception e)
{
SubmitWarning($"Error initializing game-specific data resolver:\n{e.GetType().FullName}: {e.Message}", false);
data.GameSpecificRegistry = new(); // Use a blank registry...
}
// Link texture group information, if relevant
UndertaleEmbeddedTexture.FindAllTextureInfo(data);
// Iterate over function names to see if 2023.11+ naming process was used (if necessary)
if (data.Functions is not null && data.IsVersionAtLeast(2023, 8) && !data.IsVersionAtLeast(2023, 11))
{
foreach (UndertaleFunction function in data.Functions)
{
// If name starts with "gml_Script" and contains a @ character, it should be from 2023.11
if (function.Name.Content is string functionName &&
functionName.StartsWith("gml_Script_", StringComparison.Ordinal) &&
functionName.Contains('@'))
{
data.SetGMS2Version(2023, 11);
break;
}
}
}
}
// Process any errors that may have occurred during object counting
ProcessObjectCountingErrors(poolSize);
return data;
}
internal void RequestResourceUpdate(UndertaleResourceRef res)
{
_resourceRefsToResolve.Add(res);
}
/// <summary>
/// Reads a boolean as 32 bits (0 or 1), maintaining alignment.
/// </summary>
public override bool ReadBoolean()
{
uint val = ReadUInt32();
return val switch
{
0 => false,
1 => true,
_ => throw new IOException($"Invalid boolean value: {val}")
};
}
private Dictionary<uint, UndertaleObject> objectPool;
private Dictionary<UndertaleObject, uint> objectPoolRev;
private HashSet<uint> unreadObjects = new HashSet<uint>();
private Exception countUnserializeExc = null;
private readonly Dictionary<Type, Func<UndertaleReader, uint>> unserializeFuncDict = new();
private readonly Dictionary<Type, uint> staticObjCountDict = new();
private readonly Dictionary<Type, uint> staticObjSizeDict = new();
private readonly BindingFlags publicStaticFlags
= BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy;
private readonly Type[] readerArgType = { typeof(UndertaleReader) };
private readonly Type delegateType = typeof(Func<UndertaleReader, uint>);
private readonly Func<UndertaleReader, uint> blankCountFunc = _ => 0;
private bool ProcessObjectCountingErrors(uint poolSize = 0)
{
if (countUnserializeExc is not null)
{
try
{
string fileDir = Path.GetDirectoryName(Environment.ProcessPath);
File.WriteAllText(Path.Combine(fileDir, "unserializeCountError.txt"),
countUnserializeExc + "\n"
+ countUnserializeExc.Message + "\n"
+ countUnserializeExc.StackTrace);
SubmitWarning("Warning - there was an error while trying to unserialize total object count.\n" +
"The error log is saved to \"unserializeCountError.txt\"." +
"Please report that error to UndertaleModTool GitHub.");
}
catch { }
countUnserializeExc = null;
return true;
}
if (poolSize != 0 && poolSize != objectPool.Count)
{
SubmitWarning("Warning - the estimated object pool size differs from the actual size.\n" +
$"Estimated: {poolSize}, Actual: {objectPool.Count}\n" +
"Please report this on UndertaleModTool GitHub.", false);
}
return false;
}
private void FillUnserializeCountDictionaries()
{
try
{
Assembly currAssem = Assembly.GetExecutingAssembly();
Type[] allTypes = currAssem.GetTypes();
Type utObjectType = typeof(UndertaleObject);
Type staticObjCountType = typeof(IStaticChildObjCount);
Type staticObjSizeType = typeof(IStaticChildObjectsSize);
allTypes = allTypes.Where(t => t.IsAssignableTo(utObjectType)).ToArray();
foreach (Type t in allTypes)
{
// It's not possible to call a static method of generic classes without present type argument.
if (t.ContainsGenericParameters)
continue;
MethodInfo mi = t.GetMethod("UnserializeChildObjectCount", publicStaticFlags, readerArgType);
if (mi is null)
continue;
var func = Delegate.CreateDelegate(delegateType, mi) as Func<UndertaleReader, uint>;
if (func is null)
{
Debug.WriteLine($"Can't create a delegate from MethodInfo of type \"{t.FullName}\"");
continue;
}
unserializeFuncDict[t] = func;
}
for (int i = 0; i < allTypes.Length; i++)
{
Type t = allTypes[i];
FieldInfo fi;
object res;
// It's not supported to get a static field from generic classes without present type argument.
if (t.ContainsGenericParameters)
continue;
if (t.IsAssignableTo(staticObjCountType))
{
fi = t.GetField("ChildObjectCount", publicStaticFlags);
if (fi is null)
{
Debug.WriteLine($"Can't get \"ChildObjectCount\" field of \"{t.FullName}\"");
continue;
}
res = fi.GetValue(null);
if (res is null)
{
Debug.WriteLine($"Can't get value of \"ChildObjectCount\" of \"{t.FullName}\"");
continue;
}
staticObjCountDict[t] = (uint)res;
}
if (t.IsAssignableTo(staticObjSizeType))
{
fi = t.GetField("ChildObjectsSize", publicStaticFlags);
if (fi is null)
{
Debug.WriteLine($"Can't get \"ChildObjectsSize\" field of \"{t.FullName}\"");
continue;
}
res = fi.GetValue(null);
if (res is null)
{
Debug.WriteLine($"Can't get value of \"ChildObjectsSize\" of \"{t.FullName}\"");
continue;
}
staticObjSizeDict[t] = (uint)res;
}
}
}
catch (Exception e)
{
Debug.WriteLine(e);
countUnserializeExc = e;
}
}
public Func<UndertaleReader, uint> GetUnserializeCountFunc(Type objType)
{
if (!unserializeFuncDict.TryGetValue(objType, out var res))
{
MethodInfo mi = objType.GetMethod("UnserializeChildObjectCount", publicStaticFlags, readerArgType);
if (mi is null)
{
Debug.WriteLine($"\"UndertaleReader.unserializeFuncDict\" doesn't contain a method for \"{objType.FullName}\".");
return blankCountFunc;
}
//Debug.WriteLine($"Adding a generic class method for \"{objType.FullName}\" to \"UndertaleReader.unserializeFuncDict\".");
var func = Delegate.CreateDelegate(delegateType, mi) as Func<UndertaleReader, uint>;
if (func is null)
{
Debug.WriteLine($"Can't create a delegate from MethodInfo of type \"{objType.FullName}\"");
return blankCountFunc;
}
unserializeFuncDict[objType] = func;
res = func;
}
return res;
}
public uint GetStaticChildCount(Type objType)
{
if (!staticObjCountDict.TryGetValue(objType, out uint res))
{
Debug.WriteLine($"\"UndertaleReader.staticObjCountDict\" doesn't contain type \"{objType.FullName}\".");
return 0;
}
return res;
}
public uint GetStaticChildObjectsSize(Type objType)
{
if (!staticObjSizeDict.TryGetValue(objType, out uint res))
{
Debug.WriteLine($"\"UndertaleReader.staticObjSizeDict\" doesn't contain type \"{objType.FullName}\".");
return 0;
}
return res;
}
public void SetStaticChildCount(Type objType, uint count)
{
staticObjCountDict[objType] = count;
}
public void SetStaticChildObjectsSize(Type objType, uint size)
{
staticObjSizeDict[objType] = size;
}
public Dictionary<uint, UndertaleObject> GetOffsetMap()
{
return objectPool;
}
public Dictionary<UndertaleObject, uint> GetOffsetMapRev()
{
return objectPoolRev;
}
public void InitializePools(uint objCount = 0)
{
if (objCount == 0)
{
objectPool = new();
objectPoolRev = new();
}
else
{
int objCountInt = (int)objCount;
objectPool = new(objCountInt);
objectPoolRev = new(objCountInt);
}
}
public uint GetChildObjectCount(Type t)
{
if (!unserializeFuncDict.TryGetValue(t, out var func))
{
if (staticObjSizeDict.TryGetValue(t, out uint size))
{
Position += size;
staticObjCountDict.TryGetValue(t, out uint subCount);
return subCount;
}
throw new UndertaleSerializationException(
$"\"UndertaleReader.unserializeFuncDict\" doesn't contain a method for \"{t.FullName}\".");
}
return func(this);
}
public uint GetChildObjectCount<T>() where T : UndertaleObject
{
Type t = typeof(T);
return GetChildObjectCount(t);
}
public T GetUndertaleObjectAtAddress<T>(uint address) where T : UndertaleObject, new()
{
if (address == 0)
return default;
UndertaleObject obj;
if (!objectPool.TryGetValue(address, out obj))
{
obj = new T();
objectPool.Add(address, obj);
objectPoolRev.Add(obj, address);
unreadObjects.Add(address);
}
return (T)obj;
}
public uint GetAddressForUndertaleObject(UndertaleObject obj)
{
if (obj == null)
return 0;
return objectPoolRev[obj];
}
public void ReadUndertaleObject<T>(T obj) where T : UndertaleObject, new()
{
try
{
uint expectedAddress = GetAddressForUndertaleObject(obj);
if (expectedAddress == 0)
return;
if (expectedAddress != AbsPosition)
{
SubmitWarning($"Reading misaligned at {AbsPosition:X8}, realigning back to {expectedAddress:X8}\nHIGH RISK OF DATA LOSS! The file is probably corrupted, or uses unsupported features\nProceed at your own risk");
AbsPosition = expectedAddress;
}
unreadObjects.Remove((uint)AbsPosition);
obj.Unserialize(this);
}
catch (Exception e)
{
throw new UndertaleSerializationException(e.Message + "\nat " + AbsPosition.ToString("X8") + " while reading object " + typeof(T).FullName, e);
}
}
public T ReadUndertaleObject<T>() where T : UndertaleObject, new()
{
uint address = (uint)AbsPosition;
T result;
if (objectPool.TryGetValue(address, out UndertaleObject obj))
{
result = (T)obj;
unreadObjects.Remove(address);
}
else
{
result = new T();
objectPool.Add(address, result);
objectPoolRev.Add(result, address);
}
result.Unserialize(this);
return result;
}
public T ReadUndertaleObjectNoPool<T>() where T : UndertaleObject, new()
{
T o = new();
o.Unserialize(this);
return o;
}
public T ReadUndertaleObjectPointer<T>() where T : UndertaleObject, new()
{
return GetUndertaleObjectAtAddress<T>(ReadUInt32());
}
public UndertaleString ReadUndertaleString()
{
uint addr = ReadUInt32();
if (addr == 0)
return null;
// Normally, the strings point directly to the string content
// This may be done that way because it's faster when the game accesses them, but for our purposes it's better to access the whole string resource object
return GetUndertaleObjectAtAddress<UndertaleString>(addr - 4);
}
public void ThrowIfUnreadObjects()
{
if (ReadOnlyGEN8)
return;
if (unreadObjects.Count > 0)
{
throw new IOException("Found pointer targets that were never read:\n" + String.Join("\n", unreadObjects.Take(10).Select((x) => "0x" + x.ToString("X8") + " (" + objectPool[x].GetType().Name + ")")) + (unreadObjects.Count > 10 ? "\n(and more, " + unreadObjects.Count + " total)" : ""));
}
}
public class EnsureLengthOperation
{
private readonly UndertaleReader reader;
private readonly int startPos;
private readonly uint expectedLength;
internal EnsureLengthOperation(UndertaleReader reader, uint expectedLength)
{
this.reader = reader;
this.startPos = (int)reader.Position;
this.expectedLength = expectedLength;
}
public void ToHere()
{
int endPos = (int)reader.Position;
uint length = (uint)(endPos - startPos);
if (length != expectedLength)
{
int diff = (int)expectedLength - (int)length;
reader.SubmitWarning("WARNING: File specified length " + expectedLength + ", but read only " + length + " (" + diff + " padding?)");
if (diff > 0)
reader.Position += (uint)diff;
else
throw new IOException("Read underflow");
}
}
}
public void Align(int alignment)
{
while ((AbsPosition & (alignment - 1)) != 0)
{
if (ReadByte() != 0)
{
throw new IOException("Invalid alignment padding");
}
}
}
public EnsureLengthOperation EnsureLengthFromHere(uint expectedLength)
{
return new EnsureLengthOperation(this, expectedLength);
}
}
public class UndertaleWriter : FileBinaryWriter
{
internal UndertaleData undertaleData;
public string LastChunkName;
public uint LastBytecodeAddress = 0;
public bool Bytecode14OrLower;
public delegate void MessageHandlerDelegate(string message);
private MessageHandlerDelegate MessageHandler;
public UndertaleWriter(Stream output, MessageHandlerDelegate messageHandler = null) : base(output)
{
MessageHandler = messageHandler;
}
public void SubmitMessage(string message)
{
if (MessageHandler != null)
MessageHandler.Invoke(message);
else
Debug.WriteLine(message);
}
public void Write(UndertaleChunk obj)
{
obj.Serialize(this);
}
/// <summary>
/// Writes a boolean using 32 bits (0 or 1), maintaining alignment.
/// </summary>
public override void Write(bool b)
{
Write(b ? (uint)1 : (uint)0);
}
public void WriteUndertaleData(UndertaleData data)
{
undertaleData = data;
Bytecode14OrLower = data?.GeneralInfo?.BytecodeVersion <= 14;
// Figure out the last chunk by iterating identically as it does when serializing,
// and generate the object index dictionaries for acceleration of "UndertaleResourceById.SerializeById()"
foreach (var chunk in data.FORM.Chunks)
{
LastChunkName = chunk.Key;
if (chunk.Value is IUndertaleListChunk listChunk)
{
listChunk.GenerateIndexDict();
}
}
Write(data.FORM);
}
private Dictionary<UndertaleObject, uint> objectPool = new();
private Dictionary<UndertaleObject, List<uint>> pendingWrites = new();
private Dictionary<UndertaleObject, List<uint>> pendingStringWrites = new();
public void Flush(UndertaleData data)
{
// Clear out index dictionaries (no longer needed)
foreach (var chunk in data.FORM.Chunks.Values)
{
if (chunk is IUndertaleListChunk listChunk)
{
listChunk.ClearIndexDict();
}
}
SubmitMessage("Flushing remaining buffer data...");
base.Flush();
}
public Dictionary<UndertaleObject, uint> GetObjectPool()
{
return objectPool;
}
public uint GetAddressForUndertaleObject(UndertaleObject obj)
{
if (obj == null)
return 0;
if (!objectPool.TryGetValue(obj, out uint res))
throw new KeyNotFoundException();
return res;
}
public void WriteUndertaleObject<T>(T obj) where T : UndertaleObject
{
if (obj is null)
{
// We simply shouldn't write anything.
// Pointers to this "object" are simply written as 0, and we don't need to
// put it in the pool
return;
}
try
{
// Store object address before writing it
uint objectAddr = Position;
if (typeof(T) == typeof(UndertaleString))
{
// Can skip adding strings to object pool (nothing later in the file references them).
obj.Serialize(this);
// Patch all pointers to this string, if applicable
if (pendingStringWrites.TryGetValue(obj, out List<uint> patches))
{
uint returnTo = Position;
objectAddr += 4; // Destination is where the string starts, not its length
foreach (uint pointerAddr in patches)
{
Position = pointerAddr;
Write(objectAddr);
}
Position = returnTo;
// Remove pending write
pendingStringWrites.Remove(obj);
}
}
else
{
// Add object to pool
objectPool.Add(obj, objectAddr);
// Serialize object
obj.Serialize(this);
// Patch all pointers to this object, if applicable
if (pendingWrites.TryGetValue(obj, out List<uint> patches))
{
uint returnTo = Position;
foreach (uint pointerAddr in patches)
{
Position = pointerAddr;
Write(objectAddr);
}
Position = returnTo;
// Remove pending write
pendingWrites.Remove(obj);
}
}
}
catch (Exception e)
{
throw new UndertaleSerializationException($"{e.Message}\nat {Position:X8} while writing object {typeof(T).FullName}", e);
}
}
public void WriteUndertaleObjectPointer<T>(T obj) where T : UndertaleObject
{
if (obj == null)
{
Write(0x00000000u);
return;
}
if (objectPool.ContainsKey(obj))
{
Write(objectPool[obj]);
}
else
{
if (!pendingWrites.TryGetValue(obj, out List<uint> list))
{
pendingWrites.Add(obj, list = new List<uint>());
}
list.Add(Position);
Write(0xDEADC0DEu);
}
}
public void WriteUndertaleString(UndertaleString obj)
{
if (obj == null)
{
Write(0x0000000u);
return;
}
if (!pendingStringWrites.TryGetValue(obj, out List<uint> list))
{
pendingStringWrites.Add(obj, list = new List<uint>());
}
list.Add(Position);
Write(0xDEADC0DEu);
}
public void ThrowIfUnwrittenObjects()
{
if ((pendingWrites.Count + pendingStringWrites.Count) != 0)
{
var unwrittenObjects = pendingWrites.Concat(pendingStringWrites);
throw new IOException("Found pointer targets that were never written:\n"
+ String.Join("\n", unwrittenObjects.Take(10).Select((x) => x.Key + " at " + String.Join(", ", x.Value.Select((y) => "0x" + y.ToString("X8")))))
+ (unwrittenObjects.Count() > 10
? "\n(and more, " + unwrittenObjects.Count() + " total)"
: ""));
}
}
public void Align(int alignment)
{
while ((Position & (alignment - 1)) != 0)
{
Write((byte)0);
}
}
public class WriteLengthOperation
{
private readonly UndertaleWriter writer;
private readonly uint writePos;
private uint? startPos = null;
internal WriteLengthOperation(UndertaleWriter writer)
{
this.writer = writer;
this.writePos = writer.Position;
writer.Write(0xDEADC0DEu);
}
public void FromHere()
{
this.startPos = writer.Position;
}
public uint ToHere()
{
if (!startPos.HasValue)
throw new InvalidOperationException("Forgot to call FromHere()");
uint endPos = writer.Position;
writer.Position = writePos;
uint valueToWrite = endPos - startPos.Value;