view API/Factories/Xml/WarFoundryXmlRaceFactory.cs @ 370:077e9be48438

Re #346: Add requirement schema support * Pass unit test with a specific case for one requirement - needs extensibility
author IBBoard <dev@ibboard.co.uk>
date Mon, 13 Jun 2011 15:15:04 +0000
parents 3c4a6403a88c
children bfdb95906075
line source
1 // This file (WarFoundryXmlRaceFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 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 IBBoard.Xml;
10 using IBBoard.IO;
11 using IBBoard.Limits;
12 using IBBoard.CustomMath;
13 using ICSharpCode.SharpZipLib.Zip;
14 using IBBoard.WarFoundry.API.Objects;
15 using IBBoard.WarFoundry.API.Objects.Requirement;
17 namespace IBBoard.WarFoundry.API.Factories.Xml
18 {
19 /// <summary>
20 /// A sub-factory for loading WarFoundry Race XML files
21 /// </summary>
22 public class WarFoundryXmlRaceFactory : AbstractStagedLoadedSubFactory
23 {
24 private Dictionary<Race, XmlDocument> extraData = new Dictionary<Race, XmlDocument>();
25 private WarFoundryXmlLimitParser limitParser = new WarFoundryXmlLimitParser();
27 public WarFoundryXmlRaceFactory(WarFoundryXmlFactory factory) : base (factory)
28 {
29 //Do nothing special
30 }
32 private void StoreExtraData(Race wfObject, XmlElement elem)
33 {
34 extraData[wfObject] = elem.OwnerDocument;
35 }
37 private XmlDocument GetExtraData(Race obj)
38 {
39 XmlDocument extra = null;
40 extraData.TryGetValue(obj, out extra);
41 return extra;
42 }
44 public Race CreateRaceFromElement(ZipFile file, XmlElement elem)
45 {
46 string id = elem.GetAttribute("id");
47 string subid = elem.GetAttribute("subid");
48 string systemID = elem.GetAttribute("system");
49 string name = elem.GetAttribute("name");
50 string armyDefaultName = elem.GetAttribute("defaultArmyName");
51 GameSystem gameSystem = WarFoundryLoader.GetDefault ().GetGameSystem (systemID);
53 if (gameSystem == null)
54 {
55 throw new InvalidFileException("Referenced game system, '"+systemID+"', did not exist");
56 }
58 Race race = new Race(id, subid, name, gameSystem, mainFactory);
59 race.ArmyDefaultName = armyDefaultName;
60 StoreExtraData(race, elem);
61 return race;
62 }
64 public void CompleteLoading(Race race)
65 {
66 if (!WarFoundryXmlFactoryUtils.CanCompleteLoading(race))
67 {
68 return;
69 }
71 race.SetAsLoading();
72 XmlDocument extraData = GetExtraData(race);
74 foreach (XmlElement node in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:categories/cat:cat"))
75 {
76 CreateCategoryFromElement(node, race);
77 }
79 foreach (XmlElement node in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:equipment/race:equipmentItem"))
80 {
81 CreateEquipmentItemFromElement(node, race);
82 }
84 foreach (XmlElement node in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:abilities/race:ability"))
85 {
86 CreateAbilityFromElement(node, race);
87 }
89 foreach (XmlElement node in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:memberTypes/race:memberType"))
90 {
91 CreateMemberTypeFromElement(node, race);
92 }
94 foreach (XmlElement node in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:units/race:unit"))
95 {
96 GetUnitTypeForElement(node, race);
97 }
99 race.SetAsFullyLoaded();
100 }
102 private Category CreateCategoryFromElement(XmlElement elem, Race parentRace)
103 {
104 Category cat = CreateCategoryFromElement(elem);
105 parentRace.AddCategory(cat);
106 return cat;
107 }
109 private UnitType GetUnitTypeFromDocument(XmlDocument doc, string id, Race parentRace)
110 {
111 XmlElement unitWithId = WarFoundryXmlFactoryUtils.SelectSingleElement (doc, "/race:race/race:units/race:unit[@id='" + id + "']");
113 if (unitWithId == null)
114 {
115 throw new InvalidFileException("Could not find unit with ID "+id);
116 }
118 return GetUnitTypeForElement(unitWithId, parentRace);
119 }
121 private UnitType GetUnitTypeForElement(XmlElement elem, Race parentRace)
122 {
123 string id = elem.GetAttribute("id");
124 UnitType type = parentRace.GetUnitType(id);
126 if (type==null)
127 {
128 type = CreateUnitTypeFromElement(elem, id, parentRace);
129 }
131 return type;
132 }
134 private UnitType CreateUnitTypeFromElement(XmlElement elem, string id, Race parentRace)
135 {
136 string name = elem.GetAttribute("typeName");
137 UnitType type = new UnitType(id, name, parentRace);
138 LoadCoreValuesForUnitType(elem, type);
139 LoadEquipmentSlotsForUnitType(elem, type);
140 LoadEquipmentForUnitType(elem, type);
141 LoadAbilitiesForUnitType(elem, type);
142 LoadContainedUnitsForUnitType(elem, type);
143 LoadRequirementsForUnitType(elem, type);
144 LoadExtraDataForUnitType(elem, type);
145 LoadNotesForUnitType(elem, type);
146 parentRace.AddUnitType(type);
147 return type;
148 }
150 private void LoadCoreValuesForUnitType(XmlElement elem, UnitType type)
151 {
152 try
153 {
154 type.MaxNumber = XmlTools.GetIntValueFromAttribute(elem, "maxNum");
155 type.MinNumber = XmlTools.GetIntValueFromAttribute(elem, "minNum");
156 type.MaxSize = XmlTools.GetIntValueFromAttribute(elem, "maxSize");
157 type.MinSize = XmlTools.GetIntValueFromAttribute(elem, "minSize");
158 type.BaseSize = XmlTools.GetIntValueFromAttribute(elem, "baseSize");
159 type.CostPerTrooper = XmlTools.GetDoubleValueFromAttribute(elem, "points");
160 type.BaseUnitCost = XmlTools.GetDoubleValueFromAttribute(elem, "basePoints");
161 }
162 catch (FormatException ex)
163 {
164 throw new InvalidFileException(ex.Message, ex);
165 }
167 Race race = type.Race;
168 string mainCatID = elem.GetAttribute("cat");
169 Category cat = race.GetCategory(mainCatID);
171 if (cat == null)
172 {
173 throw new InvalidFileException(String.Format("Category with ID '{1}' did not exist for UnitType '{0}'", type.Name, mainCatID));
174 }
176 type.MainCategory = cat;
178 XmlNodeList unitCategories = WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:unitCategories/race:unitCategory");
180 foreach (XmlElement unitCategory in unitCategories)
181 {
182 string catID = unitCategory.GetAttribute("catID");
183 Category unitCat = race.GetCategory(catID);
185 if (unitCat == null)
186 {
187 throw new InvalidFileException(String.Format("Category with ID '{1}' did not exist for UnitType '{0}'", type.Name, catID));
188 }
190 type.AddCategory(unitCat);
191 }
193 XmlElement statsElement = WarFoundryXmlFactoryUtils.SelectSingleElement(elem, "race:stats");
195 if (statsElement!=null)
196 {
197 Stats unitStats = ParseUnitStats(statsElement, type.GameSystem);
198 type.SetUnitStats(unitStats);
199 }
201 XmlNodeList unitMemberReferences = WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:unitMembers/race:unitMember");
203 foreach (XmlElement unitMemberRef in unitMemberReferences)
204 {
205 string typeID = unitMemberRef.GetAttribute("typeID");
206 UnitMemberType unitMemberType = race.GetUnitMemberType(typeID);
207 type.AddUnitMemberType(unitMemberType);
208 }
209 }
211 private void LoadEquipmentSlotsForUnitType(XmlElement elem, UnitType type)
212 {
213 foreach (XmlElement equipSlot in WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:equipmentSlots/race:equipmentSlot"))
214 {
215 LoadEquipmentSlotForUnitType (type, equipSlot);
216 }
217 }
219 private void LoadEquipmentSlotForUnitType(UnitType type, XmlElement equipSlot)
220 {
221 string slotName = equipSlot.GetAttribute("name");
222 ILimit limit = GetMaxLimit(equipSlot);
224 if (limit != null)
225 {
226 type.AddEquipmentSlot(slotName, limit);
227 }
228 }
230 private ILimit GetMinLimit(XmlElement elem)
231 {
232 return limitParser.GetMinLimit(elem);
233 }
235 private ILimit GetMaxLimit(XmlElement elem)
236 {
237 return limitParser.GetMaxLimit(elem);
238 }
240 private void LoadEquipmentForUnitType(XmlElement elem, UnitType type)
241 {
242 foreach (XmlElement equip in WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:unitEquipment/race:unitEquipmentItem"))
243 {
244 string id = equip.GetAttribute("id");
245 EquipmentItem equipItem = type.Race.GetEquipmentItem(id);
247 if (equipItem!=null)
248 {
249 string mutexGroupString = equip.GetAttribute("exclusivityGroups");
250 string[] mutexGroups;
252 if (mutexGroupString == "")
253 {
254 mutexGroupString = equip.GetAttribute("exclusivityGroup");
255 }
257 if (mutexGroupString != "")
258 {
259 string[] groups = mutexGroupString.Split(',');
260 int groupCount = groups.Length;
262 for (int i = 0; i < groupCount; i++)
263 {
264 groups[i] = groups[i].Trim();
265 }
267 mutexGroups = groups;
268 }
269 else
270 {
271 mutexGroups = new string[0];
272 }
274 UnitEquipmentItem unitEquipItem = new UnitEquipmentItem(equipItem, type, mutexGroups);
276 string equipSlot = equip.GetAttribute("equipmentSlot");
278 if (equipSlot != "")
279 {
280 if (type.HasEquipmentSlot(equipSlot))
281 {
282 unitEquipItem.SlotName = equipSlot;
283 }
284 else
285 {
286 throw new InvalidFileException("Attribute 'equipmentSlot' of unit equipment item " + id + " for " + type.Name + " was not a valid slot name");
287 }
288 }
290 ILimit limit = GetMaxLimit(equip);
292 if (limit != null)
293 {
294 unitEquipItem.MaxLimit = limit;
295 }
297 limit = GetMinLimit(equip);
299 if (limit != null)
300 {
301 unitEquipItem.MinLimit = limit;
302 }
304 unitEquipItem.RoundNumberUp = equip.GetAttribute("roundDirection").Equals("up");
306 try
307 {
308 unitEquipItem.IsRequired = XmlTools.GetBoolValueFromAttribute(equip, "required");
309 }
310 catch(FormatException e)
311 {
312 throw new InvalidFileException("Attribute 'required' of unit equipment item " + id + " for " + type.Name + " was not a valid boolean", e);
313 }
315 try
316 {
317 unitEquipItem.CostMultiplier = XmlTools.GetDoubleValueFromAttribute(equip, "costMultiplier");
318 }
319 catch (FormatException e)
320 {
321 throw new InvalidFileException("Attribute 'costMultiplier' of unit equipment item " + id + " for " + type.Name + " was not a valid decimal number", e);
322 }
324 try
325 {
326 unitEquipItem.CostRoundType = (RoundType) Enum.Parse(typeof(RoundType), equip.GetAttribute("costRounding"));
327 }
328 catch (ArgumentException e)
329 {
330 throw new InvalidFileException("Attribute 'costRounding' of unit equipment item " + id + " for " + type.Name + " was not a valid rounding type", e);
331 }
332 }
333 else
334 {
335 throw new InvalidFileException("Equipment item with ID '" + id + "' was required by " + type.Name + " but was not found");
336 }
337 }
338 }
340 private void LoadAbilitiesForUnitType(XmlElement elem, UnitType type)
341 {
342 foreach (XmlElement abilityElem in WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:unitAbilities/race:unitAbility"))
343 {
344 string id = abilityElem.GetAttribute("abilityID");
345 Ability ability = type.Race.GetAbility(id);
347 if (ability == null)
348 {
349 throw new InvalidFileException("Ability for "+type.Name+ " with ID "+id+ " did not exist in race definition");
350 }
352 bool required = XmlTools.GetBoolValueFromAttribute(abilityElem, "required");
353 type.AddAbility(ability, required);
354 }
355 }
357 private void LoadContainedUnitsForUnitType(XmlElement elem, UnitType type)
358 {
359 foreach (XmlElement containedUnitType in WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:contains/race:containedUnit"))
360 {
361 string id = containedUnitType.GetAttribute("containedID");
362 UnitType containedType = GetUnitTypeFromDocument(elem.OwnerDocument, id, type.Race);
364 if (containedType!=null)
365 {
366 type.AddContainedUnitType(containedType);
367 }
368 else
369 {
370 throw new InvalidFileException("Unit type " + type.Name + " tried to contain undefined unit with ID "+id);
371 }
372 }
373 }
375 private void LoadRequirementsForUnitType(XmlElement elem, UnitType type)
376 {
377 //TODO: Handle requirements more extensibly
378 foreach (XmlElement extraData in WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:requirements/race:requirement"))
379 {
380 string name = extraData.GetAttribute("requirementName");
381 if (name == "RequiresAtLeastNUnits") {
382 string data = WarFoundryXmlFactoryUtils.SelectSingleElement(extraData, "race:data").InnerText;
383 UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(type);
384 req.AddUnitTypeRequirement(GetUnitTypeFromDocument(elem.OwnerDocument, data, type.Race));
385 type.AddRequirement(req);
386 }
387 }
388 }
390 private void LoadExtraDataForUnitType(XmlElement elem, UnitType type)
391 {
392 foreach (XmlElement extraData in WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:extraData/race:data"))
393 {
394 string id = extraData.GetAttribute("id");
395 string data = extraData.InnerXml;
396 type.AddExtraData(id, data);
397 }
398 }
400 private void LoadNotesForUnitType(XmlElement elem, UnitType type)
401 {
402 XmlNode node = WarFoundryXmlFactoryUtils.SelectSingleNode(elem, "race:notes");
404 if (node!=null)
405 {
406 type.Notes = node.InnerText;
407 }
408 }
410 private Stats ParseUnitStats(XmlElement elem, GameSystem system)
411 {
412 if (elem == null)
413 {
414 return null;
415 }
417 String statsID = elem.GetAttribute("statSet");
418 SystemStats statsSet;
420 if (statsID == "")
421 {
422 statsSet = system.StandardSystemStats;
423 }
424 else
425 {
426 statsSet = system.GetSystemStatsForID(statsID);
427 }
429 Stats stats = new Stats(statsSet);
431 foreach (XmlElement stat in WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:stat"))
432 {
433 String statName = stat.GetAttribute("name");
434 stats.SetStatValue(statName, stat.InnerText);
435 }
437 return stats;
438 }
440 private EquipmentItem CreateEquipmentItemFromElement(XmlElement elem, Race race)
441 {
442 string id = elem.GetAttribute("id");
443 EquipmentItem item = race.GetEquipmentItem(id);
445 if (item == null)
446 {
447 item = CreateEquipmentItemFromElement(elem, id, race);
448 }
450 return item;
451 }
453 private EquipmentItem CreateEquipmentItemFromElement(XmlElement elem, string id, Race race)
454 {
455 string name = elem.GetAttribute("name");
456 EquipmentItem item = new EquipmentItem(id, name, race);
457 double cost = 0;
459 try
460 {
461 cost = XmlTools.GetDoubleValueFromAttribute(elem, "cost");
462 }
463 catch(FormatException ex)
464 {
465 throw new InvalidFileException("Attribute 'cost' of equipment item "+id+" was not a valid number", ex);
466 }
468 //TODO: Parse equipment stats if there are any
469 item.Cost = cost;
470 race.AddEquipmentItem(item);
471 return item;
472 }
474 private Ability CreateAbilityFromElement(XmlElement elem, Race race)
475 {
476 string id = elem.GetAttribute("id");
477 string name = elem.GetAttribute("name");
478 Ability ability = new Ability(id, name);
479 XmlNode node = WarFoundryXmlFactoryUtils.SelectSingleNode(elem, "race:description");
480 ability.Description = (node == null) ? "" : node.InnerText;
481 race.AddAbility(ability);
482 return ability;
483 }
485 private void CreateMemberTypeFromElement(XmlElement elem, Race race)
486 {
487 Stats stats = ParseUnitStats(WarFoundryXmlFactoryUtils.SelectSingleElement(elem, "race:stats"), race.GameSystem);
488 UnitMemberType unitMemberType = new UnitMemberType(elem.GetAttribute("id"), elem.GetAttribute("name"), stats);
489 race.AddUnitMemberType(unitMemberType);
490 }
491 }
492 }