示例#1
0
        private async void cmdOK_Click(object sender, EventArgs e)
        {
            // Make sure the kit and file name fields are populated.
            string strName = await txtName.DoThreadSafeFuncAsync(x => x.Text);

            if (string.IsNullOrEmpty(strName))
            {
                Program.ShowMessageBox(this, await LanguageManager.GetStringAsync("Message_CreatePACKSKit_KitName"), await LanguageManager.GetStringAsync("MessageTitle_CreatePACKSKit_KitName"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            string strFileName = await txtFileName.DoThreadSafeFuncAsync(x => x.Text);

            if (string.IsNullOrEmpty(strFileName))
            {
                Program.ShowMessageBox(this, await LanguageManager.GetStringAsync("Message_CreatePACKSKit_FileName"), await LanguageManager.GetStringAsync("MessageTitle_CreatePACKSKit_FileName"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            // Make sure the file name starts with custom and ends with _packs.xml.
            if (!strFileName.StartsWith("custom_", StringComparison.OrdinalIgnoreCase) || !strFileName.EndsWith("_packs.xml", StringComparison.OrdinalIgnoreCase))
            {
                Program.ShowMessageBox(this, await LanguageManager.GetStringAsync("Message_CreatePACKSKit_InvalidFileName"), await LanguageManager.GetStringAsync("MessageTitle_CreatePACKSKit_InvalidFileName"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            // See if a Kit with this name already exists for the Custom category.
            // This was originally done without the XmlManager, but because amends and overrides and toggling custom data directories can change names, we need to use it.
            if ((await XmlManager.LoadXPathAsync("packs.xml", _objCharacter.Settings.EnabledCustomDataDirectoryPaths))
                .SelectSingleNode("/chummer/packs/pack[name = " + strName.CleanXPath() + " and category = \"Custom\"]") != null)
            {
                Program.ShowMessageBox(this, string.Format(GlobalSettings.CultureInfo, await LanguageManager.GetStringAsync("Message_CreatePACKSKit_DuplicateName"), strName),
                                       await LanguageManager.GetStringAsync("MessageTitle_CreatePACKSKit_DuplicateName"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            string strPath = Path.Combine(Utils.GetStartupPath, "data", strFileName);

            // If this is not a new file, read in the existing contents.
            XmlDocument objXmlCurrentDocument = null;

            if (File.Exists(strPath))
            {
                try
                {
                    objXmlCurrentDocument = new XmlDocument {
                        XmlResolver = null
                    };
                    objXmlCurrentDocument.LoadStandard(strPath);
                }
                catch (IOException ex)
                {
                    Program.ShowMessageBox(this, ex.ToString());
                    return;
                }
                catch (XmlException ex)
                {
                    Program.ShowMessageBox(this, ex.ToString());
                    return;
                }
            }

            using (FileStream objStream = new FileStream(strPath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
            {
                using (XmlWriter objWriter = Utils.GetStandardXmlWriter(objStream))
                {
                    await objWriter.WriteStartDocumentAsync();

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

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

                    // If this is not a new file, write out the current contents.
                    if (objXmlCurrentDocument != null)
                    {
                        XmlNode xmlExistingPacksNode = objXmlCurrentDocument.SelectSingleNode("/chummer/packs");
                        xmlExistingPacksNode?.WriteContentTo(objWriter);
                    }

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

                    // <name />
                    await objWriter.WriteElementStringAsync("name", txtName.Text);

                    // <category />
                    await objWriter.WriteElementStringAsync("category", "Custom");

                    // Export Attributes.
                    if (await chkAttributes.DoThreadSafeFuncAsync(x => x.Checked))
                    {
                        int intBOD      = await _objCharacter.BOD.ValueAsync - (await _objCharacter.BOD.MetatypeMinimumAsync - 1);
                        int intAGI      = await _objCharacter.AGI.ValueAsync - (await _objCharacter.AGI.MetatypeMinimumAsync - 1);
                        int intREA      = await _objCharacter.REA.ValueAsync - (await _objCharacter.REA.MetatypeMinimumAsync - 1);
                        int intSTR      = await _objCharacter.STR.ValueAsync - (await _objCharacter.STR.MetatypeMinimumAsync - 1);
                        int intCHA      = await _objCharacter.CHA.ValueAsync - (await _objCharacter.CHA.MetatypeMinimumAsync - 1);
                        int intINT      = await _objCharacter.INT.ValueAsync - (await _objCharacter.INT.MetatypeMinimumAsync - 1);
                        int intLOG      = await _objCharacter.LOG.ValueAsync - (await _objCharacter.LOG.MetatypeMinimumAsync - 1);
                        int intWIL      = await _objCharacter.WIL.ValueAsync - (await _objCharacter.WIL.MetatypeMinimumAsync - 1);
                        int intEDG      = await _objCharacter.EDG.ValueAsync - (await _objCharacter.EDG.MetatypeMinimumAsync - 1);
                        int intMAG      = await _objCharacter.MAG.ValueAsync - (await _objCharacter.MAG.MetatypeMinimumAsync - 1);
                        int intMAGAdept = await _objCharacter.MAGAdept.ValueAsync - (await _objCharacter.MAGAdept.MetatypeMinimumAsync - 1);
                        int intDEP      = await _objCharacter.DEP.ValueAsync - (await _objCharacter.DEP.MetatypeMinimumAsync - 1);
                        int intRES      = await _objCharacter.RES.ValueAsync - (await _objCharacter.RES.MetatypeMinimumAsync - 1);
                        // <attributes>
                        await objWriter.WriteStartElementAsync("attributes");

                        await objWriter.WriteElementStringAsync("bod", intBOD.ToString(GlobalSettings.InvariantCultureInfo));

                        await objWriter.WriteElementStringAsync("agi", intAGI.ToString(GlobalSettings.InvariantCultureInfo));

                        await objWriter.WriteElementStringAsync("rea", intREA.ToString(GlobalSettings.InvariantCultureInfo));

                        await objWriter.WriteElementStringAsync("str", intSTR.ToString(GlobalSettings.InvariantCultureInfo));

                        await objWriter.WriteElementStringAsync("cha", intCHA.ToString(GlobalSettings.InvariantCultureInfo));

                        await objWriter.WriteElementStringAsync("int", intINT.ToString(GlobalSettings.InvariantCultureInfo));

                        await objWriter.WriteElementStringAsync("log", intLOG.ToString(GlobalSettings.InvariantCultureInfo));

                        await objWriter.WriteElementStringAsync("wil", intWIL.ToString(GlobalSettings.InvariantCultureInfo));

                        await objWriter.WriteElementStringAsync("edg", intEDG.ToString(GlobalSettings.InvariantCultureInfo));

                        if (_objCharacter.MAGEnabled)
                        {
                            await objWriter.WriteElementStringAsync("mag", intMAG.ToString(GlobalSettings.InvariantCultureInfo));

                            if (_objCharacter.Settings.MysAdeptSecondMAGAttribute && _objCharacter.IsMysticAdept)
                            {
                                await objWriter.WriteElementStringAsync("magadept", intMAGAdept.ToString(GlobalSettings.InvariantCultureInfo));
                            }
                        }

                        if (_objCharacter.RESEnabled)
                        {
                            await objWriter.WriteElementStringAsync("res", intRES.ToString(GlobalSettings.InvariantCultureInfo));
                        }
                        if (_objCharacter.DEPEnabled)
                        {
                            await objWriter.WriteElementStringAsync("dep", intDEP.ToString(GlobalSettings.InvariantCultureInfo));
                        }
                        // </attributes>
                        await objWriter.WriteEndElementAsync();
                    }

                    // Export Qualities.
                    if (await chkQualities.DoThreadSafeFuncAsync(x => x.Checked))
                    {
                        bool blnPositive = false;
                        bool blnNegative = false;
                        // Determine if Positive or Negative Qualities exist.
                        foreach (Quality objQuality in _objCharacter.Qualities)
                        {
                            switch (objQuality.Type)
                            {
                            case QualityType.Positive:
                                blnPositive = true;
                                break;

                            case QualityType.Negative:
                                blnNegative = true;
                                break;
                            }

                            if (blnPositive && blnNegative)
                            {
                                break;
                            }
                        }

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

                        // Positive Qualities.
                        if (blnPositive)
                        {
                            // <positive>
                            await objWriter.WriteStartElementAsync("positive");

                            foreach (Quality objQuality in _objCharacter.Qualities)
                            {
                                if (objQuality.Type == QualityType.Positive)
                                {
                                    await objWriter.WriteStartElementAsync("quality");

                                    if (!string.IsNullOrEmpty(objQuality.Extra))
                                    {
                                        await objWriter.WriteAttributeStringAsync("select", objQuality.Extra);
                                    }
                                    objWriter.WriteValue(objQuality.Name);
                                    await objWriter.WriteEndElementAsync();
                                }
                            }

                            // </positive>
                            await objWriter.WriteEndElementAsync();
                        }

                        // Negative Qualities.
                        if (blnPositive)
                        {
                            // <negative>
                            await objWriter.WriteStartElementAsync("negative");

                            foreach (Quality objQuality in _objCharacter.Qualities)
                            {
                                if (objQuality.Type == QualityType.Negative)
                                {
                                    await objWriter.WriteStartElementAsync("quality");

                                    if (!string.IsNullOrEmpty(objQuality.Extra))
                                    {
                                        await objWriter.WriteAttributeStringAsync("select", objQuality.Extra);
                                    }
                                    objWriter.WriteValue(objQuality.Name);
                                    await objWriter.WriteEndElementAsync();
                                }
                            }

                            // </negative>
                            await objWriter.WriteEndElementAsync();
                        }

                        // </qualities>
                        await objWriter.WriteEndElementAsync();
                    }

                    // Export Starting Nuyen.
                    if (await chkStartingNuyen.DoThreadSafeFuncAsync(x => x.Checked))
                    {
                        decimal decNuyenBP = _objCharacter.NuyenBP;
                        if (!_objCharacter.EffectiveBuildMethodUsesPriorityTables)
                        {
                            decNuyenBP /= 2.0m;
                        }
                        await objWriter.WriteElementStringAsync("nuyenbp", decNuyenBP.ToString(GlobalSettings.InvariantCultureInfo));
                    }

                    /* TODO: Add support for active and knowledge skills and skill groups
                     * // Export Active Skills.
                     * if (await chkActiveSkills.DoThreadSafeFuncAsync(x => x.Checked))
                     * {
                     *  // <skills>
                     *  await objWriter.WriteStartElementAsync("skills");
                     *
                     *  // Active Skills.
                     *  foreach (Skill objSkill in _objCharacter.SkillsSection.Skills)
                     *  {
                     *      if (!objSkill.IsKnowledgeSkill && objSkill.Rating > 0)
                     *      {
                     *          // <skill>
                     *          await objWriter.WriteStartElementAsync("skill");
                     *          await objWriter.WriteElementStringAsync("name", objSkill.Name);
                     *          await objWriter.WriteElementStringAsync("rating", objSkill.Rating.ToString());
                     *          if (!string.IsNullOrEmpty(objSkill.Specialization))
                     *              await objWriter.WriteElementStringAsync("spec", objSkill.Specialization);
                     *          // </skill>
                     *          await objWriter.WriteEndElementAsync();
                     *      }
                     *  }
                     *
                     *  // Skill Groups.
                     *  foreach (SkillGroup objSkillGroup in _objCharacter.SkillsSection.SkillGroups)
                     *  {
                     *      if (objSkillGroup.BaseUnbroken && objSkillGroup.Rating > 0)
                     *      {
                     *          // <skillgroup>
                     *          await objWriter.WriteStartElementAsync("skillgroup");
                     *          await objWriter.WriteElementStringAsync("name", objSkillGroup.Name);
                     *          await objWriter.WriteElementStringAsync("rating", objSkillGroup.Rating.ToString());
                     *          // </skillgroup>
                     *          await objWriter.WriteEndElementAsync();
                     *      }
                     *  }
                     *  // </skills>
                     *  await objWriter.WriteEndElementAsync();
                     * }
                     *
                     * // Export Knowledge Skills.
                     * if (await chkKnowledgeSkills.DoThreadSafeFuncAsync(x => x.Checked))
                     * {
                     *  // <knowledgeskills>
                     *  await objWriter.WriteStartElementAsync("knowledgeskills");
                     *  foreach (KnowledgeSkill objSkill in _objCharacter.SkillsSection.Skills.OfType<KnowledgeSkill>())
                     *  {
                     *      // <skill>
                     *      await objWriter.WriteStartElementAsync("skill");
                     *      await objWriter.WriteElementStringAsync("name", objSkill.Name);
                     *      await objWriter.WriteElementStringAsync("rating", objSkill.Rating.ToString(GlobalSettings.InvariantCultureInfo));
                     *      if (!string.IsNullOrEmpty(objSkill.Specialization))
                     *          await objWriter.WriteElementStringAsync("spec", objSkill.Specialization);
                     *      await objWriter.WriteElementStringAsync("category", objSkill.SkillCategory);
                     *      // </skill>
                     *      await objWriter.WriteEndElementAsync();
                     *  }
                     *
                     *  // </knowledgeskills>
                     *  await objWriter.WriteEndElementAsync();
                     * }
                     */

                    // Export Martial Arts.
                    if (await chkMartialArts.DoThreadSafeFuncAsync(x => x.Checked))
                    {
                        // <martialarts>
                        await objWriter.WriteStartElementAsync("martialarts");

                        foreach (MartialArt objArt in _objCharacter.MartialArts)
                        {
                            // <martialart>
                            await objWriter.WriteStartElementAsync("martialart");

                            await objWriter.WriteElementStringAsync("name", objArt.Name);

                            if (objArt.Techniques.Count > 0)
                            {
                                // <techniques>
                                await objWriter.WriteStartElementAsync("techniques");

                                foreach (MartialArtTechnique objTechnique in objArt.Techniques)
                                {
                                    await objWriter.WriteElementStringAsync("technique", objTechnique.Name);
                                }
                                // </techniques>
                                await objWriter.WriteEndElementAsync();
                            }

                            // </martialart>
                            await objWriter.WriteEndElementAsync();
                        }
                        // </martialarts>
                        await objWriter.WriteEndElementAsync();
                    }

                    // Export Spells.
                    if (await chkSpells.DoThreadSafeFuncAsync(x => x.Checked))
                    {
                        // <spells>
                        await objWriter.WriteStartElementAsync("spells");

                        foreach (Spell objSpell in _objCharacter.Spells)
                        {
                            await objWriter.WriteStartElementAsync("spell");

                            await objWriter.WriteStartElementAsync("name");

                            if (!string.IsNullOrEmpty(objSpell.Extra))
                            {
                                await objWriter.WriteAttributeStringAsync("select", objSpell.Extra);
                            }
                            objWriter.WriteValue(objSpell.Name);
                            await objWriter.WriteEndElementAsync();

                            await objWriter.WriteElementStringAsync("category", objSpell.Category);

                            await objWriter.WriteEndElementAsync();
                        }

                        // </spells>
                        await objWriter.WriteEndElementAsync();
                    }

                    // Export Complex Forms.
                    if (await chkComplexForms.DoThreadSafeFuncAsync(x => x.Checked))
                    {
                        // <programs>
                        await objWriter.WriteStartElementAsync("complexforms");

                        foreach (ComplexForm objComplexForm in _objCharacter.ComplexForms)
                        {
                            // <program>
                            await objWriter.WriteStartElementAsync("complexform");

                            await objWriter.WriteStartElementAsync("name");

                            if (!string.IsNullOrEmpty(objComplexForm.Extra))
                            {
                                await objWriter.WriteAttributeStringAsync("select", objComplexForm.Extra);
                            }
                            objWriter.WriteValue(objComplexForm.Name);
                            await objWriter.WriteEndElementAsync();

                            await objWriter.WriteEndElementAsync();

                            // </program>
                            await objWriter.WriteEndElementAsync();
                        }

                        // </programs>
                        await objWriter.WriteEndElementAsync();
                    }

                    // Export Cyberware/Bioware.
                    if (await chkCyberware.DoThreadSafeFuncAsync(x => x.Checked))
                    {
                        bool blnCyberware = false;
                        bool blnBioware   = false;
                        foreach (Cyberware objCharacterCyberware in _objCharacter.Cyberware)
                        {
                            switch (objCharacterCyberware.SourceType)
                            {
                            case Improvement.ImprovementSource.Bioware:
                                blnBioware = true;
                                break;

                            case Improvement.ImprovementSource.Cyberware:
                                blnCyberware = true;
                                break;
                            }

                            if (blnCyberware && blnBioware)
                            {
                                break;
                            }
                        }

                        if (blnCyberware)
                        {
                            // <cyberwares>
                            await objWriter.WriteStartElementAsync("cyberwares");

                            foreach (Cyberware objCyberware in _objCharacter.Cyberware)
                            {
                                if (objCyberware.SourceType == Improvement.ImprovementSource.Cyberware)
                                {
                                    // <cyberware>
                                    await objWriter.WriteStartElementAsync("cyberware");

                                    await objWriter.WriteElementStringAsync("name", objCyberware.Name);

                                    if (objCyberware.Rating > 0)
                                    {
                                        await objWriter.WriteElementStringAsync("rating", objCyberware.Rating.ToString(GlobalSettings.InvariantCultureInfo));
                                    }
                                    await objWriter.WriteElementStringAsync("grade", objCyberware.Grade.Name);

                                    if (objCyberware.Children.Count > 0)
                                    {
                                        // <cyberwares>
                                        await objWriter.WriteStartElementAsync("cyberwares");

                                        foreach (Cyberware objChildCyberware in objCyberware.Children)
                                        {
                                            if (objChildCyberware.Capacity != "[*]")
                                            {
                                                // <cyberware>
                                                await objWriter.WriteStartElementAsync("cyberware");

                                                await objWriter.WriteElementStringAsync("name", objChildCyberware.Name);

                                                if (objChildCyberware.Rating > 0)
                                                {
                                                    await objWriter.WriteElementStringAsync("rating", objChildCyberware.Rating.ToString(GlobalSettings.InvariantCultureInfo));
                                                }

                                                if (objChildCyberware.GearChildren.Count > 0)
                                                {
                                                    await WriteGear(objWriter, objChildCyberware.GearChildren);
                                                }
                                                // </cyberware>
                                                await objWriter.WriteEndElementAsync();
                                            }
                                        }

                                        // </cyberwares>
                                        await objWriter.WriteEndElementAsync();
                                    }

                                    if (objCyberware.GearChildren.Count > 0)
                                    {
                                        await WriteGear(objWriter, objCyberware.GearChildren);
                                    }

                                    // </cyberware>
                                    await objWriter.WriteEndElementAsync();
                                }
                            }

                            // </cyberwares>
                            await objWriter.WriteEndElementAsync();
                        }

                        if (blnBioware)
                        {
                            // <biowares>
                            await objWriter.WriteStartElementAsync("biowares");

                            foreach (Cyberware objCyberware in _objCharacter.Cyberware)
                            {
                                if (objCyberware.SourceType == Improvement.ImprovementSource.Bioware)
                                {
                                    // <bioware>
                                    await objWriter.WriteStartElementAsync("bioware");

                                    await objWriter.WriteElementStringAsync("name", objCyberware.Name);

                                    if (objCyberware.Rating > 0)
                                    {
                                        await objWriter.WriteElementStringAsync("rating", objCyberware.Rating.ToString(GlobalSettings.InvariantCultureInfo));
                                    }
                                    await objWriter.WriteElementStringAsync("grade", objCyberware.Grade.ToString());

                                    if (objCyberware.GearChildren.Count > 0)
                                    {
                                        await WriteGear(objWriter, objCyberware.GearChildren);
                                    }
                                    // </bioware>
                                    await objWriter.WriteEndElementAsync();
                                }
                            }

                            // </biowares>
                            await objWriter.WriteEndElementAsync();
                        }
                    }

                    // Export Lifestyle.
                    if (await chkLifestyle.DoThreadSafeFuncAsync(x => x.Checked))
                    {
                        // <lifestyles>
                        await objWriter.WriteStartElementAsync("lifestyles");

                        foreach (Lifestyle objLifestyle in _objCharacter.Lifestyles)
                        {
                            // <lifestyle>
                            await objWriter.WriteStartElementAsync("lifestyle");

                            await objWriter.WriteElementStringAsync("name", objLifestyle.Name);

                            await objWriter.WriteElementStringAsync("months", objLifestyle.Increments.ToString(GlobalSettings.InvariantCultureInfo));

                            if (!string.IsNullOrEmpty(objLifestyle.BaseLifestyle))
                            {
                                // This is an Advanced Lifestyle, so write out its properties.
                                await objWriter.WriteElementStringAsync("cost", objLifestyle.Cost.ToString(_objCharacter.Settings.NuyenFormat, GlobalSettings.CultureInfo));

                                await objWriter.WriteElementStringAsync("dice", objLifestyle.Dice.ToString(GlobalSettings.InvariantCultureInfo));

                                await objWriter.WriteElementStringAsync("multiplier", objLifestyle.Multiplier.ToString(_objCharacter.Settings.NuyenFormat, GlobalSettings.CultureInfo));

                                await objWriter.WriteElementStringAsync("baselifestyle", objLifestyle.BaseLifestyle);

                                if (objLifestyle.LifestyleQualities.Count > 0)
                                {
                                    // <qualities>
                                    await objWriter.WriteStartElementAsync("qualities");

                                    foreach (LifestyleQuality objQuality in objLifestyle.LifestyleQualities)
                                    {
                                        await objWriter.WriteElementStringAsync("quality", objQuality.Name);
                                    }
                                    // </qualities>
                                    await objWriter.WriteEndElementAsync();
                                }
                            }

                            // </lifestyle>
                            await objWriter.WriteEndElementAsync();
                        }

                        // </lifestyles>
                        await objWriter.WriteEndElementAsync();
                    }

                    // Export Armor.
                    if (await chkArmor.DoThreadSafeFuncAsync(x => x.Checked))
                    {
                        // <armors>
                        await objWriter.WriteStartElementAsync("armors");

                        foreach (Armor objArmor in _objCharacter.Armor)
                        {
                            // <armor>
                            await objWriter.WriteStartElementAsync("armor");

                            await objWriter.WriteElementStringAsync("name", objArmor.Name);

                            if (objArmor.ArmorMods.Count > 0)
                            {
                                // <mods>
                                await objWriter.WriteStartElementAsync("mods");

                                foreach (ArmorMod objMod in objArmor.ArmorMods)
                                {
                                    // <mod>
                                    await objWriter.WriteStartElementAsync("mod");

                                    await objWriter.WriteElementStringAsync("name", objMod.Name);

                                    if (objMod.Rating > 0)
                                    {
                                        await objWriter.WriteElementStringAsync("rating", objMod.Rating.ToString(GlobalSettings.InvariantCultureInfo));
                                    }
                                    // </mod>
                                    await objWriter.WriteEndElementAsync();
                                }

                                // </mods>
                                await objWriter.WriteEndElementAsync();
                            }

                            if (objArmor.GearChildren.Count > 0)
                            {
                                await WriteGear(objWriter, objArmor.GearChildren);
                            }

                            // </armor>
                            await objWriter.WriteEndElementAsync();
                        }

                        // </armors>
                        await objWriter.WriteEndElementAsync();
                    }

                    // Export Weapons.
                    if (await chkWeapons.DoThreadSafeFuncAsync(x => x.Checked))
                    {
                        // <weapons>
                        await objWriter.WriteStartElementAsync("weapons");

                        foreach (Weapon objWeapon in _objCharacter.Weapons)
                        {
                            // Don't attempt to export Cyberware and Gear Weapons since those are handled by those object types. The default Unarmed Attack Weapon should also not be exported.
                            if (objWeapon.Category != "Cyberware" && objWeapon.Category != "Gear" && objWeapon.Name != "Unarmed Attack")
                            {
                                // <weapon>
                                await objWriter.WriteStartElementAsync("weapon");

                                await objWriter.WriteElementStringAsync("name", objWeapon.Name);

                                // Weapon Accessories.
                                if (objWeapon.WeaponAccessories.Count > 0)
                                {
                                    // <accessories>
                                    await objWriter.WriteStartElementAsync("accessories");

                                    foreach (WeaponAccessory objAccessory in objWeapon.WeaponAccessories)
                                    {
                                        // Don't attempt to export items included in the Weapon.
                                        if (!objAccessory.IncludedInWeapon)
                                        {
                                            // <accessory>
                                            await objWriter.WriteStartElementAsync("accessory");

                                            await objWriter.WriteElementStringAsync("name", objAccessory.Name);

                                            await objWriter.WriteElementStringAsync("mount", objAccessory.Mount);

                                            await objWriter.WriteElementStringAsync("extramount", objAccessory.ExtraMount);

                                            if (objAccessory.GearChildren.Count > 0)
                                            {
                                                await WriteGear(objWriter, objAccessory.GearChildren);
                                            }

                                            // </accessory>
                                            await objWriter.WriteEndElementAsync();
                                        }
                                    }

                                    // </accessories>
                                    await objWriter.WriteEndElementAsync();
                                }

                                // Underbarrel Weapon.
                                if (objWeapon.UnderbarrelWeapons.Count > 0)
                                {
                                    foreach (Weapon objUnderbarrelWeapon in objWeapon.UnderbarrelWeapons)
                                    {
                                        if (!objUnderbarrelWeapon.IncludedInWeapon)
                                        {
                                            await objWriter.WriteElementStringAsync("underbarrel", objUnderbarrelWeapon.Name);
                                        }
                                    }
                                }

                                // </weapon>
                                await objWriter.WriteEndElementAsync();
                            }
                        }

                        // </weapons>
                        await objWriter.WriteEndElementAsync();
                    }

                    // Export Gear.
                    if (await chkGear.DoThreadSafeFuncAsync(x => x.Checked))
                    {
                        await WriteGear(objWriter, _objCharacter.Gear);
                    }

                    // Export Vehicles.
                    if (await chkVehicles.DoThreadSafeFuncAsync(x => x.Checked))
                    {
                        // <vehicles>
                        await objWriter.WriteStartElementAsync("vehicles");

                        foreach (Vehicle objVehicle in _objCharacter.Vehicles)
                        {
                            bool blnWeapons = false;
                            // <vehicle>
                            await objWriter.WriteStartElementAsync("vehicle");

                            await objWriter.WriteElementStringAsync("name", objVehicle.Name);

                            if (objVehicle.Mods.Count > 0)
                            {
                                // <mods>
                                await objWriter.WriteStartElementAsync("mods");

                                foreach (VehicleMod objVehicleMod in objVehicle.Mods)
                                {
                                    // Only write out the Mods that are not part of the base vehicle.
                                    if (!objVehicleMod.IncludedInVehicle)
                                    {
                                        // <mod>
                                        await objWriter.WriteStartElementAsync("mod");

                                        await objWriter.WriteElementStringAsync("name", objVehicleMod.Name);

                                        if (objVehicleMod.Rating > 0)
                                        {
                                            await objWriter.WriteElementStringAsync("rating", objVehicleMod.Rating.ToString(GlobalSettings.InvariantCultureInfo));
                                        }
                                        // </mod>
                                        await objWriter.WriteEndElementAsync();

                                        // See if this is a Weapon Mount with Weapons.
                                        if (objVehicleMod.Weapons.Count > 0)
                                        {
                                            blnWeapons = true;
                                        }
                                    }
                                    else
                                    {
                                        // See if this is a Weapon Mount with Weapons.
                                        if (objVehicleMod.Weapons.Count > 0)
                                        {
                                            blnWeapons = true;
                                        }
                                    }
                                }

                                // </mods>
                                await objWriter.WriteEndElementAsync();
                            }

                            // If there are Weapons, add them.
                            if (blnWeapons)
                            {
                                // <weapons>
                                await objWriter.WriteStartElementAsync("weapons");

                                foreach (VehicleMod objVehicleMod in objVehicle.Mods)
                                {
                                    foreach (Weapon objWeapon in objVehicleMod.Weapons)
                                    {
                                        // <weapon>
                                        await objWriter.WriteStartElementAsync("weapon");

                                        await objWriter.WriteElementStringAsync("name", objWeapon.Name);

                                        // Weapon Accessories.
                                        if (objWeapon.WeaponAccessories.Count > 0)
                                        {
                                            // <accessories>
                                            await objWriter.WriteStartElementAsync("accessories");

                                            foreach (WeaponAccessory objAccessory in objWeapon.WeaponAccessories)
                                            {
                                                // Don't attempt to export items included in the Weapon.
                                                if (!objAccessory.IncludedInWeapon)
                                                {
                                                    // <accessory>
                                                    await objWriter.WriteStartElementAsync("accessory");

                                                    await objWriter.WriteElementStringAsync("name", objAccessory.Name);

                                                    await objWriter.WriteElementStringAsync("mount", objAccessory.Mount);

                                                    await objWriter.WriteElementStringAsync("extramount", objAccessory.ExtraMount);

                                                    // </accessory>
                                                    await objWriter.WriteEndElementAsync();
                                                }
                                            }

                                            // </accessories>
                                            await objWriter.WriteEndElementAsync();
                                        }

                                        // Underbarrel Weapon.
                                        if (objWeapon.UnderbarrelWeapons.Count > 0)
                                        {
                                            foreach (Weapon objUnderbarrelWeapon in objWeapon.UnderbarrelWeapons)
                                            {
                                                await objWriter.WriteElementStringAsync("underbarrel", objUnderbarrelWeapon.Name);
                                            }
                                        }

                                        // </weapon>
                                        await objWriter.WriteEndElementAsync();
                                    }
                                }

                                // </weapons>
                                await objWriter.WriteEndElementAsync();
                            }

                            // Gear.
                            if (objVehicle.GearChildren.Count > 0)
                            {
                                await WriteGear(objWriter, objVehicle.GearChildren);
                            }

                            // </vehicle>
                            await objWriter.WriteEndElementAsync();
                        }

                        // </vehicles>
                        await objWriter.WriteEndElementAsync();
                    }

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

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

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

                    await objWriter.WriteEndDocumentAsync();
                }
            }

            Program.ShowMessageBox(this, string.Format(GlobalSettings.CultureInfo, await LanguageManager.GetStringAsync("Message_CreatePACKSKit_SuiteCreated"), strName),
                                   await LanguageManager.GetStringAsync("MessageTitle_CreatePACKSKit_SuiteCreated"), MessageBoxButtons.OK, MessageBoxIcon.Information);
            await this.DoThreadSafeAsync(x =>
            {
                x.DialogResult = DialogResult.OK;
                x.Close();
            });
        }
示例#2
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;
        }
        private async void cmdOK_Click(object sender, EventArgs e)
        {
            // Make sure the suite and file name fields are populated.
            if (string.IsNullOrEmpty(txtName.Text))
            {
                Program.ShowMessageBox(this, await LanguageManager.GetStringAsync("Message_CyberwareSuite_SuiteName"), await LanguageManager.GetStringAsync("MessageTitle_CyberwareSuite_SuiteName"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (string.IsNullOrEmpty(txtFileName.Text))
            {
                Program.ShowMessageBox(this, await LanguageManager.GetStringAsync("Message_CyberwareSuite_FileName"), await LanguageManager.GetStringAsync("MessageTitle_CyberwareSuite_FileName"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            // Make sure the file name starts with custom and ends with _cyberware.xml.
            if (!txtFileName.Text.StartsWith("custom_", StringComparison.OrdinalIgnoreCase) || !txtFileName.Text.EndsWith('_' + _strType + ".xml", StringComparison.OrdinalIgnoreCase))
            {
                Program.ShowMessageBox(this, string.Format(GlobalSettings.CultureInfo, await LanguageManager.GetStringAsync("Message_CyberwareSuite_InvalidFileName"), _strType),
                                       await LanguageManager.GetStringAsync("MessageTitle_CyberwareSuite_InvalidFileName"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            // See if a Suite with this name already exists for the Custom category.
            // This was originally done without the XmlManager, but because amends and overrides and toggling custom data directories can change names, we need to use it.
            string strName = txtName.Text;

            if ((await _objCharacter.LoadDataXPathAsync(_strType + ".xml")).SelectSingleNode("/chummer/suites/suite[name = " + strName.CleanXPath() + ']') != null)
            {
                Program.ShowMessageBox(this, string.Format(GlobalSettings.CultureInfo, await LanguageManager.GetStringAsync("Message_CyberwareSuite_DuplicateName"), strName),
                                       await LanguageManager.GetStringAsync("MessageTitle_CyberwareSuite_DuplicateName"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            string strPath = Path.Combine(Utils.GetStartupPath, "data", txtFileName.Text);

            bool blnNewFile = !File.Exists(strPath);

            // If this is not a new file, read in the existing contents.
            XmlDocument objXmlCurrentDocument = new XmlDocument {
                XmlResolver = null
            };

            if (!blnNewFile)
            {
                try
                {
                    objXmlCurrentDocument.LoadStandard(strPath);
                }
                catch (IOException ex)
                {
                    Program.ShowMessageBox(this, ex.ToString());
                    return;
                }
                catch (XmlException ex)
                {
                    Program.ShowMessageBox(this, ex.ToString());
                    return;
                }
            }

            using (FileStream objStream = new FileStream(strPath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
            {
                using (XmlWriter objWriter = Utils.GetStandardXmlWriter(objStream))
                {
                    await objWriter.WriteStartDocumentAsync();

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

                    if (!blnNewFile)
                    {
                        // <cyberwares>
                        await objWriter.WriteStartElementAsync(_strType + 's');

                        using (XmlNodeList xmlCyberwareList = objXmlCurrentDocument.SelectNodes("/chummer/" + _strType + 's'))
                            if (xmlCyberwareList?.Count > 0)
                            {
                                foreach (XmlNode xmlCyberware in xmlCyberwareList)
                                {
                                    xmlCyberware.WriteContentTo(objWriter);
                                }
                            }
                        // </cyberwares>
                        await objWriter.WriteEndElementAsync();
                    }

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

                    // If this is not a new file, write out the current contents.
                    if (!blnNewFile)
                    {
                        using (XmlNodeList xmlCyberwareList = objXmlCurrentDocument.SelectNodes("/chummer/suites"))
                            if (xmlCyberwareList?.Count > 0)
                            {
                                foreach (XmlNode xmlCyberware in xmlCyberwareList)
                                {
                                    xmlCyberware.WriteContentTo(objWriter);
                                }
                            }
                    }

                    string strGrade = string.Empty;
                    // Determine the Grade of Cyberware.
                    foreach (Cyberware objCyberware in _objCharacter.Cyberware)
                    {
                        if (objCyberware.SourceType == _objSource)
                        {
                            strGrade = objCyberware.Grade.Name;
                            break;
                        }
                    }

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

                    // <name />
                    await objWriter.WriteElementStringAsync("id", Guid.NewGuid().ToString());

                    // <name />
                    await objWriter.WriteElementStringAsync("name", txtName.Text);

                    // <grade />
                    await objWriter.WriteElementStringAsync("grade", strGrade);

                    // <cyberwares>
                    await objWriter.WriteStartElementAsync(_strType + 's');

                    // Write out the Cyberware.
                    foreach (Cyberware objCyberware in _objCharacter.Cyberware)
                    {
                        if (objCyberware.SourceType == _objSource)
                        {
                            // <cyberware>
                            await objWriter.WriteStartElementAsync(_strType);

                            await objWriter.WriteElementStringAsync("name", objCyberware.Name);

                            if (objCyberware.Rating > 0)
                            {
                                await objWriter.WriteElementStringAsync("rating", objCyberware.Rating.ToString(GlobalSettings.InvariantCultureInfo));
                            }
                            // Write out child items.
                            if (objCyberware.Children.Count > 0)
                            {
                                // <cyberwares>
                                await objWriter.WriteStartElementAsync(_strType + 's');

                                foreach (Cyberware objChild in objCyberware.Children)
                                {
                                    // Do not include items that come with the base item by default.
                                    if (objChild.Capacity != "[*]")
                                    {
                                        await objWriter.WriteStartElementAsync(_strType);

                                        await objWriter.WriteElementStringAsync("name", objChild.Name);

                                        if (objChild.Rating > 0)
                                        {
                                            await objWriter.WriteElementStringAsync("rating", objChild.Rating.ToString(GlobalSettings.InvariantCultureInfo));
                                        }
                                        // </cyberware>
                                        await objWriter.WriteEndElementAsync();
                                    }
                                }

                                // </cyberwares>
                                await objWriter.WriteEndElementAsync();
                            }

                            // </cyberware>
                            await objWriter.WriteEndElementAsync();
                        }
                    }

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

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

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

                    await objWriter.WriteEndDocumentAsync();
                }
            }

            Program.ShowMessageBox(this, string.Format(GlobalSettings.CultureInfo, await LanguageManager.GetStringAsync("Message_CyberwareSuite_SuiteCreated"), txtName.Text),
                                   await LanguageManager.GetStringAsync("MessageTitle_CyberwareSuite_SuiteCreated"), MessageBoxButtons.OK, MessageBoxIcon.Information);
            DialogResult = DialogResult.OK;
        }