Mercurial > repos > IBBoard.WarFoundry.API
comparison API/Factories/Xml/WarFoundryXmlArmyParser.cs @ 337:3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
no-open-ticket
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 03 Apr 2011 18:50:32 +0000 |
parents | |
children | 00d6cf940c3c |
comparison
equal
deleted
inserted
replaced
336:3631c1493c7f | 337:3c4a6403a88c |
---|---|
1 // This file (WarFoundryXmlArmyParser.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. | |
4 | |
5 using System; | |
6 using System.Collections.Generic; | |
7 using System.Xml; | |
8 using IBBoard.IO; | |
9 using IBBoard.Xml; | |
10 using ICSharpCode.SharpZipLib.Zip; | |
11 using IBBoard.WarFoundry.API.Objects; | |
12 | |
13 namespace IBBoard.WarFoundry.API.Factories.Xml | |
14 { | |
15 public class WarFoundryXmlArmyParser | |
16 { | |
17 private ZipFile file; | |
18 private XmlElement elem; | |
19 private Army army; | |
20 private Dictionary<String, Unit> units; | |
21 | |
22 public WarFoundryXmlArmyParser(ZipFile file, XmlElement elem) | |
23 { | |
24 this.file = file; | |
25 this.elem = elem; | |
26 } | |
27 | |
28 public Army GetArmy() | |
29 { | |
30 if (army == null) | |
31 { | |
32 ParseArmy(); | |
33 } | |
34 | |
35 return army; | |
36 } | |
37 | |
38 private void ParseArmy() | |
39 { | |
40 string name = elem.GetAttribute("name"); | |
41 string systemID = elem.GetAttribute("system"); | |
42 GameSystem system = WarFoundryLoader.GetDefault().GetGameSystem(systemID); | |
43 | |
44 if (system == null) | |
45 { | |
46 throw new RequiredDataMissingException(file.Name, "Game System", systemID); | |
47 } | |
48 | |
49 string raceID = elem.GetAttribute("race"); | |
50 Race race = WarFoundryLoader.GetDefault().GetRace(system, raceID); | |
51 | |
52 if (race == null) | |
53 { | |
54 throw new RequiredDataMissingException(file.Name, "Race", raceID); | |
55 } | |
56 | |
57 int points = XmlTools.GetIntValueFromAttribute(elem, "maxPoints"); | |
58 army = new Army(race, name, points, file); | |
59 LoadUnits(); | |
60 } | |
61 | |
62 private void LoadUnits() | |
63 { | |
64 units = new Dictionary<string, Unit>(); | |
65 | |
66 foreach (XmlElement unitElem in WarFoundryXmlFactoryUtils.SelectNodes(elem, "/army:army/army:units/army:unit")) | |
67 { | |
68 string id = unitElem.GetAttribute("id"); | |
69 | |
70 if (!units.ContainsKey(id)) | |
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, "Unit Type", unitTypeId); | |
78 } | |
79 | |
80 string name = unitElem.GetAttribute("unitName"); | |
81 int size = XmlTools.GetIntValueFromAttribute(unitElem, "size"); | |
82 | |
83 string catID = unitElem.GetAttribute("category"); | |
84 Category cat = army.Race.GetCategory(catID); | |
85 | |
86 if (cat == null) | |
87 { | |
88 cat = unitType.MainCategory; | |
89 } | |
90 | |
91 Unit unit = new Unit(id, name, size, unitType, army.GetCategory(cat)); | |
92 army.AddUnit(unit, cat); | |
93 units.Add(id, unit); | |
94 | |
95 LoadUnitEquipment(unitElem, unit); | |
96 } | |
97 else | |
98 { | |
99 throw new InvalidFileException("Duplicate unit ID found in army file: "+id); | |
100 } | |
101 } | |
102 } | |
103 | |
104 private void LoadUnitEquipment(XmlElement unitElem, Unit unit) | |
105 { | |
106 foreach (XmlElement elem in WarFoundryXmlFactoryUtils.SelectNodes(unitElem, "army:equipment/army:equipItem")) | |
107 { | |
108 string equipID = elem.GetAttribute("id"); | |
109 UnitEquipmentItem item = unit.UnitType.GetEquipmentItem(equipID); | |
110 | |
111 if (item == null) | |
112 { | |
113 throw new RequiredDataMissingException(file.Name, "Equipment Item", equipID); | |
114 } | |
115 | |
116 double amount = XmlTools.GetDoubleValueFromAttribute(elem, "amount"); | |
117 string equipTypeString = elem.GetAttribute("amountType"); | |
118 | |
119 if (equipTypeString == "ratio") | |
120 { | |
121 unit.SetEquipmentRatio(item, amount); | |
122 } | |
123 else | |
124 { | |
125 //amount should be a whole number, so do type-cast rounding | |
126 unit.SetEquipmentAmount(item, (int) amount); | |
127 } | |
128 } | |
129 } | |
130 } | |
131 } |