Mercurial > repos > IBBoard.WarFoundry.API
annotate api/Factories/Xml/WarFoundryXmlRaceFactory.cs @ 226:c931684f9024
Re #234: Invalid data file doesn't stop load
* Add internal methods to remove game systems or races that fail to load
* Add error handling on XML CompleteLoading so that game system or race is removed, but make sure not to affect error
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 19 Dec 2009 15:40:50 +0000 |
parents | f097888efcfe |
children | d1c90159547a |
rev | line source |
---|---|
52 | 1 // This file (WarFoundryXmlRaceFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard |
2 // | |
104
2f3cafb69799
Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents:
99
diff
changeset
|
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. |
52 | 4 |
5 using System; | |
6 using System.Collections.Generic; | |
7 using System.IO; | |
8 using System.Xml; | |
9 using IBBoard.Xml; | |
10 using IBBoard.IO; | |
176
22429737cd77
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
175
diff
changeset
|
11 using IBBoard.Limits; |
171
85dc413279a4
* Change imports to stop references to deprecated code
IBBoard <dev@ibboard.co.uk>
parents:
170
diff
changeset
|
12 using IBBoard.CustomMath; |
52 | 13 using ICSharpCode.SharpZipLib.Zip; |
14 using IBBoard.WarFoundry.API.Objects; | |
15 | |
16 namespace IBBoard.WarFoundry.API.Factories.Xml | |
17 { | |
18 /// <summary> | |
19 /// A sub-factory for loading WarFoundry Race XML files | |
20 /// </summary> | |
21 public class WarFoundryXmlRaceFactory : AbstractStagedLoadedSubFactory | |
22 { | |
23 private Dictionary<Race, XmlDocument> extraData = new Dictionary<Race, XmlDocument>(); | |
24 | |
25 public WarFoundryXmlRaceFactory(WarFoundryXmlFactory factory) : base (factory) | |
26 { | |
99 | 27 //Do nothing special |
52 | 28 } |
29 | |
30 private void StoreExtraData(Race wfObject, XmlElement elem) | |
31 { | |
32 extraData[wfObject] = elem.OwnerDocument; | |
33 } | |
34 | |
35 private XmlDocument GetExtraData(Race obj) | |
36 { | |
37 XmlDocument extra = null; | |
38 extraData.TryGetValue(obj, out extra); | |
39 return extra; | |
40 } | |
41 | |
42 public Race CreateRaceFromElement(ZipFile file, XmlElement elem) | |
43 { | |
44 string id = elem.GetAttribute("id"); | |
45 string subid = elem.GetAttribute("subid"); | |
46 string systemID = elem.GetAttribute("system"); | |
47 string name = elem.GetAttribute("name"); | |
55
9080366031c0
Re #9 - Refactor for small methods
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
48 Race race = new Race(id, subid, name, WarFoundryLoader.GetDefault().GetGameSystem(systemID), mainFactory); |
52 | 49 StoreExtraData(race, elem); |
50 return race; | |
51 } | |
52 | |
53 public void CompleteLoading(Race race) | |
54 { | |
55 if (!WarFoundryXmlFactoryUtils.CanCompleteLoading(race)) | |
56 { | |
57 return; | |
58 } | |
59 | |
60 race.SetAsLoading(); | |
61 XmlDocument extraData = GetExtraData(race); | |
62 | |
63 foreach (XmlElement node in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:categories/cat:cat")) | |
64 { | |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
65 CreateCategoryFromElement(node, race); |
52 | 66 } |
67 | |
85
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
68 foreach (XmlElement node in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:equipment/race:equipmentItem")) |
52 | 69 { |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
70 CreateEquipmentItemFromElement(node, race); |
52 | 71 } |
72 | |
85
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
73 foreach (XmlElement node in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:abilities/race:ability")) |
52 | 74 { |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
75 CreateAbilityFromElement(node, race); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
76 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
77 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
78 foreach (XmlElement node in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:units/race:unit")) |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
79 { |
70
780f3926abad
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
80 GetUnitTypeForElement(node, race); |
52 | 81 } |
82 | |
83 race.SetAsFullyLoaded(); | |
84 } | |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
85 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
86 private Category CreateCategoryFromElement(XmlElement elem, Race parentRace) |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
87 { |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
88 Category cat = CreateCategoryFromElement(elem); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
89 parentRace.AddCategory(cat); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
90 return cat; |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
91 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
92 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
93 private UnitType GetUnitTypeFromDocument(XmlDocument doc, string id, Race parentRace) |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
94 { |
217
89e26d51afc2
Fixes #226: "NullReferenceException" for a unitID
IBBoard <dev@ibboard.co.uk>
parents:
207
diff
changeset
|
95 XmlElement unitWithId = WarFoundryXmlFactoryUtils.SelectSingleElement (doc, "/race:race/race:units/race:unit[@id='" + id + "']"); |
89e26d51afc2
Fixes #226: "NullReferenceException" for a unitID
IBBoard <dev@ibboard.co.uk>
parents:
207
diff
changeset
|
96 |
89e26d51afc2
Fixes #226: "NullReferenceException" for a unitID
IBBoard <dev@ibboard.co.uk>
parents:
207
diff
changeset
|
97 if (unitWithId == null) |
89e26d51afc2
Fixes #226: "NullReferenceException" for a unitID
IBBoard <dev@ibboard.co.uk>
parents:
207
diff
changeset
|
98 { |
89e26d51afc2
Fixes #226: "NullReferenceException" for a unitID
IBBoard <dev@ibboard.co.uk>
parents:
207
diff
changeset
|
99 throw new InvalidFileException("Could not find unit with ID "+id); |
89e26d51afc2
Fixes #226: "NullReferenceException" for a unitID
IBBoard <dev@ibboard.co.uk>
parents:
207
diff
changeset
|
100 } |
89e26d51afc2
Fixes #226: "NullReferenceException" for a unitID
IBBoard <dev@ibboard.co.uk>
parents:
207
diff
changeset
|
101 |
89e26d51afc2
Fixes #226: "NullReferenceException" for a unitID
IBBoard <dev@ibboard.co.uk>
parents:
207
diff
changeset
|
102 return GetUnitTypeForElement(unitWithId, parentRace); |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
103 } |
52 | 104 |
70
780f3926abad
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
105 private UnitType GetUnitTypeForElement(XmlElement elem, Race parentRace) |
52 | 106 { |
107 string id = elem.GetAttribute("id"); | |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
108 UnitType type = parentRace.GetUnitType(id); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
109 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
110 if (type==null) |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
111 { |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
112 type = CreateUnitTypeFromElement(elem, id, parentRace); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
113 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
114 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
115 return type; |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
116 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
117 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
118 private UnitType CreateUnitTypeFromElement(XmlElement elem, string id, Race parentRace) |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
119 { |
52 | 120 string name = elem.GetAttribute("typeName"); |
121 UnitType type = new UnitType(id, name, parentRace); | |
170 | 122 LoadCoreValuesForUnitType(elem, type); |
167
9ba56a8e5096
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
158
diff
changeset
|
123 LoadEquipmentSlotsForUnitType(elem, type); |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
124 LoadEquipmentForUnitType(elem, type); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
125 LoadAbilitiesForUnitType(elem, type); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
126 LoadContainedUnitsForUnitType(elem, type); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
127 LoadRequirementsForUnitType(elem, type); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
128 LoadExtraDataForUnitType(elem, type); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
129 LoadNotesForUnitType(elem, type); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
130 parentRace.AddUnitType(type); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
131 return type; |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
132 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
133 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
134 private void LoadCoreValuesForUnitType(XmlElement elem, UnitType type) |
158
eb9a6d91a6db
Fixes #190: Mutex groups aren't honoured when adding equipment
IBBoard <dev@ibboard.co.uk>
parents:
156
diff
changeset
|
135 { |
eb9a6d91a6db
Fixes #190: Mutex groups aren't honoured when adding equipment
IBBoard <dev@ibboard.co.uk>
parents:
156
diff
changeset
|
136 try |
eb9a6d91a6db
Fixes #190: Mutex groups aren't honoured when adding equipment
IBBoard <dev@ibboard.co.uk>
parents:
156
diff
changeset
|
137 { |
eb9a6d91a6db
Fixes #190: Mutex groups aren't honoured when adding equipment
IBBoard <dev@ibboard.co.uk>
parents:
156
diff
changeset
|
138 type.MaxNumber = XmlTools.GetIntValueFromAttribute(elem, "maxNum"); |
eb9a6d91a6db
Fixes #190: Mutex groups aren't honoured when adding equipment
IBBoard <dev@ibboard.co.uk>
parents:
156
diff
changeset
|
139 type.MinNumber = XmlTools.GetIntValueFromAttribute(elem, "minNum"); |
eb9a6d91a6db
Fixes #190: Mutex groups aren't honoured when adding equipment
IBBoard <dev@ibboard.co.uk>
parents:
156
diff
changeset
|
140 type.MaxSize = XmlTools.GetIntValueFromAttribute(elem, "maxSize"); |
eb9a6d91a6db
Fixes #190: Mutex groups aren't honoured when adding equipment
IBBoard <dev@ibboard.co.uk>
parents:
156
diff
changeset
|
141 type.MinSize = XmlTools.GetIntValueFromAttribute(elem, "minSize"); |
eb9a6d91a6db
Fixes #190: Mutex groups aren't honoured when adding equipment
IBBoard <dev@ibboard.co.uk>
parents:
156
diff
changeset
|
142 type.BaseSize = XmlTools.GetIntValueFromAttribute(elem, "baseSize"); |
eb9a6d91a6db
Fixes #190: Mutex groups aren't honoured when adding equipment
IBBoard <dev@ibboard.co.uk>
parents:
156
diff
changeset
|
143 type.CostPerTrooper = XmlTools.GetDoubleValueFromAttribute(elem, "points"); |
224
f097888efcfe
Fixes #233: "unitPoints" attribute is badly named
IBBoard <dev@ibboard.co.uk>
parents:
222
diff
changeset
|
144 type.BaseUnitCost = XmlTools.GetDoubleValueFromAttribute(elem, "basePoints"); |
158
eb9a6d91a6db
Fixes #190: Mutex groups aren't honoured when adding equipment
IBBoard <dev@ibboard.co.uk>
parents:
156
diff
changeset
|
145 } |
eb9a6d91a6db
Fixes #190: Mutex groups aren't honoured when adding equipment
IBBoard <dev@ibboard.co.uk>
parents:
156
diff
changeset
|
146 catch (FormatException ex) |
eb9a6d91a6db
Fixes #190: Mutex groups aren't honoured when adding equipment
IBBoard <dev@ibboard.co.uk>
parents:
156
diff
changeset
|
147 { |
217
89e26d51afc2
Fixes #226: "NullReferenceException" for a unitID
IBBoard <dev@ibboard.co.uk>
parents:
207
diff
changeset
|
148 throw new InvalidFileException(ex.Message, ex); |
156
62ff1ac132d2
Fixes #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
154
diff
changeset
|
149 } |
62ff1ac132d2
Fixes #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
154
diff
changeset
|
150 |
52 | 151 string mainCatID = elem.GetAttribute("cat"); |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
152 Category cat = type.Race.GetCategory(mainCatID); |
52 | 153 |
154 if (cat == null) | |
155 { | |
217
89e26d51afc2
Fixes #226: "NullReferenceException" for a unitID
IBBoard <dev@ibboard.co.uk>
parents:
207
diff
changeset
|
156 throw new InvalidFileException(String.Format("Category with ID '{1}' did not exist for UnitType '{0}'", type.Name, mainCatID)); |
52 | 157 } |
158 | |
159 type.MainCategory = cat; | |
133
a6d1cc17ec33
* Fix XPath query so that units get the right stats instead of just the first stats
IBBoard <dev@ibboard.co.uk>
parents:
128
diff
changeset
|
160 XmlElement statsElement = WarFoundryXmlFactoryUtils.SelectSingleElement(elem, "race:stats"); |
81
032b174fc17a
Re #10 - Refactoring for readability
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
161 Stats unitStats = ParseUnitStats(statsElement, type.GameSystem); |
170 | 162 type.SetUnitStats(unitStats); |
163 } | |
164 | |
165 private void LoadEquipmentSlotsForUnitType(XmlElement elem, UnitType type) | |
166 { | |
176
22429737cd77
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
175
diff
changeset
|
167 foreach (XmlElement equipSlot in WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:equipmentSlots/race:equipmentSlot")) |
170 | 168 { |
176
22429737cd77
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
175
diff
changeset
|
169 LoadEquipmentSlotForUnitType (type, equipSlot); |
22429737cd77
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
175
diff
changeset
|
170 } |
22429737cd77
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
175
diff
changeset
|
171 } |
22429737cd77
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
175
diff
changeset
|
172 |
22429737cd77
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
175
diff
changeset
|
173 private static void LoadEquipmentSlotForUnitType (UnitType type, XmlElement equipSlot) |
22429737cd77
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
175
diff
changeset
|
174 { |
22429737cd77
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
175
diff
changeset
|
175 string slotName = equipSlot.GetAttribute ("name"); |
183
36adabb1c3ea
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
176
diff
changeset
|
176 AbstractLimit limit = GetMaxLimit (equipSlot); |
176
22429737cd77
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
175
diff
changeset
|
177 |
22429737cd77
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
175
diff
changeset
|
178 if (limit!=null) |
22429737cd77
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
175
diff
changeset
|
179 { |
22429737cd77
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
175
diff
changeset
|
180 type.AddEquipmentSlot (slotName, limit); |
170 | 181 } |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
182 } |
183
36adabb1c3ea
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
176
diff
changeset
|
183 |
36adabb1c3ea
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
176
diff
changeset
|
184 private static AbstractLimit GetMaxLimit (XmlElement equipSlot) |
36adabb1c3ea
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
176
diff
changeset
|
185 { |
36adabb1c3ea
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
176
diff
changeset
|
186 XmlElement limitElem = WarFoundryXmlFactoryUtils.SelectSingleElement(equipSlot, "race:maxLimit/*[1]"); |
36adabb1c3ea
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
176
diff
changeset
|
187 return GetLimitFromElement(limitElem); |
36adabb1c3ea
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
176
diff
changeset
|
188 } |
176
22429737cd77
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
175
diff
changeset
|
189 |
22429737cd77
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
175
diff
changeset
|
190 private static AbstractLimit GetLimitFromElement(XmlElement limitElem) |
22429737cd77
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
175
diff
changeset
|
191 { |
198 | 192 AbstractLimit limit = null; |
193 | |
194 if (limitElem != null) | |
195 { | |
196 switch (limitElem.LocalName) | |
197 { | |
198 case "percentageLimit": | |
199 double limitPercent = XmlTools.GetDoubleValueFromAttribute(limitElem, "limit"); | |
200 bool roundUp = limitElem.GetAttribute("round").Equals("up"); | |
201 limit = new SimpleRoundedPercentageLimit(limitPercent, roundUp); | |
202 break; | |
203 case "sizeConstrainedLimit": | |
204 limit = new NumericSizeConstrainedLimit(XmlTools.GetIntValueFromAttribute(limitElem, "limit")); | |
205 break; | |
206 case "absoluteLimit": | |
207 limit = new AbsoluteNumericLimit(XmlTools.GetIntValueFromAttribute(limitElem, "limit")); | |
208 break; | |
209 case "unitSizeLimit": | |
207 | 210 limit = new SimpleRoundedPercentageLimit(100); |
198 | 211 break; |
212 default: | |
213 //TODO: Warn of missing handler for when we've extended the limit list | |
214 break; | |
215 } | |
176
22429737cd77
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
175
diff
changeset
|
216 } |
22429737cd77
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
175
diff
changeset
|
217 |
22429737cd77
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
175
diff
changeset
|
218 return limit; |
22429737cd77
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
175
diff
changeset
|
219 } |
22429737cd77
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
175
diff
changeset
|
220 |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
221 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
222 private void LoadEquipmentForUnitType(XmlElement elem, UnitType type) |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
223 { |
85
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
224 foreach (XmlElement equip in WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:unitEquipment/race:unitEquipmentItem")) |
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
225 { |
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
226 string id = equip.GetAttribute("id"); |
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
227 EquipmentItem equipItem = type.Race.GetEquipmentItem(id); |
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
228 |
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
229 if (equipItem!=null) |
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
230 { |
154
4a02c07278e7
Re #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
152
diff
changeset
|
231 string mutexGroupString = equip.GetAttribute("exclusivityGroups"); |
4a02c07278e7
Re #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
152
diff
changeset
|
232 string[] mutexGroups; |
4a02c07278e7
Re #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
152
diff
changeset
|
233 |
4a02c07278e7
Re #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
152
diff
changeset
|
234 if (mutexGroupString == "") |
4a02c07278e7
Re #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
152
diff
changeset
|
235 { |
4a02c07278e7
Re #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
152
diff
changeset
|
236 mutexGroupString = equip.GetAttribute("exclusivityGroup"); |
4a02c07278e7
Re #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
152
diff
changeset
|
237 } |
4a02c07278e7
Re #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
152
diff
changeset
|
238 |
4a02c07278e7
Re #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
152
diff
changeset
|
239 if (mutexGroupString != "") |
4a02c07278e7
Re #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
152
diff
changeset
|
240 { |
4a02c07278e7
Re #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
152
diff
changeset
|
241 string[] groups = mutexGroupString.Split(','); |
4a02c07278e7
Re #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
152
diff
changeset
|
242 int groupCount = groups.Length; |
4a02c07278e7
Re #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
152
diff
changeset
|
243 |
4a02c07278e7
Re #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
152
diff
changeset
|
244 for (int i = 0; i < groupCount; i++) |
4a02c07278e7
Re #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
152
diff
changeset
|
245 { |
4a02c07278e7
Re #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
152
diff
changeset
|
246 groups[i] = groups[i].Trim(); |
4a02c07278e7
Re #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
152
diff
changeset
|
247 } |
4a02c07278e7
Re #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
152
diff
changeset
|
248 |
4a02c07278e7
Re #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
152
diff
changeset
|
249 mutexGroups = groups; |
4a02c07278e7
Re #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
152
diff
changeset
|
250 } |
4a02c07278e7
Re #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
152
diff
changeset
|
251 else |
4a02c07278e7
Re #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
152
diff
changeset
|
252 { |
4a02c07278e7
Re #185: Problems with decimals in race definitions
IBBoard <dev@ibboard.co.uk>
parents:
152
diff
changeset
|
253 mutexGroups = new string[0]; |
152
0c0e14f03785
Re #180: Add multiple mutex groups
IBBoard <dev@ibboard.co.uk>
parents:
138
diff
changeset
|
254 } |
0c0e14f03785
Re #180: Add multiple mutex groups
IBBoard <dev@ibboard.co.uk>
parents:
138
diff
changeset
|
255 |
170 | 256 UnitEquipmentItem unitEquipItem = new UnitEquipmentItem(equipItem, type, mutexGroups); |
257 | |
258 string equipSlot = equip.GetAttribute("equipmentSlot"); | |
259 | |
260 if (equipSlot != "") | |
261 { | |
262 if (type.HasEquipmentSlot(equipSlot)) | |
263 { | |
264 unitEquipItem.SlotName = equipSlot; | |
265 } | |
266 else | |
267 { | |
268 throw new InvalidFileException("Attribute 'equipmentSlot' of unit equipment item " + id + " for " + type.Name + " was not a valid slot name"); | |
269 } | |
198 | 270 } |
271 | |
272 AbstractLimit limit = GetMaxLimit(equip); | |
273 | |
274 if (limit != null) | |
275 { | |
276 unitEquipItem.MaxLimit = limit; | |
277 } | |
278 | |
279 limit = GetMinLimit(equip); | |
280 | |
281 if (limit != null) | |
282 { | |
283 unitEquipItem.MinLimit = limit; | |
167
9ba56a8e5096
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
158
diff
changeset
|
284 } |
183
36adabb1c3ea
Re #198: Add slots with counts to units
IBBoard <dev@ibboard.co.uk>
parents:
176
diff
changeset
|
285 |
90
bd2aaa9a61df
Re #50: Complete loading of core XML file
IBBoard <dev@ibboard.co.uk>
parents:
88
diff
changeset
|
286 unitEquipItem.RoundNumberUp = equip.GetAttribute("roundDirection").Equals("up"); |
86
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
287 |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
288 try |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
289 { |
219
f609bcf7035b
Fixes #222: decimal comma/point not handled correctly (again?) in costMultiplier
IBBoard <dev@ibboard.co.uk>
parents:
217
diff
changeset
|
290 unitEquipItem.IsRequired = XmlTools.GetBoolValueFromAttribute(equip, "required"); |
86
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
291 } |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
292 catch(FormatException e) |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
293 { |
87
9fba3b4ccdcd
Re #50: Complete core XML loading
IBBoard <dev@ibboard.co.uk>
parents:
86
diff
changeset
|
294 throw new InvalidFileException("Attribute 'required' of unit equipment item " + id + " for " + type.Name + " was not a valid boolean", e); |
86
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
295 } |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
296 |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
297 try |
91
571d8ddc7d9a
Fixes #111: "Replace Weapon" button doesn't always enable
IBBoard <dev@ibboard.co.uk>
parents:
90
diff
changeset
|
298 { |
219
f609bcf7035b
Fixes #222: decimal comma/point not handled correctly (again?) in costMultiplier
IBBoard <dev@ibboard.co.uk>
parents:
217
diff
changeset
|
299 unitEquipItem.CostMultiplier = XmlTools.GetDoubleValueFromAttribute(equip, "costMultiplier"); |
86
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
300 } |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
301 catch (FormatException e) |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
302 { |
87
9fba3b4ccdcd
Re #50: Complete core XML loading
IBBoard <dev@ibboard.co.uk>
parents:
86
diff
changeset
|
303 throw new InvalidFileException("Attribute 'costMultiplier' of unit equipment item " + id + " for " + type.Name + " was not a valid decimal number", e); |
86
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
304 } |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
305 |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
306 try |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
307 { |
88
110a7b0bcab5
Re #50: Complete core loading from XML
IBBoard <dev@ibboard.co.uk>
parents:
87
diff
changeset
|
308 unitEquipItem.CostRoundType = (RoundType) Enum.Parse(typeof(RoundType), equip.GetAttribute("costRounding")); |
86
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
309 } |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
310 catch (ArgumentException e) |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
311 { |
87
9fba3b4ccdcd
Re #50: Complete core XML loading
IBBoard <dev@ibboard.co.uk>
parents:
86
diff
changeset
|
312 throw new InvalidFileException("Attribute 'costRounding' of unit equipment item " + id + " for " + type.Name + " was not a valid rounding type", e); |
86
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
313 } |
85
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
314 } |
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
315 else |
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
316 { |
128
45a9452579a2
Fixes #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
317 throw new InvalidFileException("Equipment item with ID '" + id + "' was required by " + type.Name + " but was not found"); |
85
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
318 } |
195
11bad32cfa06
* Implement new equipment limit loading
IBBoard <dev@ibboard.co.uk>
parents:
192
diff
changeset
|
319 } |
198 | 320 } |
321 | |
322 private static AbstractLimit GetMinLimit(XmlElement elem) | |
323 { | |
324 XmlElement limitElem = WarFoundryXmlFactoryUtils.SelectSingleElement(elem, "race:minLimit/*[1]"); | |
325 return GetLimitFromElement(limitElem); | |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
326 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
327 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
328 private void LoadAbilitiesForUnitType(XmlElement elem, UnitType type) |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
329 { |
222
2f9ad5385707
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
221
diff
changeset
|
330 foreach (XmlElement abilityElem in WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:unitAbilities/race:unitAbility")) |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
331 { |
222
2f9ad5385707
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
221
diff
changeset
|
332 string id = abilityElem.GetAttribute("abilityID"); |
221
78f7456f6419
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
219
diff
changeset
|
333 Ability ability = type.Race.GetAbility(id); |
78f7456f6419
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
219
diff
changeset
|
334 |
78f7456f6419
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
219
diff
changeset
|
335 if (ability == null) |
78f7456f6419
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
219
diff
changeset
|
336 { |
78f7456f6419
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
219
diff
changeset
|
337 throw new InvalidFileException("Ability for "+type.Name+ " with ID "+id+ " did not exist in race definition"); |
78f7456f6419
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
219
diff
changeset
|
338 } |
222
2f9ad5385707
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
221
diff
changeset
|
339 |
2f9ad5385707
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
221
diff
changeset
|
340 bool required = XmlTools.GetBoolValueFromAttribute(abilityElem, "required"); |
221
78f7456f6419
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
219
diff
changeset
|
341 type.AddAbility(ability, required); |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
342 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
343 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
344 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
345 private void LoadContainedUnitsForUnitType(XmlElement elem, UnitType type) |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
346 { |
69
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
347 foreach (XmlElement containedUnitType in WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:contains/race:containedUnit")) |
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
348 { |
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
349 string id = containedUnitType.GetAttribute("containedID"); |
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
350 UnitType containedType = GetUnitTypeFromDocument(elem.OwnerDocument, id, type.Race); |
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
351 |
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
352 if (containedType!=null) |
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
353 { |
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
354 type.AddContainedUnitType(containedType); |
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
355 } |
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
356 else |
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
357 { |
128
45a9452579a2
Fixes #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
358 throw new InvalidFileException("Unit type " + type.Name + " tried to contain undefined unit with ID "+id); |
69
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
359 } |
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
360 } |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
361 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
362 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
363 private void LoadRequirementsForUnitType(XmlElement elem, UnitType type) |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
364 { |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
365 //TODO: Load requirements |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
366 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
367 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
368 private void LoadExtraDataForUnitType(XmlElement elem, UnitType type) |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
369 { |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
370 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
371 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
372 private void LoadNotesForUnitType(XmlElement elem, UnitType type) |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
373 { |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
374 XmlNode node = WarFoundryXmlFactoryUtils.SelectSingleNode(elem, "race:notes"); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
375 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
376 if (node!=null) |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
377 { |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
378 type.Notes = node.InnerText; |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
379 } |
52 | 380 } |
381 | |
382 private Stats ParseUnitStats(XmlElement elem, GameSystem system) | |
383 { | |
384 String statsID = elem.GetAttribute("statSet"); | |
385 SystemStats statsSet; | |
386 | |
387 if (statsID == "") | |
388 { | |
389 statsSet = system.StandardSystemStats; | |
390 } | |
391 else | |
392 { | |
393 statsSet = system.GetSystemStatsForID(statsID); | |
394 } | |
395 | |
396 Stats stats = new Stats(statsSet); | |
397 | |
53 | 398 foreach (XmlElement stat in WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:stat")) |
52 | 399 { |
67
e6200220ece3
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
55
diff
changeset
|
400 String statName = stat.GetAttribute("name"); |
e6200220ece3
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
55
diff
changeset
|
401 stats.SetStatValue(statName, stat.InnerText); |
52 | 402 } |
403 | |
404 return stats; | |
405 } | |
406 | |
407 private EquipmentItem CreateEquipmentItemFromElement(XmlElement elem, Race race) | |
408 { | |
409 string id = elem.GetAttribute("id"); | |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
410 EquipmentItem item = race.GetEquipmentItem(id); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
411 |
85
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
412 if (item == null) |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
413 { |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
414 item = CreateEquipmentItemFromElement(elem, id, race); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
415 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
416 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
417 return item; |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
418 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
419 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
420 private EquipmentItem CreateEquipmentItemFromElement(XmlElement elem, string id, Race race) |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
421 { |
52 | 422 string name = elem.GetAttribute("name"); |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
423 EquipmentItem item = new EquipmentItem(id, name, race); |
55
9080366031c0
Re #9 - Refactor for small methods
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
424 double cost = 0; |
52 | 425 ArmourType armourType; |
426 | |
427 try | |
428 { | |
429 cost = XmlTools.GetDoubleValueFromAttribute(elem, "cost"); | |
430 } | |
431 catch(FormatException ex) | |
432 { | |
433 throw new InvalidFileException("Attribute 'cost' of equipment item "+id+" was not a valid number", ex); | |
434 } | |
435 | |
436 try | |
437 { | |
438 armourType = (ArmourType)Enum.Parse(typeof(ArmourType), elem.GetAttribute("armourType")); | |
439 } | |
440 catch(ArgumentException ex) | |
441 { | |
442 throw new InvalidFileException("Attribute 'armourType' of equipment "+id+" was not a valid value from the enumeration", ex); | |
443 } | |
444 | |
445 //TODO: Parse equipment stats if there are any | |
55
9080366031c0
Re #9 - Refactor for small methods
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
446 item.Cost = cost; |
9080366031c0
Re #9 - Refactor for small methods
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
447 item.ItemArmourType = armourType; |
85
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
448 race.AddEquipmentItem(item); |
55
9080366031c0
Re #9 - Refactor for small methods
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
449 return item; |
52 | 450 } |
451 | |
452 private Ability CreateAbilityFromElement(XmlElement elem, Race race) | |
453 { | |
454 string id = elem.GetAttribute("id"); | |
455 string name = elem.GetAttribute("name"); | |
456 Ability ability = new Ability(id, name); | |
457 XmlNode node = elem.SelectSingleNode("description", WarFoundryXmlFactoryUtils.GetNamespaceManager()); | |
458 ability.Description = (node == null) ? "" : node.InnerText; | |
85
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
459 race.AddAbility(ability); |
52 | 460 return ability; |
461 } | |
462 } | |
463 } |