Mercurial > repos > IBDev-IBBoard.WarFoundry.API
comparison api/Factories/Xml/WarFoundryXmlFactory.cs @ 47:85f2b9c3609c
Re #13 - Use XPath for file loading
* Replace loading of race unit types, categories and equipment with XPath method
* Add loading of abilities using XPath
Also:
* Add implementation for Ability
* Add AddAbility method to Race
* Alter some logging so that "for ID" is followed by ID not name
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Thu, 26 Mar 2009 20:49:42 +0000 |
parents | 75a44b7753d4 |
children | b49372dd8afa |
comparison
equal
deleted
inserted
replaced
46:a5855fcd75ab | 47:85f2b9c3609c |
---|---|
201 XmlNodeList nodeList = SelectNodes(elem, "/system:system/system:sysStatsList"); | 201 XmlNodeList nodeList = SelectNodes(elem, "/system:system/system:sysStatsList"); |
202 XmlNode statsElem = nodeList.Item(0); | 202 XmlNode statsElem = nodeList.Item(0); |
203 string defaultStatsID = ((XmlElement)statsElem).GetAttribute("defaultStats"); | 203 string defaultStatsID = ((XmlElement)statsElem).GetAttribute("defaultStats"); |
204 LoadSystemStatsForSystem(system, elem); | 204 LoadSystemStatsForSystem(system, elem); |
205 system.StandardSystemStatsID = defaultStatsID; | 205 system.StandardSystemStatsID = defaultStatsID; |
206 LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.Name); | 206 LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.ID); |
207 LogNotifier.DebugFormat(GetType(), "GameSystem with ID {0} default stats: {1}", system.Name, system.StandardSystemStatsID); | 207 LogNotifier.DebugFormat(GetType(), "GameSystem with ID {0} default stats: {1}", system.ID, system.StandardSystemStatsID); |
208 system.SetAsFullyLoaded(); | 208 system.SetAsFullyLoaded(); |
209 } | 209 } |
210 | 210 |
211 private void LoadCategoriesForSystem(GameSystem system, XmlNode elem) | 211 private void LoadCategoriesForSystem(GameSystem system, XmlNode elem) |
212 { | 212 { |
242 } | 242 } |
243 | 243 |
244 race.SetAsLoading(); | 244 race.SetAsLoading(); |
245 | 245 |
246 XmlNode elem = GetExtraData(race); | 246 XmlNode elem = GetExtraData(race); |
247 XmlNode colNode = elem.FirstChild; | 247 |
248 | 248 foreach (XmlElement node in SelectNodes(elem, "/race:race/race:units/race:unit")) |
249 foreach (XmlElement node in colNode.ChildNodes) | |
250 { | 249 { |
251 UnitType type = CreateUnitTypeFromElement(node, race, race.GameSystem); | 250 UnitType type = CreateUnitTypeFromElement(node, race, race.GameSystem); |
252 race.AddUnitType(type); | 251 race.AddUnitType(type); |
253 } | 252 } |
254 | 253 |
255 colNode = colNode.NextSibling; | 254 foreach (XmlElement node in SelectNodes(elem, "/race:race/race:categories/cat:cat")) |
256 | 255 { |
257 if (colNode!=null && colNode.Name == WarFoundryXmlElementName.CATEGORIES_ELEMENT.Value) | 256 race.AddCategory(CreateCategoryFromElement(node)); |
258 { | |
259 foreach (XmlElement node in colNode.ChildNodes) | |
260 { | |
261 race.AddCategory(CreateCategoryFromElement(node)); | |
262 } | |
263 | |
264 colNode = colNode.NextSibling; | |
265 } | 257 } |
266 | 258 |
267 if (colNode!=null && colNode.Name == WarFoundryXmlElementName.RACE_EQUIPMENT_ITEMS_ELEMENT.Value) | 259 foreach (XmlElement node in SelectNodes(elem, "/race:race/race:equipment/cat:equipmentItem")) |
268 { | 260 { |
269 foreach (XmlElement node in colNode.ChildNodes) | 261 EquipmentItem item = CreateEquipmentItemFromElement(node, race); |
270 { | 262 race.AddEquipmentItem(item); |
271 EquipmentItem item = CreateEquipmentItemFromElement(node, race); | 263 } |
272 race.AddEquipmentItem(item); | 264 |
273 } | 265 foreach (XmlElement node in SelectNodes(elem, "/race:race/race:abilities/cat:ability")) |
274 } | 266 { |
275 | 267 Ability ability = CreateAbilityFromElement(node, race); |
276 Dictionary<string, Ability> raceAbilities = new Dictionary<string, Ability>(); | 268 race.AddAbility(ability); |
277 //TODO: Load abilities | 269 } |
278 race.SetAbilities(raceAbilities); | 270 |
279 race.SetAsFullyLoaded(); | 271 race.SetAsFullyLoaded(); |
280 LogNotifier.DebugFormat(GetType(), "Completed loading of Race with ID {0}", race.Name); | 272 LogNotifier.DebugFormat(GetType(), "Completed loading of Race with ID {0}", race.ID); |
281 } | 273 } |
282 | 274 |
283 protected XmlDocument CreateXmlDocumentFromStream(Stream stream) | 275 protected XmlDocument CreateXmlDocumentFromStream(Stream stream) |
284 { | 276 { |
285 XmlDocument doc = new XmlDocument(); | 277 XmlDocument doc = new XmlDocument(); |
286 XmlReader reader = XmlReader.Create(stream, GetReaderSettings()); | 278 XmlReader reader = XmlReader.Create(stream, GetReaderSettings()); |
287 | 279 |
288 try | 280 try |
289 { | 281 { |
290 doc.Load(reader); | 282 doc.Load(reader); |
291 } | 283 } |
292 //Don't catch XMLSchemaExceptions - let them get thrown out | 284 //Don't catch XMLSchemaExceptions - let them get thrown out |
293 finally | 285 finally |
294 { | 286 { |
295 reader.Close(); | 287 reader.Close(); |
547 | 539 |
548 //TODO: Parse equipment stats if there are any | 540 //TODO: Parse equipment stats if there are any |
549 | 541 |
550 return new EquipmentItem(id, name, cost, min, max, armourType, race); | 542 return new EquipmentItem(id, name, cost, min, max, armourType, race); |
551 } | 543 } |
544 | |
545 private Ability CreateAbilityFromElement(XmlElement elem, Race race) | |
546 { | |
547 string id = elem.GetAttribute("id"); | |
548 string name = elem.GetAttribute("name"); | |
549 Ability ability = new Ability(id, name); | |
550 XmlNode node = elem.SelectSingleNode("description", GetNamespaceManager()); | |
551 ability.Description = (node == null) ? "" : node.InnerText; | |
552 return ability; | |
553 } | |
552 } | 554 } |
553 } | 555 } |