/// <summary> /// I have to compromise against my old shit code /// </summary> /// <returns>The imported basin.</returns> public ImportResult Import() { // Implement later try { ImportResult IR = new ImportResult(); OpenFileDialog OFD = new OpenFileDialog(); OFD.Title = $"Import from {GetName()}"; OFD.Filter = "Track Maker Project files|*.tproj2"; OFD.ShowDialog(); if (OFD.FileName == "") { IR.Status = ExportResults.Cancelled; return(IR); } else { XMLImportResult__DEPRECATED XER = ImportCore(OFD.FileName); if (XER.Successful && !XER.Cancelled) { IR.Status = ExportResults.OK; IR.Project = XER.Project; return(IR); } else { IR.Status = ExportResults.Error; return(IR); } } } catch (XmlException) { Error.Throw("Invalid basin!", "One of the basins in this project file is corrupted!", ErrorSeverity.Error, 182); return(null); } }
/// <summary> /// Imports from Tproj2. /// /// This is a disaster and terrible. It shall soon be rewritten using the "magic" of XML schemas and serialisation... /// </summary> /// <param name="FileName"></param> /// <returns></returns> public XMLImportResult__DEPRECATED ImportCore(string FileName) { XMLImportResult__DEPRECATED XER = new XMLImportResult__DEPRECATED(); XER.Successful = false; XmlDocument XD = new XmlDocument(); XD.Load(FileName); XmlNode XDR = XD.FirstChild; Project Proj = new Project(); Proj.FileName = FileName; Logging.Log("ALERT: Running complete disaster known as TProjv2 export code, stand by for any and all possible errors up to and including complete and utter obliteration of program state..."); #if DANO Proj.Basins = GlobalState.LoadedBasins; #else MainWindow MnWindow = (MainWindow)Application.Current.MainWindow; //Proj.Basins = MnWindow.CurrentProject.Basins; #endif if (!XDR.HasChildNodes) { return(XER); } List <Layer> Layers = new List <Layer>(); foreach (XmlNode XDRA in XDR.ChildNodes) { switch (XDRA.Name) { case "Metadata": // version check XmlNodeList MetadataNodes = XDRA.ChildNodes; // 2.1 was the earliest TProj2 format version that worked, although it was 2.2c? / 2.3 for import int FileMajorFormatVersion = 2; int FileMinorFormatVersion = 1; foreach (XmlNode MetadataNode in MetadataNodes) { switch (MetadataNode.Name) { case "FormatVersionMajor": FileMajorFormatVersion = Convert.ToInt32(MetadataNode.InnerText); continue; case "FormatVersionMinor": FileMinorFormatVersion = Convert.ToInt32(MetadataNode.InnerText); continue; } } string VersionString = $"{FileMajorFormatVersion}.{FileMinorFormatVersion}"; string CurrentVersionString = $"{FormatVersionMajor}.{FormatVersionMinor}"; // when new code is ready move this // switch statement? all of this is temporary if (FileMajorFormatVersion < FormatVersionMajor) { // Major versions break compatibility Error.Throw("Error!", $"This file uses version {VersionString}; the current version is {CurrentVersionString}. This project file is not compatible with this version of the Track Maker.", ErrorSeverity.Error, 405); XER.Successful = false; return(XER); } else if (FileMajorFormatVersion > FormatVersionMajor) { Error.Throw("Error!", $"This file uses version {VersionString}; the current version is {CurrentVersionString}. This project file was created in a future version of the Track Maker and is not compatible with this version. You may wish to update.", ErrorSeverity.Error, 405); XER.Successful = false; return(XER); } continue; case "Basins": // Check that there are not no valid basins. if (!XDRA.HasChildNodes) { Error.Throw("No valid basins!", "There are no valid basins!", ErrorSeverity.Error, 181); return(XER); } XmlNodeList XDRBList = XDRA.ChildNodes; foreach (XmlNode XDRACB in XDRBList) { // Create a new basin. Basin Bas = new Basin(); if (!XDRACB.HasChildNodes) { Error.Throw("Invalid basin!", "One of the basins in this Proj2 file is corrupted!", ErrorSeverity.Error, 121); return(XER); } else { XmlNodeList XDRAList = XDRACB.ChildNodes; // Iterate through the child nodes of the basin. foreach (XmlNode XDRAC in XDRAList) { switch (XDRAC.Name) { case "Name": // The name of this basin. Triggers a GlobalState load. Bas = Proj.GetBasinWithName(XDRAC.InnerText); continue; case "UserTag": // The user-given name of this basin Bas.UserTag = XDRAC.InnerText; continue; case "IsOpen": // Not sure if I'll use this Bas.IsOpen = Convert.ToBoolean(XDRAC.InnerText); continue; case "IsSelected": // Not sure if I'll use this Bas.IsSelected = Convert.ToBoolean(XDRAC.InnerText); continue; case "Layers": // Detect if somehow an invalid layer was created if (!XDRAC.HasChildNodes) { Error.Throw("Invalid basin!", "There are no layers!", ErrorSeverity.Error, 122); return(XER); } else { // Iterate through the layers XmlNodeList XDRACList = XDRAC.ChildNodes; foreach (XmlNode XDRACL in XDRACList) { switch (XDRACL.Name) { case "Layer": Layer Lyr = new Layer(); if (!XDRACL.HasChildNodes) { Error.Throw("Invalid basin!", "Empty layer detected!", ErrorSeverity.Error, 123); return(XER); } else { XmlNodeList XDRACLList = XDRACL.ChildNodes; // Yeah foreach (XmlNode XDRACLL in XDRACLList) { switch (XDRACLL.Name) { case "GUID": // GUID of this basin Lyr.LayerId = Guid.Parse(XDRACLL.ChildNodes[0].InnerText); continue; case "Name": // Name of this basin Lyr.Name = XDRACLL.InnerText; continue; case "Storms": // Storms of this basin if (!XDRACLL.HasChildNodes) { continue; // empty layer } else { // Find each storm node XmlNodeList XDRACLLSList = XDRACLL.ChildNodes; foreach (XmlNode XDRACLLS in XDRACLLSList) { Storm Sto = new Storm(); switch (XDRACLLS.Name) { case "Storm": if (!XDRACLLS.HasChildNodes) { Error.Throw("Invalid basin!", "Empty layer detected!", ErrorSeverity.Error, 186); return(XER); } else { XmlNodeList XDRACLLSSList = XDRACLLS.ChildNodes; foreach (XmlNode XDRACLLSS in XDRACLLSSList) { switch (XDRACLLSS.Name) { case "FormationDate": // The formation date of this system. Sto.FormationDate = DateTime.Parse(XDRACLLSS.ChildNodes[0].InnerText); continue; case "ID": // The ID of this system. Sto.Id = Convert.ToInt32(XDRACLLSS.ChildNodes[0].InnerText); continue; case "Name": // Name of this system. Sto.Name = XDRACLLSS.ChildNodes[0].InnerText; continue; case "Nodes": // this code is bad and will be entirely scrapped in version 2.1, fixing it in v551 if (!XDRACLLSS.HasChildNodes) { continue; } else { NodeImportResult NIR = ImportNodes(XDRACLLSS); if (NIR.Successful && !NIR.Empty) { Sto.NodeList = NIR.Nodes; } } continue; case "DeletedNodes": NodeImportResult DNIR = ImportNodes(XDRACLLSS); if (DNIR.Successful && !DNIR.Empty) { Sto.NodeList_Deleted = DNIR.Nodes; } continue; } } } // Get the storm nodes Lyr.AddStorm(Sto); continue; } } } continue; } } } if (Lyr.Name == null) { Error.Throw("Invalid basin!", "Layer with no name!", ErrorSeverity.Error, 125); return(XER); } else { Layers.Add(Lyr); } continue; } continue; } } continue; } continue; } } List <string> IXmlParse = XDRA.InnerXml.InnerXml_Parse(); // this is a complete hack, // in version 2.1 this code will be thrown out and replaced with xml deserialisation as this is genuinely terrible string BasinName = null; for (int i = 0; i < IXmlParse.Count; i++) { string IXmlParseString = IXmlParse[i]; IXmlParseString = IXmlParseString.Xaml2Cs(); // this should in all reasonable circumstances hit the storm first. if (IXmlParseString == "Name") { // if we are one element before the end of the list, name is empty if (i - IXmlParse.Count == 1) { Error.Throw("Fatal Error!", "Invalid Proj file - no name specified for basin.", ErrorSeverity.Error, 180); return(XER); } else { // also convert the next element string PlusOne = IXmlParse[i + 1].Xaml2Cs(); BasinName = PlusOne; break; // avoid multiple hits } } } if (BasinName != null) { Basin NewBasin = Proj.GetBasinWithName(BasinName); // really dumb hack foreach (Layer Lyrs in Layers) { NewBasin.AddLayer(Lyrs); } Proj.AddBasin(NewBasin, true); Proj.SelectedBasin = NewBasin; } } continue; } } XER.Successful = true; XER.Project = Proj; return(XER); }