private void getFiles(EndianBinaryReader f_Stream, int offset) { int fileCount = f_Stream.ReadInt32(); for (int i = 0; i < fileCount; i++) { var memFile = new MemoryAssetsFile(); memFile.fileName = f_Stream.ReadStringToNull(); int fileOffset = f_Stream.ReadInt32(); fileOffset += offset; int fileSize = f_Stream.ReadInt32(); long nextFile = f_Stream.Position; f_Stream.Position = fileOffset; var buffer = f_Stream.ReadBytes(fileSize); memFile.memStream = new MemoryStream(buffer); MemoryAssetsFileList.Add(memFile); f_Stream.Position = nextFile; } }
private void getFiles(EndianStream f_Stream, int offset) { int fileCount = f_Stream.ReadInt32(); for (int i = 0; i < fileCount; i++) { MemoryAssetsFile memFile = new MemoryAssetsFile(); memFile.fileName = f_Stream.ReadStringToNull(); int fileOffset = f_Stream.ReadInt32(); fileOffset += offset; int fileSize = f_Stream.ReadInt32(); long nextFile = f_Stream.Position; f_Stream.Position = fileOffset; byte[] buffer = new byte[fileSize]; f_Stream.Read(buffer, 0, fileSize); memFile.memStream = new MemoryStream(buffer); MemoryAssetsFileList.Add(memFile); f_Stream.Position = nextFile; } }
private void ReadFormat6(EndianStream b_Stream, bool padding = false) { var bundleSize = b_Stream.ReadInt64(); int compressedSize = b_Stream.ReadInt32(); int uncompressedSize = b_Stream.ReadInt32(); int flag = b_Stream.ReadInt32(); if (padding) { b_Stream.ReadByte(); } byte[] blocksInfoBytes; if ((flag & 0x80) != 0)//at end of file { var position = b_Stream.Position; b_Stream.Position = b_Stream.BaseStream.Length - compressedSize; blocksInfoBytes = b_Stream.ReadBytes(compressedSize); b_Stream.Position = position; } else { blocksInfoBytes = b_Stream.ReadBytes(compressedSize); } EndianStream blocksInfo; switch (flag & 0x3F) { default: //None { blocksInfo = new EndianStream(new MemoryStream(blocksInfoBytes), EndianType.BigEndian); break; } case 1: //LZMA { blocksInfo = new EndianStream(SevenZipHelper.StreamDecompress(new MemoryStream(blocksInfoBytes)), EndianType.BigEndian); break; } case 2: //LZ4 case 3: //LZ4HC { byte[] uncompressedBytes = new byte[uncompressedSize]; using (var mstream = new MemoryStream(blocksInfoBytes)) { var decoder = new Lz4DecoderStream(mstream); decoder.Read(uncompressedBytes, 0, uncompressedSize); decoder.Dispose(); } blocksInfo = new EndianStream(new MemoryStream(uncompressedBytes), EndianType.BigEndian); break; } //case 4:LZHAM? } using (blocksInfo) { blocksInfo.Position = 0x10; int blockcount = blocksInfo.ReadInt32(); EndianStream assetsData; var assetsDataStream = new MemoryStream(); for (int i = 0; i < blockcount; i++) { uncompressedSize = blocksInfo.ReadInt32(); compressedSize = blocksInfo.ReadInt32(); flag = blocksInfo.ReadInt16(); var compressedBytes = b_Stream.ReadBytes(compressedSize); switch (flag & 0x3F) { default: //None { assetsDataStream.Write(compressedBytes, 0, compressedSize); break; } case 1: //LZMA { var uncompressedBytes = new byte[uncompressedSize]; using (var mstream = new MemoryStream(compressedBytes)) { var decoder = SevenZipHelper.StreamDecompress(mstream, uncompressedSize); decoder.Read(uncompressedBytes, 0, uncompressedSize); decoder.Dispose(); } assetsDataStream.Write(uncompressedBytes, 0, uncompressedSize); break; } case 2: //LZ4 case 3: //LZ4HC { var uncompressedBytes = new byte[uncompressedSize]; using (var mstream = new MemoryStream(compressedBytes)) { var decoder = new Lz4DecoderStream(mstream); decoder.Read(uncompressedBytes, 0, uncompressedSize); decoder.Dispose(); } assetsDataStream.Write(uncompressedBytes, 0, uncompressedSize); break; } //case 4:LZHAM? } } assetsData = new EndianStream(assetsDataStream, EndianType.BigEndian); using (assetsData) { var entryinfo_count = blocksInfo.ReadInt32(); for (int i = 0; i < entryinfo_count; i++) { var memFile = new MemoryAssetsFile(); var entryinfo_offset = blocksInfo.ReadInt64(); var entryinfo_size = blocksInfo.ReadInt64(); var unknown = blocksInfo.ReadInt32(); memFile.fileName = blocksInfo.ReadStringToNull(); assetsData.Position = entryinfo_offset; var buffer = new byte[entryinfo_size]; assetsData.Read(buffer, 0, (int)entryinfo_size); memFile.memStream = new MemoryStream(buffer); MemoryAssetsFileList.Add(memFile); } } } }
public BundleFile(string fileName) { if (Path.GetExtension(fileName) == ".lz4") { byte[] filebuffer; using (BinaryReader lz4Stream = new BinaryReader(File.OpenRead(fileName))) { int version = lz4Stream.ReadInt32(); int uncompressedSize = lz4Stream.ReadInt32(); int compressedSize = lz4Stream.ReadInt32(); int something = lz4Stream.ReadInt32(); //1 byte[] lz4buffer = new byte[compressedSize]; lz4Stream.Read(lz4buffer, 0, compressedSize); using (var inputStream = new MemoryStream(lz4buffer)) { var decoder = new Lz4DecoderStream(inputStream); filebuffer = new byte[uncompressedSize]; //is this ok? for (;;) { int nRead = decoder.Read(filebuffer, 0, uncompressedSize); if (nRead == 0) break; } } } Stream = new EndianStream(new MemoryStream(filebuffer), EndianType.BigEndian); } else { Stream = new EndianStream(File.OpenRead(fileName), EndianType.BigEndian); } long magicHeader = Stream.ReadInt64(); if (magicHeader == -361700864190383366 || magicHeader == 6155973689634940258 || magicHeader == 6155973689634611575) { int dummy = Stream.ReadInt32(); ver1 = Stream.ReadByte(); ver2 = Stream.ReadStringToNull(); ver3 = Stream.ReadStringToNull(); int lzmaSize = 0; int fileSize = Stream.ReadInt32(); short dummy2 = Stream.ReadInt16(); int offset = Stream.ReadInt16(); int dummy3 = Stream.ReadInt32(); int lzmaChunks = Stream.ReadInt32(); for (int i = 0; i < lzmaChunks; i++) { lzmaSize = Stream.ReadInt32(); fileSize = Stream.ReadInt32(); } Stream.Position = offset; switch (magicHeader) { case -361700864190383366: //.bytes case 6155973689634940258: //UnityWeb { byte[] lzmaBuffer = new byte[lzmaSize]; Stream.Read(lzmaBuffer, 0, lzmaSize); Stream.Close(); Stream.Dispose(); Stream = new EndianStream(SevenZip.Compression.LZMA.SevenZipHelper.StreamDecompress(new MemoryStream(lzmaBuffer)), EndianType.BigEndian); offset = 0; break; } case 6155973689634611575: //UnityRaw { break; } } int fileCount = Stream.ReadInt32(); for (int i = 0; i < fileCount; i++) { MemoryAssetsFile memFile = new MemoryAssetsFile(); memFile.fileName = Stream.ReadStringToNull(); int fileOffset = Stream.ReadInt32(); fileOffset += offset; fileSize = Stream.ReadInt32(); long nextFile = Stream.Position; Stream.Position = fileOffset; byte[] buffer = new byte[fileSize]; Stream.Read(buffer, 0, fileSize); memFile.memStream = new MemoryStream(buffer); MemoryAssetsFileList.Add(memFile); Stream.Position = nextFile; } } Stream.Close(); }