static InfocardTextNode CopyAttributes(InfocardTextNode src) { return(new InfocardTextNode() { Bold = src.Bold, Italic = src.Italic, Underline = src.Underline, FontIndex = src.FontIndex, Color = src.Color, Alignment = src.Alignment }); }
static void ParseTextRenderAttributes(Dictionary <string, string> attrs, InfocardTextNode node) { uint data = 0; uint mask = 0; uint def = 0; string temp; if (attrs.TryGetValue("DATA", out temp)) { data = (uint)int.Parse(temp); } if (attrs.TryGetValue("MASK", out temp)) { mask = (uint)int.Parse(temp); } if (attrs.TryGetValue("DEF", out temp)) { def = (uint)int.Parse(temp); } if (attrs.TryGetValue("COLOR", out temp)) { mask |= TRA_color; if (temp.ToUpperInvariant() == "DEFAULT") { def |= TRA_color; } else { data &= ~TRA_color; data |= GetColor(temp); } } if (attrs.TryGetValue("FONT", out temp)) { mask |= TRA_font; if (temp.ToUpperInvariant() == "DEFAULT") { def |= TRA_font; } else { data &= ~TRA_font; data |= (uint.Parse(temp) - 1) << 3; } } if (attrs.TryGetValue("BOLD", out temp)) { mask |= TRA_bold; if (temp.ToUpperInvariant() == "DEFAULT") { def |= TRA_bold; } else if (temp.ToUpperInvariant() == "TRUE") { data |= TRA_bold; } else { data &= ~TRA_bold; } } if (attrs.TryGetValue("ITALIC", out temp)) { mask |= TRA_italic; if (temp.ToUpperInvariant() == "DEFAULT") { def |= TRA_italic; } else if (temp.ToUpperInvariant() == "TRUE") { data |= TRA_italic; } else { data &= ~TRA_italic; } } if (attrs.TryGetValue("UNDERLINE", out temp)) { mask |= TRA_underline; if (temp.ToUpperInvariant() == "DEFAULT") { def |= TRA_underline; } else if (temp.ToUpperInvariant() == "TRUE") { data |= TRA_underline; } else { data &= ~TRA_underline; } } if ((def & TRA_bold) != 0) { node.Bold = false; } else if ((mask & TRA_bold) != 0) { node.Bold = (data & TRA_bold) != 0; } if ((def & TRA_italic) != 0) { node.Italic = false; } else if ((mask & TRA_italic) != 0) { node.Italic = (data & TRA_italic) != 0; } if ((def & TRA_font) != 0) { node.FontIndex = 0; } else if ((data & TRA_font) != 0) { node.FontIndex = (int)(data & TRA_font); } if ((def & TRA_underline) != 0) { node.Underline = false; } else if ((data & TRA_underline) != 0) { node.Underline = (data & TRA_underline) != 0; } if ((def & TRA_color) != 0) { node.Color = Color4.White; } else if ((mask & TRA_color) != 0) { var bytes = BitConverter.GetBytes((data & TRA_color)); node.Color = new Color4((float)bytes[1] / 255f, (float)bytes[2] / 255f, (float)bytes[3] / 255f, 1f); } }
//Main Parsing public static Infocard Parse(string input) { var nodes = new List <InfocardNode>(); var current = new InfocardTextNode(); using (var reader = XmlReader.Create(new StringReader(input))) { while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: string elemname = reader.Name.ToUpperInvariant(); if (SkipElements.Contains(elemname)) { continue; } Dictionary <string, string> attrs = null; if (reader.HasAttributes) { attrs = new Dictionary <string, string>(); for (int attInd = 0; attInd < reader.AttributeCount; attInd++) { reader.MoveToAttribute(attInd); attrs.Add(reader.Name.ToUpper(), reader.Value); } reader.MoveToElement(); } switch (elemname) { case "PARA": nodes.Add(new InfocardParagraphNode()); break; case "JUST": TextAlignment v; if (Aligns.TryGetValue(attrs["LOC"].ToUpperInvariant(), out v)) { current.Alignment = v; } break; case "TRA": ParseTextRenderAttributes(attrs, current); break; case "TEXT": break; default: throw new Exception("Unexpected element " + elemname); } break; case XmlNodeType.Text: current.Contents = reader.Value; nodes.Add(current); current = CopyAttributes(current); break; } } } return(new Infocard() { Nodes = nodes }); }