private static MapElement ReadElement(BinaryReader reader, MapElement parent, string[] lookupTable) { MapElement element = new MapElement(); element.Name = lookupTable[reader.ReadInt16()]; element.Parent = parent; int attributes = reader.ReadByte(); for (int i = 0; i < attributes; i++) { string key = lookupTable[reader.ReadInt16()]; byte type = reader.ReadByte(); object value = null; if (type == 0) { value = reader.ReadBoolean(); } else if (type == 1) { value = (int)reader.ReadByte(); } else if (type == 2) { value = (int)reader.ReadInt16(); } else if (type == 3) { value = reader.ReadInt32(); } else if (type == 4) { value = reader.ReadSingle(); } else if (type == 5) { value = lookupTable[reader.ReadInt16()]; } else if (type == 6) { value = reader.ReadString(); } else if (type == 7) { int count = reader.ReadInt16(); value = RunLengthEncoding.Decode(reader.ReadBytes(count)); } element.Attributes.Add(key, value); } int elements = reader.ReadInt16(); for (int j = 0; j < elements; j++) { element.Children.Add(ReadElement(reader, element, lookupTable)); } return(element); }
private static void WriteElement(Dictionary <string, short> ids, BinaryWriter writer, XmlElement element) { int children = 0; for (int i = element.ChildNodes.Count - 1; i >= 0; i--) { if (element.ChildNodes[i] is XmlElement) { children++; } } int attributes = 0; for (int i = element.Attributes.Count - 1; i >= 0; i--) { if (!IgnoreAttributes.Contains(element.Attributes[i].Name)) { attributes++; } } if (element.InnerText.Length > 0 && children == 0) { attributes++; } writer.Write(ids[element.Name]); writer.Write((byte)attributes); int count = element.Attributes.Count; for (int i = 0; i < count; i++) { XmlAttribute attribute = element.Attributes[i]; if (IgnoreAttributes.Contains(attribute.Name)) { continue; } ParseValue(attribute.Value, out byte type, out object result); writer.Write(ids[attribute.Name]); writer.Write(type); switch (type) { case 0: writer.Write((bool)result); break; case 1: writer.Write((byte)result); break; case 2: writer.Write((short)result); break; case 3: writer.Write((int)result); break; case 4: writer.Write((float)result); break; case 5: writer.Write(ids[(string)result]); break; } } if (element.InnerText.Length > 0 && children == 0) { writer.Write(ids[InnerTextAttributeName]); if (element.Name == "solids" || element.Name == "bg") { byte[] encoded = RunLengthEncoding.Encode(element.InnerText); writer.Write((byte)7); writer.Write((short)encoded.Length); writer.Write(encoded); } else { writer.Write((byte)6); writer.Write(element.InnerText); } } writer.Write((short)children); children = element.ChildNodes.Count; for (int i = 0; i < children; i++) { XmlElement node = element.ChildNodes[i] as XmlElement; if (node != null) { WriteElement(ids, writer, node); } } }