Mercurial > repos > snowblizz-super-API-ideas
annotate api/Factories/Xml/WarFoundryXmlFactory.cs @ 11:5a1df00b0359
Re #9 - Make WarFoundry API load files in small methods
* Add "add unit type" and "add equipment" methods to Race
* Deprecate old "set unit types" and "set equipment" methods on Race
* Update WarFoundryXmlFactory to use new methods
* Create DuplicateItemException for later use
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 18 Jan 2009 16:24:03 +0000 |
parents | 607c3232d689 |
children | ac232763858b |
rev | line source |
---|---|
0 | 1 // WarFoundryXmlFactory.cs |
2 // | |
3 // Copyright (C) 2007 IBBoard | |
4 // | |
5 // This library is free software; you can redistribute it and/or | |
6 // modify it under the terms of the GNU Lesser General Public | |
7 // License version 2.1 of the License as published by the Free | |
8 // Software Foundation. | |
9 // | |
10 // This library is distributed in the hope that it will be useful, | |
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 // Lesser General Public License for more details. | |
14 // | |
15 // You should have received a copy of the GNU Lesser General Public | |
16 // License along with this library; if not, write to the Free Software | |
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 // | |
19 // | |
20 | |
21 using System; | |
22 using System.IO; | |
23 using System.Xml; | |
24 using System.Xml.Schema; | |
25 using System.Collections.Generic; | |
26 using System.Text; | |
27 using IBBoard; | |
28 using IBBoard.IO; | |
29 using IBBoard.Lang; | |
30 using IBBoard.Logging; | |
31 using IBBoard.Xml; | |
32 using IBBoard.WarFoundry.API.Requirements; | |
33 using IBBoard.WarFoundry.API.Objects; | |
34 using ICSharpCode.SharpZipLib.Zip; | |
35 | |
36 namespace IBBoard.WarFoundry.API.Factories.Xml | |
37 { | |
38 /// <summary> | |
10 | 39 /// The WarFoundryXmlFactory loads WarFoundry classes from the native "XML in a zip" file format. Files are validated using the schema for the file type, so structurally invalid files should be identified at initial load. |
0 | 40 /// </summary> |
41 public class WarFoundryXmlFactory : AbstractNativeWarFoundryFactory | |
42 { | |
43 private Dictionary<IWarFoundryObject, XmlDocument> extraData = new Dictionary<IWarFoundryObject, XmlDocument>(); | |
44 private XmlResolver xmlResolver; | |
45 | |
46 public static AbstractNativeWarFoundryFactory CreateFactory() | |
47 { | |
48 return new WarFoundryXmlFactory(); | |
49 } | |
50 | |
51 protected WarFoundryXmlFactory() : base() | |
52 { | |
53 xmlResolver = new IBBXmlResolver(Constants.ExecutablePath); | |
54 } | |
55 | |
56 protected override bool CheckCanFindArmyFileContent(ZipFile file) | |
57 { | |
58 return file.FindEntry("data.armyx", true) > -1; | |
59 } | |
60 | |
61 protected override bool CheckCanFindSystemFileContent(ZipFile file) | |
62 { | |
63 return file.FindEntry("data.systemx", true) > -1; | |
64 } | |
65 | |
66 protected override bool CheckCanFindRaceFileContent(ZipFile file) | |
67 { | |
68 return file.FindEntry("data.racex", true) > -1; | |
69 } | |
70 | |
71 protected XmlElement GetRootElementFromStream(Stream stream, WarFoundryXmlElementName elementName) | |
72 { | |
73 XmlDocument doc = CreateXmlDocumentFromStream(stream); | |
74 XmlElement elem = (XmlElement)doc.LastChild; | |
75 | |
76 if (!elem.Name.Equals(elementName.Value)) | |
77 { | |
78 throw new InvalidFileException(String.Format("Root element of XML was not valid. Expected {0} but got {1}", elementName.Value, elem.Name)); | |
79 } | |
80 | |
81 return elem; | |
82 } | |
83 | |
84 protected override Stream GetArmyDataStream(ZipFile file) | |
85 { | |
86 return file.GetInputStream(file.FindEntry("data.armyx", true)); | |
87 } | |
88 | |
89 protected override Army CreateArmyFromStream (ZipFile file, Stream dataStream) | |
90 { | |
91 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.ARMY_ELEMENT); | |
92 return CreateArmyFromElement(file, elem); | |
93 } | |
94 | |
95 private Army CreateArmyFromElement(ZipFile file, XmlElement elem) | |
96 { | |
97 string name = elem.GetAttribute("name"); | |
98 string systemID = elem.GetAttribute("gameSystem"); | |
99 GameSystem system = WarFoundryLoader.GetDefault().GetGameSystem(systemID); | |
100 string raceID = elem.GetAttribute("race"); | |
101 Race race = WarFoundryLoader.GetDefault().GetRace(system, raceID); | |
102 string pointsString = elem.GetAttribute("maxPoints"); | |
103 int points = 0; | |
104 | |
105 try | |
106 { | |
107 points = int.Parse(pointsString); | |
108 } | |
109 catch(FormatException) | |
110 { | |
111 throw new FormatException("Attribute 'maxPoints' of army '"+name+"' was not a valid number"); | |
112 } | |
113 | |
10 | 114 Army army = new Army(race, name, points, file); |
0 | 115 extraData[army] = elem.OwnerDocument; |
116 return army; | |
117 } | |
118 | |
119 protected override Stream GetGameSystemDataStream (ZipFile file) | |
120 { | |
121 return file.GetInputStream(file.FindEntry("data.systemx", true)); | |
122 } | |
123 | |
124 protected override GameSystem CreateGameSystemFromStream (ZipFile file, Stream dataStream) | |
125 { | |
126 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.SYSTEM_ELEMENT); | |
127 LogNotifier.Debug(GetType(), "Create GameSystem"); | |
128 return CreateSystemFromElement(file, elem); | |
129 } | |
130 | |
131 private GameSystem CreateSystemFromElement(ZipFile file, XmlElement elem) | |
132 { | |
133 string id = elem.GetAttribute("id"); | |
134 string name = elem.GetAttribute("name"); | |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
135 GameSystem system = new GameSystem(id, name, this); |
0 | 136 extraData[system] = elem.OwnerDocument; |
137 return system; | |
138 } | |
139 | |
140 protected override Stream GetRaceDataStream (ZipFile file) | |
141 { | |
142 return file.GetInputStream(file.FindEntry("data.racex", true)); | |
143 } | |
144 | |
145 protected override Race CreateRaceFromStream (ZipFile file, Stream dataStream) | |
146 { | |
147 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.RACE_ELEMENT); | |
148 LogNotifier.Debug(GetType(), "Create Race"); | |
149 return CreateRaceFromElement(file, elem); | |
150 } | |
151 | |
152 private Race CreateRaceFromElement(ZipFile file, XmlElement elem) | |
153 { | |
154 string id = elem.GetAttribute("id"); | |
155 string subid = elem.GetAttribute("subid"); | |
156 string systemID = elem.GetAttribute("system"); | |
157 string name = elem.GetAttribute("name"); | |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
158 Race race = new Race(id, subid, name, systemID, this); |
0 | 159 extraData[race] = elem.OwnerDocument; |
160 return race; | |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
161 } |
0 | 162 |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
163 public override void CompleteLoading(IWarFoundryStagedLoadObject obj) |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
164 { |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
165 LogNotifier.DebugFormat(GetType(), "Complete loading of {0} with ID {1}", obj.GetType().Name, obj.ID); |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
166 |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
167 if (obj is GameSystem) |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
168 { |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
169 CompleteLoading((GameSystem)obj); |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
170 } |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
171 else if (obj is Race) |
0 | 172 { |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
173 CompleteLoading((Race)obj); |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
174 } |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
175 } |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
176 |
10 | 177 public XmlNode GetExtraData(IWarFoundryObject obj) |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
178 { |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
179 XmlDocument extra = null; |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
180 extraData.TryGetValue(obj, out extra); |
10 | 181 XmlNode node = null; |
182 | |
183 if (extra !=null) | |
184 { | |
185 node = extra.LastChild; | |
186 } | |
187 | |
188 return node; | |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
189 } |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
190 |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
191 public void CompleteLoading(GameSystem system) |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
192 { |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
193 if (system.IsFullyLoaded) |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
194 { |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
195 LogNotifier.DebugFormat(GetType(), "Object of type GameSystem with ID {0} is already fully loaded", system.ID); |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
196 return; |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
197 } |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
198 |
10 | 199 XmlNode elem = GetExtraData(system); |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
200 XmlNode catsElem = elem.FirstChild; |
10 | 201 LoadCategoriesForSystem(system, elem); |
202 | |
203 XmlElement statsElem = (XmlElement)catsElem.NextSibling; | |
204 LoadSystemStatsFromElement(statsElem, system); | |
205 string defaultStatsID = statsElem.GetAttribute("defaultStats"); | |
206 | |
207 LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.Name); | |
208 system.StandardSystemStatsID = defaultStatsID; | |
209 system.SetAsFullyLoaded(); | |
210 } | |
211 | |
212 private void LoadCategoriesForSystem(GameSystem system, XmlNode elem) | |
213 { | |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
214 WarFoundryObject tempObj; |
10 | 215 |
216 foreach (XmlElement cat in elem.SelectNodes("//"+WarFoundryXmlElementName.CATEGORY_ELEMENT.Value)) | |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
217 { |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
218 tempObj = CreateObjectFromElement(cat); |
0 | 219 |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
220 if (tempObj is Category) |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
221 { |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
222 system.AddCategory((Category)tempObj); |
0 | 223 } |
224 else | |
225 { | |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
226 LogNotifier.WarnFormat(GetType(), "Object for string {0} was not of type Category", cat.OuterXml); |
0 | 227 } |
228 } | |
229 } | |
230 | |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
231 public void CompleteLoading(Race race) |
0 | 232 { |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
233 if (race.IsFullyLoaded) |
0 | 234 { |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
235 LogNotifier.DebugFormat(GetType(), "Object of type Race with ID {0} is already fully loaded", race.ID); |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
236 return; |
0 | 237 } |
238 | |
10 | 239 XmlNode elem = GetExtraData(race); |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
240 XmlNode colNode = elem.FirstChild; |
11
5a1df00b0359
Re #9 - Make WarFoundry API load files in small methods
IBBoard <dev@ibboard.co.uk>
parents:
10
diff
changeset
|
241 |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
242 foreach (XmlElement node in colNode.ChildNodes) |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
243 { |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
244 UnitType type = CreateUnitTypeFromElement(node, race, race.GameSystem); |
11
5a1df00b0359
Re #9 - Make WarFoundry API load files in small methods
IBBoard <dev@ibboard.co.uk>
parents:
10
diff
changeset
|
245 race.AddUnitType(type); |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
246 } |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
247 |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
248 colNode = colNode.NextSibling; |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
249 |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
250 if (colNode!=null && colNode.Name == WarFoundryXmlElementName.CATEGORIES_ELEMENT.Value) |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
251 { |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
252 foreach (XmlElement node in colNode.ChildNodes) |
0 | 253 { |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
254 race.AddCategory(CreateCategoryFromElement(node)); |
0 | 255 } |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
256 |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
257 colNode = colNode.NextSibling; |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
258 } |
11
5a1df00b0359
Re #9 - Make WarFoundry API load files in small methods
IBBoard <dev@ibboard.co.uk>
parents:
10
diff
changeset
|
259 |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
260 if (colNode!=null && colNode.Name == WarFoundryXmlElementName.RACE_EQUIPMENT_ITEMS_ELEMENT.Value) |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
261 { |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
262 foreach (XmlElement node in colNode.ChildNodes) |
0 | 263 { |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
264 EquipmentItem item = CreateEquipmentItemFromElement(node, race); |
11
5a1df00b0359
Re #9 - Make WarFoundry API load files in small methods
IBBoard <dev@ibboard.co.uk>
parents:
10
diff
changeset
|
265 race.AddEquipmentItem(item); |
0 | 266 } |
267 } | |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
268 |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
269 Dictionary<string, Ability> raceAbilities = new Dictionary<string, Ability>(); |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
270 //TODO: Load abilities |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
271 race.SetAbilities(raceAbilities); |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
272 race.SetAsFullyLoaded(); |
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
273 LogNotifier.DebugFormat(GetType(), "Completed loading of Race with ID {0}", race.Name); |
0 | 274 } |
275 | |
276 protected XmlDocument CreateXmlDocumentFromStream(Stream stream) | |
277 { | |
278 XmlDocument doc = new XmlDocument(); | |
279 XmlReaderSettings settings = new XmlReaderSettings(); | |
280 settings.XmlResolver = xmlResolver; | |
281 settings.ValidationType = ValidationType.DTD; | |
282 settings.ProhibitDtd = false; | |
283 settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod); | |
284 XmlReader reader = XmlReader.Create(stream, settings); | |
285 | |
286 try | |
287 { | |
288 doc.Load(reader); | |
289 } | |
290 //Don't catch XMLSchemaExceptions - let them get thrown out | |
291 finally | |
292 { | |
293 reader.Close(); | |
294 } | |
295 | |
296 if (doc.ChildNodes.Count!=3) | |
297 { | |
298 throw new InvalidFileException(Translation.GetTranslation("ErrorInvalidXmlFile", "XML file was not a valid XML file. It should contain three child nodes - XML definition, DocType and root node.")); | |
299 } | |
300 | |
301 return doc; | |
302 } | |
303 | |
304 protected XmlDocument CreateXmlDocumentFromString(string xmlString) | |
305 { | |
306 XmlReaderSettings settings = new XmlReaderSettings(); | |
307 settings.XmlResolver = xmlResolver; | |
308 settings.ValidationType = ValidationType.DTD; | |
309 settings.ProhibitDtd = false; | |
310 settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod); | |
311 XmlReader reader = XmlReader.Create(new MemoryStream(Encoding.UTF8.GetBytes(xmlString)), settings); | |
312 XmlDocument doc = new XmlDocument(); | |
313 | |
314 try | |
315 { | |
316 doc.Load(reader); | |
317 } | |
318 catch(XmlSchemaException ex) | |
319 { | |
320 throw new InvalidFileException(Translation.GetTranslation("ErrorInvalidXmlString", "Failed to parse invalid XML string"), ex); | |
321 } | |
322 finally | |
323 { | |
324 //We might not need to make sure the memory stream is closed, but do it just in case | |
325 reader.Close(); | |
326 } | |
327 | |
328 if (doc.ChildNodes.Count!=3) | |
329 { | |
330 throw new InvalidFileException(String.Format(Translation.GetTranslation("ErrorInvalidXmlFile", "XML string was not a valid XML file. It should contain three child nodes - XML definition, DocType and root node."))); | |
331 } | |
332 | |
333 return doc; | |
334 } | |
335 | |
336 private WarFoundryObject CreateObjectFromElement(XmlElement elem) | |
337 { | |
338 WarFoundryObject obj = null; | |
339 LogNotifier.DebugFormat(GetType(), "Create object for <{0}>", elem.Name); | |
340 | |
341 if (elem.Name.Equals(WarFoundryXmlElementName.CATEGORY_ELEMENT.Value)) | |
342 { | |
343 LogNotifier.Debug(GetType(), "Create Category"); | |
344 obj = CreateCategoryFromElement(elem); | |
345 } | |
346 else | |
347 { | |
348 LogNotifier.Debug(GetType(), "No match"); | |
349 } | |
350 | |
351 return obj; | |
352 } | |
353 | |
354 private Category CreateCategoryFromElement(XmlElement elem) | |
355 { | |
356 string id = elem.GetAttribute("id"); | |
357 string name = elem.GetAttribute("name"); | |
9
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
358 int minPc, maxPc, minPts, maxPts, minChoices, maxChoices, baseValue, incValue, incAmount; |
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
359 minPc = GetIntValueFromAttribute(elem, "minPercentage"); |
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
360 maxPc = GetIntValueFromAttribute(elem, "maxPercentage"); |
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
361 minPts = GetIntValueFromAttribute(elem, "minPoints"); |
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
362 maxPts = GetIntValueFromAttribute(elem, "maxPoints"); |
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
363 minChoices = GetIntValueFromAttribute(elem, "minChoices"); |
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
364 maxChoices = GetIntValueFromAttribute(elem, "maxChoices"); |
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
365 baseValue = GetIntValueFromAttribute(elem, "baseValue"); |
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
366 incValue = GetIntValueFromAttribute(elem, "incValue"); |
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
367 incAmount = GetIntValueFromAttribute(elem, "incAmount"); |
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
368 |
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
369 return new Category(id, name, minPts, maxPts, minPc, maxPc, minChoices, maxChoices, baseValue, incValue, incAmount); |
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
370 } |
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
371 |
10 | 372 private int GetIntValueFromAttribute(XmlElement elem, string attributeName) |
9
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
373 { |
0 | 374 try |
375 { | |
9
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
376 return int.Parse(elem.GetAttribute(attributeName)); |
0 | 377 } |
378 catch(FormatException) | |
379 { | |
9
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
380 throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id"))); |
0 | 381 } |
382 } | |
383 | |
384 private UnitType CreateUnitTypeFromElement(XmlElement elem, Race parentRace, GameSystem system) | |
385 { | |
386 string id = elem.GetAttribute("id"); | |
387 string name = elem.GetAttribute("typeName"); | |
388 string mainCatID = elem.GetAttribute("cat"); | |
389 int minNum, maxNum, minSize, maxSize, baseSize;//TODO: Add base size | |
390 float points, unitPoints; | |
391 Stats stats; | |
392 List<UnitRequirement> unitRequirements = new List<UnitRequirement>(); | |
393 bool found = false; | |
394 List<string> catIDs = new List<string>(); | |
395 string catID; | |
9
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
396 minNum = GetIntValueFromAttribute(elem, "minNum"); |
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
397 maxNum = GetIntValueFromAttribute(elem, "maxNum"); |
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
398 minSize = GetIntValueFromAttribute(elem, "minSize"); |
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
399 maxSize = GetIntValueFromAttribute(elem, "maxSize"); |
0 | 400 |
401 if (minSize > maxSize && maxSize!=-1) | |
402 { | |
403 minSize = maxSize; | |
404 } | |
405 | |
9
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
406 points = GetIntValueFromAttribute(elem, "points"); |
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
407 unitPoints = GetIntValueFromAttribute(elem, "unitPoints"); |
0 | 408 |
409 XmlNode node = elem.FirstChild; | |
410 | |
411 foreach(XmlElement cat in node.ChildNodes) | |
412 { | |
413 catID = cat.GetAttribute("catID"); | |
414 catIDs.Add(catID); | |
415 | |
416 if (catID == mainCatID) | |
417 { | |
418 found = true; | |
419 } | |
420 } | |
421 | |
422 if (!found) | |
423 { | |
424 throw new InvalidFileException("The main cat "+mainCatID+" was not found in the list of categories for unit "+id); | |
425 } | |
426 | |
427 node = node.NextSibling; | |
428 stats = ParseUnitStats((XmlElement)node, system); | |
429 //TODO: Add unit requirements | |
430 UnitType type = new UnitType(id, name, mainCatID, catIDs.ToArray(), minNum, maxNum, minSize, maxSize, unitPoints, points, stats, unitRequirements.ToArray(), parentRace); | |
431 | |
432 return type; | |
433 } | |
434 | |
435 private Stats ParseUnitStats(XmlElement elem, GameSystem system) | |
436 { | |
437 List<Stat> statsList = new List<Stat>(); | |
438 String statsID = elem.GetAttribute("statSet"); | |
439 SystemStats statsSet; | |
440 | |
441 if (statsID == "") | |
442 { | |
443 statsSet = system.StandardSystemStats; | |
444 } | |
445 else | |
446 { | |
6 | 447 statsSet = system.GetSystemStatsForID(statsID); |
0 | 448 } |
449 | |
450 Stats stats = new Stats(statsSet); | |
451 | |
452 foreach (XmlElement stat in elem.ChildNodes) | |
453 { | |
454 String statID = stat.GetAttribute("name"); | |
455 StatSlot slot = statsSet[statID]; | |
456 | |
457 if (slot!=null) | |
458 { | |
459 statsList.Add(new Stat(slot, stat.InnerText)); | |
460 } | |
461 else | |
462 { | |
463 throw new InvalidFileException("The stat "+statID+" was not found in stats set "+statsID); | |
464 } | |
465 } | |
466 | |
467 stats.SetStats(statsList); | |
468 | |
469 return stats; | |
470 } | |
471 | |
6 | 472 private void LoadSystemStatsFromElement(XmlElement elem, GameSystem system) |
0 | 473 { |
474 foreach (XmlElement stats in elem.ChildNodes) | |
475 { | |
476 SystemStats sysStats = CreateSystemStatsFromElement(stats); | |
6 | 477 system.AddSystemStats(sysStats); |
0 | 478 } |
479 } | |
480 | |
481 private SystemStats CreateSystemStatsFromElement(XmlElement elem) | |
482 { | |
483 List<StatSlot> slots = new List<StatSlot>(); | |
484 string id = elem.GetAttribute("id"); | |
485 | |
486 foreach (XmlElement slot in elem.ChildNodes) | |
487 { | |
488 StatSlot statSlot = new StatSlot(slot.GetAttribute("name")); | |
489 slots.Add(statSlot); | |
490 } | |
491 | |
492 return new SystemStats(id, slots.ToArray()); | |
493 } | |
494 | |
495 private EquipmentItem CreateEquipmentItemFromElement(XmlElement elem, Race race) | |
496 { | |
497 string id = elem.GetAttribute("id"); | |
498 string name = elem.GetAttribute("name"); | |
499 float cost = 0, min = 0, max = 0; | |
500 ArmourType armourType; | |
501 | |
502 try | |
503 { | |
504 cost = float.Parse(elem.GetAttribute("cost")); | |
505 } | |
506 catch(FormatException) | |
507 { | |
508 throw new FormatException("Attribute 'cost' of equipment item "+id+" was not a valid number"); | |
509 } | |
510 | |
511 try | |
512 { | |
513 min = float.Parse(elem.GetAttribute("min")); | |
514 } | |
515 catch(FormatException) | |
516 { | |
517 throw new FormatException("Attribute 'min' of equipment item "+id+" was not a valid number"); | |
518 } | |
519 | |
520 try | |
521 { | |
522 max = float.Parse(elem.GetAttribute("max")); | |
523 } | |
524 catch(FormatException) | |
525 { | |
526 throw new FormatException("Attribute 'max' of equipment item "+id+" was not a valid number"); | |
527 } | |
528 | |
529 try | |
530 { | |
531 armourType = (ArmourType)Enum.Parse(typeof(ArmourType), elem.GetAttribute("armourType")); | |
532 } | |
533 catch(FormatException) | |
534 { | |
535 throw new InvalidFileException("Attribute 'armourType' of equipment "+id+" was not a valid value from the enumeration"); | |
536 } | |
537 | |
538 if (elem.ChildNodes.Count>0) | |
539 { | |
540 //It has stats! | |
541 //TODO: Parse equipment stats | |
542 } | |
543 | |
544 return new EquipmentItem(id, name, cost, min, max, armourType, race); | |
545 } | |
546 | |
547 private void ValidationEventMethod(object sender, ValidationEventArgs e) | |
548 { | |
549 //TODO: Fire a validation failure event | |
550 LogNotifier.WarnFormat(GetType(), "Validation Error: {0}", e.Message); | |
551 } | |
552 } | |
553 } |