public BinaryFile(byte[] contents) { using (MemoryStream file = new MemoryStream(contents)) using (BinaryReader inStream = new BinaryReader(file)) { Headers = new BinaryChunkHEAD(inStream); Instances = new ClassDescriptor[Headers.NumInstances]; // Begin reading the file chunks. bool firstChunk = true; Chunks = new List <BinaryChunk>(); while (true) { try { BinaryChunk chunk = new BinaryChunk(inStream); Chunks.Add(chunk); if (chunk.ChunkType == BinaryChunkType.INST) { BinaryChunkINST inst = new BinaryChunkINST(chunk); INSTs.Add(inst); } else if (chunk.ChunkType == BinaryChunkType.PROP) { BinaryChunkPROP prop = new BinaryChunkPROP(chunk); PROPs.Add(prop); } else if (chunk.ChunkType == BinaryChunkType.PRNT) { ParentLinks = new BinaryChunkPRNT(chunk); } else if (chunk.ChunkType == BinaryChunkType.META) { if (firstChunk) { Metadata = new BinaryChunkMETA(chunk); } else { throw new Exception("Unexpected metadata chunk"); } } else if (chunk.ChunkType == BinaryChunkType.END) { break; } firstChunk = false; } catch (EndOfStreamException) { throw new Exception("Unexpected end of file!"); } } foreach (BinaryChunkINST inst in INSTs) { foreach (int id in inst.InstanceIds) { ClassDescriptor bind = new ClassDescriptor(); bind.ClassName = inst.TypeName; Instances[id] = bind; } } foreach (BinaryChunkPROP prop in PROPs) { BinaryChunkINST inst = INSTs[prop.Index]; prop.ReadPropertyValues(inst, Instances); } for (int i = 0; i < ParentLinks.LinkCount; i++) { int objectId = ParentLinks.ObjectIds[i]; int parentId = ParentLinks.ParentIds[i]; if (parentId >= 0) { Instances[objectId].Parent = Instances[parentId]; } else { TreeRoot.Add(Instances[objectId]); } } } }