Exemple #1
0
        private void ImportBattleFromTypeAndFile(int selectedFileType, string fileName, bool setBattle = true)
        {
            DataOptions options = new DataOptions();

            if (selectedFileType == (int)Import_Filters.json)
            {
                var importedBattle = XmlHelper.DeserializeFromFile(fileName);

                importedBattle.battle.msbtUpdated = true;   // We want to be sure we write the new title when we save.

                options.AddDataTbl(importedBattle.battle);
                options.AddRangeDataTbl(importedBattle.fighters);
                options.AddDataTbl(importedBattle.spirit);
            }
            else if (selectedFileType == (int)Import_Filters.prc)
            {
                options = XmlHelper.ReadXML(fileName, dataTbls.config.labels_file_location);
            }

            SaveImportToDataTbls(options);
            if (setBattle)
            {
                var battle = (Battle)options.GetItemsOfType(typeof(Battle)).FirstOrDefault();
                SetSelectedBattleByBattle(battle);
            }
        }
Exemple #2
0
        private void ImportBattleOverFile_Click(object sender, EventArgs e)
        {
            var importDialog = FileHelper.GetImportBattleFileDialog(title: "Import Custom Spirit Over Current.", initialDirectory: dataTbls.config.file_directory_custom_battles);

            if (importDialog.ShowDialog() == CommonFileDialogResult.Ok && !String.IsNullOrWhiteSpace(importDialog?.FileName))
            {
                DataOptions options = new DataOptions();

                if (importDialog.SelectedFileTypeIndex == (int)Import_Filters.json)
                {
                    var importedBattle = XmlHelper.DeserializeFromFile(importDialog.FileName);

                    importedBattle.battle.msbtUpdated = true;   // We want to be sure we write the new title when we save.

                    options.AddDataTbl(importedBattle.battle);
                    options.AddRangeDataTbl(importedBattle.fighters);
                }
                else if (importDialog.SelectedFileTypeIndex == (int)Import_Filters.prc)
                {
                    options = XmlHelper.ReadXML(importDialog.FileName, dataTbls.config.labels_file_location);
                }
                options.SetBattleIdsForAll(dataTbls.selectedBattle.battle_id);

                var battle = (Battle)options.GetItemsOfType(typeof(Battle)).FirstOrDefault();

                SaveImportToDataTbls(options);
                SetSelectedBattleByBattle(battle);
            }
        }
Exemple #3
0
        public static DataOptions ReadXML(string fileName, string fileLocationLabels = "", OrderedDictionary <ulong, string> hashes = null)
        {
            bool     parseData = false;
            bool     firstPass = false;
            bool     sharedXmlName;
            IDataTbl dataTable;
            var      results = new DataOptions();

            var stream = GetStreamFromFile(fileName);

            if (stream == null)
            {
                return(results);
            }

            var reader = GetXmlReaderFromStream(stream);

            // Try to read.  If we can't read - it might be encrypted.  Try to decrypt.
            try
            {
                reader.Read();
            }
            catch
            {
                try
                {
                    // Close and dispose reader and stream.
                    reader.Dispose();
                    stream = GetStreamFromEncryptedFile(fileName, fileLocationLabels);
                    reader = GetXmlReaderFromStream(stream);
                    reader.Read();
                }
                catch
                {
                    return(results);
                }
            }

            // Read through entire file, and determine type.
            try
            {
                // Read the whole file.
                while (!reader.EOF)
                {
                    var attribute = reader?.GetAttribute("hash");
                    dataTable = attribute == null? null :  DataTbl.GetDataTblFromXmlName(attribute);

                    if (dataTable != null)
                    {
                        parseData     = true;
                        firstPass     = true;
                        sharedXmlName = dataTable.GetType() == typeof(DataTbl);

                        if (sharedXmlName)
                        {
                            // Store the position, read from the beginning, restore the position.
                            var position = stream.Position;
                            stream.Position = 0;
                            dataTable       = DataTbl.DetermineXmlTypeFromFirstLevel(stream);
                            stream.Position = position;

                            if (dataTable == null)
                            {
                                parseData = false;
                            }
                        }
                    }

                    if (parseData)
                    {
                        // Read until start of data.
                        XmlHelper.ReadUntilName(reader, stopper: "struct");

                        // Lists have index values, so keep reading until we've left the list.
                        // Added first pass to handle when a struct is not in a list.
                        while (firstPass || reader.GetAttribute("index") != null)
                        {
                            dataTable = (IDataTbl)Activator.CreateInstance(dataTable.GetType());
                            dataTable.BuildFromXml(reader);

                            results.AddDataTbl(dataTable);

                            XmlHelper.ReadUntilNodeType(reader, node: XmlNodeType.Element);

                            firstPass = false;
                        }

                        //Left the list.  Don't parse the next lines.
                        parseData = false;
                        Console.WriteLine("{0} Table Complete.", dataTable.GetType().ToString());

                        continue;
                    }

                    reader.Read();
                }
                reader.Dispose();
                stream.Dispose();
                return(results);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }