public void Load(Stream stream) { Stack <ColorEntryCollection> colors; ColorGroupCollection groups; ColorEntryCollection globalColors; groups = new ColorGroupCollection(); globalColors = new ColorEntryCollection(); colors = new Stack <ColorEntryCollection>(); // add the global collection to the bottom of the stack to handle color blocks outside of a group colors.Push(globalColors); int blockCount; this.ReadAndValidateVersion(stream); blockCount = stream.ReadUInt32BigEndian(); for (int i = 0; i < blockCount; i++) { this.ReadBlock(stream, groups, colors); } this.Groups = groups; this.Colors = globalColors; }
private void ReadBlock(Stream stream, ColorGroupCollection groups, Stack <ColorEntryCollection> colorStack) { BlockType blockType; int blockLength; int offset; int dataLength; Block block; blockType = (BlockType)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 BlockType.Color: block = this.ReadColorBlock(stream, colorStack); break; case BlockType.GroupStart: block = this.ReadGroupBlock(stream, groups, colorStack); break; case BlockType.GroupEnd: block = null; colorStack.Pop(); break; default: throw new InvalidDataException($"Unsupported block type '{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; } } }
private Block ReadGroupBlock(Stream stream, ColorGroupCollection groups, Stack <ColorEntryCollection> colorStack) { ColorGroup block; string name; // read the name of the group name = stream.ReadStringBigEndian(); // create the group and add it to the results set block = new ColorGroup { Name = name }; groups.Add(block); // add the group color collection to the stack, so when subsequent color blocks // are read, they will be added to the correct collection colorStack.Push(block.Colors); return(block); }