private static TileInfo Create(XmlReader reader, TileInfo tileInfo) { var name = reader["name"]; tileInfo.Name = name is null ? tileInfo.Name : name; var color = reader["color"]; if (color != null) { tileInfo.Color = Color24.Parse(color); } tileInfo.IsTransparent = ReadBoolean(reader, "letLight", tileInfo.IsTransparent); tileInfo.IsSolid = ReadBoolean(reader, "solid", tileInfo.IsSolid); tileInfo.HasExtra = ReadBoolean(reader, "hasExtra", tileInfo.HasExtra); tileInfo.IsGrass = ReadBoolean(reader, "isGrass", tileInfo.IsGrass); tileInfo.IsStone = ReadBoolean(reader, "isStone", tileInfo.IsStone); tileInfo.CanMerge = ReadBoolean(reader, "merge", tileInfo.CanMerge); tileInfo.Light = ReadDouble(reader, "light", tileInfo.Light); tileInfo.LightR = ReadDouble(reader, "lightr", tileInfo.LightR); tileInfo.LightG = ReadDouble(reader, "lightg", tileInfo.LightG); tileInfo.LightB = ReadDouble(reader, "lightb", tileInfo.LightB); tileInfo.TileId = ReadInt16(reader, "num", tileInfo.TileId); tileInfo.U = ReadInt16(reader, "u", tileInfo.U); tileInfo.V = ReadInt16(reader, "v", tileInfo.V); tileInfo.MinU = ReadInt16(reader, "minu", tileInfo.MinU); tileInfo.MinV = ReadInt16(reader, "minv", tileInfo.MinV); tileInfo.MaxU = ReadInt16(reader, "maxu", tileInfo.MaxU); tileInfo.MaxV = ReadInt16(reader, "maxv", tileInfo.MaxV); if (!reader.IsEmptyElement) { var builder = ImmutableArray.CreateBuilder <TileInfo>(); while (reader.Read() && reader.NodeType != XmlNodeType.EndElement) { if (reader.Name != "var") { throw new InvalidOperationException($"Expected 'var'. Actual '{reader.Name}'."); } var clone = tileInfo.Clone(); var variant = Create(reader, clone); builder.Add(variant); } tileInfo.Variants = builder.ToImmutable(); } return(tileInfo); }
public static WorldInfo FromWorldInfoData(string xml) { using var stringReader = new StringReader(xml); var settings = new XmlReaderSettings { IgnoreWhitespace = true, IgnoreComments = true }; using var xmlReader = XmlReader.Create(stringReader, settings); var tileInfoById = new List <KeyValuePair <int, TileInfo> >(); var itemNameById = new List <KeyValuePair <int, string> >(); var npcById = new List <KeyValuePair <int, string> >(); var prefixById = new List <KeyValuePair <int, string> >(); var wallInfoById = new List <KeyValuePair <int, WallInfo> >(); var global = new Dictionary <string, Color24>(); while (xmlReader.Read()) { if (xmlReader.NodeType == XmlNodeType.Element) { switch (xmlReader.Name) { case "tile": { var tileInfo = TileInfo.Create(xmlReader); tileInfoById.Add(KeyValuePair.Create((int)tileInfo.TileId, tileInfo)); break; } case "item": { var itemId = int.Parse(xmlReader["num"]); var itemName = xmlReader["name"]; itemNameById.Add(KeyValuePair.Create(itemId, itemName)); break; } case "Npc": { var npcId = int.Parse(xmlReader["Id"]); var npcName = xmlReader["Name"]; npcById.Add(KeyValuePair.Create(npcId, npcName)); break; } case "prefix": { var prefixId = int.Parse(xmlReader["num"]); var prefixName = xmlReader["name"]; prefixById.Add(KeyValuePair.Create(prefixId, prefixName)); break; } case "wall": { var color = xmlReader["color"]; var blend = xmlReader["blend"]; var wallInfo = new WallInfo( int.Parse(xmlReader["num"]), xmlReader["name"], color is null ? default(Color24?) : Color24.Parse(color), blend is null ? -1 : int.Parse(blend)); wallInfoById.Add(KeyValuePair.Create(wallInfo.WallId, wallInfo)); break; } case "global": { global.Add( xmlReader["id"], Color24.Parse(xmlReader["color"])); break; } } } } var worldInfo = new WorldInfo { TileInfoById = tileInfoById.ToImmutableDictionary(), ItemNameById = itemNameById.ToImmutableDictionary(), NpcById = npcById.ToImmutableDictionary(), PrefixById = prefixById.ToImmutableDictionary(), WallInfoById = wallInfoById.ToImmutableDictionary(), Global = global.ToImmutableDictionary(), SkyColor = global["sky"], EarthColor = global["earth"], RockColor = global["rock"], HellColor = global["hell"], WaterColor = global["water"], LavaColor = global["lava"], HoneyColor = global["honey"] }; return(worldInfo); }