Mercurial > repos > IBBoard.WarFoundry.API
annotate api/Factories/Xml/WarFoundryXmlSaver.cs @ 260:b72cc74a240b
Re #270: Add multiple categories to API
* Save out category if it isn't the main category
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Fri, 21 May 2010 19:41:02 +0000 |
parents | ca2905c9b225 |
children | 5434e648379c |
rev | line source |
---|---|
104
2f3cafb69799
Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
1 // This file (WarFoundryXmlSaver.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2008, 2009 IBBoard. |
15 | 2 // |
104
2f3cafb69799
Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
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. |
15 | 4 |
0 | 5 using System; |
6 using System.Collections.Generic; | |
7 using System.IO; | |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
8 using System.Xml; |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
9 using System.Xml.Schema; |
0 | 10 using IBBoard.Lang; |
109 | 11 using IBBoard.Xml; |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
12 using IBBoard.WarFoundry.API.Factories.Xml.Zip; |
216 | 13 using IBBoard.WarFoundry.API.Objects; |
14 using IBBoard.WarFoundry.API.Savers; | |
214
1b718b67f7f6
Re #179: Make sure that translations are used throughout UI
IBBoard <dev@ibboard.co.uk>
parents:
137
diff
changeset
|
15 using IBBoard.WarFoundry.API.Util; |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
16 using ICSharpCode.SharpZipLib.Zip; |
0 | 17 |
18 namespace IBBoard.WarFoundry.API.Factories.Xml | |
19 { | |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
20 public class WarFoundryXmlSaver : IWarFoundryFileSaver |
0 | 21 { |
109 | 22 public const string ARMY_FILE_EXTENSION = ".army"; |
23 | |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
24 public bool Save(Army toSave, string savePath) |
0 | 25 { |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
26 bool success = false; |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
27 ZipFile file = null; |
109 | 28 |
29 if (!savePath.EndsWith(ARMY_FILE_EXTENSION)) | |
30 { | |
31 savePath = savePath + ARMY_FILE_EXTENSION; | |
32 } | |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
33 |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
34 try |
0 | 35 { |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
36 file = ZipFile.Create(savePath); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
37 file.BeginUpdate(); |
110 | 38 file.Add(new StringZipEntrySource(CreateXmlString(toSave)), "data.armyx"); |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
39 file.CommitUpdate(); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
40 success = true; |
0 | 41 } |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
42 finally |
0 | 43 { |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
44 if (file != null) |
82 | 45 { |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
46 file.Close(); |
0 | 47 } |
48 } | |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
49 |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
50 return success; |
0 | 51 } |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
52 |
0 | 53 private string CreateXmlString(WarFoundryObject toSave) |
54 { | |
108 | 55 string xmlString = ""; |
56 | |
57 if (toSave is Army) | |
58 { | |
59 xmlString = CreateArmyXmlString((Army)toSave); | |
60 } | |
61 | |
62 return xmlString; | |
63 } | |
64 | |
65 private string CreateArmyXmlString(Army toSave) | |
66 { | |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
67 XmlDocument doc = new XmlDocument(); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
68 XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
69 doc.AppendChild(declaration); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
70 XmlSchema schema = new XmlSchema(); |
125
c60fd5664698
Re #53: Create WarFoundry save class to store army as file
IBBoard <dev@ibboard.co.uk>
parents:
122
diff
changeset
|
71 schema.Namespaces.Add("", "http://ibboard.co.uk/warfoundry/army"); |
c60fd5664698
Re #53: Create WarFoundry save class to store army as file
IBBoard <dev@ibboard.co.uk>
parents:
122
diff
changeset
|
72 schema.Namespaces.Add("core", "http://ibboard.co.uk/warfoundry/core"); |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
73 doc.Schemas.Add(schema); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
74 XmlElement root = doc.CreateElement("army"); |
112
863518044d38
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
110
diff
changeset
|
75 root.SetAttribute("xmlns", "http://ibboard.co.uk/warfoundry/army"); |
863518044d38
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
110
diff
changeset
|
76 root.SetAttribute("xmlns:core", "http://ibboard.co.uk/warfoundry/core"); |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
77 doc.AppendChild(root); |
109 | 78 root.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(toSave.ID)); |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
79 root.SetAttribute("name", toSave.Name); |
109 | 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 | |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
82 root.SetAttribute("system", toSave.GameSystem.ID); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
83 root.SetAttribute("race", toSave.Race.ID); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
84 root.SetAttribute("maxPoints", toSave.MaxPoints.ToString()); |
106 | 85 XmlElement units = doc.CreateElement("units"); |
86 root.AppendChild(units); | |
87 | |
88 foreach (Unit unit in toSave.GetUnits()) | |
89 { | |
108 | 90 units.AppendChild(CreateUnitElement(unit, doc)); |
106 | 91 } |
92 | |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
93 return doc.OuterXml; |
0 | 94 } |
108 | 95 |
96 private XmlElement CreateUnitElement(Unit unit, XmlDocument doc) | |
97 { | |
98 XmlElement unitElem = doc.CreateElement("unit"); | |
109 | 99 unitElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(unit.ID)); |
137
f58051572ec7
Fixes #128: Don't save out default names
IBBoard <dev@ibboard.co.uk>
parents:
125
diff
changeset
|
100 unitElem.SetAttribute("unitName", (unit.HasDefaultName() ? "" : unit.Name)); |
108 | 101 unitElem.SetAttribute("unitType", unit.UnitType.ID); |
102 unitElem.SetAttribute("size", unit.Size.ToString()); | |
103 | |
104 if (!unit.Race.Equals(unit.Army.Race)) | |
105 { | |
106 unitElem.SetAttribute("race", unit.Race.ID); | |
107 } | |
260
b72cc74a240b
Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents:
236
diff
changeset
|
108 |
b72cc74a240b
Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents:
236
diff
changeset
|
109 Category unitCategory = unit.Category.Category; |
b72cc74a240b
Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents:
236
diff
changeset
|
110 if (!unit.UnitType.MainCategory.Equals(unitCategory)) |
b72cc74a240b
Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents:
236
diff
changeset
|
111 { |
b72cc74a240b
Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents:
236
diff
changeset
|
112 unitElem.SetAttribute("category", unitCategory.ID); |
b72cc74a240b
Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents:
236
diff
changeset
|
113 } |
108 | 114 |
115 XmlElement equipmentElem = CreateEquipmentItemsElement(unit, doc); | |
116 | |
117 if (equipmentElem != null) | |
118 { | |
119 unitElem.AppendChild(equipmentElem); | |
120 } | |
121 | |
109 | 122 XmlElement containedElem = CreateContainedUnitsElement(unit, doc); |
123 | |
124 if (containedElem != null) | |
125 { | |
126 unitElem.AppendChild(containedElem); | |
127 } | |
128 | |
108 | 129 return unitElem; |
130 } | |
131 | |
132 private XmlElement CreateEquipmentItemsElement(Unit unit, XmlDocument doc) | |
133 { | |
134 UnitEquipmentItem[] equipItems = unit.GetEquipment(); | |
135 int equipItemCount = equipItems.Length; | |
136 XmlElement equipmentElem = null; | |
137 | |
138 if (equipItemCount > 0) | |
139 { | |
140 equipmentElem = doc.CreateElement("equipment"); | |
141 | |
142 for (int i = 0; i < equipItemCount; i++) | |
143 { | |
144 equipmentElem.AppendChild(CreateEquipmentElement(equipItems[i], unit, doc)); | |
145 } | |
146 } | |
147 | |
148 return equipmentElem; | |
149 } | |
150 | |
151 private XmlElement CreateEquipmentElement(UnitEquipmentItem item, Unit unit, XmlDocument doc) | |
152 { | |
153 XmlElement equipmentItemElem = doc.CreateElement("equipItem"); | |
122 | 154 equipmentItemElem.SetAttribute("id", item.ID); |
216 | 155 equipmentItemElem.SetAttribute("amount", UnitEquipmentUtil.GetEquipmentAmount(unit, item).ToString()); |
214
1b718b67f7f6
Re #179: Make sure that translations are used throughout UI
IBBoard <dev@ibboard.co.uk>
parents:
137
diff
changeset
|
156 equipmentItemElem.SetAttribute("amountType", UnitEquipmentUtil.GetEquipmentAmountIsRatio(unit, item) ? "ratio" : "fixed"); |
108 | 157 return equipmentItemElem; |
158 } | |
109 | 159 |
160 private XmlElement CreateContainedUnitsElement(Unit unit, XmlDocument doc) | |
161 { | |
162 Unit[] containedUnits = unit.ContainedUnits; | |
163 int containedCount = containedUnits.Length; | |
164 XmlElement containedElem = null; | |
165 | |
166 if (containedCount > 0) | |
167 { | |
168 containedElem = doc.CreateElement("contained"); | |
169 | |
170 for (int i = 0; i < containedCount; i++) | |
171 { | |
172 containedElem.AppendChild(CreateContainedUnitElement(containedUnits[i], doc)); | |
173 } | |
174 } | |
175 | |
176 return containedElem; | |
177 } | |
178 | |
179 private XmlElement CreateContainedUnitElement(Unit unit, XmlDocument doc) | |
180 { | |
181 XmlElement containedUnitElem = doc.CreateElement("containedUnit"); | |
182 containedUnitElem.SetAttribute("containedID", XmlTools.GetAsciiXmlIdForString(unit.ID)); | |
183 return containedUnitElem; | |
184 } | |
0 | 185 } |
186 } |