/// <summary> /// Processes the marshal buffer header and decompress the data if needed /// </summary> /// <exception cref="InvalidDataException">If the header is not a correct marshal header</exception> protected virtual void ProcessPacketHeader() { // check the header and ensure we use the correct stream to read from it byte header = (byte)this.mStream.ReadByte(); if (header == Specification.ZLIB_HEADER) { this.mStream.Seek(-1, SeekOrigin.Current); this.mStream = ZlibHelper.DecompressStream(this.mStream); header = (byte)this.mStream.ReadByte(); } if (header != Specification.MARSHAL_HEADER) { throw new InvalidDataException($"Expected Marshal header, but got {header}"); } // create the reader this.mReader = new BinaryReader(this.mStream); // read the save list information int saveCount = this.mReader.ReadInt32(); // check if there are elements in the save list and parse the list-map first if (saveCount > 0) { // compressed streams cannot be seek'd so ensure that the stream is decompressed first if (this.mStream is ZInputStream) { MemoryStream newStream = new MemoryStream(); this.mStream.CopyTo(newStream); this.mStream = newStream; this.mReader = new BinaryReader(this.mStream); this.mStream.Seek(0, SeekOrigin.Begin); } // reserve space for the savelist map and the actual elements in the save list this.mSavedElementsMap = new int[saveCount]; this.mSavedList = new PyDataType[saveCount]; long currentPosition = this.mStream.Position; // read at the end of the stream to get the correct index to element mapping this.mStream.Seek(-saveCount * 4, SeekOrigin.End); for (int i = 0; i < saveCount; i++) { this.mSavedElementsMap[i] = this.mReader.ReadInt32(); } // go back to where the stream was after reading the amount of saved elements this.mStream.Seek(currentPosition, SeekOrigin.Begin); } }