-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMiloFile.cs
More file actions
654 lines (565 loc) · 27.5 KB
/
MiloFile.cs
File metadata and controls
654 lines (565 loc) · 27.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
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
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using MiloLib.Assets;
using MiloLib.Utils;
using System.IO;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography.X509Certificates;
using System.Text;
namespace MiloLib
{
/// <summary>
/// Represents a Milo scene (with the extension .milo_{platform}), which is a container for assets and directories.
/// </summary>
public class MiloFile
{
/// <summary>
/// The maximum size a block can be.
/// 0x20000 is what DirLoader::SaveObjects uses, so presumably that is a safe default.
/// </summary>
private const int MAX_BLOCK_SIZE = 0x20000;
/// <summary>
/// The type of the Milo file. Determines if it's compressed or not and how it's compressed.
/// </summary>
public enum Type : uint
{
/// <summary>
/// no compression, root dir starts at startOffset
/// </summary>
Uncompressed = 0xCABEDEAF,
/// <summary>
/// zlib compressed, without uncompressed size before the start of blocks
/// </summary>
CompressedZlib = 0xCBBEDEAF,
/// <summary>
/// normal gzip
/// </summary>
CompressedGzip = 0xCCBEDEAF,
/// <summary>
/// zlib compressed, with uncompressed size before the start of blocks
/// </summary>
CompressedZlibAlt = 0xCDBEDEAF,
}
/// <summary>
/// The path to the Milo file.
/// </summary>
public string? filePath;
/// <summary>
/// The Milo's compression type.
/// </summary>
public Type compressionType;
/// <summary>
/// The offset to the start of the root asset.
/// </summary>
private uint startOffset;
/// <summary>
/// Gets the starting offset that was read from the file when it was loaded.
/// Returns 0 if the file was created new (not loaded from disk).
/// </summary>
public uint StartOffset => startOffset;
/// <summary>
/// The uncompressed size of the largest block in the Milo file.
/// </summary>
private uint largestBlock;
/// <summary>
/// The sizes of each block in the Milo file.
/// </summary>
private List<uint> blockSizes = new List<uint>();
/// <summary>
/// The root directory and it's metadata such as the entries and string table data.
/// </summary>
public DirectoryMeta dirMeta;
/// <summary>
/// The endianness of the body. Header is always little endian.
/// </summary>
public Endian endian = Endian.BigEndian;
/// <summary>
/// Loads a Milo file from a file path.
/// </summary>
public MiloFile(string path)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
filePath = path;
// buffer entire file into memory first
byte[] fileBuffer = File.ReadAllBytes(path);
using (EndianReader reader = new EndianReader(new MemoryStream(fileBuffer), Endian.LittleEndian))
{
compressionType = (Type)reader.ReadUInt32();
// detect if the type is one of the compressed types
if (compressionType != Type.Uncompressed && compressionType != Type.CompressedZlib && compressionType != Type.CompressedGzip && compressionType != Type.CompressedZlibAlt)
{
// this might be a headerless milo (e.g. Phase .milo_pc) so treat it as such and just start reading the root directory
reader.SeekTo(0);
dirMeta = new DirectoryMeta().Read(reader);
endian = reader.Endianness;
return;
}
startOffset = reader.ReadUInt32();
uint numBlocks = reader.ReadUInt32();
largestBlock = reader.ReadUInt32();
for (int i = 0; i < numBlocks; i++)
{
blockSizes.Add(reader.ReadUInt32());
}
switch (compressionType)
{
case Type.CompressedZlib:
{
reader.SeekTo(startOffset);
reader.Endianness = Endian.BigEndian;
// Pre-read all raw block data sequentially
byte[][] rawBlocks = new byte[numBlocks][];
for (int i = 0; i < numBlocks; i++)
{
rawBlocks[i] = reader.ReadBlock((int)blockSizes[i]);
}
// Decompress all blocks in parallel
byte[][] decompressedBlocks = new byte[numBlocks][];
Parallel.For(0, (int)numBlocks, i =>
{
using MemoryStream blockStream = new MemoryStream(rawBlocks[i]);
using InflaterInputStream inflater = new InflaterInputStream(blockStream, new Inflater(true));
using MemoryStream output = new MemoryStream();
inflater.CopyTo(output);
decompressedBlocks[i] = output.ToArray();
});
// Concatenate in order
MemoryStream compressedStream = new MemoryStream();
for (int i = 0; i < numBlocks; i++)
{
compressedStream.Write(decompressedBlocks[i], 0, decompressedBlocks[i].Length);
}
EndianReader decompressedReader = new EndianReader(compressedStream, Endian.BigEndian);
compressedStream.Seek(0, SeekOrigin.Begin);
DirectoryMeta meta = new DirectoryMeta();
meta.platform = DetectPlatform();
dirMeta = meta.Read(decompressedReader);
endian = decompressedReader.Endianness;
break;
}
case Type.CompressedZlibAlt:
{
reader.SeekTo(startOffset);
reader.Endianness = Endian.BigEndian;
// Pre-read all raw block data sequentially, tracking per-block metadata
byte[][] rawBlocks = new byte[numBlocks][];
bool[] blockUncompressed = new bool[numBlocks];
for (int i = 0; i < numBlocks; i++)
{
blockUncompressed[i] = (blockSizes[i] & 0xFF000000) != 0;
if (blockUncompressed[i])
{
blockSizes[i] &= 0x00FFFFFF;
rawBlocks[i] = reader.ReadBlock((int)blockSizes[i]);
}
else
{
uint uncompressedSize = reader.ReadUInt32();
rawBlocks[i] = reader.ReadBlock((int)blockSizes[i] - 4);
}
}
// Decompress all blocks in parallel
byte[][] decompressedBlocks = new byte[numBlocks][];
Parallel.For(0, (int)numBlocks, i =>
{
if (!blockUncompressed[i])
{
using MemoryStream blockStream = new MemoryStream(rawBlocks[i]);
using InflaterInputStream inflater = new InflaterInputStream(blockStream, new Inflater(true));
using MemoryStream output = new MemoryStream();
inflater.CopyTo(output);
decompressedBlocks[i] = output.ToArray();
}
else
{
decompressedBlocks[i] = rawBlocks[i];
}
});
// Concatenate in order
MemoryStream compressedStream = new MemoryStream();
for (int i = 0; i < numBlocks; i++)
{
compressedStream.Write(decompressedBlocks[i], 0, decompressedBlocks[i].Length);
}
EndianReader decompressedReader = new EndianReader(compressedStream, Endian.BigEndian);
compressedStream.Seek(0, SeekOrigin.Begin);
DirectoryMeta meta = new DirectoryMeta();
meta.platform = DetectPlatform();
dirMeta = meta.Read(decompressedReader);
endian = decompressedReader.Endianness;
break;
}
case Type.CompressedGzip:
{
reader.SeekTo(startOffset);
reader.Endianness = Endian.BigEndian;
// Pre-read all raw block data sequentially
byte[][] rawBlocks = new byte[numBlocks][];
for (int i = 0; i < numBlocks; i++)
{
rawBlocks[i] = reader.ReadBlock((int)blockSizes[i]);
}
// Decompress all blocks in parallel
byte[][] decompressedBlocks = new byte[numBlocks][];
Parallel.For(0, (int)numBlocks, i =>
{
using MemoryStream blockStream = new MemoryStream(rawBlocks[i]);
using GZipInputStream inflater = new GZipInputStream(blockStream);
using MemoryStream output = new MemoryStream();
inflater.CopyTo(output);
decompressedBlocks[i] = output.ToArray();
});
// Concatenate in order
MemoryStream compressedStream = new MemoryStream();
for (int i = 0; i < numBlocks; i++)
{
compressedStream.Write(decompressedBlocks[i], 0, decompressedBlocks[i].Length);
}
EndianReader decompressedReader = new EndianReader(compressedStream, Endian.BigEndian);
compressedStream.Seek(0, SeekOrigin.Begin);
DirectoryMeta meta = new DirectoryMeta();
meta.platform = DetectPlatform();
dirMeta = meta.Read(decompressedReader);
endian = decompressedReader.Endianness;
break;
}
case Type.Uncompressed:
{
reader.SeekTo(startOffset);
reader.Endianness = Endian.BigEndian;
DirectoryMeta meta = new DirectoryMeta();
meta.platform = DetectPlatform();
dirMeta = meta.Read(reader);
endian = reader.Endianness;
break;
}
default:
break;
}
System.Diagnostics.Debug.WriteLine("Done reading Milo file " + path);
}
}
private DirectoryMeta.Platform DetectPlatform()
{
// detect the platform from the file extension
string extension = Path.GetExtension(filePath);
if (extension == null)
{
throw new Exception("Could not detect platform from file extension");
}
// ps2 if extension ends in _ps2
if (extension.EndsWith("_ps2"))
{
return DirectoryMeta.Platform.PS2;
}
// xbox if extension ends in _xbox
if (extension.EndsWith("_xbox"))
{
return DirectoryMeta.Platform.Xbox;
}
// pc if extension ends in _pc
if (extension.EndsWith("_pc"))
{
return DirectoryMeta.Platform.PC_or_iPod;
}
// wii if extension ends in _wii
if (extension.EndsWith("_wii"))
{
return DirectoryMeta.Platform.Wii;
}
// ps3 if extension ends in _ps3
if (extension.EndsWith("_ps3"))
{
return DirectoryMeta.Platform.PS3;
}
// gamecube if extension ends in gc
if (extension.EndsWith("_gc"))
{
return DirectoryMeta.Platform.GameCube;
}
return DirectoryMeta.Platform.PS3;
}
private void WriteHandler(object sender, DirectoryMeta.Entry.EntryOperationEventArgs args, uint startingOffset, List<uint> blockSizes, ref uint bytesWritten, ref uint cumulativeBlockSize, MiloFile.Type? type = MiloFile.Type.Uncompressed)
{
if (blockSizes.Count == 0)
{
if (type == Type.CompressedZlibAlt)
{
bytesWritten = (uint)args.Writer.BaseStream.Position;
blockSizes.Add(bytesWritten);
cumulativeBlockSize += bytesWritten;
bytesWritten = 0;
return;
}
else
{
bytesWritten = (uint)args.Writer.BaseStream.Position;
}
}
else
{
bytesWritten = (uint)args.Writer.BaseStream.Position - cumulativeBlockSize;
}
if (bytesWritten > MAX_BLOCK_SIZE)
{
blockSizes.Add(bytesWritten);
cumulativeBlockSize += bytesWritten;
bytesWritten = 0;
}
}
/// <summary>
/// Constructs a new MiloFile.
/// </summary>
/// <param name="meta">The root directory to create the MiloFile with.</param>
public MiloFile(DirectoryMeta meta)
{
this.compressionType = Type.Uncompressed;
this.dirMeta = meta;
this.startOffset = 0x810;
this.blockSizes = new List<uint>();
return;
}
/// <summary>
/// Saves a Milo scene to disk.
/// </summary>
/// <param name="path">The path at which the Milo scene will be saved.</param>
/// <param name="type">The compression type to use.</param>
/// <param name="startingOffset">The offset at which the root directory starts. If null, uses the original startOffset from the loaded file, or 0x810 for new files.</param>
/// <param name="headerEndian">The endianness of the header.</param>
/// <param name="bodyEndian">The endianness of the body. Certain games require little endian bodies, such as GH2.</param>
public void Save(string? path, Type? type, uint? startingOffset = null, Endian headerEndian = Endian.LittleEndian, Endian bodyEndian = Endian.BigEndian)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
endian = bodyEndian;
if (path == null)
{
path = filePath;
}
// Use the original startOffset if not specified, or 0x810 for new files
uint actualStartingOffset = startingOffset ?? (startOffset != 0 ? startOffset : 0x810);
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
{
fs.SetLength(0);
}
using (EndianWriter writer = new EndianWriter(new BufferedStream(File.Create(path), 65536), headerEndian))
{
if (type == null)
{
// no specified type, so just use what is already there
type = compressionType;
}
writer.WriteUInt32((uint)type);
writer.WriteUInt32(actualStartingOffset);
writer.WriteUInt32(1);
// block sizes, write nothing for now
writer.WriteUInt32(0);
writer.WriteUInt32(0);
writer.WriteBlock(new byte[actualStartingOffset - (int)writer.BaseStream.Position]);
// switch to big endian, only the header is little
writer.Endianness = bodyEndian;
List<uint> uncompressedBlockSizes = new List<uint>();
// CREATE UNCOMPRESSED BLOCKS
MemoryStream uncompressedStream = new MemoryStream();
EndianWriter uncompressedWriter = new EndianWriter(uncompressedStream, bodyEndian);
uint bytesWritten = 0;
uint cumulativeBlockSize = 0;
// handler fired after any asset is saved
EventHandler<DirectoryMeta.Entry.EntryOperationEventArgs> handler = (sender, args) =>
WriteHandler(sender, args, actualStartingOffset, uncompressedBlockSizes, ref bytesWritten, ref cumulativeBlockSize, type);
// recursively traverse all entries to add our handler
foreach (var entry in dirMeta.entries)
{
AddHandlerRecursively(entry, handler);
}
// make sure we also traverse inline subdirectories
if (dirMeta.directory is ObjectDir objectDir)
{
TraverseInlineSubDirs(objectDir, handler);
}
dirMeta.Write(uncompressedWriter);
if (uncompressedBlockSizes.Count == 0)
{
// if we have no uncompressed block sizes, add the size of the entire stream as a single block
uncompressedBlockSizes.Add((uint)uncompressedStream.Length);
}
else
{
// get the last block's uncompressed size by taking the length of the uncompressed stream and subtracting all the blocks combined
uint lastBlockSize = (uint)uncompressedStream.Length - cumulativeBlockSize;
if (lastBlockSize > 0)
{
uncompressedBlockSizes.Add(lastBlockSize);
}
}
// now that we have the entire uncompressed stream, we can begin splitting it into blocks depending on compression type
uint maxBlockSize = uncompressedBlockSizes.Max();
switch (type)
{
case Type.Uncompressed:
// if we have no block sizes, write a single block size of the total size
if (uncompressedBlockSizes.Count == 0)
{
uncompressedBlockSizes.Add(bytesWritten);
}
// write the uncompressed stream to the writer
writer.WriteBlock(uncompressedStream.GetBuffer(), 0, (int)uncompressedStream.Length);
writer.SeekTo(0x8);
writer.Endianness = Endian.LittleEndian;
// Calculate the total size of the data (after writing the directory)
uint totalSize = (uint)writer.BaseStream.Length;
// Calculate the number of blocks
writer.WriteUInt32((uint)uncompressedBlockSizes.Count);
// get the size of the largest block and write it
writer.WriteUInt32(maxBlockSize);
foreach (uint blockSize in uncompressedBlockSizes)
{
writer.WriteUInt32(blockSize);
}
break;
case Type.CompressedZlib:
case Type.CompressedGzip:
{
// Pre-compute block offsets
int[] blockOffsets = new int[uncompressedBlockSizes.Count];
int runningOffset = 0;
for (int i = 0; i < uncompressedBlockSizes.Count; i++)
{
blockOffsets[i] = runningOffset;
runningOffset += (int)uncompressedBlockSizes[i];
}
// Compress all blocks in parallel
byte[] uncompressedBuffer = uncompressedStream.GetBuffer();
byte[][] compressedBlocks = new byte[uncompressedBlockSizes.Count][];
Parallel.For(0, uncompressedBlockSizes.Count, i =>
{
using MemoryStream blockStream = new MemoryStream();
if (type == Type.CompressedGzip)
{
using GZipOutputStream gzipStream = new GZipOutputStream(blockStream);
gzipStream.Write(uncompressedBuffer, blockOffsets[i], (int)uncompressedBlockSizes[i]);
gzipStream.Close();
}
else
{
using DeflaterOutputStream deflater = new DeflaterOutputStream(blockStream, new Deflater(Deflater.BEST_COMPRESSION, true));
deflater.Write(uncompressedBuffer, blockOffsets[i], (int)uncompressedBlockSizes[i]);
deflater.Close();
}
compressedBlocks[i] = blockStream.ToArray();
});
// Write compressed blocks sequentially
foreach (var block in compressedBlocks)
{
writer.WriteBlock(block);
}
writer.SeekTo(0x8);
writer.Endianness = Endian.LittleEndian;
writer.WriteUInt32((uint)compressedBlocks.Length);
uint maxUncompressedBlockSize = maxBlockSize;
writer.WriteUInt32(maxUncompressedBlockSize);
foreach (var block in compressedBlocks)
{
writer.WriteUInt32((uint)block.Length);
}
break;
}
case Type.CompressedZlibAlt:
{
// Pre-compute block offsets
int[] blockOffsets = new int[uncompressedBlockSizes.Count];
int runningOffset = 0;
for (int i = 0; i < uncompressedBlockSizes.Count; i++)
{
blockOffsets[i] = runningOffset;
runningOffset += (int)uncompressedBlockSizes[i];
}
// Compress all blocks in parallel (block 0 is written uncompressed but still compress for size header)
byte[] uncompressedBuffer = uncompressedStream.GetBuffer();
byte[][] compressedBlocks = new byte[uncompressedBlockSizes.Count][];
Parallel.For(0, uncompressedBlockSizes.Count, i =>
{
using MemoryStream blockStream = new MemoryStream();
using DeflaterOutputStream deflater = new DeflaterOutputStream(blockStream, new Deflater(Deflater.BEST_COMPRESSION, true));
deflater.Write(uncompressedBuffer, blockOffsets[i], (int)uncompressedBlockSizes[i]);
deflater.Close();
compressedBlocks[i] = blockStream.ToArray();
});
// Write compressed blocks sequentially
for (int i = 0; i < compressedBlocks.Length; i++)
{
byte[] block = compressedBlocks[i];
if (i == 0)
{
// first block always uncompressed
writer.WriteBlock(uncompressedStream.GetBuffer(), 0, (int)uncompressedBlockSizes[0]);
}
else
{
writer.WriteUInt32((uint)block.Length + 4);
writer.WriteBlock(block);
}
}
writer.SeekTo(0x8);
writer.Endianness = Endian.LittleEndian;
writer.WriteUInt32((uint)compressedBlocks.Length);
uint maxUncompressedBlockSize = (uint)uncompressedBlockSizes.Max();
writer.WriteUInt32(maxUncompressedBlockSize);
for (int i = 0; i < compressedBlocks.Length; i++)
{
if (i == 0)
{
// apply flag to the first block to indicate it's uncompressed
writer.WriteUInt32(uncompressedBlockSizes[0] | 0x01000000);
}
else
{
writer.WriteUInt32((uint)compressedBlocks[i].Length + 4);
}
}
break;
}
}
}
}
void AddHandlerRecursively(DirectoryMeta.Entry entry, EventHandler<DirectoryMeta.Entry.EntryOperationEventArgs> handler)
{
// Attach the handler to the current entry
entry.AfterWrite -= handler;
entry.AfterWrite += handler;
// If entry has a directory, iterate over its entries
if (entry.dir != null)
{
foreach (var subEntry in entry.dir.entries)
{
AddHandlerRecursively(subEntry, handler);
}
// Now check if entry.dir itself is an ObjectDir (cast DirectoryMeta to ObjectDir)
if (entry.dir is DirectoryMeta dirMeta && dirMeta.directory is ObjectDir objDir)
{
TraverseInlineSubDirs(objDir, handler);
}
}
}
void TraverseInlineSubDirs(ObjectDir objDir, EventHandler<DirectoryMeta.Entry.EntryOperationEventArgs> handler)
{
foreach (var subDir in objDir.inlineSubDirs)
{
foreach (var entry in subDir.entries)
{
AddHandlerRecursively(entry, handler);
}
// Recursively traverse deeper inline subdirectories
if (subDir.directory is ObjectDir subObjDir)
{
TraverseInlineSubDirs(subObjDir, handler);
}
}
}
public override string ToString()
{
return string.Format("{0}", filePath);
}
}
}