Пример #1
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            TempAttribMods.Revision      = Convert.ToInt32(lblRevision.Text);
            TempAttribMods.RevisionDate  = Convert.ToDateTime(lblRevisionDate.Text);
            Database.Instance.AttribMods = (Modifiers)TempAttribMods.Clone();
            //Database.Instance.AttribMods.Store(MyApplication.GetSerializer());
            //DatabaseAPI.UpdateModifiersDict(Database.Instance.AttribMods.Modifier);

            // This one barely shows up.
            // May only be needed if one has Mids on a ZIP drive!
            BusyMsg("Saving newly imported Attribute Modifiers...");
            Database.Instance.AttribMods = (Modifiers)TempAttribMods.Clone();
            DatabaseAPI.Database.AttribMods?.Store(MyApplication.GetSerializer());
            BusyHide();
            //MessageBox.Show($@"Attribute Modifiers have been saved.", @"Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
            DialogResult = DialogResult.OK;
            Close();
        }
Пример #2
0
        private void btnImportJson_Click(object sender, EventArgs e)
        {
            using OpenFileDialog f = new OpenFileDialog()
                  {
                      Title           = "Select JSON source",
                      DefaultExt      = "json",
                      Filter          = "JSON files(*.json)|*.json|All files(*.*)|*.*",
                      FilterIndex     = 0,
                      CheckFileExists = true,
                      CheckPathExists = true,
                      Multiselect     = false
                  };

            DialogResult r = f.ShowDialog();

            if (r != DialogResult.OK)
            {
                return;
            }

            Modifiers?m   = new Modifiers();
            string    src = File.ReadAllText(f.FileName);
            JsonSerializerSettings jsonOpt = new JsonSerializerSettings
            {
                TypeNameHandling           = TypeNameHandling.Auto,
                NullValueHandling          = NullValueHandling.Ignore,
                PreserveReferencesHandling = PreserveReferencesHandling.None,
                ReferenceLoopHandling      = ReferenceLoopHandling.Ignore,
                DefaultValueHandling       = DefaultValueHandling.Ignore
            };

            try
            {
                if (src.TrimStart(' ', '\t', '\r', '\n', '\0').StartsWith("["))
                {
                    Modifiers.ModifierTable[]? tables =
                        JsonConvert.DeserializeObject <Modifiers.ModifierTable[]>(src, jsonOpt);
                    if (tables == null)
                    {
                        throw new FormatException("JSON file contains no modifier tables.");
                    }

                    m.Modifier     = (Modifiers.ModifierTable[])tables.Clone();
                    m.Revision     = Database.Instance.AttribMods.Revision + 1;
                    m.RevisionDate = DateTime.Now;
                    m.SourceIndex  = string.Empty;
                    m.SourceTables = f.FileName;
                }
                else if (src.TrimStart(' ', '\t', '\r', '\n', '\0').StartsWith("{"))
                {
                    m = JsonConvert.DeserializeObject <Modifiers>(src, jsonOpt);
                    if (m == null)
                    {
                        throw new FormatException("JSON file contains no usable data.");
                    }
                }
                else
                {
                    throw new FormatException("Malformed JSON file.");
                }
            }
            catch (JsonSerializationException ex)
            {
                MessageBox.Show("[JsonSerializationException] Error deserializing JSON.\r\n" + ex.Message + "\r\n" + ex.StackTrace);
                return;
            }
            catch (JsonReaderException ex)
            {
                MessageBox.Show("[JsonReaderException] Error reading JSON.\r\n" + ex.Message + "\r\n" + ex.StackTrace);
                return;
            }
            catch (FormatException ex)
            {
                MessageBox.Show("[FormatException] JSON file format exception.\r\n" + ex.Message + "\r\n" + ex.StackTrace);
                return;
            }

            int nAt   = m.Modifier[0].Table.Length;
            int nMods = m.Modifier.Length;

            r = MessageBox.Show(
                Convert.ToString(nAt, null) + " Archetype" + (nAt != 1 ? "s" : "") + "/Entit" + (nAt != 1 ? "ies" : "y") + " found,\n" +
                Convert.ToString(nMods, null) + " Mod" + (nMods != 1 ? "s" : "") + " found.\n\nProceed to import? This will overwrite current values.\n",
                "JSON tables import", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (r != DialogResult.Yes)
            {
                return;
            }

            TempAttribMods = (Modifiers)m.Clone();

            lblRevision.Text     = Convert.ToString(TempAttribMods.Revision, null);
            lblRevisionDate.Text = TempAttribMods.RevisionDate.ToString("dd/MM/yyyy", null);

            UpdateModifiersList();
            UpdateClassesList();
        }