private void ReadBlock(Stream stream, AseColorGroupCollection groups, Stack <AseColorEntryCollection> colorStack) { AseBlockType blockType; int blockLength; int offset; int dataLength; _BlockData block = null; blockType = (AseBlockType)stream.ReadUInt16BigEndian(); blockLength = stream.ReadUInt32BigEndian(); // store the current position of the stream, so we can calculate the offset // from bytes read to the block length in order to skip the bits we didn't // read, support or know what they are offset = (int)stream.Position; // process the actual block switch (blockType) { case AseBlockType.Color: block = this.ReadColorBlock(stream, colorStack); break; case AseBlockType.GroupStart: block = this.ReadGroupBlock(stream, groups, colorStack); break; case AseBlockType.GroupEnd: block = null; colorStack.Pop(); break; default: throw new InvalidDataException(string.Format("Unsupported block type '{0}'.", blockType)); } // load in any custom data and attach it to the // current block (if available) as raw byte data dataLength = blockLength - (int)(stream.Position - offset); if (dataLength > 0) { byte[] extraData; extraData = new byte[dataLength]; stream.Read(extraData, 0, dataLength); if (block != null) { block.ExtraData = extraData; } } }
/*--- Method: private -----------------------------------------------------------------------------------------------------------------------------------------*/ private int GetBlockLength(_BlockData block) { int blockLength; // name data (2 bytes per character + null terminator, plus 2 bytes to describe that first number ) blockLength = 2 + (((block.Name ?? string.Empty).Length + 1) * 2); if (block.ExtraData != null) { blockLength += block.ExtraData.Length; // data we can't process but keep anyway } return(blockLength); }