private void ExportFeat(FeatViewModel featViewModel) { Microsoft.Win32.SaveFileDialog saveFileDialog = new Microsoft.Win32.SaveFileDialog(); saveFileDialog.Filter = "XML Document|*.xml"; saveFileDialog.Title = "Save Feat"; saveFileDialog.FileName = featViewModel.Name; if (saveFileDialog.ShowDialog() == true) { try { string ext = System.IO.Path.GetExtension(saveFileDialog.FileName); if (ext == ".xml") { string xml = _xmlExporter.FormatXMLWithHeader(_xmlExporter.GetXML(featViewModel.FeatModel)); System.IO.File.WriteAllText(saveFileDialog.FileName, xml, System.Text.Encoding.UTF8); } else { _dialogService.ShowConfirmationDialog("Unable To Export", "Invalid file extension.", "OK", null, null); } } catch (Exception) { _dialogService.ShowConfirmationDialog("Unable To Export", "An error occurred when attempting to export the feat.", "OK", null, null); } } }
/// <summary> /// Creates a character archive from the character model /// </summary> public byte[] CreateCharacterArchive(CharacterModel characterModel) { byte[] bytes = null; using (MemoryStream stream = new MemoryStream()) { using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create)) { ZipArchiveEntry entry = archive.CreateEntry("character.ccc"); using (BinaryWriter writer = new BinaryWriter(entry.Open())) { byte[] characterBytes = GetCharacterBytes(characterModel); writer.Write(characterBytes); } archive.CreateEntry("resources/"); ZipArchiveEntry backgrounds = archive.CreateEntry("resources/backgrounds.xml"); using (StreamWriter writer = new StreamWriter(backgrounds.Open(), Encoding.UTF8)) { string xml = _xmlExporter.GetXML(characterModel.Background); xml = _xmlExporter.WrapAndFormatXMLWithHeader(xml); writer.Write(xml); } ZipArchiveEntry classes = archive.CreateEntry("resources/classes.xml"); using (StreamWriter writer = new StreamWriter(classes.Open(), Encoding.UTF8)) { string xml = String.Empty; List <Guid> ids = new List <Guid>(); foreach (LevelModel levelModel in characterModel.Levels) { if (levelModel.Class != null && !ids.Any(x => x == levelModel.Class.Id)) { ids.Add(levelModel.Class.Id); xml += _xmlExporter.GetXML(levelModel.Class); } } xml = _xmlExporter.WrapAndFormatXMLWithHeader(xml); writer.Write(xml); } ZipArchiveEntry conditions = archive.CreateEntry("resources/conditions.xml"); using (StreamWriter writer = new StreamWriter(conditions.Open(), Encoding.UTF8)) { string xml = String.Empty; List <Guid> ids = new List <Guid>(); foreach (AppliedConditionModel appliedCondition in characterModel.Conditions) { if (appliedCondition.ConditionModel != null && !ids.Any(x => x == appliedCondition.ConditionModel.Id)) { ids.Add(appliedCondition.ConditionModel.Id); xml += _xmlExporter.GetXML(appliedCondition.ConditionModel); } } xml = _xmlExporter.WrapAndFormatXMLWithHeader(xml); writer.Write(xml); } ZipArchiveEntry feats = archive.CreateEntry("resources/feats.xml"); using (StreamWriter writer = new StreamWriter(feats.Open(), Encoding.UTF8)) { string xml = String.Empty; List <Guid> ids = new List <Guid>(); foreach (LevelModel levelModel in characterModel.Levels) { foreach (FeatModel feat in levelModel.Feats) { if (!ids.Any(x => x == feat.Id)) { ids.Add(feat.Id); xml += _xmlExporter.GetXML(feat); } } } xml = _xmlExporter.WrapAndFormatXMLWithHeader(xml); writer.Write(xml); } ZipArchiveEntry items = archive.CreateEntry("resources/items.xml"); using (StreamWriter writer = new StreamWriter(items.Open(), Encoding.UTF8)) { string xml = String.Empty; List <Guid> ids = new List <Guid>(); foreach (BagModel bagModel in characterModel.Bags) { foreach (EquipmentModel equipmentModel in bagModel.Equipment) { if (equipmentModel.Item != null && !ids.Any(x => x == equipmentModel.Item.Id)) { ids.Add(equipmentModel.Item.Id); xml += _xmlExporter.GetXML(equipmentModel.Item); } } } xml = _xmlExporter.WrapAndFormatXMLWithHeader(xml); writer.Write(xml); } ZipArchiveEntry languages = archive.CreateEntry("resources/languages.csv"); using (StreamWriter writer = new StreamWriter(languages.Open(), Encoding.UTF8)) { foreach (LanguageModel language in characterModel.Languages) { writer.WriteLine($"{language.Id},{language.Name}"); } } ZipArchiveEntry monsters = archive.CreateEntry("resources/monsters.xml"); using (StreamWriter writer = new StreamWriter(monsters.Open(), Encoding.UTF8)) { string xml = String.Empty; List <Guid> ids = new List <Guid>(); foreach (CompanionModel companionModel in characterModel.Companions) { if (companionModel.MonsterModel != null && !ids.Any(x => x == companionModel.MonsterModel.Id)) { ids.Add(companionModel.MonsterModel.Id); xml += _xmlExporter.GetXML(companionModel.MonsterModel); } } xml = _xmlExporter.WrapAndFormatXMLWithHeader(xml); writer.Write(xml); } ZipArchiveEntry races = archive.CreateEntry("resources/races.xml"); using (StreamWriter writer = new StreamWriter(races.Open(), Encoding.UTF8)) { string xml = _xmlExporter.GetXML(characterModel.Race); xml = _xmlExporter.WrapAndFormatXMLWithHeader(xml); writer.Write(xml); } ZipArchiveEntry spells = archive.CreateEntry("resources/spells.xml"); using (StreamWriter writer = new StreamWriter(spells.Open(), Encoding.UTF8)) { string xml = String.Empty; List <Guid> ids = new List <Guid>(); foreach (SpellbookModel spellbookModel in characterModel.Spellbooks) { foreach (SpellbookEntryModel spellbookEntryModel in spellbookModel.Spells) { if (spellbookEntryModel.Spell != null && !ids.Any(x => x == spellbookEntryModel.Spell.Id)) { ids.Add(spellbookEntryModel.Spell.Id); xml += _xmlExporter.GetXML(spellbookEntryModel.Spell); } } } xml = _xmlExporter.WrapAndFormatXMLWithHeader(xml); writer.Write(xml); } } bytes = stream.ToArray(); } return(bytes); }