/// <summary> /// Loads the star systems defined in the map. /// </summary> /// <param name="starSystems">The list of star systems.</param> /// <param name="streamProvider">Provider of map data streams.</param> /// <param name="map">The map.</param> private void LoadStarSystems(IList <string> starSystems, IGalaxyMapDataStreamProvider streamProvider, GalaxyMap map) { StarSystemLoader loader = new StarSystemLoader(); StarSystem starSystem; foreach (string starSystemName in starSystems) { starSystem = loader.LoadStarSystem(starSystemName, streamProvider); if (map.ContainsKey(starSystem.Name)) { throw new XmlException("Duplicate star system name in starsystems"); } else { map.Add(starSystem); } } }
/// <summary> /// Metoda pro nacteni souboru s galaxii, jeho zpracovani a pak ulozeni do pameti /// </summary> public static void LoadGalaxyFile() { prepareDataFolder(); //Log("dialog"); OpenFileDialog dlg = new OpenFileDialog(); dlg.DefaultExt = ".xml"; dlg.Filter = "GalaxyMap XML File(.xml)|*.xml"; Nullable <bool> result = dlg.ShowDialog(); if (result == true) { XmlReaderSettings readerSettings = new XmlReaderSettings(); readerSettings.IgnoreComments = true; // Open document string filename = dlg.FileName; FileStream fs = new FileStream(filename, FileMode.Open); //Editor.Log(dlg.InitialDirectory); GalaxyMap map = new GalaxyMap(); List <string> starSystemNamesList = new List <string> { }; List <string> loadList = new List <string> { }; using (XmlReader reader = XmlReader.Create(fs, readerSettings)) { XmlDocument doc = new XmlDocument(); doc.Load(reader); XmlNode galaxyNode = doc.GetElementsByTagName("galaxy")[0]; if (galaxyNode == null) { throw new FileFormatException("This file doesn't contain galaxy map!"); } map.MapName = galaxyNode.Attributes["name"].Value.ToString(); foreach (XmlNode childNode in galaxyNode.ChildNodes) { switch (childNode.Name.ToLowerInvariant()) { case "starsystems": foreach (XmlNode starSystemNode in childNode.ChildNodes) { string starSystemFileName = starSystemNode.Attributes["name"].Value.ToString(); starSystemNamesList.Add(starSystemFileName); //Editor.Log(starSystemFileName); if (File.Exists(dlg.FileName.Replace(dlg.SafeFileName, "") + Path.DirectorySeparatorChar + starSystemFileName + ".xml")) { Editor.Log("parsing"); loadList.Add(starSystemFileName); } else { StarSystem starSystem = new StarSystem(); starSystem.Name = starSystemFileName; Star star = new Star(); star.Name = starSystemFileName; starSystem.Star = star; map.Add(starSystem); } } break; case "wormholes": //TODO: Implementation //connections = childNode.ParseWormholes(); break; default: throw new XmlException("Unexpected childNode."); } } } fs.Close(); foreach (String starSystemName in loadList) { String starSystemPath = (dlg.FileName.Replace(dlg.SafeFileName, "") + starSystemName + ".xml"); map.Add(LoadStarSystem(starSystemPath, readerSettings)); } Editor.GalaxyMap = map; } }