/// <summary> /// Builds the classes from a sets file /// </summary> public static DisplaySettings ReadSettings(FileInfo file, Map map, Monitor monitor) { Debug.Assert(file.Exists); var newReader = XDocument.Load(file.FullName); var sets = new XmlReaderSettings(); sets.IgnoreWhitespace = true; sets.CloseInput = true; using (XmlReader r = XmlReader.Create(File.Open(file.FullName, FileMode.Open, FileAccess.Read), sets)) { r.ReadStartElement(); //DateTime date = XmlConvert.ToDateTime(r.GetAttribute("Date")); // You string youString = r.GetAttribute("Name"); r.ReadStartElement(); Player ply = World.Default.GetPlayer(youString); if (ply != null) { World.Default.You = ply; } else { World.Default.You = new Player(); } map.MarkerManager.ReadDefaultMarkers(r); r.ReadEndElement(); // Monitor monitor.ReadXml(r); // MainMap r.ReadStartElement(); // MainMap: Location Point? location = World.Default.GetCoordinates(r.GetAttribute("XY")); int x = 500; int y = 500; if (location.HasValue) { x = location.Value.X; y = location.Value.Y; } int z = Convert.ToInt32(r.GetAttribute("Zoom")); var displayType = (DisplayTypes)Enum.Parse(typeof(DisplayTypes), r.GetAttribute("Display"), true); map.HomeLocation = new Location(displayType, x, y, z); // MainMap: Display r.ReadStartElement(); Color backgroundColor = XmlHelper.GetColor(r.GetAttribute("BackgroundColor")); r.ReadStartElement(); bool continentLines = Convert.ToBoolean(r.ReadElementString("LinesContinent")); bool provinceLines = Convert.ToBoolean(r.ReadElementString("LinesProvince")); bool hideAbandoned = Convert.ToBoolean(r.ReadElementString("HideAbandoned")); bool markedOnly = Convert.ToBoolean(r.ReadElementString("MarkedOnly")); r.Skip(); r.ReadEndElement(); var displaySettings = new DisplaySettings(backgroundColor, continentLines, provinceLines, hideAbandoned, markedOnly); // Views World.Default.Views = ReadViews(newReader); // MainMap: Markers r.ReadStartElement(); map.MarkerManager.ReadUserDefinedMarkers(r); // MainMap: Manipulators Dictionary<ManipulatorManagerTypes, ManipulatorManagerBase> dict = map.Manipulators.Manipulators; map.Manipulators.CleanUp(); r.ReadToFollowing("Manipulator"); while (r.IsStartElement("Manipulator")) { var manipulatorType = (ManipulatorManagerTypes)Enum.Parse(typeof(ManipulatorManagerTypes), r.GetAttribute("Type")); if (dict.ContainsKey(manipulatorType)) { if (dict[manipulatorType].UseLegacyXmlWriter) { dict[manipulatorType].ReadXml(r); } else { r.Skip(); dict[manipulatorType].ReadXml(newReader); } } else { r.Skip(); } } r.ReadEndElement(); if (r.IsStartElement("RoamingManipulators")) { r.Skip(); map.Manipulators.ReadRoamingXml(newReader); } // End Main Map r.ReadEndElement(); return displaySettings; } }
/// <summary> /// Write the settings file /// </summary> public static void WriteSettings(FileInfo file, Map map, Monitor monitor) { // There is the old and the new 'way' // old = XmlReader/Writer: error prone, difficult maintenance // new = XDocument/Linq: easier... var sets = new XmlWriterSettings(); sets.Indent = true; sets.IndentChars = " "; using (XmlWriter w = XmlWriter.Create(file.FullName, sets)) { w.WriteStartElement("Settings"); w.WriteAttributeString("Date", DateTime.Now.ToLongDateString()); w.WriteStartElement("You"); w.WriteAttributeString("Name", World.Default.You.Name); map.MarkerManager.WriteDefaultMarkers(w); w.WriteEndElement(); monitor.WriteXml(w); w.WriteStartElement("MainMap"); w.WriteStartElement("Location"); w.WriteAttributeString("Display", map.HomeLocation.Display.ToString()); w.WriteAttributeString("XY", map.HomeLocation.X + "|" + map.HomeLocation.Y); w.WriteAttributeString("Zoom", map.HomeLocation.Zoom.ToString(CultureInfo.InvariantCulture)); w.WriteEndElement(); w.WriteStartElement("Display"); w.WriteAttributeString("BackgroundColor", XmlHelper.SetColor(map.Display.Settings.BackgroundColor)); w.WriteElementString("LinesContinent", map.Display.Settings.ContinentLines.ToString()); w.WriteElementString("LinesProvince", map.Display.Settings.ProvinceLines.ToString()); w.WriteElementString("HideAbandoned", map.Display.Settings.HideAbandoned.ToString()); w.WriteElementString("MarkedOnly", map.Display.Settings.MarkedOnly.ToString()); w.WriteRaw(World.Default.Views.WriteViews()); w.WriteEndElement(); w.WriteStartElement("Markers"); map.MarkerManager.WriteUserDefinedMarkers(w); w.WriteEndElement(); // Manipulators w.WriteStartElement("Manipulators"); foreach (KeyValuePair<ManipulatorManagerTypes, ManipulatorManagerBase> pair in map.Manipulators.Manipulators) { w.WriteStartElement("Manipulator"); w.WriteAttributeString("Type", pair.Key.ToString()); if (pair.Value.UseLegacyXmlWriter) { pair.Value.WriteXml(w); } else { string resultingXml = pair.Value.WriteXml(); w.WriteRaw(resultingXml); } w.WriteEndElement(); } w.WriteEndElement(); w.WriteStartElement("RoamingManipulators"); w.WriteRaw(map.Manipulators.GetRoamingXml()); w.WriteEndElement(); // end MainMap w.WriteEndElement(); // end Settings w.WriteEndElement(); } }