-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrossFireDatDecoder.cs
More file actions
751 lines (645 loc) · 26 KB
/
CrossFireDatDecoder.cs
File metadata and controls
751 lines (645 loc) · 26 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
using System.Buffers.Binary;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
namespace CFRezManager;
internal sealed record CrossFireDatDocument(
int Version,
string ObjectKind,
int ObjectCount,
IReadOnlyList<CrossFireDatZone> Zones,
IReadOnlyList<CrossFireDatEnvSound> EnvSounds,
CrossFireDatMovePath? MovePath,
CrossFireDatCamera? Camera,
string Text,
string StorageDescription,
int SourceByteCount,
int DecodedByteCount);
internal sealed record CrossFireDatZone(
string Name,
CrossFireDatVector3? Position,
CrossFireDatVector3? Rotation,
int? Floor,
string SoundType,
IReadOnlyList<string> EnvFModEvents,
IReadOnlyList<CrossFireDatVector3> Points);
internal sealed record CrossFireDatEnvSound(
string Name,
string ZoneName,
CrossFireDatVector3? Position,
IReadOnlyList<string> SoundNames,
double? Decay,
double? RadiusIn,
double? RadiusOut,
string SoundType);
internal sealed record CrossFireDatMovePath(
double? FirstFrame,
double? LastFrame,
double? FrameSpeed,
int? AnimationFrameCount,
IReadOnlyList<CrossFireDatPathSample> Samples);
internal sealed record CrossFireDatCamera(
double? FirstFrame,
double? LastFrame,
double? FrameSpeed,
IReadOnlyList<CrossFireDatCameraTrack> Tracks);
internal sealed record CrossFireDatCameraTrack(
string Name,
CrossFireDatVector3? LocalPosition,
int? AnimationFrameCount,
IReadOnlyList<CrossFireDatPathSample> Samples);
internal sealed record CrossFireDatPathSample(double Frame, CrossFireDatVector3 Position);
internal readonly record struct CrossFireDatVector3(double X, double Y, double Z);
internal static class CrossFireDatDecoder
{
private const int HeaderLength = sizeof(int);
private const int MaxVersion = 1024;
private const string BaseStorageDescription = "CrossFire DAT";
private const string NumberPattern = @"-?\d+(?:\.\d+)?";
private static readonly Regex ZoneStartRegex = new(@"\(\s*Zoneman(?!Name)", RegexOptions.CultureInvariant);
private static readonly Regex EnvSoundStartRegex = new(@"\(\s*EnvSound(?!Name)", RegexOptions.CultureInvariant);
private static readonly Regex CameraTrackStartRegex = new(@"\*CAMERA_(FOV|LOOK|EYE)_ANIMATION", RegexOptions.CultureInvariant);
private static readonly Regex PathSampleRegex = new(
$@"(?<![\d.-])({NumberPattern})\s+({NumberPattern})\s+({NumberPattern})\s+({NumberPattern})",
RegexOptions.CultureInvariant);
public static bool IsCandidate(string extension)
{
return string.Equals(extension, "dat", StringComparison.OrdinalIgnoreCase);
}
public static bool TryDecode(
byte[] data,
string fallbackName,
out CrossFireDatDocument? document,
out string? errorMessage)
{
document = null;
errorMessage = null;
byte[]? prepared = LzmaAloneDecoder.TryPrepareData(data);
if (prepared is null)
{
errorMessage = "DAT compression could not be decoded.";
return false;
}
if (prepared.Length <= HeaderLength)
{
errorMessage = "DAT file is too small.";
return false;
}
byte[] versionBytes = prepared.AsSpan(0, HeaderLength).ToArray();
RezCrypto.Decode(versionBytes, 0);
int version = BinaryPrimitives.ReadInt32LittleEndian(versionBytes);
if (version <= 0 || version > MaxVersion)
{
errorMessage = "DAT header is not a recognized CrossFire object file.";
return false;
}
byte[] decodedBody = prepared.AsSpan(HeaderLength).ToArray();
// CrossFire stores this DAT as two independently encoded chunks: version header, then body.
RezCrypto.Decode(decodedBody, 0);
string rawText = Encoding.Latin1.GetString(decodedBody);
List<CrossFireDatZone> zones = ParseZones(rawText);
if (zones.Count > 0)
{
string storageDescription = FormatStorageDescription(prepared, data, "Zoneman");
string text = FormatZoneText(fallbackName, version, data.Length, prepared.Length, zones);
document = new CrossFireDatDocument(
version,
"Zoneman",
zones.Count,
zones,
[],
null,
null,
text,
storageDescription,
data.Length,
prepared.Length);
return true;
}
List<CrossFireDatEnvSound> envSounds = ParseEnvSounds(rawText);
if (envSounds.Count > 0)
{
string storageDescription = FormatStorageDescription(prepared, data, "EnvSound");
string text = FormatEnvSoundText(fallbackName, version, data.Length, prepared.Length, envSounds);
document = new CrossFireDatDocument(
version,
"EnvSound",
envSounds.Count,
[],
envSounds,
null,
null,
text,
storageDescription,
data.Length,
prepared.Length);
return true;
}
CrossFireDatMovePath? movePath = ParseMovePath(rawText);
if (movePath is not null)
{
string storageDescription = FormatStorageDescription(prepared, data, "MovePath");
string text = FormatMovePathText(fallbackName, version, data.Length, prepared.Length, movePath);
document = new CrossFireDatDocument(
version,
"MovePath",
1,
[],
[],
movePath,
null,
text,
storageDescription,
data.Length,
prepared.Length);
return true;
}
CrossFireDatCamera? camera = ParseCamera(rawText);
if (camera is not null)
{
string storageDescription = FormatStorageDescription(prepared, data, "CameraAnimation");
string text = FormatCameraText(fallbackName, version, data.Length, prepared.Length, camera);
document = new CrossFireDatDocument(
version,
"CameraAnimation",
camera.Tracks.Count,
[],
[],
null,
camera,
text,
storageDescription,
data.Length,
prepared.Length);
return true;
}
errorMessage = "DAT body did not contain recognizable CrossFire objects.";
return false;
}
private static List<CrossFireDatZone> ParseZones(string rawText)
{
MatchCollection matches = ZoneStartRegex.Matches(rawText);
var zones = new List<CrossFireDatZone>(matches.Count);
for (int i = 0; i < matches.Count; i++)
{
int start = matches[i].Index;
int end = i + 1 < matches.Count ? matches[i + 1].Index : rawText.Length;
if (end <= start)
{
continue;
}
string chunk = rawText[start..end];
string name = ReadStringProperty(chunk, "ZonemanName") ?? "Zoneman";
CrossFireDatVector3? position = ReadVectorProperty(chunk, "Pos");
CrossFireDatVector3? rotation = ReadVectorProperty(chunk, "Rot");
int? floor = ReadOptionalIntProperty(chunk, "Floor");
string soundType = ReadStringProperty(chunk, "SoundType") ?? string.Empty;
IReadOnlyList<string> envFModEvents = new[]
{
ReadStringProperty(chunk, "EnvFModEvent1"),
ReadStringProperty(chunk, "EnvFModEvent2"),
ReadStringProperty(chunk, "EnvFModEvent3")
}
.Where(value => value is not null)
.Select(value => value!)
.ToArray();
IReadOnlyList<CrossFireDatVector3> points = ReadVectorProperties(chunk, "Point");
if (position is null && points.Count == 0 && string.Equals(name, "Zoneman", StringComparison.Ordinal))
{
continue;
}
zones.Add(new CrossFireDatZone(name, position, rotation, floor, soundType, envFModEvents, points));
}
return zones;
}
private static List<CrossFireDatEnvSound> ParseEnvSounds(string rawText)
{
MatchCollection matches = EnvSoundStartRegex.Matches(rawText);
var envSounds = new List<CrossFireDatEnvSound>(matches.Count);
for (int i = 0; i < matches.Count; i++)
{
int start = matches[i].Index;
int end = i + 1 < matches.Count ? matches[i + 1].Index : rawText.Length;
if (end <= start)
{
continue;
}
string chunk = rawText[start..end];
string name = ReadStringProperty(chunk, "EnvSoundName") ?? "EnvSound";
string zoneName = ReadStringProperty(chunk, "ZoneName") ?? string.Empty;
CrossFireDatVector3? position = ReadVectorProperty(chunk, "Pos");
string[] soundNames =
[
ReadLooseTextProperty(chunk, "SoundName1") ?? string.Empty,
ReadLooseTextProperty(chunk, "SoundName2") ?? string.Empty,
ReadLooseTextProperty(chunk, "SoundName3") ?? string.Empty
];
double? decay = ReadOptionalDoubleProperty(chunk, "Decay");
double? radiusIn = ReadOptionalDoubleProperty(chunk, "Radius_In");
double? radiusOut = ReadOptionalDoubleProperty(chunk, "Radius_Out");
string soundType = ReadStringProperty(chunk, "soundType") ?? ReadStringProperty(chunk, "SoundType") ?? string.Empty;
if (position is null && soundNames.All(string.IsNullOrEmpty) && string.Equals(name, "EnvSound", StringComparison.Ordinal))
{
continue;
}
envSounds.Add(new CrossFireDatEnvSound(name, zoneName, position, soundNames, decay, radiusIn, radiusOut, soundType));
}
return envSounds;
}
private static CrossFireDatMovePath? ParseMovePath(string rawText)
{
if (!rawText.Contains("*MOVE_PATH", StringComparison.Ordinal))
{
return null;
}
double? firstFrame = ReadStarDouble(rawText, "SCENE_FIRSTFRAME");
double? lastFrame = ReadStarDouble(rawText, "SCENE_LASTFRAME");
double? frameSpeed = ReadStarDouble(rawText, "SCENE_FRAMESPEED");
int? animationFrameCount = ReadStarInt(rawText, "ANIMATION_FRAME_COUNT");
IReadOnlyList<CrossFireDatPathSample> samples = ParsePathSamples(rawText);
return firstFrame is null &&
lastFrame is null &&
frameSpeed is null &&
animationFrameCount is null &&
samples.Count == 0
? null
: new CrossFireDatMovePath(firstFrame, lastFrame, frameSpeed, animationFrameCount, samples);
}
private static CrossFireDatCamera? ParseCamera(string rawText)
{
if (!rawText.Contains("*CAMERA_", StringComparison.Ordinal))
{
return null;
}
MatchCollection matches = CameraTrackStartRegex.Matches(rawText);
if (matches.Count == 0)
{
return null;
}
double? firstFrame = ReadStarDouble(rawText, "SCENE_FIRSTFRAME");
double? lastFrame = ReadStarDouble(rawText, "SCENE_LASTFRAME");
double? frameSpeed = ReadStarDouble(rawText, "SCENE_FRAMESPEED");
var tracks = new List<CrossFireDatCameraTrack>(matches.Count);
for (int i = 0; i < matches.Count; i++)
{
Match match = matches[i];
int start = match.Index;
int end = i + 1 < matches.Count ? matches[i + 1].Index : rawText.Length;
if (end <= start)
{
continue;
}
string chunk = rawText[start..end];
string name = match.Groups[1].Value;
CrossFireDatVector3? localPosition = ReadStarVector(chunk, "LOCAL_POS");
int? animationFrameCount = ReadStarInt(chunk, "ANIMATION_FRAME_COUNT");
int sampleStart = chunk.IndexOf("*CONTROL_POS_SAMPLE", StringComparison.Ordinal);
IReadOnlyList<CrossFireDatPathSample> samples = sampleStart >= 0
? ParsePathSamples(chunk[sampleStart..])
: [];
tracks.Add(new CrossFireDatCameraTrack(name, localPosition, animationFrameCount, samples));
}
return tracks.Count == 0 ? null : new CrossFireDatCamera(firstFrame, lastFrame, frameSpeed, tracks);
}
private static IReadOnlyList<CrossFireDatPathSample> ParsePathSamples(string text)
{
var samples = new List<CrossFireDatPathSample>();
foreach (Match match in PathSampleRegex.Matches(text))
{
double frame = ReadDouble(match.Groups[1].Value);
var position = new CrossFireDatVector3(
ReadDouble(match.Groups[2].Value),
ReadDouble(match.Groups[3].Value),
ReadDouble(match.Groups[4].Value));
samples.Add(new CrossFireDatPathSample(frame, position));
}
return samples;
}
private static string? ReadStringProperty(string chunk, string propertyName)
{
Match match = Regex.Match(
chunk,
$@"\(\s*{Regex.Escape(propertyName)}\s+""([^""]*)""\s*\)",
RegexOptions.CultureInvariant);
return match.Success ? match.Groups[1].Value : null;
}
private static string? ReadLooseTextProperty(string chunk, string propertyName)
{
string? quoted = ReadStringProperty(chunk, propertyName);
if (quoted is not null)
{
return quoted;
}
Match match = Regex.Match(
chunk,
$@"\(\s*{Regex.Escape(propertyName)}\s+([^)]*?)\s*\)",
RegexOptions.CultureInvariant);
return match.Success ? CleanLooseText(match.Groups[1].Value) : null;
}
private static int? ReadOptionalIntProperty(string chunk, string propertyName)
{
Match match = Regex.Match(
chunk,
$@"\(\s*{Regex.Escape(propertyName)}\s+(-?\d*)\s*\)",
RegexOptions.CultureInvariant);
if (!match.Success || string.IsNullOrWhiteSpace(match.Groups[1].Value))
{
return null;
}
return int.TryParse(match.Groups[1].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out int value)
? value
: null;
}
private static double? ReadOptionalDoubleProperty(string chunk, string propertyName)
{
Match match = Regex.Match(
chunk,
$@"\(\s*{Regex.Escape(propertyName)}\s+(-?\d+(?:\.\d+)?)\s*\)",
RegexOptions.CultureInvariant);
if (!match.Success)
{
return null;
}
return double.TryParse(match.Groups[1].Value, NumberStyles.Float, CultureInfo.InvariantCulture, out double value)
? value
: null;
}
private static double? ReadStarDouble(string text, string propertyName)
{
Match match = Regex.Match(
text,
$@"\*{Regex.Escape(propertyName)}\s+(-?\d+(?:\.\d+)?)",
RegexOptions.CultureInvariant);
return match.Success ? ReadDouble(match.Groups[1].Value) : null;
}
private static int? ReadStarInt(string text, string propertyName)
{
Match match = Regex.Match(
text,
$@"\*{Regex.Escape(propertyName)}\s+(-?\d+)",
RegexOptions.CultureInvariant);
if (!match.Success)
{
return null;
}
return int.TryParse(match.Groups[1].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out int value)
? value
: null;
}
private static CrossFireDatVector3? ReadStarVector(string text, string propertyName)
{
Match match = Regex.Match(
text,
$@"\*{Regex.Escape(propertyName)}\s+({NumberPattern})\s+({NumberPattern})\s+({NumberPattern})",
RegexOptions.CultureInvariant);
return match.Success ? ReadVector(match) : null;
}
private static CrossFireDatVector3? ReadVectorProperty(string chunk, string propertyName)
{
Match match = CreateVectorRegex(propertyName).Match(chunk);
return match.Success ? ReadVector(match) : null;
}
private static IReadOnlyList<CrossFireDatVector3> ReadVectorProperties(string chunk, string propertyName)
{
MatchCollection matches = CreateVectorRegex(propertyName).Matches(chunk);
var vectors = new List<CrossFireDatVector3>(matches.Count);
foreach (Match match in matches)
{
vectors.Add(ReadVector(match));
}
return vectors;
}
private static Regex CreateVectorRegex(string propertyName)
{
const string number = @"(-?\d+(?:\.\d+)?)";
return new Regex(
$@"\(\s*{Regex.Escape(propertyName)}\s+{number}\s+{number}\s+{number}\s*\)",
RegexOptions.CultureInvariant);
}
private static CrossFireDatVector3 ReadVector(Match match)
{
return new CrossFireDatVector3(
ReadDouble(match.Groups[1].Value),
ReadDouble(match.Groups[2].Value),
ReadDouble(match.Groups[3].Value));
}
private static double ReadDouble(string value)
{
return double.Parse(value, CultureInfo.InvariantCulture);
}
private static string FormatStorageDescription(byte[] prepared, byte[] source, string objectKind)
{
return ReferenceEquals(prepared, source)
? $"{BaseStorageDescription} / {objectKind}"
: $"LZMA-compressed {BaseStorageDescription} / {objectKind}";
}
private static string FormatZoneText(
string fallbackName,
int version,
int sourceByteCount,
int decodedByteCount,
IReadOnlyList<CrossFireDatZone> zones)
{
var builder = new StringBuilder();
builder.AppendLine($"; decoded from {fallbackName}");
builder.AppendLine($"; version {version}, zones {zones.Count}, bytes {sourceByteCount} -> {decodedByteCount}");
builder.AppendLine();
foreach (CrossFireDatZone zone in zones)
{
builder.AppendLine("( Zoneman");
builder.AppendLine($"\t( ZonemanName \"{Escape(zone.Name)}\" )");
if (zone.Position is { } position)
{
builder.AppendLine($"\t( Pos {FormatVector(position)} )");
}
if (zone.Rotation is { } rotation)
{
builder.AppendLine($"\t( Rot {FormatVector(rotation)} )");
}
builder.AppendLine($"\t( Floor {zone.Floor?.ToString(CultureInfo.InvariantCulture) ?? string.Empty} )");
if (!string.IsNullOrEmpty(zone.SoundType))
{
builder.AppendLine($"\t( SoundType \"{Escape(zone.SoundType)}\" )");
}
for (int i = 0; i < zone.EnvFModEvents.Count; i++)
{
builder.AppendLine($"\t( EnvFModEvent{i + 1} \"{Escape(zone.EnvFModEvents[i])}\" )");
}
foreach (CrossFireDatVector3 point in zone.Points)
{
builder.AppendLine($"\t( Point {FormatVector(point)} )");
}
builder.AppendLine(")");
builder.AppendLine();
}
return builder.ToString();
}
private static string FormatEnvSoundText(
string fallbackName,
int version,
int sourceByteCount,
int decodedByteCount,
IReadOnlyList<CrossFireDatEnvSound> envSounds)
{
var builder = new StringBuilder();
builder.AppendLine($"; decoded from {fallbackName}");
builder.AppendLine($"; version {version}, env sounds {envSounds.Count}, bytes {sourceByteCount} -> {decodedByteCount}");
builder.AppendLine();
foreach (CrossFireDatEnvSound envSound in envSounds)
{
builder.AppendLine("( EnvSound");
builder.AppendLine($"\t( EnvSoundName \"{Escape(envSound.Name)}\" )");
if (!string.IsNullOrEmpty(envSound.ZoneName))
{
builder.AppendLine($"\t( ZoneName \"{Escape(envSound.ZoneName)}\" )");
}
if (envSound.Position is { } position)
{
builder.AppendLine($"\t( Pos {FormatVector(position)} )");
}
for (int i = 0; i < envSound.SoundNames.Count; i++)
{
builder.AppendLine($"\t( SoundName{i + 1} \"{Escape(envSound.SoundNames[i])}\" )");
}
if (envSound.Decay is { } decay)
{
builder.AppendLine($"\t( Decay {FormatNumber(decay)} )");
}
if (envSound.RadiusIn is { } radiusIn)
{
builder.AppendLine($"\t( Radius_In {FormatNumber(radiusIn)} )");
}
if (envSound.RadiusOut is { } radiusOut)
{
builder.AppendLine($"\t( Radius_Out {FormatNumber(radiusOut)} )");
}
if (!string.IsNullOrEmpty(envSound.SoundType))
{
builder.AppendLine($"\t( soundType \"{Escape(envSound.SoundType)}\" )");
}
builder.AppendLine(")");
builder.AppendLine();
}
return builder.ToString();
}
private static string FormatMovePathText(
string fallbackName,
int version,
int sourceByteCount,
int decodedByteCount,
CrossFireDatMovePath movePath)
{
var builder = new StringBuilder();
builder.AppendLine($"; decoded from {fallbackName}");
builder.AppendLine($"; version {version}, path samples {movePath.Samples.Count}, bytes {sourceByteCount} -> {decodedByteCount}");
builder.AppendLine();
if (movePath.FirstFrame is { } firstFrame)
{
builder.AppendLine($"*SCENE_FIRSTFRAME {FormatNumber(firstFrame)}");
}
if (movePath.LastFrame is { } lastFrame)
{
builder.AppendLine($"*SCENE_LASTFRAME {FormatNumber(lastFrame)}");
}
if (movePath.FrameSpeed is { } frameSpeed)
{
builder.AppendLine($"*SCENE_FRAMESPEED {FormatNumber(frameSpeed)}");
}
builder.AppendLine("*MOVE_PATH");
builder.AppendLine("{");
if (movePath.AnimationFrameCount is { } animationFrameCount)
{
builder.AppendLine($"\t*ANIMATION_FRAME_COUNT {animationFrameCount.ToString(CultureInfo.InvariantCulture)}");
}
builder.AppendLine("\t*CONTROL_POS_SAMPLE");
builder.AppendLine("\t{");
foreach (CrossFireDatPathSample sample in movePath.Samples)
{
builder.AppendLine($"\t\t{FormatNumber(sample.Frame)} {FormatVector(sample.Position)}");
}
builder.AppendLine("\t}");
builder.AppendLine("}");
return builder.ToString();
}
private static string FormatCameraText(
string fallbackName,
int version,
int sourceByteCount,
int decodedByteCount,
CrossFireDatCamera camera)
{
int sampleCount = camera.Tracks.Sum(track => track.Samples.Count);
var builder = new StringBuilder();
builder.AppendLine($"; decoded from {fallbackName}");
builder.AppendLine($"; version {version}, camera tracks {camera.Tracks.Count}, samples {sampleCount}, bytes {sourceByteCount} -> {decodedByteCount}");
builder.AppendLine();
if (camera.FirstFrame is { } firstFrame)
{
builder.AppendLine($"*SCENE_FIRSTFRAME {FormatNumber(firstFrame)}");
}
if (camera.LastFrame is { } lastFrame)
{
builder.AppendLine($"*SCENE_LASTFRAME {FormatNumber(lastFrame)}");
}
if (camera.FrameSpeed is { } frameSpeed)
{
builder.AppendLine($"*SCENE_FRAMESPEED {FormatNumber(frameSpeed)}");
}
if (camera.Tracks.Count > 0)
{
builder.AppendLine();
}
foreach (CrossFireDatCameraTrack track in camera.Tracks)
{
builder.AppendLine($"*CAMERA_{track.Name}_ANIMATION");
builder.AppendLine("{");
if (track.LocalPosition is { } localPosition)
{
builder.AppendLine($"\t*LOCAL_POS {FormatVector(localPosition)}");
}
if (track.AnimationFrameCount is { } animationFrameCount)
{
builder.AppendLine($"\t*ANIMATION_FRAME_COUNT {animationFrameCount.ToString(CultureInfo.InvariantCulture)}");
}
if (track.Samples.Count > 0)
{
builder.AppendLine("\t*CONTROL_POS_SAMPLE");
builder.AppendLine("\t{");
foreach (CrossFireDatPathSample sample in track.Samples)
{
builder.AppendLine($"\t\t{FormatNumber(sample.Frame)} {FormatVector(sample.Position)}");
}
builder.AppendLine("\t}");
}
builder.AppendLine("}");
builder.AppendLine();
}
return builder.ToString();
}
private static string FormatVector(CrossFireDatVector3 vector)
{
return $"{FormatNumber(vector.X)} {FormatNumber(vector.Y)} {FormatNumber(vector.Z)}";
}
private static string FormatNumber(double value)
{
return Math.Abs(value % 1) < 0.000001
? value.ToString("0", CultureInfo.InvariantCulture)
: value.ToString("0.######", CultureInfo.InvariantCulture);
}
private static string Escape(string value)
{
return value.Replace("\\", "\\\\", StringComparison.Ordinal).Replace("\"", "\\\"", StringComparison.Ordinal);
}
private static string CleanLooseText(string value)
{
var builder = new StringBuilder(value.Length);
foreach (char ch in value)
{
if (ch >= ' ' && ch <= '~')
{
builder.Append(ch);
}
}
return builder.ToString().Trim();
}
}