private ContentReader GetContentReaderFromXnb(string originalAssetName, Stream stream, BinaryReader xnbReader, Action <IDisposable> recordDisposableObject) { // The first 4 bytes should be the "XNB" header. i use that to detect an invalid file byte x = xnbReader.ReadByte(); byte n = xnbReader.ReadByte(); byte b = xnbReader.ReadByte(); byte platform = xnbReader.ReadByte(); if (x != 'X' || n != 'N' || b != 'B' || !(targetPlatformIdentifiers.Contains((char)platform))) { throw new ContentLoadException("Asset does not appear to be a valid XNB file. Did you process your content for Windows?"); } byte version = xnbReader.ReadByte(); byte flags = xnbReader.ReadByte(); bool compressedLzx = (flags & ContentCompressedLzx) != 0; bool compressedLz4 = (flags & ContentCompressedLz4) != 0; if (version != 5 && version != 4) { throw new ContentLoadException("Invalid XNB version"); } // The next int32 is the length of the XNB file int xnbLength = xnbReader.ReadInt32(); Stream decompressedStream = null; #if WEB decompressedStream = stream; #else if (compressedLzx || compressedLz4) { // Decompress the xnb int decompressedSize = xnbReader.ReadInt32(); if (compressedLzx) { int compressedSize = xnbLength - 14; decompressedStream = new LzxDecoderStream(stream, decompressedSize, compressedSize); } else if (compressedLz4) { decompressedStream = new Lz4DecoderStream(stream); } } else { decompressedStream = stream; } #endif var reader = new ContentReader(this, decompressedStream, this.graphicsDeviceService.GraphicsDevice, originalAssetName, version, recordDisposableObject); return(reader); }
public static ContentReaderShim GetContentReaderFromXnb(string originalAssetName, Stream stream, BinaryReader xnbReader) { // The first 4 bytes should be the "XNB" header. i use that to detect an invalid file byte x = xnbReader.ReadByte(); byte n = xnbReader.ReadByte(); byte b = xnbReader.ReadByte(); byte platform = xnbReader.ReadByte(); if (x != 'X' || n != 'N' || b != 'B' || !(targetPlatformIdentifiers.Contains((char)platform))) { throw new ContentLoadException("Asset does not appear to be a valid XNB file. Did you process your content for Windows?"); } byte version = xnbReader.ReadByte(); byte flags = xnbReader.ReadByte(); bool compressedLzx = (flags & ContentCompressedLzx) != 0; bool compressedLz4 = (flags & ContentCompressedLz4) != 0; if (version != 6 && version != 5) { throw new ContentLoadException("Invalid XNB version"); } // The next int32 is the length of the XNB file int xnbLength = xnbReader.ReadInt32(); Stream decompressedStream = null; if (compressedLzx || compressedLz4) { // Decompress the xnb int decompressedSize = xnbReader.ReadInt32(); if (compressedLzx) { int compressedSize = xnbLength - 14; decompressedStream = new LzxDecoderStream(stream, decompressedSize, compressedSize); } else if (compressedLz4) { decompressedStream = new Lz4DecoderStream(stream); } } else { decompressedStream = stream; } var reader = new ContentReaderShim(decompressedStream, version); return(reader); }
private XnbStreamReader DecodeXnbStreamReader(XnbStreamReader endcodedXnbStreamReader, XnbFlags flags, int size) { // check if file size is equal to stream size if (endcodedXnbStreamReader.BaseStream.Length != size) { throw new XnbException("XNB file has been truncated!"); } // check if stream is compressed if (!flags.HasFlag(XnbFlags.ContentCompressedLzx)) { return(endcodedXnbStreamReader); } var decompressedSize = (int)endcodedXnbStreamReader.ReadUInt32(); var compressedSize = size - XnbCompressedPrologueSize; var decompressedStream = new LzxDecoderStream(endcodedXnbStreamReader.BaseStream, decompressedSize, compressedSize); return(new XnbStreamReader(decompressedStream)); }