-
Notifications
You must be signed in to change notification settings - Fork 2
/
PathSpline.cs
1422 lines (1167 loc) · 63.3 KB
/
PathSpline.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
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 Newtonsoft.Json.Converters;
namespace KnuxLib.Engines.Hedgehog
{
// Based on https://github.com/blueskythlikesclouds/SkythTools/tree/master/Sonic%20Forces/Path%20Scripts
// TODO: Figure out and properly read the k-d tree data.
// TODO: Check to see if Lost World and Frontiers handle anything other than their tags differently, if so, handle them with the FormatVersion check.
// TODO: Slightly tidy this up after the porting process.
// TODO: Potentially combine the spline types? Considering the only difference is SideView and Rail swapping around?
public class PathSpline : FileBase
{
// Generic VS stuff to allow creating an object that instantly loads a file.
public PathSpline() { }
public PathSpline(string filepath, FormatVersion version = FormatVersion.Wars, bool export = false, string saveExtension = ".path", bool bigEndianSave = false)
{
// Check if the input file is an OBJ.
if (StringHelpers.GetExtension(filepath) == ".obj")
{
// Import this OBJ.
Data = ImportOBJ(filepath, version);
// If the export flag is set, then save this format.
if (export)
Save($@"{StringHelpers.GetExtension(filepath, true)}{saveExtension}", version, bigEndianSave);
}
// Check if the input file isn't an OBJ.
else
{
// Load this file.
Load(filepath, version);
// If the export flag is set, then export this format.
if (export)
ExportOBJ($@"{StringHelpers.GetExtension(filepath, true)}.obj");
}
}
// Classes for this format.
public enum FormatVersion
{
sonic_2013 = 0,
Wars = 1,
Rangers = 2
}
[JsonConverter(typeof(StringEnumConverter))]
public enum SplineType2013Wars : ulong
{
Default = 0,
SideView = 1,
GrindRail = 2
}
[JsonConverter(typeof(StringEnumConverter))]
public enum SplineTypeRangersMiller : ulong
{
Default = 0,
GrindRail = 1,
SideView = 2
}
[JsonConverter(typeof(StringEnumConverter))]
public enum GrindSpeed : ulong
{
Normal = 0,
Slow = 1,
Fast = 2
}
public class SplinePath
{
/// <summary>
/// The name of this spline.
/// </summary>
public string Name { get; set; } = "objpath_001";
/// <summary>
/// An unknown short value.
/// TODO: What is this? Lost World uses it quite a bit, but objpath_001 in w7b02_path.path is the only time this is 0 rather than 1 in Forces.
/// TODO: Could this indicate whether a spline is open or not? If this was the case then objpath_003 in Forces' w7a03_path.path and svpath_320_SV + svpath_321_SV in Frontiers' w6d08_sv_path.path should have it too?
/// </summary>
public ushort UnknownUShort_1 { get; set; } = 0x01;
/// <summary>
/// An unknown array of boolean values. Forces and Frontiers have all of these set to true, but Lost World sometimes has values set to false in it.
/// TODO: What do these do?
/// </summary>
public bool[] UnknownBooleanArray_1 { get; set; } = [];
/// <summary>
/// This spline's distance array.
/// TODO: Should we really be storing these values rather than just calculating them on the fly?
/// </summary>
public float[] Distance { get; set; } = [];
/// <summary>
/// This spline's knot points.
/// </summary>
public Vector3[] Knots { get; set; } = [];
/// <summary>
/// This spline's up vector array.
/// TODO: Should we really be storing these values rather than just calculating them on the fly?
/// </summary>
public Vector3[] UpVector { get; set; } = [];
/// <summary>
/// This spline's forward vector array.
/// TODO: Should we really be storing these values rather than just calculating them on the fly?
/// </summary>
public Vector3[] ForwardVector { get; set; } = [];
/// <summary>
/// This spline's knot points for double splines.
/// </summary>
public Vector3[]? DoubleKnots { get; set; }
/// <summary>
/// This spline's axis aligned bounding box.
/// TODO: Should we really be storing this value rather than just calculating it on the fly?
/// </summary>
public AABB AxisAlignedBoundingBox { get; set; } = new();
/// <summary>
/// This spline's type.
/// </summary>
public object Type { get; set; } = SplineType2013Wars.Default;
/// <summary>
/// This path's UID, if it has one.
/// </summary>
public ulong? UID { get; set; }
/// <summary>
/// The name of the path that this one should flow into, if it has one.
/// </summary>
public string? NextPathName { get; set; }
/// <summary>
/// This path's grind speed, if it has one.
/// </summary>
public GrindSpeed? GrindSpeed { get; set; }
/// <summary>
/// This path's k-d tree.
/// </summary>
public KDTree KDTree { get; set; } = new();
/// <summary>
/// Displays this path's name in the debugger.
/// </summary>
public override string ToString() => Name;
/// <summary>
/// Initialises this path with default data.
/// </summary>
public SplinePath() { }
/// <summary>
/// Initialises this path with the provided data.
/// </summary>
public SplinePath(string name, ushort unknownUShort_1, bool[] unknownBooleanArray_1, float[] distance, Vector3[] knots, Vector3[] upVector, Vector3[] forwardVector, Vector3[]? doubleKnots, AABB axisAlignedBoundingBox, object type, ulong? uid, string? nextPathName, GrindSpeed? grindSpeed, KDTree kdTree)
{
Name = name;
UnknownUShort_1 = unknownUShort_1;
UnknownBooleanArray_1 = unknownBooleanArray_1;
Distance = distance;
Knots = knots;
UpVector = upVector;
ForwardVector = forwardVector;
DoubleKnots = doubleKnots;
AxisAlignedBoundingBox = axisAlignedBoundingBox;
Type = type;
UID = uid;
NextPathName = nextPathName;
GrindSpeed = grindSpeed;
KDTree = kdTree;
}
/// <summary>
/// Initialises this path by reading its data from a BINAReader.
/// </summary>
public SplinePath(BINAReader reader, FormatVersion version) => Read(reader, version);
/// <summary>
/// Reads the data for this path.
/// </summary>
public void Read(BINAReader reader, FormatVersion version)
{
// Read this path's name.
if (version == FormatVersion.sonic_2013)
Name = StringHelpers.ReadNullTerminatedStringTableEntry(reader, 0x04);
else
Name = StringHelpers.ReadNullTerminatedStringTableEntry(reader, 0x08);
// Read an unknown ushort value that is always 1 except for a single instance.
UnknownUShort_1 = reader.ReadUInt16();
// Read the count of spline knots for this path.
ushort knotCount = reader.ReadUInt16();
// Skip a floating point value which is always the same as the final value in the distance array.
reader.JumpAhead(0x04);
// Read the offset to an unknown table (of knotCount length) of booleans..
long unknownBooleanTableOffset = reader.ReadInt64();
// If this is a sonic_2013 format path, then jump back and read unknownBooleanTableOffset as a 32 bit integer instead.
// TODO: Lost World can use false in this array, but Forces and Frontiers always have every value be true.
if (version == FormatVersion.sonic_2013)
{
reader.JumpBehind(0x08);
unknownBooleanTableOffset = reader.ReadUInt32();
}
// Read the offset to this path's distance array.
long distanceOffset = reader.ReadInt64();
// If this is a sonic_2013 format path, then jump back and read distanceOffset as a 32 bit integer instead.
if (version == FormatVersion.sonic_2013)
{
reader.JumpBehind(0x08);
distanceOffset = reader.ReadUInt32();
}
// Read the offset to this path's spline knot array.
long knotOffset = reader.ReadInt64();
// If this is a sonic_2013 format path, then jump back and read knotOffset as a 32 bit integer instead.
if (version == FormatVersion.sonic_2013)
{
reader.JumpBehind(0x08);
knotOffset = reader.ReadUInt32();
}
// Read the offset to this path's up vector array.
long upVectorOffset = reader.ReadInt64();
// If this is a sonic_2013 format path, then jump back and read upVectorOffset as a 32 bit integer instead.
if (version == FormatVersion.sonic_2013)
{
reader.JumpBehind(0x08);
upVectorOffset = reader.ReadUInt32();
}
// Read the offset to this path's forward vector array.
long forwardVectorOffset = reader.ReadInt64();
// If this is a sonic_2013 format path, then jump back and read forwardVectorOffset as a 32 bit integer instead.
if (version == FormatVersion.sonic_2013)
{
reader.JumpBehind(0x08);
forwardVectorOffset = reader.ReadUInt32();
}
// Read offset to this path's double spline knot count.
ulong doubleKnotCount = reader.ReadUInt64();
// If this is a sonic_2013 format path, then jump back and read this path's double spline knot count as a 32 bit integer instead.
if (version == FormatVersion.sonic_2013)
{
reader.JumpBehind(0x08);
doubleKnotCount = reader.ReadUInt32();
}
// Read offset to this path's double spline knot array.
long doubleKnotOffset = reader.ReadInt64();
// If this is a sonic_2013 format path, then jump back and read doubleKnotOffset as a 32 bit integer instead.
if (version == FormatVersion.sonic_2013)
{
reader.JumpBehind(0x08);
doubleKnotOffset = reader.ReadUInt32();
}
// Read this path's axis aligned bounding box.
AxisAlignedBoundingBox = new(reader);
// Read the count of tags in this path, usually 2 ("type" and "uid") but some don't have the "uid" one.
ulong tagCount = reader.ReadUInt64();
// If this is a sonic_2013 format path, then jump back and read the tag count as a 32 bit integer instead.
if (version == FormatVersion.sonic_2013)
{
reader.JumpBehind(0x08);
tagCount = reader.ReadUInt32();
}
// Read the offset to this path's tag data.
long tagOffset = reader.ReadInt64();
// If this is a sonic_2013 format path, then jump back and read tagOffset as a 32 bit integer instead.
if (version == FormatVersion.sonic_2013)
{
reader.JumpBehind(0x08);
tagOffset = reader.ReadUInt32();
}
// Skip an unknown value that is always 0.
if (version != FormatVersion.sonic_2013)
reader.CheckValue(0x00L);
else
reader.CheckValue(0x00);
// Read the offset to this path's k-d tree.
long kdTreeOffset = reader.ReadInt64();
// If this is a sonic_2013 format path, then jump back and read kdTreeOffset as a 32 bit integer instead.
if (version == FormatVersion.sonic_2013)
{
reader.JumpBehind(0x08);
kdTreeOffset = reader.ReadUInt32();
}
// Save our position so we can jump back for the next path.
long position = reader.BaseStream.Position;
// Jump to the unknown boolean array's offset.
reader.JumpTo(unknownBooleanTableOffset, false);
// Initialise this path's unknown boolean array.
UnknownBooleanArray_1 = new bool[knotCount];
// Read each value in this path's unknown boolean array.
for (int booleanIndex = 0; booleanIndex < knotCount; booleanIndex++)
UnknownBooleanArray_1[booleanIndex] = reader.ReadBoolean();
// Jump to the distance array's offset.
reader.JumpTo(distanceOffset, false);
// Initialise this path's distance array.
Distance = new float[knotCount];
// Read each value in this path's distance array.
for (int distanceIndex = 0; distanceIndex < knotCount; distanceIndex++)
Distance[distanceIndex] = reader.ReadSingle();
// Jump to the spline knot array's offset.
reader.JumpTo(knotOffset, false);
// Initialise this path's spline knot array.
Knots = new Vector3[knotCount];
// Read each knot for this spline.
for (int knotIndex = 0; knotIndex < knotCount; knotIndex++)
Knots[knotIndex] = reader.ReadVector3();
// Jump to the up vector array's offset.
reader.JumpTo(upVectorOffset, false);
// Initialise this path's up vector array.
UpVector = new Vector3[knotCount];
// Read each value in this path's up vector array.
for (int upVectorIndex = 0; upVectorIndex < knotCount; upVectorIndex++)
UpVector[upVectorIndex] = reader.ReadVector3();
// Jump to the forward vector array's offset.
reader.JumpTo(forwardVectorOffset, false);
// Initialise this path's forward vector array.
ForwardVector = new Vector3[knotCount];
// Read each value in this path's forward vector array.
for (int forwardVectorIndex = 0; forwardVectorIndex < knotCount; forwardVectorIndex++)
ForwardVector[forwardVectorIndex] = reader.ReadVector3();
// Only handle the double knot data if there is any, one path in Frontiers doesn't have any.
if (doubleKnotCount != 0)
{
// Jump to the double spline knot array's offset.
reader.JumpTo(doubleKnotOffset, false);
// Initialise this path's double spline knot array.
DoubleKnots = new Vector3[doubleKnotCount];
// Read each knot for this double spline.
for (ulong doubleKnotIndex = 0; doubleKnotIndex < doubleKnotCount; doubleKnotIndex++)
DoubleKnots[doubleKnotIndex] = reader.ReadVector3();
}
// Jump to this path's tag data offset.
reader.JumpTo(tagOffset, false);
// Loop through each tag in this path.
for (ulong tagIndex = 0; tagIndex < tagCount; tagIndex++)
{
// Set up a string for the tag type.
string tagType;
// Read this tag's type.
if (version == FormatVersion.sonic_2013)
tagType = StringHelpers.ReadNullTerminatedStringTableEntry(reader, 0x04);
else
tagType = StringHelpers.ReadNullTerminatedStringTableEntry(reader, 0x08);
// Skip an unused value that is either 2 (if the tag is "next") or 0 (in every other tag).
if (version == FormatVersion.sonic_2013)
reader.JumpAhead(0x04);
else
reader.JumpAhead(0x08);
// Read the next value depending on the tag type.
switch (tagType)
{
case "type":
// Read this path's type.
switch (version)
{
case FormatVersion.sonic_2013: Type = (SplineType2013Wars)reader.ReadUInt32(); break;
case FormatVersion.Wars: Type = (SplineType2013Wars)reader.ReadUInt64(); break;
case FormatVersion.Rangers: Type = (SplineTypeRangersMiller)reader.ReadUInt64(); break;
}
break;
case "uid":
// Read this path's UID value.
if (version == FormatVersion.sonic_2013)
UID = reader.ReadUInt32();
else
UID = reader.ReadUInt64();
break;
case "next":
// Read this path's next name.
if (version == FormatVersion.sonic_2013)
NextPathName = StringHelpers.ReadNullTerminatedStringTableEntry(reader, 0x04);
else
NextPathName = StringHelpers.ReadNullTerminatedStringTableEntry(reader, 0x08);
break;
case "grind_speed":
// Read this path's grind speed value.
if (version == FormatVersion.sonic_2013)
GrindSpeed = (GrindSpeed)reader.ReadUInt32();
else
GrindSpeed = (GrindSpeed)reader.ReadUInt64();
break;
default: throw new NotImplementedException($"Path tag with type '{tagType}' not yet handled!");
}
}
// Jump to the offset for this path's k-d tree.
reader.JumpTo(kdTreeOffset, false);
// Read this path's k-d tree.
KDTree = new(reader, version);
// Jump back to read the next path.
reader.JumpTo(position);
}
/// <summary>
/// Writes the table for this path.
/// </summary>
public void WriteTable(BINAWriter writer, FormatVersion version, int index)
{
// Add a string for this path's name.
if (version == FormatVersion.sonic_2013)
writer.AddString($"Path{index}Name", Name, 0x04);
else
writer.AddString($"Path{index}Name", Name, 0x08);
// Write this path's unknown short value.
writer.Write(UnknownUShort_1);
// Write this path's knot count.
writer.Write((ushort)Knots.Length);
// Write this path's distance value.
writer.Write(Distance[^1]);
// Add an offset to this path's table of unknown, always true, booleans.
if (version == FormatVersion.sonic_2013)
writer.AddOffset($"Path{index}UnknownBooleanTableOffset", 0x04);
else
writer.AddOffset($"Path{index}UnknownBooleanTableOffset", 0x08);
// Add an offset to this path's distance array.
if (version == FormatVersion.sonic_2013)
writer.AddOffset($"Path{index}DistanceOffset", 0x04);
else
writer.AddOffset($"Path{index}DistanceOffset", 0x08);
// Add an offset to this path's spline knot array.
if (version == FormatVersion.sonic_2013)
writer.AddOffset($"Path{index}KnotOffset", 0x04);
else
writer.AddOffset($"Path{index}KnotOffset", 0x08);
// Add an offset to this path's up vector array.
if (version == FormatVersion.sonic_2013)
writer.AddOffset($"Path{index}UpVectorOffset", 0x04);
else
writer.AddOffset($"Path{index}UpVectorOffset", 0x08);
// Add an offset to this path's forward vector array.
if (version == FormatVersion.sonic_2013)
writer.AddOffset($"Path{index}ForwardVectorOffset", 0x04);
else
writer.AddOffset($"Path{index}ForwardVectorOffset", 0x08);
// Handle whether or not this spline uses double knots.
if (DoubleKnots != null)
{
// Write this spline's double knot count.
if (version == FormatVersion.sonic_2013)
writer.Write((uint)DoubleKnots.Length);
else
writer.Write((ulong)DoubleKnots.Length);
// Add an offset to this path's double spline knot table.
if (version == FormatVersion.sonic_2013)
writer.AddOffset($"Path{index}DoubleKnotOffset", 0x04);
else
writer.AddOffset($"Path{index}DoubleKnotOffset", 0x08);
}
else
{
// Write two zero values in place of the double knot data.
if (version == FormatVersion.sonic_2013)
{
writer.Write(0);
writer.Write(0);
}
else
{
writer.Write(0L);
writer.Write(0L);
}
}
// Write this path's axis aligned bounding box.
AxisAlignedBoundingBox.Write(writer);
// Calculate the tag count, depending on if this path has a UID, Next Path and/or Grind Speed.
ulong tagCount = 1;
if (UID != null)
tagCount++;
if (NextPathName != null)
tagCount++;
if (GrindSpeed != null)
tagCount++;
// Write the tag count.
if (version == FormatVersion.sonic_2013)
writer.Write((uint)tagCount);
else
writer.Write(tagCount);
// Add an offset to this path's tag data.
if (version == FormatVersion.sonic_2013)
writer.AddOffset($"Path{index}TagOffset", 0x04);
else
writer.AddOffset($"Path{index}TagOffset", 0x08);
// Write an unknown value that is always 0.
if (version == FormatVersion.sonic_2013)
writer.Write(0);
else
writer.Write(0L);
// Add an offset to this path's k-d tree.
if (version == FormatVersion.sonic_2013)
writer.AddOffset($"Path{index}kdTreeOffset", 0x04);
else
writer.AddOffset($"Path{index}kdTreeOffset", 0x08);
}
/// <summary>
/// Writes the data for this path.
/// </summary>
public void WriteData(BINAWriter writer, FormatVersion version, int index)
{
// Fill in the offset for this path's unknown boolean table.
writer.FillInOffset($"Path{index}UnknownBooleanTableOffset", false);
// Write a the value for each boolean.
for (int booleanIndex = 0; booleanIndex < UnknownBooleanArray_1.Length; booleanIndex++)
writer.Write(UnknownBooleanArray_1[booleanIndex]);
// Realign to 0x04 bytes.
writer.FixPadding(0x04);
// Fill in the offset for this path's distance array.
writer.FillInOffset($"Path{index}DistanceOffset", false);
// Loop through and write each value in this path's distance array.
for (int distanceIndex = 0; distanceIndex < Distance.Length; distanceIndex++)
writer.Write(Distance[distanceIndex]);
// Fill in the offset for this path's spline knot table.
writer.FillInOffset($"Path{index}KnotOffset", false);
// Loop through and write each spline knot's position.
for (int knotIndex = 0; knotIndex < Knots.Length; knotIndex++)
writer.Write(Knots[knotIndex]);
// Fill in the offset for this path's up vector array.
writer.FillInOffset($"Path{index}UpVectorOffset", false);
// Loop through and write each value in this path's up vector array.
for (int upVectorIndex = 0; upVectorIndex < UpVector.Length; upVectorIndex++)
writer.Write(UpVector[upVectorIndex]);
// Fill in the offset for this path's forward vector array.
writer.FillInOffset($"Path{index}ForwardVectorOffset", false);
// Loop through and write each value in this path's forward vector array.
for (int forwardVectorIndex = 0; forwardVectorIndex < ForwardVector.Length; forwardVectorIndex++)
writer.Write(ForwardVector[forwardVectorIndex]);
// If this path has a double knot spline, then handle it.
if (DoubleKnots != null)
{
// Fill in the offset for this path's double knot spline.
writer.FillInOffset($"Path{index}DoubleKnotOffset", false);
// Loop through and write the position for each double spline knot.
for (int doubleKnotIndex = 0; doubleKnotIndex < DoubleKnots.Length; doubleKnotIndex++)
writer.Write(DoubleKnots[doubleKnotIndex]);
}
// Realign to 0x04 or 0x08 (depending on the format version).
if (version == FormatVersion.sonic_2013)
writer.FixPadding(0x04);
else
writer.FixPadding(0x08);
// Fill in the offset for this path's type data.
writer.FillInOffset($"Path{index}TagOffset", false);
// Write the tags in different orders depending on the format version.
// TODO: Can Forces and Frontiers use grind_speed and next? If not, then throw them out.
if (version == FormatVersion.sonic_2013)
{
WritePathTag(writer, version, index, "uid", UID, NextPathName);
WritePathTag(writer, version, index, "grind_speed", (ulong?)GrindSpeed, NextPathName);
WritePathTag(writer, version, index, "type", (ulong)Type, NextPathName);
WritePathTag(writer, version, index, "next", null, NextPathName);
}
else
{
WritePathTag(writer, version, index, "grind_speed", (ulong?)GrindSpeed, NextPathName);
WritePathTag(writer, version, index, "type", (ulong)Type, NextPathName);
WritePathTag(writer, version, index, "uid", UID, NextPathName);
WritePathTag(writer, version, index, "next", null, NextPathName);
}
// Fill in the offset for this path's k-d tree.
writer.FillInOffset($"Path{index}kdTreeOffset", false);
KDTree.Write(writer, version, index);
}
}
public class KDTree
{
/// <summary>
/// An unknown integer value.
/// TODO: What is this?
/// </summary>
public uint UnknownUInt32_1 { get; set; }
/// <summary>
/// An unknown integer value.
/// TODO: What is this?
/// </summary>
public uint UnknownUInt32_2 { get; set; }
/// <summary>
/// An unknown chunk of data.
/// TODO: What is this?
/// </summary>
public byte[] UnknownData_1 { get; set; } = [];
/// <summary>
/// An unknown set of values, consisting of two integer values each.
/// TODO: What is this?
/// </summary>
public List<uint[]> UnknownData_2 { get; set; } = [];
/// <summary>
/// An unknown list of integer values.
/// TODO: What is this?
/// </summary>
public uint[] UnknownData_3 = [];
/// <summary>
/// Initialises this k-d tree with default data.
/// </summary>
public KDTree() { }
/// <summary>
/// Initialises this k-d tree with the provided data.
/// </summary>
public KDTree(uint unknownUInt32_1, uint unknownUInt32_2, byte[] unknownData_1, List<uint[]> unknownData_2, uint[] unknownData_3)
{
UnknownUInt32_1 = unknownUInt32_1;
UnknownUInt32_2 = unknownUInt32_2;
UnknownData_1 = unknownData_1;
UnknownData_2 = unknownData_2;
UnknownData_3 = unknownData_3;
}
/// <summary>
/// Initialises this k-d tree by reading its data from a BINAReader.
/// </summary>
public KDTree(BINAReader reader, FormatVersion version) => Read(reader, version);
/// <summary>
/// Reads the data for this k-d tree.
/// </summary>
public void Read(BINAReader reader, FormatVersion version)
{
UnknownUInt32_1 = reader.ReadUInt32();
UnknownUInt32_2 = reader.ReadUInt32();
long UnknownData_1_Offset = reader.ReadInt64();
if (version == FormatVersion.sonic_2013)
{
reader.JumpBehind(0x08);
UnknownData_1_Offset = reader.ReadUInt32();
}
ulong UnknownData_2_Count = reader.ReadUInt64();
if (version == FormatVersion.sonic_2013)
{
reader.JumpBehind(0x08);
UnknownData_2_Count = reader.ReadUInt32();
}
long UnknownData_2_Offset = reader.ReadInt64();
if (version == FormatVersion.sonic_2013)
{
reader.JumpBehind(0x08);
UnknownData_2_Offset = reader.ReadUInt32();
}
ulong UnknownData_3_Count = reader.ReadUInt64();
if (version == FormatVersion.sonic_2013)
{
reader.JumpBehind(0x08);
UnknownData_3_Count = reader.ReadUInt32();
}
long UnknownData_3_Offset = reader.ReadInt64();
if (version == FormatVersion.sonic_2013)
{
reader.JumpBehind(0x08);
UnknownData_3_Offset = reader.ReadUInt32();
}
reader.JumpTo(UnknownData_1_Offset, false);
UnknownData_1 = reader.ReadBytes((int)(UnknownData_2_Offset - UnknownData_1_Offset));
reader.JumpTo(UnknownData_2_Offset, false);
for (ulong unknownIndex = 0; unknownIndex < UnknownData_2_Count; unknownIndex++)
{
uint[] unknownValues = [reader.ReadUInt32(), reader.ReadUInt32()];
UnknownData_2.Add(unknownValues);
}
UnknownData_3 = new uint[(int)UnknownData_3_Count];
reader.JumpTo(UnknownData_3_Offset, false);
for (ulong unknownIndex = 0; unknownIndex < UnknownData_3_Count; unknownIndex++)
UnknownData_3[unknownIndex] = reader.ReadUInt32();
}
/// <summary>
/// Writes the data for this k-d tree.
/// </summary>
public void Write(BINAWriter writer, FormatVersion version, int index)
{
writer.Write(UnknownUInt32_1);
writer.Write(UnknownUInt32_2);
if (version == FormatVersion.sonic_2013)
{
writer.AddOffset($"Path{index}kdTreeUnknownData1_Offset", 0x04);
writer.Write((uint)UnknownData_2.Count);
writer.AddOffset($"Path{index}kdTreeUnknownData2_Offset", 0x04);
writer.Write((uint)UnknownData_3.Length);
writer.AddOffset($"Path{index}kdTreeUnknownData3_Offset", 0x04);
}
else
{
writer.AddOffset($"Path{index}kdTreeUnknownData1_Offset", 0x08);
writer.Write((ulong)UnknownData_2.Count);
writer.AddOffset($"Path{index}kdTreeUnknownData2_Offset", 0x08);
writer.Write((ulong)UnknownData_3.Length);
writer.AddOffset($"Path{index}kdTreeUnknownData3_Offset", 0x08);
}
writer.FillInOffset($"Path{index}kdTreeUnknownData1_Offset", false);
writer.Write(UnknownData_1);
writer.FillInOffset($"Path{index}kdTreeUnknownData2_Offset", false);
foreach (uint[] value in UnknownData_2)
{
writer.Write(value[0]);
writer.Write(value[1]);
}
writer.FillInOffset($"Path{index}kdTreeUnknownData3_Offset", false);
foreach (uint value in UnknownData_3)
writer.Write(value);
}
}
// Actual data presented to the end user.
public SplinePath[] Data = [];
// Internal values used for accurately resaving Sonic Lost World paths.
bool sonic2013_HasPathOffsetTable = true;
/// <summary>
/// Loads and parses this format's file.
/// </summary>
/// <param name="filepath">The path to the file to load and parse.</param>
/// <param name="version">The game version to read this file as.</param>
public void Load(string filepath, FormatVersion version = FormatVersion.Wars)
{
// Load this file into a BINAReader.
BINAReader reader = new(File.OpenRead(filepath));
// Read this file's signature.
reader.ReadSignature(0x04, "HTAP");
// Skip an unknown value that is always 0x200.
reader.CheckValue(0x200);
// Read the amount of paths in this file.
long pathCount = reader.ReadInt64();
// If this is a sonic_2013 format path, then jump back and read the path count as a 32 bit integer instead.
if (version == FormatVersion.sonic_2013)
{
reader.JumpBehind(0x08);
pathCount = reader.ReadInt32();
}
// Initialise the data array.
Data = new SplinePath[pathCount];
// If this is NOT a sonic_2013 format path, then read the path table offset and jump to it.
if (version != FormatVersion.sonic_2013)
{
// Read the offset to this file's path table.
long pathTableOffset = reader.ReadInt64();
// Jump to this file's path table.
reader.JumpTo(pathTableOffset, false);
}
// If this is a sonic_2013 format path, read an unknown value that is either 0x04, or 0x10.
// If the value is 0x10, then its an offset to the path table, if it's 0x04, then I have no idea.
else
{
if (reader.ReadUInt32() == 0x04)
sonic2013_HasPathOffsetTable = false;
}
// Loop through and read each path in this file.
for (int pathIndex = 0; pathIndex < Data.Length; pathIndex++)
Data[pathIndex] = new(reader, version);
// Close our BINAReader.
reader.Close();
}
/// <summary>
/// Saves this format's file.
/// </summary>
/// <param name="filepath">The path to save to.</param>
/// <param name="version">The game version to save this file as.</param>
/// <param name="bigEndianSave">Whether this format should be saved in big endian for the Wii U version (sonic_2013 only).</param>
public void Save(string filepath, FormatVersion version = FormatVersion.Wars, bool bigEndianSave = false)
{
// Set up a BINA Version 2 Header.
BINAv2Header header = new(210);
// If this is a Sonic Lost World path, then change the BINA Header to v200.
if (version == FormatVersion.sonic_2013)
header = new BINAv2Header(200, bigEndianSave);
// Set up our BINAWriter and write the BINAV2 header.
BINAWriter writer = new(File.Create(filepath), header);
// If this is a Wii U Sonic Lost World path, then flip the endianness.
if (version == FormatVersion.sonic_2013 && bigEndianSave)
writer.IsBigEndian = true;
// Write this file's signature.
if (!writer.IsBigEndian)
writer.Write("HTAP");
else
writer.Write("PATH");
// Write an unknown value that is always 0x200.
writer.Write(0x200);
// Write the count of splines in this file.
if (version == FormatVersion.sonic_2013)
writer.Write(Data.Length);
else
writer.Write((ulong)Data.Length);
if (version == FormatVersion.sonic_2013)
{
// Determine if we need to write a 0x04, or an offset to the path table.
if (sonic2013_HasPathOffsetTable)
{
// Add an offset to the path table.
writer.AddOffset("PathTableOffset", 0x04);
// Fill in the path table offset.
writer.FillInOffset("PathTableOffset", false);
}
else
{
writer.Write(0x04);
}
}
else
{
// Add an offset to the path table.
writer.AddOffset("PathTableOffset", 0x08);
// Fill in the path table offset.
writer.FillInOffset("PathTableOffset", false);
}
// Loop through and write each path's table.
for (int pathIndex = 0; pathIndex < Data.Length; pathIndex++)
Data[pathIndex].WriteTable(writer, version, pathIndex);
// Loop through and write each path's data.
for (int pathIndex = 0; pathIndex < Data.Length; pathIndex++)
Data[pathIndex].WriteData(writer, version, pathIndex);
// Close our BINAWriter.
writer.Close(header);
}
/// <summary>
/// Write a tag entry for this path.
/// </summary>
/// <param name="writer">The BINAWriter to use.</param>
/// <param name="version">The format version, used to determine offset and data lengths.</param>
/// <param name="pathIndex">The index of the path this tag belongs to.</param>
/// <param name="tagType">The type of tag.</param>
/// <param name="value">The data (if any) for this tag.</param>
private static void WritePathTag(BINAWriter writer, FormatVersion version, int pathIndex, string tagType, ulong? value, string? nextPathName)
{
// If this tag doesn't have a value and isn't a "next" tag with a path name, then don't write anything.
if (value == null)
if (tagType != "next")
return;
else if (nextPathName == null)
return;
// Add the this tag type's string to the string table.
if (version == FormatVersion.sonic_2013)
writer.AddString($"Path{pathIndex}{tagType}", tagType, 0x04);
else
writer.AddString($"Path{pathIndex}{tagType}", tagType, 0x08);
// Write an unknown value that is always 0 on every tag except for "next", which uses 2.
if (version == FormatVersion.sonic_2013)
if (tagType != "next")
writer.Write(0);
else
writer.Write([0x02, 0x00, 0x00, 0x00]); // TODO: This is probably a byte then, considering it's still this sequence in the Wii U's big endian files.
else
if (tagType != "next")
writer.Write(0L);
else
writer.Write(2L);
// Write this path's data value, unless this is a "next" tag, then write a string entry instead.
if (version == FormatVersion.sonic_2013)
if (tagType != "next")
writer.Write((uint)value);
else
writer.AddString($"Path{pathIndex}NextPath", nextPathName, 0x04);
else
if (tagType != "next")
writer.Write((ulong)value);
else