comparison api/Factories/Xml/WarFoundryXmlArmyParser.cs @ 117:093ee2da0f6e

Re #54: Add Army support to WarFoundryFactory * Add starts of army unit loading
author IBBoard <dev@ibboard.co.uk>
date Sat, 22 Aug 2009 20:02:29 +0000
parents 2010a7e8bf9a
children 0cdc0b07fe11
comparison
equal deleted inserted replaced
116:2010a7e8bf9a 117:093ee2da0f6e
1 // This file (WarFoundryXmlArmyParser.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard 1 // This file (WarFoundryXmlArmyParser.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard
2 // 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. 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.
4 4
5 using System; 5 using System;
6 using System.Collections.Generic;
6 using System.Xml; 7 using System.Xml;
7 using IBBoard.Xml; 8 using IBBoard.Xml;
8 using ICSharpCode.SharpZipLib.Zip; 9 using ICSharpCode.SharpZipLib.Zip;
9 using IBBoard.WarFoundry.API.Objects; 10 using IBBoard.WarFoundry.API.Objects;
10 11
13 public class WarFoundryXmlArmyParser 14 public class WarFoundryXmlArmyParser
14 { 15 {
15 private ZipFile file; 16 private ZipFile file;
16 private XmlElement elem; 17 private XmlElement elem;
17 private Army army; 18 private Army army;
19 private Dictionary<String, Unit> units;
18 20
19 public WarFoundryXmlArmyParser(ZipFile file, XmlElement elem) 21 public WarFoundryXmlArmyParser(ZipFile file, XmlElement elem)
20 { 22 {
21 this.file = file; 23 this.file = file;
22 this.elem = elem; 24 this.elem = elem;
51 throw new RequiredDataMissingException(file.Name, "race", raceID); 53 throw new RequiredDataMissingException(file.Name, "race", raceID);
52 } 54 }
53 55
54 int points = XmlTools.GetIntValueFromAttribute(elem, "maxPoints"); 56 int points = XmlTools.GetIntValueFromAttribute(elem, "maxPoints");
55 army = new Army(race, name, points, file); 57 army = new Army(race, name, points, file);
56 //TODO: Complete loading of army 58 LoadUnits();
59 }
60
61 private void LoadUnits()
62 {
63 units = new Dictionary<string, Unit>();
64
65 foreach (XmlElement unitElem in WarFoundryXmlFactoryUtils.SelectNodes(elem, "/army:army/army:units/army:unit"))
66 {
67 string id = unitElem.GetAttribute("id");
68 Unit unit = DictionaryUtils.GetValue(units, id);
69
70 if (unit == null)
71 {
72 string unitTypeId = unitElem.GetAttribute("unitType");
73 UnitType unitType = army.Race.GetUnitType(unitTypeId);
74
75 if (unitType == null)
76 {
77 throw new RequiredDataMissingException(file.Name, "unitType", unitTypeId);
78 }
79
80 unit = new Unit(unitType, army.GetCategory(unitType.MainCategory));
81 units.Add(id, unit);
82 }
83 }
57 } 84 }
58 } 85 }
59 } 86 }