public BSFile(string fileName, byte[] data, AesManaged aes, BSFileHeader header) { this.fileName = fileName; this._data = data; this._aes = aes; this._header = header; }
/// <summary> /// Reads the compressed header /// </summary> private void readHeader() { _reader.BaseStream.Seek(89, SeekOrigin.Begin); int compressedSize = _reader.ReadInt32(); int ucompressedSize = _reader.ReadInt32(); int padding = (compressedSize % 16 != 0) ? 16 - (compressedSize % 16) : 0; byte[] buffer = _reader.ReadBytes(compressedSize + padding); byte[] data = Crypto.Decrypt(buffer, _aes); if (data.Length != ucompressedSize) { throw new Exception("Decompression failed. Data.length != uncompressedSize"); } int positionCheck = _reader.ReadInt32(); if (positionCheck != _reader.BaseStream.Position) { throw new Exception("Decompression failed. positionCheck != _reader.BaseStream.Position"); } using (MemoryStream ms = new MemoryStream(data)) { using (BinaryReader br = new BinaryReader(ms)) { int size = Marshal.SizeOf(typeof(BSFileHeader)); //80 while (ms.Position < ms.Length) { var strSize = br.ReadInt32(); var fileName = Encoding.Unicode.GetString(br.ReadBytes(strSize * 2)); var _struct = br.ReadBytes(size); GCHandle handle = GCHandle.Alloc(_struct, GCHandleType.Pinned); BSFileHeader header = (BSFileHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(BSFileHeader)); handle.Free(); BSFile bnsFile = new BSFile(fileName, _reader.ReadBytes(header.compressed), _aes, header); _files.Add(fileName, bnsFile); } } } }