コード例 #1
0
        /// <summary>
        /// Accept the values on the Form and create the required XML data.
        /// </summary>
        private async ValueTask AcceptForm()
        {
            // Make sure a value has been selected if necessary.
            if (txtTranslateSelection.Visible && string.IsNullOrEmpty(txtSelect.Text))
            {
                Program.ShowMessageBox(this, await LanguageManager.GetStringAsync("Message_SelectItem"), await LanguageManager.GetStringAsync("MessageTitle_SelectItem"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Make sure a value has been provided for the name.
            if (string.IsNullOrEmpty(txtName.Text))
            {
                Program.ShowMessageBox(this, await LanguageManager.GetStringAsync("Message_ImprovementName"), await LanguageManager.GetStringAsync("MessageTitle_ImprovementName"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtName.Focus();
                return;
            }

            XmlDocument objBonusXml = new XmlDocument {
                XmlResolver = null
            };

            using (MemoryStream objStream = new MemoryStream())
            {
                using (XmlWriter objWriter = Utils.GetStandardXmlWriter(objStream))
                {
                    // Build the XML for the Improvement.
                    XmlNode objFetchNode = _objDocument.SelectSingleNode("/chummer/improvements/improvement[id = " + cboImprovemetType.SelectedValue.ToString().CleanXPath() + ']');
                    string  strInternal  = objFetchNode?["internal"]?.InnerText;
                    if (string.IsNullOrEmpty(strInternal))
                    {
                        return;
                    }
                    await objWriter.WriteStartDocumentAsync();

                    // <bonus>
                    await objWriter.WriteStartElementAsync("bonus");

                    // <whatever element>
                    await objWriter.WriteStartElementAsync(strInternal);

                    string strRating = string.Empty;
                    if (chkApplyToRating.Checked)
                    {
                        strRating = "<applytorating>True</applytorating>";
                    }

                    // Retrieve the XML data from the document and replace the values as necessary.
                    XmlAttributeCollection xmlAttributeCollection = objFetchNode["xml"]?.Attributes;
                    if (xmlAttributeCollection != null)
                    {
                        foreach (XmlAttribute xmlAttribute in xmlAttributeCollection)
                        {
                            await objWriter.WriteAttributeStringAsync(xmlAttribute.LocalName, xmlAttribute.Value);
                        }
                    }
                    // ReSharper disable once PossibleNullReferenceException
                    string strXml = objFetchNode["xml"].InnerText
                                    .Replace("{val}", nudVal.Value.ToString(GlobalSettings.InvariantCultureInfo))
                                    .Replace("{min}", nudMin.Value.ToString(GlobalSettings.InvariantCultureInfo))
                                    .Replace("{max}", nudMax.Value.ToString(GlobalSettings.InvariantCultureInfo))
                                    .Replace("{aug}", nudAug.Value.ToString(GlobalSettings.InvariantCultureInfo))
                                    .Replace("{free}", chkFree.Checked.ToString(GlobalSettings.InvariantCultureInfo).ToLowerInvariant())
                                    .Replace("{select}", txtSelect.Text)
                                    .Replace("{applytorating}", strRating);
                    await objWriter.WriteRawAsync(strXml);

                    // Write the rest of the document.
                    // </whatever element>
                    await objWriter.WriteEndElementAsync();

                    // </bonus>
                    await objWriter.WriteEndElementAsync();

                    await objWriter.WriteEndDocumentAsync();

                    await objWriter.FlushAsync();
                }

                objStream.Position = 0;

                // Read it back in as an XmlDocument.
                using (StreamReader objReader = new StreamReader(objStream, Encoding.UTF8, true))
                    using (XmlReader objXmlReader = XmlReader.Create(objReader, GlobalSettings.SafeXmlReaderSettings))
                        objBonusXml.Load(objXmlReader);
            }

            // Pluck out the bonus information.
            XmlNode objNode = objBonusXml.SelectSingleNode("/bonus");

            // Pass it to the Improvement Manager so that it can be added to the character.
            string strGuid = Guid.NewGuid().ToString("D", GlobalSettings.InvariantCultureInfo);
            await ImprovementManager.CreateImprovementsAsync(_objCharacter, Improvement.ImprovementSource.Custom, strGuid, objNode, 1, txtName.Text);

            // If an Improvement was passed in, remove it from the character.
            string strNotes = string.Empty;
            int    intOrder = 0;

            if (EditImprovementObject != null)
            {
                // Copy the notes over to the new item.
                strNotes = EditImprovementObject.Notes;
                intOrder = EditImprovementObject.SortOrder;
                await ImprovementManager.RemoveImprovementsAsync(_objCharacter, Improvement.ImprovementSource.Custom, EditImprovementObject.SourceName);
            }

            // Find the newly-created Improvement and attach its custom name.
            Improvement objImprovement = _objCharacter.Improvements.FirstOrDefault(imp => imp.SourceName == strGuid);

            if (objImprovement != null)
            {
                objImprovement.CustomName  = txtName.Text;
                objImprovement.CustomId    = cboImprovemetType.SelectedValue.ToString();
                objImprovement.Custom      = true;
                objImprovement.Notes       = strNotes;
                objImprovement.SortOrder   = intOrder;
                objImprovement.CustomGroup = _strCustomGroup;
                NewImprovement             = objImprovement;
            }
            else
            {
                Utils.BreakIfDebug();
            }

            DialogResult = DialogResult.OK;
        }