public static AssetHeader ReadAssetHeader(BinaryReader reader) { AssetHeader header = new AssetHeader(); header.compression = (AssetCompression)reader.ReadInt32(); header.sizeInBytes = reader.ReadInt64(); header.uncompressedBytes = reader.ReadInt64(); header.assetName = reader.ReadUtf8String(); string typeName = reader.ReadUtf8String(); header.assetType = TypeFinder.FindType(typeName); return(header); }
private void ReadHeaders() { var stream = GetThreadStream(); stream.Seek(0, SeekOrigin.Begin); BinaryReader reader = new BinaryReader(stream, Encoding.UTF8, true); int numberOfAssets = reader.ReadInt32(); // Read number of assets for (int i = 0; i < numberOfAssets; i++) { AssetHeader header = AssetHeader.ReadAssetHeader(reader); long assetPosition = stream.Position; assetPositions.Add(header.assetName, assetPosition); assetHeaders.Add(header.assetName, header); stream.Seek(header.sizeInBytes, SeekOrigin.Current); } }
public Stream GetUncompressedFileStream(string assetName) { if (!assetHeaders.ContainsKey(assetName)) { throw new ArgumentException($"No asset found with name {assetName} in package {assetFile.Name}"); } AssetHeader header = assetHeaders[assetName]; long position = assetPositions[assetName]; FileStream fs = GetThreadStream(); fs.Seek(position, SeekOrigin.Begin); var source = new NonClosingFileStreamReaderWrapper(fs, header.sizeInBytes); //var source = new DeflateStream(stream, CompressionMode.Decompress, true); //var output = new byte[header.uncompressedBytes]; //MemoryStream outStream = new MemoryStream(output); //source.CopyTo(outStream); switch (header.compression) { case AssetCompression.NotCompressed: return(source); break; case AssetCompression.Deflate: return(new DeflateStream(source, CompressionMode.Decompress, true)); break; case AssetCompression.Gzip: return(new GZipStream(source, CompressionMode.Decompress, true)); break; default: throw new ArgumentOutOfRangeException(); } }