Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
        // pre-globalstate...refactor this in Dano to not have that passed to it
        // this code is terrible
        public Project ImportCore(StormTypeManager ST2M, string FolderName)
        {
            // this is one of the worst f*****g file formats I have ever laid my f*****g eyes on, NOAA are a bunch of f*****g wanker twats, nobody should use this pile of crap

            string[] Storms = Directory.GetFiles(FolderName);

            // this is terrible design
            Project Proj = new Project();

            Proj.FileName = $"{FolderName}/*.*";
            Basin Bas = new Basin();

            // this is still a mess for now
            // not foreach as we can use it for setting up the basin
            for (int i = 0; i < Storms.Count(); i++)
            {
                string StormFileName = Storms[i];

                string[] ATCFLines = File.ReadAllLines(StormFileName);

                // holy f*****g shit i hate the ATCF format so f*****g muc
                Layer Lyr = new Layer();

                Storm Sto = new Storm();

                StormType2 StormType        = new StormType2();
                DateTime   StormFormationDT = new DateTime(1959, 3, 10);

                // XML OR JSON OR F*****G ANYTHING PLS
                // not foreach because it makes it slightly easier to set the date
                for (int j = 0; j < ATCFLines.Length; j++)
                {
                    string ATCFLine = ATCFLines[j];

                    string[] Components = ATCFLine.Split(',');

                    string _StrAbbreviation = Components[0];
                    // get the stuff we actually need
                    string _StrId   = Components[1];
                    string _StrTime = Components[2];
                    string _StrTimeSinceFormation = Components[3];
                    string _StrCoordX             = Components[6];
                    string _StrCoordY             = Components[7];
                    string _StrIntensity          = Components[8];
                    string _StrPressure           = Components[9];
                    string _StrCategory           = Components[10];
                    string _StrName = Components[28]; // bleh

                    // trim it
                    _StrAbbreviation       = _StrAbbreviation.Trim();
                    _StrId                 = _StrId.Trim();
                    _StrTime               = _StrTime.Trim();
                    _StrTimeSinceFormation = _StrTimeSinceFormation.Trim();
                    _StrPressure           = _StrPressure.Trim();
                    _StrCoordX             = _StrCoordX.Trim();
                    _StrCoordY             = _StrCoordY.Trim();
                    _StrIntensity          = _StrIntensity.Trim();
                    _StrCategory           = _StrCategory.Trim();
                    _StrName               = _StrName.Trim();

                    // initialise the basin with the abbreviation loaded from XML
                    // we just use the name if there is no abbreviation specified in XML
                    int Intensity = 0;
                    // first iteration...
                    if (j == 0)
                    {
                        if (i == 0)
                        {
                            Bas = Proj.GetBasinWithAbbreviation(_StrAbbreviation);

                            if (Bas.CoordsHigher == null || Bas.CoordsLower == null)
                            {
                                Error.Throw("Error!", "This basin is not supported by the ATCF format as it does not have defined borders in Basins.xml.", ErrorSeverity.Error, 249);
                                return(null);
                            }
                        }

                        Lyr.Name = _StrName;
                    }

                    Intensity = Convert.ToInt32(_StrIntensity);

                    Sto.FormationDate = ParsingUtil.ParseATCFDateTime(_StrTime, CoordinateFormat.ATCF);

                    if (_StrName == null)
                    {
                        Error.Throw("Error!", "Attempted to load storm with an invalid name!", ErrorSeverity.Error, 245);
                        return(null);
                    }
                    else
                    {
                        Sto.Name = _StrName;
                    }

                    int        Id    = Convert.ToInt32(_StrId);
                    Coordinate Coord = Coordinate.FromSplitCoordinate(_StrCoordX, _StrCoordY);

                    // create a node and add it

                    Node Nod = new Node();
                    Nod.Id        = Id;
                    Nod.Intensity = Intensity;

                    Nod.Position = Bas.FromCoordinateToRelativeNodePosition(Coord, new Point(MnWindow.Width, MnWindow.Height));
                    Nod.NodeType = ATCFHelperMethods.Export_GetStormType(_StrCategory);
                    Nod.Pressure = Convert.ToInt32(_StrPressure);

                    Sto.AddNode(Nod);
                }

                Lyr.AddStorm(Sto);
                Bas.AddLayer(Lyr);
            }

            Proj.AddBasin(Bas);

            return(Proj);
        }