/// <summary> /// Async, load all of the data for this project. /// If it doesn't exist then we will create all of the default data. /// </summary> private bool Load(ref string error) { if (Path.IsPathRooted(Name)) { _DirectoryLocation = Path.GetDirectoryName(Name); } else { _DirectoryLocation = Path.Combine(_Configuration.ProjectDirectory, Name); } if (_DirectoryLocation == null) { error = "Invalid directory path!"; return(false); } _IsLoaded = false; string fileLocation = Path.Combine(_DirectoryLocation, "Project.xml"); if (RemoteProject) { _IsLoaded = true; return(true); } _ProjectModelSystems.Clear(); if (!Directory.Exists(_DirectoryLocation) || !File.Exists(fileLocation)) { _IsLoaded = true; return(true); } try { XmlDocument doc = new XmlDocument(); doc.Load(fileLocation); XmlNode rootNode = doc["Root"]; if (rootNode != null) { var description = rootNode.Attributes?["Description"]; if (description != null) { Description = description.InnerText; } var rootChildren = rootNode.ChildNodes; var toLoad = new ProjectModelSystem[rootChildren.Count]; Parallel.For(0, rootChildren.Count, i => { XmlNode child = rootChildren[i]; // check for the 3.0 file name switch (child.Name) { case "AdvancedModelSystem": { if (LoadAdvancedModelSystem(child, i, Guid.NewGuid().ToString(), out var pms)) { toLoad[i] = pms; } } break; case "DetachedModelSystem": { var guid = child.Attributes["GUID"]?.InnerText ?? string.Empty; if (LoadDetachedModelSystem(_DirectoryLocation, guid, out var pms)) { toLoad[i] = pms; } } break; } }); var validToLoad = toLoad.Select(load => load).Where(load => load != null).ToList(); _ProjectModelSystems.AddRange(validToLoad); _IsLoaded = true; return(true); } } catch (Exception e) { error = String.Concat(e.InnerException?.Message ?? e.Message, "\r\n", e.InnerException?.StackTrace ?? e.StackTrace); Console.WriteLine(e.Message + "\r\n" + e.StackTrace); } return(false); }
/// <summary> /// /// </summary> /// <param name="child"></param> /// <param name="index"></param> /// <param name="guid"></param> /// <param name="pms"></param> /// <returns></returns> private bool LoadAdvancedModelSystem(XmlNode child, int index, string guid, out ProjectModelSystem pms) { pms = new ProjectModelSystem() { GUID = guid }; bool hasDescription = false; var attributes = child.Attributes; if (attributes != null) { foreach (XmlAttribute attribute in attributes) { if (attribute.Name == "Description") { hasDescription = true; pms.Description = attribute.InnerText; break; } } } if (!hasDescription) { pms.Description = "No Description"; } if (child.HasChildNodes) { for (int i = 0; i < child.ChildNodes.Count; i++) { switch (child.ChildNodes[i].Name) { case "ModelSystem": { if (pms.Root == null) { if (child.ChildNodes[i].FirstChild != null) { ModelSystemStructure ms = XTMF.ModelSystemStructure.Load(child.ChildNodes[i], _Configuration); if (ms != null) { pms.Root = ms; } } } } break; } } if (pms.Root == null) { return(false); } // now do a second pass for Linked parameters, since we need the current model system to actually link things for (int i = 0; i < child.ChildNodes.Count; i++) { switch (child.ChildNodes[i].Name) { case "LastModified": { var result = DateTime.TryParse(child.ChildNodes[i].Attributes?["Time"]?.InnerText, out var modified); pms.LastModified = result ? modified : DateTime.Now; break; } case "LinkedParameters": { pms.LinkedParameters = LoadLinkedParameters(child.ChildNodes[i], pms.Root); break; } case "Regions": { pms.RegionDisplays = LoadRegionDisplays(child.ChildNodes[i], pms.Root); } break; } } } return(true); }
/// <summary> /// /// </summary> /// <param name="directory"></param> /// <param name="guid"></param> /// <param name="pms"></param> /// <returns></returns> private bool LoadDetachedModelSystem(string directory, string guid, out ProjectModelSystem pms) { var msPath = Path.Combine(_DirectoryLocation, "._ModelSystems", $"Project.ms-{guid}.xml"); if (!File.Exists(msPath)) { msPath = Path.Combine(_DirectoryLocation, $"Project.ms-{guid}.xml"); if (!File.Exists(msPath)) { pms = null; return(false); } else { var newMsPath = Path.Combine(EnsureModelSystemDirectoryExists(_DirectoryLocation), $"Project.ms-{guid}.xml"); File.Move(msPath, newMsPath); msPath = newMsPath; } } XmlDocument msDoc = new XmlDocument(); msDoc.Load(msPath); pms = new ProjectModelSystem() { GUID = guid }; var child = msDoc["Root"] ?? msDoc["AdvancedModelSystem"]; bool hasDescription = false; var attributes = child.Attributes; if (attributes != null) { foreach (XmlAttribute attribute in attributes) { if (attribute.Name == "Description") { hasDescription = true; pms.Description = attribute.InnerText; break; } } } if (child.HasChildNodes) { ModelSystemStructure ms = XTMF.ModelSystemStructure.Load(child, _Configuration); if (ms != null) { pms.Root = ms; } } if (pms.Root == null) { return(false); } if (!hasDescription) { pms.Description = pms.Root.Description; } // now do a second pass for Linked parameters, since we need the current model system to actually link things for (int i = 0; i < child.ChildNodes.Count; i++) { switch (child.ChildNodes[i].Name) { case "LastModified": { var result = DateTime.TryParse(child.ChildNodes[i].Attributes?["Time"]?.InnerText, out var modified); pms.LastModified = result ? modified : DateTime.Now; break; } case "LinkedParameters": { pms.LinkedParameters = LoadLinkedParameters(child.ChildNodes[i], pms.Root); break; } case "Regions": { pms.RegionDisplays = LoadRegionDisplays(child.ChildNodes[i], pms.Root); } break; } } return(true); }