Mercurial > repos > IBBoard.WarFoundry.API
annotate api/Factories/Xml/WarFoundryXmlSaver.cs @ 221:78f7456f6419
Re #228: Crash with missing abilityID
* Quick fix to check if an ability ID doesn't match an ability and throw a standard error if it isn't found
TODO:
* Need to check why it isn't handled by the attribute type (IDREF)
* Need to test it works!
Problem shouldn't occur for <notes> because it is just a block of text and isn't referenced anywhere
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 07 Dec 2009 19:40:12 +0000 |
parents | 65553d2c8612 |
children | ca2905c9b225 |
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"); |
115 | 39 file.SetComment(AbstractNativeWarFoundryFactory.ARMY_ZIP_IDENTIFIER); |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
40 file.CommitUpdate(); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
41 success = true; |
0 | 42 } |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
43 finally |
0 | 44 { |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
45 if (file != null) |
82 | 46 { |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
47 file.Close(); |
0 | 48 } |
49 } | |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
50 |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
51 return success; |
0 | 52 } |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
53 |
0 | 54 private string CreateXmlString(WarFoundryObject toSave) |
55 { | |
108 | 56 string xmlString = ""; |
57 | |
58 if (toSave is Army) | |
59 { | |
60 xmlString = CreateArmyXmlString((Army)toSave); | |
61 } | |
62 | |
63 return xmlString; | |
64 } | |
65 | |
66 private string CreateArmyXmlString(Army toSave) | |
67 { | |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
68 XmlDocument doc = new XmlDocument(); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
69 XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
70 doc.AppendChild(declaration); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
71 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
|
72 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
|
73 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
|
74 doc.Schemas.Add(schema); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
75 XmlElement root = doc.CreateElement("army"); |
112
863518044d38
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
110
diff
changeset
|
76 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
|
77 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
|
78 doc.AppendChild(root); |
109 | 79 root.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(toSave.ID)); |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
80 root.SetAttribute("name", toSave.Name); |
109 | 81 //Don't convert system and race to ID format as they could be stored in non-XML file formats |
82 //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
|
83 root.SetAttribute("system", toSave.GameSystem.ID); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
84 root.SetAttribute("race", toSave.Race.ID); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
85 root.SetAttribute("maxPoints", toSave.MaxPoints.ToString()); |
106 | 86 XmlElement units = doc.CreateElement("units"); |
87 root.AppendChild(units); | |
88 | |
89 foreach (Unit unit in toSave.GetUnits()) | |
90 { | |
108 | 91 units.AppendChild(CreateUnitElement(unit, doc)); |
106 | 92 } |
93 | |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
94 return doc.OuterXml; |
0 | 95 } |
108 | 96 |
97 private XmlElement CreateUnitElement(Unit unit, XmlDocument doc) | |
98 { | |
99 XmlElement unitElem = doc.CreateElement("unit"); | |
109 | 100 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
|
101 unitElem.SetAttribute("unitName", (unit.HasDefaultName() ? "" : unit.Name)); |
108 | 102 unitElem.SetAttribute("unitType", unit.UnitType.ID); |
103 unitElem.SetAttribute("size", unit.Size.ToString()); | |
104 | |
105 if (!unit.Race.Equals(unit.Army.Race)) | |
106 { | |
107 unitElem.SetAttribute("race", unit.Race.ID); | |
108 } | |
109 | |
110 XmlElement equipmentElem = CreateEquipmentItemsElement(unit, doc); | |
111 | |
112 if (equipmentElem != null) | |
113 { | |
114 unitElem.AppendChild(equipmentElem); | |
115 } | |
116 | |
109 | 117 XmlElement containedElem = CreateContainedUnitsElement(unit, doc); |
118 | |
119 if (containedElem != null) | |
120 { | |
121 unitElem.AppendChild(containedElem); | |
122 } | |
123 | |
108 | 124 return unitElem; |
125 } | |
126 | |
127 private XmlElement CreateEquipmentItemsElement(Unit unit, XmlDocument doc) | |
128 { | |
129 UnitEquipmentItem[] equipItems = unit.GetEquipment(); | |
130 int equipItemCount = equipItems.Length; | |
131 XmlElement equipmentElem = null; | |
132 | |
133 if (equipItemCount > 0) | |
134 { | |
135 equipmentElem = doc.CreateElement("equipment"); | |
136 | |
137 for (int i = 0; i < equipItemCount; i++) | |
138 { | |
139 equipmentElem.AppendChild(CreateEquipmentElement(equipItems[i], unit, doc)); | |
140 } | |
141 } | |
142 | |
143 return equipmentElem; | |
144 } | |
145 | |
146 private XmlElement CreateEquipmentElement(UnitEquipmentItem item, Unit unit, XmlDocument doc) | |
147 { | |
148 XmlElement equipmentItemElem = doc.CreateElement("equipItem"); | |
122 | 149 equipmentItemElem.SetAttribute("id", item.ID); |
216 | 150 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
|
151 equipmentItemElem.SetAttribute("amountType", UnitEquipmentUtil.GetEquipmentAmountIsRatio(unit, item) ? "ratio" : "fixed"); |
108 | 152 return equipmentItemElem; |
153 } | |
109 | 154 |
155 private XmlElement CreateContainedUnitsElement(Unit unit, XmlDocument doc) | |
156 { | |
157 Unit[] containedUnits = unit.ContainedUnits; | |
158 int containedCount = containedUnits.Length; | |
159 XmlElement containedElem = null; | |
160 | |
161 if (containedCount > 0) | |
162 { | |
163 containedElem = doc.CreateElement("contained"); | |
164 | |
165 for (int i = 0; i < containedCount; i++) | |
166 { | |
167 containedElem.AppendChild(CreateContainedUnitElement(containedUnits[i], doc)); | |
168 } | |
169 } | |
170 | |
171 return containedElem; | |
172 } | |
173 | |
174 private XmlElement CreateContainedUnitElement(Unit unit, XmlDocument doc) | |
175 { | |
176 XmlElement containedUnitElem = doc.CreateElement("containedUnit"); | |
177 containedUnitElem.SetAttribute("containedID", XmlTools.GetAsciiXmlIdForString(unit.ID)); | |
178 return containedUnitElem; | |
179 } | |
0 | 180 } |
181 } |