view api/Factories/Xml/WarFoundryXmlSaver.cs @ 115:d0c60b3204c1

Re #53: Add XML army saving * Make file identifiers public * Add army identifier to comment no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sat, 22 Aug 2009 19:41:15 +0000
parents 863518044d38
children 571eee2b7b30
line source
1 // This file (WarFoundryXmlSaver.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2008, 2009 IBBoard.
2 //
3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
5 using System;
6 using System.Collections.Generic;
7 using System.IO;
8 using System.Xml;
9 using System.Xml.Schema;
10 using IBBoard.Lang;
11 using IBBoard.Xml;
12 using IBBoard.WarFoundry.API.Factories.Xml.Zip;
13 using IBBoard.WarFoundry.API.Objects;
14 using IBBoard.WarFoundry.API.Savers;
15 using ICSharpCode.SharpZipLib.Zip;
17 namespace IBBoard.WarFoundry.API.Factories.Xml
18 {
19 public class WarFoundryXmlSaver : IWarFoundryFileSaver
20 {
21 public const string ARMY_FILE_EXTENSION = ".army";
23 public bool Save(Army toSave, string savePath)
24 {
25 bool success = false;
26 ZipFile file = null;
28 if (!savePath.EndsWith(ARMY_FILE_EXTENSION))
29 {
30 savePath = savePath + ARMY_FILE_EXTENSION;
31 }
33 try
34 {
35 file = ZipFile.Create(savePath);
36 file.BeginUpdate();
37 file.Add(new StringZipEntrySource(CreateXmlString(toSave)), "data.armyx");
38 file.SetComment(AbstractNativeWarFoundryFactory.ARMY_ZIP_IDENTIFIER);
39 file.CommitUpdate();
40 success = true;
41 }
42 finally
43 {
44 if (file != null)
45 {
46 file.Close();
47 }
48 }
50 return success;
51 }
53 private string CreateXmlString(WarFoundryObject toSave)
54 {
55 string xmlString = "";
57 if (toSave is Army)
58 {
59 xmlString = CreateArmyXmlString((Army)toSave);
60 }
62 return xmlString;
63 }
65 private string CreateArmyXmlString(Army toSave)
66 {
67 XmlDocument doc = new XmlDocument();
68 XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null);
69 doc.AppendChild(declaration);
70 XmlSchema schema = new XmlSchema();
71 schema.Namespaces.Add("xmlns", "http://ibboard.co.uk/warfoundry/army");
72 schema.Namespaces.Add("xmlns:core", "http://ibboard.co.uk/warfoundry/core");
73 doc.Schemas.Add(schema);
74 XmlElement root = doc.CreateElement("army");
75 root.SetAttribute("xmlns", "http://ibboard.co.uk/warfoundry/army");
76 root.SetAttribute("xmlns:core", "http://ibboard.co.uk/warfoundry/core");
77 doc.AppendChild(root);
78 root.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(toSave.ID));
79 root.SetAttribute("name", toSave.Name);
80 //Don't convert system and race to ID format as they could be stored in non-XML file formats
81 //If they are in XML files then they'll already be valid
82 root.SetAttribute("system", toSave.GameSystem.ID);
83 root.SetAttribute("race", toSave.Race.ID);
84 root.SetAttribute("maxPoints", toSave.MaxPoints.ToString());
85 XmlElement units = doc.CreateElement("units");
86 root.AppendChild(units);
88 foreach (Unit unit in toSave.GetUnits())
89 {
90 units.AppendChild(CreateUnitElement(unit, doc));
91 }
93 return doc.OuterXml;
94 }
96 private XmlElement CreateUnitElement(Unit unit, XmlDocument doc)
97 {
98 XmlElement unitElem = doc.CreateElement("unit");
99 unitElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(unit.ID));
100 unitElem.SetAttribute("unitName", unit.Name);
101 unitElem.SetAttribute("unitType", unit.UnitType.ID);
102 unitElem.SetAttribute("size", unit.Size.ToString());
104 if (!unit.Race.Equals(unit.Army.Race))
105 {
106 unitElem.SetAttribute("race", unit.Race.ID);
107 }
109 XmlElement equipmentElem = CreateEquipmentItemsElement(unit, doc);
111 if (equipmentElem != null)
112 {
113 unitElem.AppendChild(equipmentElem);
114 }
116 XmlElement containedElem = CreateContainedUnitsElement(unit, doc);
118 if (containedElem != null)
119 {
120 unitElem.AppendChild(containedElem);
121 }
123 return unitElem;
124 }
126 private XmlElement CreateEquipmentItemsElement(Unit unit, XmlDocument doc)
127 {
128 UnitEquipmentItem[] equipItems = unit.GetEquipment();
129 int equipItemCount = equipItems.Length;
130 XmlElement equipmentElem = null;
132 if (equipItemCount > 0)
133 {
134 equipmentElem = doc.CreateElement("equipment");
136 for (int i = 0; i < equipItemCount; i++)
137 {
138 equipmentElem.AppendChild(CreateEquipmentElement(equipItems[i], unit, doc));
139 }
140 }
142 return equipmentElem;
143 }
145 private XmlElement CreateEquipmentElement(UnitEquipmentItem item, Unit unit, XmlDocument doc)
146 {
147 XmlElement equipmentItemElem = doc.CreateElement("equipItem");
148 equipmentItemElem.SetAttribute("id", item.EquipmentItemID);
149 equipmentItemElem.SetAttribute("amount", unit.GetEquipmentAmount(item).ToString());
150 equipmentItemElem.SetAttribute("amountType", unit.GetEquipmentAmountIsRatio(item) ? "ratio" : "fixed");
151 return equipmentItemElem;
152 }
154 private XmlElement CreateContainedUnitsElement(Unit unit, XmlDocument doc)
155 {
156 Unit[] containedUnits = unit.ContainedUnits;
157 int containedCount = containedUnits.Length;
158 XmlElement containedElem = null;
160 if (containedCount > 0)
161 {
162 containedElem = doc.CreateElement("contained");
164 for (int i = 0; i < containedCount; i++)
165 {
166 containedElem.AppendChild(CreateContainedUnitElement(containedUnits[i], doc));
167 }
168 }
170 return containedElem;
171 }
173 private XmlElement CreateContainedUnitElement(Unit unit, XmlDocument doc)
174 {
175 XmlElement containedUnitElem = doc.CreateElement("containedUnit");
176 containedUnitElem.SetAttribute("containedID", XmlTools.GetAsciiXmlIdForString(unit.ID));
177 return containedUnitElem;
178 }
179 }
180 }