Beispiel #1
0
 public void ReadFiles(Stream blocksStream, string path)
 {
     fileList = new StreamFile[m_DirectoryInfo.Length];
     for (int i = 0; i < m_DirectoryInfo.Length; i++)
     {
         var node = m_DirectoryInfo[i];
         var file = new StreamFile();
         fileList[i]   = file;
         file.fileName = Path.GetFileName(node.path);
         if (node.size >= int.MaxValue)
         {
             /*var memoryMappedFile = MemoryMappedFile.CreateNew(file.fileName, entryinfo_size);
              * file.stream = memoryMappedFile.CreateViewStream();*/
             var extractPath = path + "_unpacked" + Path.DirectorySeparatorChar;
             Directory.CreateDirectory(extractPath);
             file.stream = new FileStream(extractPath + file.fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
         }
         else
         {
             file.stream = new MemoryStream((int)node.size);
         }
         blocksStream.Position = node.offset;
         blocksStream.CopyTo(file.stream, node.size);
         file.stream.Position = 0;
     }
 }
Beispiel #2
0
        public WebFile(EndianBinaryReader reader)
        {
            reader.endian = EndianType.LittleEndian;
            var signature  = reader.ReadStringToNull();
            var headLength = reader.ReadInt32();
            var dataList   = new List <WebData>();

            while (reader.BaseStream.Position < headLength)
            {
                var data = new WebData();
                data.dataOffset = reader.ReadInt32();
                data.dataLength = reader.ReadInt32();
                var pathLength = reader.ReadInt32();
                data.path = Encoding.UTF8.GetString(reader.ReadBytes(pathLength));
                dataList.Add(data);
            }
            fileList = new StreamFile[dataList.Count];
            for (int i = 0; i < dataList.Count; i++)
            {
                var data = dataList[i];
                var file = new StreamFile();
                file.path     = data.path;
                file.fileName = Path.GetFileName(data.path);
                reader.BaseStream.Position = data.dataOffset;
                file.stream = new MemoryStream(reader.ReadBytes(data.dataLength));
                fileList[i] = file;
            }
        }
Beispiel #3
0
        private void ReadWebData(BinaryReader reader)
        {
            var signature = reader.ReadStringToNull();

            if (signature != "UnityWebData1.0")
            {
                return;
            }
            var headLength = reader.ReadInt32();
            var dataList   = new List <WebData>();

            while (reader.BaseStream.Position < headLength)
            {
                var data = new WebData();
                data.dataOffset = reader.ReadInt32();
                data.dataLength = reader.ReadInt32();
                var pathLength = reader.ReadInt32();
                data.path = Encoding.UTF8.GetString(reader.ReadBytes(pathLength));
                dataList.Add(data);
            }
            fileList = new StreamFile[dataList.Count];
            for (int i = 0; i < dataList.Count; i++)
            {
                var data = dataList[i];
                var file = new StreamFile();
                file.path     = data.path;
                file.fileName = Path.GetFileName(data.path);
                reader.BaseStream.Position = data.dataOffset;
                file.stream = new MemoryStream(reader.ReadBytes(data.dataLength));
                fileList[i] = file;
            }
        }
Beispiel #4
0
        private void ReadWebData(EndianBinaryReader reader)
        {
            var signature = reader.ReadStringToNull();

            if (signature != "UnityWebData1.0")
            {
                return;
            }
            var headLength = reader.ReadInt32();
            var dataList   = new List <WebData>();

            while (reader.Position < headLength)
            {
                var data = new WebData();
                data.dataOffset = reader.ReadInt32();
                data.dataLength = reader.ReadInt32();
                var pathLength = reader.ReadInt32();
                data.path = Encoding.UTF8.GetString(reader.ReadBytes(pathLength));
                dataList.Add(data);
            }

            foreach (var data in dataList)
            {
                var file = new StreamFile();
                file.fileName   = Path.GetFileName(data.path);
                reader.Position = data.dataOffset;
                file.stream     = new MemoryStream(reader.ReadBytes(data.dataLength));
                fileList.Add(file);
            }
        }
Beispiel #5
0
        private void GetAssetsFiles(EndianBinaryReader reader, int offset)
        {
            int fileCount = reader.ReadInt32();

            for (int i = 0; i < fileCount; i++)
            {
                var file = new StreamFile();
                file.fileName = Path.GetFileName(reader.ReadStringToNull());
                int fileOffset = reader.ReadInt32();
                fileOffset += offset;
                int  fileSize = reader.ReadInt32();
                long nextFile = reader.Position;
                reader.Position = fileOffset;
                var buffer = reader.ReadBytes(fileSize);
                file.stream = new MemoryStream(buffer);
                fileList.Add(file);
                reader.Position = nextFile;
            }
        }
Beispiel #6
0
        private void ReadFormat6(EndianBinaryReader bundleReader, bool padding = false)
        {
            var bundleSize       = bundleReader.ReadInt64();
            int compressedSize   = bundleReader.ReadInt32();
            int uncompressedSize = bundleReader.ReadInt32();
            int flag             = bundleReader.ReadInt32();

            if (padding)
            {
                bundleReader.ReadByte();
            }
            byte[] blocksInfoBytes;
            if ((flag & 0x80) != 0)//at end of file
            {
                var position = bundleReader.Position;
                bundleReader.Position = bundleReader.BaseStream.Length - compressedSize;
                blocksInfoBytes       = bundleReader.ReadBytes(compressedSize);
                bundleReader.Position = position;
            }
            else
            {
                blocksInfoBytes = bundleReader.ReadBytes(compressedSize);
            }
            var          blocksInfoCompressedStream = new MemoryStream(blocksInfoBytes);
            MemoryStream blocksInfoDecompressedStream;

            switch (flag & 0x3F)
            {
            default:    //None
            {
                blocksInfoDecompressedStream = blocksInfoCompressedStream;
                break;
            }

            case 1:    //LZMA
            {
                blocksInfoDecompressedStream = SevenZipHelper.StreamDecompress(blocksInfoCompressedStream);
                blocksInfoCompressedStream.Close();
                break;
            }

            case 2:    //LZ4
            case 3:    //LZ4HC
            {
                byte[] uncompressedBytes = new byte[uncompressedSize];
                using (var decoder = new Lz4DecoderStream(blocksInfoCompressedStream))
                {
                    decoder.Read(uncompressedBytes, 0, uncompressedSize);
                }
                blocksInfoDecompressedStream = new MemoryStream(uncompressedBytes);
                break;
            }
                //case 4:LZHAM?
            }
            using (var blocksInfoReader = new EndianBinaryReader(blocksInfoDecompressedStream))
            {
                blocksInfoReader.Position = 0x10;
                int blockcount = blocksInfoReader.ReadInt32();
                var blockInfos = new BlockInfo[blockcount];
                for (int i = 0; i < blockcount; i++)
                {
                    blockInfos[i] = new BlockInfo
                    {
                        uncompressedSize = blocksInfoReader.ReadUInt32(),
                        compressedSize   = blocksInfoReader.ReadUInt32(),
                        flag             = blocksInfoReader.ReadInt16()
                    };
                }
                Stream dataStream;
                var    uncompressedSizeSum = blockInfos.Sum(x => x.uncompressedSize);
                if (uncompressedSizeSum > int.MaxValue)
                {
                    /*var memoryMappedFile = MemoryMappedFile.CreateNew(Path.GetFileName(path), uncompressedSizeSum);
                     * assetsDataStream = memoryMappedFile.CreateViewStream();*/
                    dataStream = new FileStream(path + ".temp", FileMode.Create, FileAccess.ReadWrite, FileShare.None, 4096, FileOptions.DeleteOnClose);
                }
                else
                {
                    dataStream = new MemoryStream((int)uncompressedSizeSum);
                }
                foreach (var blockInfo in blockInfos)
                {
                    switch (blockInfo.flag & 0x3F)
                    {
                    default:    //None
                    {
                        bundleReader.BaseStream.CopyTo(dataStream, blockInfo.compressedSize);
                        break;
                    }

                    case 1:    //LZMA
                    {
                        SevenZipHelper.StreamDecompress(bundleReader.BaseStream, dataStream, blockInfo.compressedSize, blockInfo.uncompressedSize);
                        break;
                    }

                    case 2:    //LZ4
                    case 3:    //LZ4HC
                    {
                        var lz4Stream = new Lz4DecoderStream(bundleReader.BaseStream, blockInfo.compressedSize);
                        lz4Stream.CopyTo(dataStream, blockInfo.uncompressedSize);
                        break;
                    }
                        //case 4:LZHAM?
                    }
                }
                dataStream.Position = 0;
                using (dataStream)
                {
                    var entryinfo_count = blocksInfoReader.ReadInt32();
                    for (int i = 0; i < entryinfo_count; i++)
                    {
                        var file             = new StreamFile();
                        var entryinfo_offset = blocksInfoReader.ReadInt64();
                        var entryinfo_size   = blocksInfoReader.ReadInt64();
                        flag          = blocksInfoReader.ReadInt32();
                        file.fileName = Path.GetFileName(blocksInfoReader.ReadStringToNull());
                        if (entryinfo_size > int.MaxValue)
                        {
                            /*var memoryMappedFile = MemoryMappedFile.CreateNew(file.fileName, entryinfo_size);
                             * file.stream = memoryMappedFile.CreateViewStream();*/
                            var extractPath = path + "_unpacked\\";
                            Directory.CreateDirectory(extractPath);
                            file.stream = File.Create(extractPath + file.fileName);
                        }
                        else
                        {
                            file.stream = new MemoryStream((int)entryinfo_size);
                        }
                        dataStream.Position = entryinfo_offset;
                        dataStream.CopyTo(file.stream, entryinfo_size);
                        file.stream.Position = 0;
                        fileList.Add(file);
                    }
                }
            }
        }