-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathAssetsBundleFile.cs
More file actions
768 lines (668 loc) · 31.5 KB
/
AssetsBundleFile.cs
File metadata and controls
768 lines (668 loc) · 31.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
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
using AssetsTools.NET.Extra;
using AssetsTools.NET.Extra.Decompressors.LZ4;
using LZ4ps;
using SevenZip.Compression.LZMA;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace AssetsTools.NET
{
public class AssetBundleFile
{
public AssetBundleHeader03 bundleHeader3;
public AssetBundleHeader06 bundleHeader6;
public AssetsList assetsLists3;
public AssetBundleBlockAndDirectoryList06 bundleInf6;
public AssetsFileReader reader;
public void Close()
{
reader.Close();
}
public bool Read(AssetsFileReader reader, bool allowCompressed = false)
{
this.reader = reader;
reader.ReadNullTerminated();
uint version = reader.ReadUInt32();
switch (version)
{
case 3:
reader.Position = 0;
bundleHeader3 = new AssetBundleHeader03();
bundleHeader3.Read(reader);
if (bundleHeader3.signature != "UnityRaw")
throw new NotImplementedException("Non UnityRaw bundles are not supported yet.");
if (bundleHeader3.blockList.Any(b => b.compressed != b.uncompressed))
throw new NotImplementedException("Compressed UnityRaw bundles are not supported yet.");
assetsLists3 = new AssetsList();
assetsLists3.Read(reader);
return true;
case 6:
case 7:
reader.Position = 0;
bundleHeader6 = new AssetBundleHeader06();
bundleHeader6.Read(reader);
if (bundleHeader6.fileVersion >= 7)
reader.Align16();
if (bundleHeader6.signature != "UnityFS")
throw new NotImplementedException("Non UnityFS bundles are not supported yet.");
bundleInf6 = new AssetBundleBlockAndDirectoryList06();
if ((bundleHeader6.flags & 0x3F) != 0)
{
if (allowCompressed)
{
return true;
}
else
{
Close();
return false;
}
}
else
{
bundleInf6.Read(bundleHeader6.GetBundleInfoOffset(), reader);
return true;
}
default:
throw new Exception("AssetsBundleFile.Read : Unknown file version!");
}
}
public bool Write(AssetsFileWriter writer, List<BundleReplacer> replacers, ClassDatabaseFile typeMeta = null)
{
if (bundleHeader3 != null)
return Write03(writer, replacers);
if (bundleHeader6 != null)
return Write06(writer, replacers);
return false;
}
private bool Write03(AssetsFileWriter writer, List<BundleReplacer> replacers)
{
AssetBundleHeader03 newBundleHeader3 = bundleHeader3.Clone();
newBundleHeader3.blockList = new[] { new AssetsBundleOffsetPair() };
newBundleHeader3.Write(writer);
newBundleHeader3.bundleDataOffs = (uint)writer.Position;
Dictionary<string, BundleReplacer> addingReplacers =
replacers.Where(r => r.GetReplacementType() == BundleReplacementType.AddOrModify)
.ToDictionary(r => r.GetOriginalEntryName());
List<AssetsBundleEntry> newEntries = new List<AssetsBundleEntry>();
Dictionary<AssetsBundleEntry, AssetsBundleEntry> newEntryToOldEntry = new Dictionary<AssetsBundleEntry, AssetsBundleEntry>();
Dictionary<AssetsBundleEntry, BundleReplacer> newEntryToReplacer = new Dictionary<AssetsBundleEntry, BundleReplacer>();
foreach (AssetsBundleEntry entry in assetsLists3.entries)
{
addingReplacers.Remove(entry.name);
BundleReplacer replacer = replacers.FirstOrDefault(r => r.GetOriginalEntryName() == entry.name);
if (replacer == null ||
replacer.GetReplacementType() == BundleReplacementType.AddOrModify ||
replacer.GetReplacementType() == BundleReplacementType.Rename)
{
AssetsBundleEntry newEntry = new AssetsBundleEntry { name = replacer?.GetEntryName() ?? entry.name };
newEntries.Add(newEntry);
newEntryToOldEntry.Add(newEntry, entry);
if (replacer != null && replacer.GetReplacementType() == BundleReplacementType.AddOrModify)
newEntryToReplacer.Add(newEntry, replacer);
}
}
foreach (BundleReplacer replacer in addingReplacers.Values)
{
AssetsBundleEntry newEntry = new AssetsBundleEntry { name = replacer.GetEntryName() };
newEntries.Add(newEntry);
newEntryToReplacer.Add(newEntry, replacer);
}
bundleHeader3.numberOfAssetsToDownload = (uint)newEntries.Count;
AssetsList newAssetsList = new AssetsList { entries = newEntries.ToArray() };
newAssetsList.Write(writer);
writer.Align16();
bundleHeader3.assetsListSize = (uint)writer.Position - bundleHeader3.bundleDataOffs;
foreach (AssetsBundleEntry newEntry in newEntries)
{
long newEntryPosition = writer.Position;
newEntry.offset = (uint)(writer.Position - bundleHeader3.bundleDataOffs);
if (newEntryToReplacer.TryGetValue(newEntry, out BundleReplacer replacer))
{
replacer.Write(writer);
}
else
{
AssetsBundleEntry oldEntry = newEntryToOldEntry[newEntry];
reader.Position = bundleHeader3.bundleDataOffs + oldEntry.offset;
reader.BaseStream.CopyToCompat(writer.BaseStream, oldEntry.length);
}
newEntry.length = (uint)(writer.Position - newEntryPosition);
}
newBundleHeader3.minimumStreamedBytes = (uint)writer.Position;
newBundleHeader3.blockList[0].compressed = (uint)writer.Position - newBundleHeader3.bundleDataOffs;
newBundleHeader3.blockList[0].uncompressed = (uint)writer.Position - newBundleHeader3.bundleDataOffs;
newBundleHeader3.fileSize2 = (uint)writer.Position;
writer.Position = 0;
newBundleHeader3.Write(writer);
newAssetsList.Write(writer);
writer.Position = newBundleHeader3.fileSize2;
return true;
}
private bool Write06(AssetsFileWriter writer, List<BundleReplacer> replacers)
{
bundleHeader6.Write(writer);
if (bundleHeader6.fileVersion >= 7)
{
writer.Align16();
}
AssetBundleBlockAndDirectoryList06 newBundleInf6 = new AssetBundleBlockAndDirectoryList06
{
checksumLow = 0,
checksumHigh = 0
};
//I could map the assets to their blocks but I don't
//have any more-than-1-block files to test on
//this should work just fine as far as I know
newBundleInf6.blockInf = new[]
{
new AssetBundleBlockInfo06
{
compressedSize = 0,
decompressedSize = 0,
flags = 0x40
}
};
//assets that did not have their data modified but need
//the original info to read from the original file
var newToOriginalDirInfoLookup = new Dictionary<AssetBundleDirectoryInfo06, AssetBundleDirectoryInfo06>();
List<AssetBundleDirectoryInfo06> originalDirInfos = new List<AssetBundleDirectoryInfo06>();
List<AssetBundleDirectoryInfo06> dirInfos = new List<AssetBundleDirectoryInfo06>();
List<BundleReplacer> currentReplacers = replacers.ToList();
//write all original files, modify sizes if needed and skip those to be removed
for (int i = 0; i < bundleInf6.directoryCount; i++)
{
AssetBundleDirectoryInfo06 info = bundleInf6.dirInf[i];
originalDirInfos.Add(info);
AssetBundleDirectoryInfo06 newInfo = new AssetBundleDirectoryInfo06
{
decompressedSize = info.decompressedSize,
flags = info.flags,
name = info.name
};
BundleReplacer replacer = currentReplacers.FirstOrDefault(n => n.GetOriginalEntryName() == newInfo.name);
if (replacer != null)
{
currentReplacers.Remove(replacer);
if (replacer.GetReplacementType() == BundleReplacementType.AddOrModify)
{
newInfo = new AssetBundleDirectoryInfo06
{
flags = info.flags,
name = replacer.GetEntryName()
};
}
else if (replacer.GetReplacementType() == BundleReplacementType.Rename)
{
newInfo = new AssetBundleDirectoryInfo06
{
decompressedSize = info.decompressedSize,
flags = info.flags,
name = replacer.GetEntryName()
};
newToOriginalDirInfoLookup[newInfo] = info;
}
else if (replacer.GetReplacementType() == BundleReplacementType.Remove)
{
continue;
}
}
else
{
newToOriginalDirInfoLookup[newInfo] = info;
}
dirInfos.Add(newInfo);
}
//write new files
while (currentReplacers.Count > 0)
{
BundleReplacer replacer = currentReplacers[0];
if (replacer.GetReplacementType() == BundleReplacementType.AddOrModify)
{
AssetBundleDirectoryInfo06 info = new AssetBundleDirectoryInfo06()
{
flags = (uint)(replacer.HasSerializedData() ? 0x04 : 0x00),
name = replacer.GetEntryName()
};
dirInfos.Add(info);
}
currentReplacers.Remove(replacer);
}
//write the listings
long bundleInfPos = writer.Position;
newBundleInf6.dirInf = dirInfos.ToArray(); //this is only here to allocate enough space so it's fine if it's inaccurate
newBundleInf6.Write(writer);
long assetDataPos = writer.Position;
//actually write the file data to the bundle now
for (int i = 0; i < dirInfos.Count; i++)
{
AssetBundleDirectoryInfo06 info = dirInfos[i];
BundleReplacer replacer = replacers.FirstOrDefault(n => n.GetEntryName() == info.name);
if (replacer != null)
{
if (replacer.GetReplacementType() == BundleReplacementType.AddOrModify)
{
long startPos = writer.Position;
long endPos = replacer.Write(writer);
long size = endPos - startPos;
dirInfos[i].decompressedSize = size;
dirInfos[i].offset = startPos - assetDataPos;
}
else if (replacer.GetReplacementType() == BundleReplacementType.Remove)
{
continue;
}
}
else
{
if (newToOriginalDirInfoLookup.TryGetValue(info, out AssetBundleDirectoryInfo06 originalInfo))
{
long startPos = writer.Position;
reader.Position = bundleHeader6.GetFileDataOffset() + originalInfo.offset;
reader.BaseStream.CopyToCompat(writer.BaseStream, originalInfo.decompressedSize);
dirInfos[i].offset = startPos - assetDataPos;
}
}
}
//now that we know what the sizes are of the written files let's go back and fix them
long finalSize = writer.Position;
uint assetSize = (uint)(finalSize - assetDataPos);
writer.Position = bundleInfPos;
newBundleInf6.blockInf[0].decompressedSize = assetSize;
newBundleInf6.blockInf[0].compressedSize = assetSize;
newBundleInf6.dirInf = dirInfos.ToArray();
newBundleInf6.Write(writer);
uint infoSize = (uint)(assetDataPos - bundleInfPos);
writer.Position = 0;
AssetBundleHeader06 newBundleHeader6 = new AssetBundleHeader06()
{
signature = bundleHeader6.signature,
fileVersion = bundleHeader6.fileVersion,
minPlayerVersion = bundleHeader6.minPlayerVersion,
fileEngineVersion = bundleHeader6.fileEngineVersion,
totalFileSize = finalSize,
compressedSize = infoSize,
decompressedSize = infoSize,
flags = bundleHeader6.flags & unchecked((uint)~0x80) & unchecked((uint)~0x3f) //unset info at end flag and compression value
};
newBundleHeader6.Write(writer);
return true;
}
public bool Unpack(AssetsFileReader reader, AssetsFileWriter writer)
{
reader.Position = 0;
if (Read(reader, true))
{
reader.Position = bundleHeader6.GetBundleInfoOffset();
MemoryStream blocksInfoStream;
AssetsFileReader memReader;
int compressedSize = (int)bundleHeader6.compressedSize;
int decompressedSize = (int)bundleHeader6.decompressedSize;
switch (bundleHeader6.GetCompressionType())
{
case 1:
using (MemoryStream mstream = new MemoryStream(reader.ReadBytes(compressedSize)))
{
blocksInfoStream = new MemoryStream();
SevenZipHelper.StreamDecompress(mstream, blocksInfoStream, compressedSize, decompressedSize);
}
break;
case 2:
case 3:
byte[] uncompressedBytes = new byte[bundleHeader6.decompressedSize];
using (MemoryStream mstream = new MemoryStream(reader.ReadBytes(compressedSize)))
{
var decoder = new Lz4DecoderStream(mstream);
decoder.Read(uncompressedBytes, 0, (int)bundleHeader6.decompressedSize);
decoder.Dispose();
}
blocksInfoStream = new MemoryStream(uncompressedBytes);
break;
default:
blocksInfoStream = null;
break;
}
if (bundleHeader6.GetCompressionType() != 0)
{
using (memReader = new AssetsFileReader(blocksInfoStream))
{
memReader.Position = 0;
bundleInf6.Read(0, memReader);
}
}
AssetBundleHeader06 newBundleHeader6 = new AssetBundleHeader06()
{
signature = bundleHeader6.signature,
fileVersion = bundleHeader6.fileVersion,
minPlayerVersion = bundleHeader6.minPlayerVersion,
fileEngineVersion = bundleHeader6.fileEngineVersion,
totalFileSize = 0,
compressedSize = bundleHeader6.decompressedSize,
decompressedSize = bundleHeader6.decompressedSize,
flags = bundleHeader6.flags & (0x40 | 0x200) //set compression and block position to 0
};
long fileSize = newBundleHeader6.GetFileDataOffset();
for (int i = 0; i < bundleInf6.blockCount; i++)
fileSize += bundleInf6.blockInf[i].decompressedSize;
newBundleHeader6.totalFileSize = fileSize;
AssetBundleBlockAndDirectoryList06 newBundleInf6 = new AssetBundleBlockAndDirectoryList06()
{
checksumLow = 0, //-todo, figure out how to make real checksums, uabe sets these to 0 too
checksumHigh = 0,
blockCount = bundleInf6.blockCount,
directoryCount = bundleInf6.directoryCount
};
newBundleInf6.blockInf = new AssetBundleBlockInfo06[newBundleInf6.blockCount];
for (int i = 0; i < newBundleInf6.blockCount; i++)
{
newBundleInf6.blockInf[i] = new AssetBundleBlockInfo06()
{
compressedSize = bundleInf6.blockInf[i].decompressedSize,
decompressedSize = bundleInf6.blockInf[i].decompressedSize,
flags = (ushort)(bundleInf6.blockInf[i].flags & 0xC0) //set compression to none
};
}
newBundleInf6.dirInf = new AssetBundleDirectoryInfo06[newBundleInf6.directoryCount];
for (int i = 0; i < newBundleInf6.directoryCount; i++)
{
newBundleInf6.dirInf[i] = new AssetBundleDirectoryInfo06()
{
offset = bundleInf6.dirInf[i].offset,
decompressedSize = bundleInf6.dirInf[i].decompressedSize,
flags = bundleInf6.dirInf[i].flags,
name = bundleInf6.dirInf[i].name
};
}
newBundleHeader6.Write(writer);
if (newBundleHeader6.fileVersion >= 7)
{
writer.Align16();
}
newBundleInf6.Write(writer);
if ((newBundleHeader6.flags & 0x200) != 0)
{
writer.Align16();
}
reader.Position = bundleHeader6.GetFileDataOffset();
for (int i = 0; i < newBundleInf6.blockCount; i++)
{
AssetBundleBlockInfo06 info = bundleInf6.blockInf[i];
switch (info.GetCompressionType())
{
case 0:
reader.BaseStream.CopyToCompat(writer.BaseStream, info.compressedSize);
break;
case 1:
SevenZipHelper.StreamDecompress(reader.BaseStream, writer.BaseStream, info.compressedSize, info.decompressedSize);
break;
case 2:
case 3:
using (MemoryStream tempMs = new MemoryStream())
{
reader.BaseStream.CopyToCompat(tempMs, info.compressedSize);
tempMs.Position = 0;
using (Lz4DecoderStream decoder = new Lz4DecoderStream(tempMs))
{
decoder.CopyToCompat(writer.BaseStream, info.decompressedSize);
}
}
break;
}
}
return true;
}
return false;
}
public bool Pack(AssetsFileReader reader, AssetsFileWriter writer, AssetBundleCompressionType compType, bool blockDirAtEnd = true)
{
reader.Position = 0;
writer.Position = 0;
if (Read(reader, false))
{
AssetBundleHeader06 newHeader = new AssetBundleHeader06()
{
signature = bundleHeader6.signature,
fileVersion = bundleHeader6.fileVersion,
minPlayerVersion = bundleHeader6.minPlayerVersion,
fileEngineVersion = bundleHeader6.fileEngineVersion,
totalFileSize = 0,
compressedSize = 0,
decompressedSize = 0,
flags = (uint)(0x43 | (blockDirAtEnd ? 0x80 : 0x00))
};
AssetBundleBlockAndDirectoryList06 newBlockAndDirList = new AssetBundleBlockAndDirectoryList06()
{
checksumLow = 0,
checksumHigh = 0,
blockCount = 0,
blockInf = null,
directoryCount = bundleInf6.directoryCount,
dirInf = bundleInf6.dirInf
};
//write header now and overwrite it later
long startPos = writer.Position;
newHeader.Write(writer);
if (newHeader.fileVersion >= 7)
writer.Align16();
int headerSize = (int)(writer.Position - startPos);
long totalCompressedSize = 0;
List<AssetBundleBlockInfo06> newBlocks = new List<AssetBundleBlockInfo06>();
List<Stream> newStreams = new List<Stream>(); //used if blockDirAtEnd == false
long fileDataOffset = bundleHeader6.GetFileDataOffset();
int fileDataLength = (int)(bundleHeader6.totalFileSize - fileDataOffset);
SegmentStream bundleDataStream = new SegmentStream(reader.BaseStream, fileDataOffset, fileDataLength);
switch (compType)
{
case AssetBundleCompressionType.LZMA:
{
Stream writeStream;
if (blockDirAtEnd)
writeStream = writer.BaseStream;
else
writeStream = GetTempFileStream();
long writeStreamStart = writeStream.Position;
SevenZipHelper.Compress(bundleDataStream, writeStream);
uint writeStreamLength = (uint)(writeStream.Position - writeStreamStart);
AssetBundleBlockInfo06 blockInfo = new AssetBundleBlockInfo06()
{
compressedSize = writeStreamLength,
decompressedSize = (uint)fileDataLength,
flags = 0x41
};
totalCompressedSize += blockInfo.compressedSize;
newBlocks.Add(blockInfo);
if (!blockDirAtEnd)
newStreams.Add(writeStream);
break;
}
case AssetBundleCompressionType.LZ4:
{
//compress into 0x20000 blocks
BinaryReader bundleDataReader = new BinaryReader(bundleDataStream);
byte[] uncompressedBlock = bundleDataReader.ReadBytes(0x20000);
while (uncompressedBlock.Length != 0)
{
Stream writeStream;
if (blockDirAtEnd)
writeStream = writer.BaseStream;
else
writeStream = GetTempFileStream();
byte[] compressedBlock = LZ4Codec.Encode32HC(uncompressedBlock, 0, uncompressedBlock.Length);
if (compressedBlock.Length > uncompressedBlock.Length)
{
writeStream.Write(uncompressedBlock, 0, uncompressedBlock.Length);
AssetBundleBlockInfo06 blockInfo = new AssetBundleBlockInfo06()
{
compressedSize = (uint)uncompressedBlock.Length,
decompressedSize = (uint)uncompressedBlock.Length,
flags = 0x0
};
totalCompressedSize += blockInfo.compressedSize;
newBlocks.Add(blockInfo);
}
else
{
writeStream.Write(compressedBlock, 0, compressedBlock.Length);
AssetBundleBlockInfo06 blockInfo = new AssetBundleBlockInfo06()
{
compressedSize = (uint)compressedBlock.Length,
decompressedSize = (uint)uncompressedBlock.Length,
flags = 0x3
};
totalCompressedSize += blockInfo.compressedSize;
newBlocks.Add(blockInfo);
}
if (!blockDirAtEnd)
newStreams.Add(writeStream);
uncompressedBlock = bundleDataReader.ReadBytes(0x20000);
}
break;
}
case AssetBundleCompressionType.NONE:
{
AssetBundleBlockInfo06 blockInfo = new AssetBundleBlockInfo06()
{
compressedSize = (uint)fileDataLength,
decompressedSize = (uint)fileDataLength,
flags = 0x00
};
totalCompressedSize += blockInfo.compressedSize;
newBlocks.Add(blockInfo);
if (blockDirAtEnd)
bundleDataStream.CopyToCompat(writer.BaseStream);
else
newStreams.Add(bundleDataStream);
break;
}
}
newBlockAndDirList.blockInf = newBlocks.ToArray();
byte[] bundleInfoBytes;
using (MemoryStream memStream = new MemoryStream())
{
AssetsFileWriter infoWriter = new AssetsFileWriter(memStream);
newBlockAndDirList.Write(infoWriter);
bundleInfoBytes = memStream.ToArray();
}
//listing is usually lz4 even if the data blocks are lzma
byte[] bundleInfoBytesCom = LZ4Codec.Encode32HC(bundleInfoBytes, 0, bundleInfoBytes.Length);
long totalFileSize = headerSize + bundleInfoBytesCom.Length + totalCompressedSize;
newHeader.totalFileSize = totalFileSize;
newHeader.decompressedSize = (uint)bundleInfoBytes.Length;
newHeader.compressedSize = (uint)bundleInfoBytesCom.Length;
if (!blockDirAtEnd)
{
writer.Write(bundleInfoBytesCom);
foreach (Stream newStream in newStreams)
{
newStream.Position = 0;
newStream.CopyToCompat(writer.BaseStream);
newStream.Close();
}
}
else
{
writer.Write(bundleInfoBytesCom);
}
writer.Position = 0;
newHeader.Write(writer);
if (newHeader.fileVersion >= 7)
writer.Align16();
return true;
}
return false;
}
public int NumFiles
{
get
{
if (bundleHeader3 != null)
return assetsLists3.entries.Length;
if (bundleHeader6 != null)
return bundleInf6.dirInf.Length;
return 0;
}
}
public bool IsAssetsFile(int index)
{
if (bundleHeader3 != null)
return IsAssetsFile(assetsLists3.entries[index]);
if (bundleHeader6 != null)
return IsAssetsFile(bundleInf6.dirInf[index]);
return false;
}
[Obsolete]
public bool IsAssetsFile(AssetsBundleEntry entry)
{
long offset = bundleHeader3.bundleDataOffs + entry.offset;
return AssetsFile.IsAssetsFile(reader, offset, entry.length);
}
[Obsolete]
public bool IsAssetsFile(AssetBundleDirectoryInfo06 entry)
{
long offset = bundleHeader6.GetFileDataOffset() + entry.offset;
return AssetsFile.IsAssetsFile(reader, offset, entry.decompressedSize);
}
public int GetFileIndex(string name)
{
if (bundleHeader3 != null)
{
for (int i = 0; i < assetsLists3.entries.Length; i++)
{
if (assetsLists3.entries[i].name == name)
return i;
}
}
else if (bundleHeader6 != null)
{
for (int i = 0; i < bundleInf6.dirInf.Length; i++)
{
if (bundleInf6.dirInf[i].name == name)
return i;
}
}
return -1;
}
public string GetFileName(int index)
{
if (bundleHeader3 != null)
return assetsLists3.entries[index].name;
if (bundleHeader6 != null)
return bundleInf6.dirInf[index].name;
return null;
}
internal void GetFileRange(int index, out long offset, out long length)
{
if (bundleHeader3 != null)
{
AssetsBundleEntry entry = assetsLists3.entries[index];
offset = bundleHeader3.bundleDataOffs + entry.offset;
length = entry.length;
}
else if (bundleHeader6 != null)
{
AssetBundleDirectoryInfo06 entry = bundleInf6.dirInf[index];
offset = bundleHeader6.GetFileDataOffset() + entry.offset;
length = entry.decompressedSize;
}
else
{
throw new NotSupportedException();
}
}
private FileStream GetTempFileStream()
{
string tempFilePath = Path.GetTempFileName();
FileStream tempFileStream = new FileStream(tempFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read, 4096, FileOptions.DeleteOnClose);
return tempFileStream;
}
}
public enum AssetBundleCompressionType
{
NONE = 0,
LZMA,
LZ4
}
}