Mercurial > repos > IBBoard.WarFoundry.API
annotate api/Factories/Xml/WarFoundryXmlRaceFactory.cs @ 86:129636305ad7
Re #50: Complete core loading of XML files
* Load remaining attributes of UnitEquipmentItem from XML files
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 01 Aug 2009 17:55:35 +0000 |
parents | 46ad6f478203 |
children | 9fba3b4ccdcd |
rev | line source |
---|---|
52 | 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 under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. | |
4 // | |
5 | |
6 using System; | |
7 using System.Collections.Generic; | |
8 using System.IO; | |
9 using System.Xml; | |
10 using IBBoard.Xml; | |
11 using IBBoard.IO; | |
86
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
12 using IBBoard.Lang; |
52 | 13 using IBBoard.Logging; |
14 using ICSharpCode.SharpZipLib.Zip; | |
15 using IBBoard.WarFoundry.API.Objects; | |
16 | |
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 | |
26 public WarFoundryXmlRaceFactory(WarFoundryXmlFactory factory) : base (factory) | |
27 { | |
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 LogNotifier.DebugFormat(GetType(), "Completed loading of Race with ID {0}", race.ID); | |
85 } | |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
86 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
87 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
|
88 { |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
89 Category cat = CreateCategoryFromElement(elem); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
90 parentRace.AddCategory(cat); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
91 return cat; |
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 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
94 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
|
95 { |
71
0303ac938fc5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
70
diff
changeset
|
96 return GetUnitTypeForElement(WarFoundryXmlFactoryUtils.SelectSingleElement(doc, "/race:race/race:units/race:unit[@id='"+id+"']"), parentRace); |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
97 } |
52 | 98 |
70
780f3926abad
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
99 private UnitType GetUnitTypeForElement(XmlElement elem, Race parentRace) |
52 | 100 { |
101 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
|
102 UnitType type = parentRace.GetUnitType(id); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
103 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
104 if (type==null) |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
105 { |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
106 type = CreateUnitTypeFromElement(elem, id, parentRace); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
107 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
108 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
109 return type; |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
110 } |
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 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
|
113 { |
52 | 114 string name = elem.GetAttribute("typeName"); |
115 UnitType type = new UnitType(id, name, parentRace); | |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
116 LoadCoreValuesForUnitType(elem, type); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
117 LoadEquipmentForUnitType(elem, type); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
118 LoadAbilitiesForUnitType(elem, type); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
119 LoadContainedUnitsForUnitType(elem, type); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
120 LoadRequirementsForUnitType(elem, type); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
121 LoadExtraDataForUnitType(elem, type); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
122 LoadNotesForUnitType(elem, type); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
123 parentRace.AddUnitType(type); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
124 return type; |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
125 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
126 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
127 private void LoadCoreValuesForUnitType(XmlElement elem, UnitType type) |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
128 { |
52 | 129 type.MaxNumber = XmlTools.GetIntValueFromAttribute(elem, "maxNum"); |
130 type.MinNumber = XmlTools.GetIntValueFromAttribute(elem, "minNum"); | |
131 type.MaxSize = XmlTools.GetIntValueFromAttribute(elem, "maxSize"); | |
132 type.MinSize = XmlTools.GetIntValueFromAttribute(elem, "minSize"); | |
133 type.BaseSize = XmlTools.GetIntValueFromAttribute(elem, "baseSize"); | |
134 type.CostPerTrooper = XmlTools.GetIntValueFromAttribute(elem, "points"); | |
135 type.BaseUnitCost = XmlTools.GetIntValueFromAttribute(elem, "unitPoints"); | |
136 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
|
137 Category cat = type.Race.GetCategory(mainCatID); |
52 | 138 |
139 if (cat == null) | |
140 { | |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
141 throw new InvalidDataException(String.Format("Attribute 'cat' of UnitType {0} (value: {1}) did not reference a valid category", type.ID, mainCatID)); |
52 | 142 } |
143 | |
144 type.MainCategory = cat; | |
145 XmlElement statsElement = WarFoundryXmlFactoryUtils.SelectSingleElement(elem, "/race:race/race:units/race:unit/race:stats"); | |
81
032b174fc17a
Re #10 - Refactoring for readability
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
146 Stats unitStats = ParseUnitStats(statsElement, type.GameSystem); |
032b174fc17a
Re #10 - Refactoring for readability
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
147 type.SetUnitStats(unitStats); |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
148 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
149 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
150 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
|
151 { |
85
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
152 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
|
153 { |
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
154 string id = equip.GetAttribute("id"); |
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
155 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
|
156 |
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
157 if (equipItem!=null) |
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
158 { |
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
159 UnitEquipmentItem unitEquipItem = new UnitEquipmentItem(equipItem); |
86
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
160 unitEquipItem.MutexGroup = elem.GetAttribute("exclusivityGroup"); |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
161 unitEquipItem.RoundNumberUp = elem.GetAttribute("roundDirection").Equals("up"); |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
162 |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
163 |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
164 try |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
165 { |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
166 unitEquipItem.IsRequired = bool.Parse(elem.GetAttribute("required")); |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
167 } |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
168 catch(FormatException e) |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
169 { |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
170 throw new InvalidFileException("Attribute 'required' of unit equipment item " + id + " for " + type.Name + " was not a valid boolean", ex); |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
171 } |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
172 |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
173 try |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
174 { |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
175 unitEquipItem.MinNumber = int.Parse(elem.GetAttribute("minNum")); |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
176 } |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
177 catch (FormatException e) |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
178 { |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
179 throw new InvalidFileException("Attribute 'minNum' of unit equipment item " + id + " for " + type.Name + " was not a valid integer", ex); |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
180 } |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
181 |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
182 try |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
183 { |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
184 unitEquipItem.MaxNumber = int.Parse(elem.GetAttribute("maxNum")); |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
185 } |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
186 catch (FormatException e) |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
187 { |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
188 throw new InvalidFileException("Attribute 'maxNum' of unit equipment item " + id + " for " + type.Name + " was not a valid integer", ex); |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
189 } |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
190 |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
191 try |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
192 { |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
193 unitEquipItem.MinPercentage = double.Parse(elem.GetAttribute("minPercentage")); |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
194 } |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
195 catch (FormatException e) |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
196 { |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
197 throw new InvalidFileException("Attribute 'minPercentage' of unit equipment item " + id + " for " + type.Name + " was not a valid decimal number", ex); |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
198 } |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
199 |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
200 try |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
201 { |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
202 unitEquipItem.MaxPercentage = double.Parse(elem.GetAttribute("maxPercentage")); |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
203 } |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
204 catch (FormatException e) |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
205 { |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
206 throw new InvalidFileException("Attribute 'maxPercentage' of unit equipment item " + id + " for " + type.Name + " was not a valid decimal number", ex); |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
207 } |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
208 |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
209 try |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
210 { |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
211 unitEquipItem.CostMultiplier = double.Parse(elem.GetAttribute("costMultiplier")); |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
212 } |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
213 catch (FormatException e) |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
214 { |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
215 throw new InvalidFileException("Attribute 'costMultiplier' of unit equipment item " + id + " for " + type.Name + " was not a valid decimal number", ex); |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
216 } |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
217 |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
218 try |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
219 { |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
220 unitEquipItem.CostRoundType = Enum.Parse(typeof(RoundType), elem.GetAttribute("costRounding")); |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
221 } |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
222 catch (ArgumentException e) |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
223 { |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
224 throw new InvalidFileException("Attribute 'costRounding' of unit equipment item " + id + " for " + type.Name + " was not a valid rounding type", ex); |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
225 } |
129636305ad7
Re #50: Complete core loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
85
diff
changeset
|
226 |
85
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
227 type.AddEquipmentItem(unitEquipItem); |
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 else |
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
230 { |
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
231 //TODO: Warn about missing item |
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
232 } |
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
233 } |
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
234 |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
235 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
236 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
237 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
|
238 { |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
239 foreach (XmlElement ability in WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:unitAbilities/race:unitAbility")) |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
240 { |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
241 string id = ability.GetAttribute("abilityID"); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
242 bool required = XmlTools.GetBoolValueFromAttribute(ability, "required"); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
243 type.AddAbility(type.Race.GetAbility(id), required); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
244 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
245 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
246 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
247 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
|
248 { |
69
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
249 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
|
250 { |
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
251 string id = containedUnitType.GetAttribute("containedID"); |
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
252 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
|
253 |
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
254 if (containedType!=null) |
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
255 { |
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
256 type.AddContainedUnitType(containedType); |
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
257 } |
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
258 else |
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
259 { |
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
260 // TODO: Warn on invalid data, but don't exception |
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
261 } |
91cf8efbea0b
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
68
diff
changeset
|
262 } |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
263 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
264 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
265 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
|
266 { |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
267 //TODO: Load requirements |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
268 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
269 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
270 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
|
271 { |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
272 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
273 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
274 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
|
275 { |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
276 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
|
277 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
278 if (node!=null) |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
279 { |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
280 type.Notes = node.InnerText; |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
281 } |
52 | 282 } |
283 | |
284 private Stats ParseUnitStats(XmlElement elem, GameSystem system) | |
285 { | |
286 String statsID = elem.GetAttribute("statSet"); | |
287 SystemStats statsSet; | |
288 | |
289 if (statsID == "") | |
290 { | |
291 statsSet = system.StandardSystemStats; | |
292 } | |
293 else | |
294 { | |
295 statsSet = system.GetSystemStatsForID(statsID); | |
296 } | |
297 | |
298 Stats stats = new Stats(statsSet); | |
299 | |
53 | 300 foreach (XmlElement stat in WarFoundryXmlFactoryUtils.SelectNodes(elem, "race:stat")) |
52 | 301 { |
67
e6200220ece3
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
55
diff
changeset
|
302 String statName = stat.GetAttribute("name"); |
e6200220ece3
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
55
diff
changeset
|
303 stats.SetStatValue(statName, stat.InnerText); |
52 | 304 } |
305 | |
306 return stats; | |
307 } | |
308 | |
309 private EquipmentItem CreateEquipmentItemFromElement(XmlElement elem, Race race) | |
310 { | |
311 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
|
312 EquipmentItem item = race.GetEquipmentItem(id); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
313 |
85
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
314 if (item == null) |
68
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
315 { |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
316 item = CreateEquipmentItemFromElement(elem, id, race); |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
317 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
318 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
319 return item; |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
320 } |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
321 |
10d14a7051d5
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
322 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
|
323 { |
52 | 324 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
|
325 EquipmentItem item = new EquipmentItem(id, name, race); |
55
9080366031c0
Re #9 - Refactor for small methods
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
326 double cost = 0; |
52 | 327 ArmourType armourType; |
328 | |
329 try | |
330 { | |
331 cost = XmlTools.GetDoubleValueFromAttribute(elem, "cost"); | |
332 } | |
333 catch(FormatException ex) | |
334 { | |
335 throw new InvalidFileException("Attribute 'cost' of equipment item "+id+" was not a valid number", ex); | |
336 } | |
337 | |
338 try | |
339 { | |
340 armourType = (ArmourType)Enum.Parse(typeof(ArmourType), elem.GetAttribute("armourType")); | |
341 } | |
342 catch(ArgumentException ex) | |
343 { | |
344 throw new InvalidFileException("Attribute 'armourType' of equipment "+id+" was not a valid value from the enumeration", ex); | |
345 } | |
346 | |
347 //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
|
348 item.Cost = cost; |
9080366031c0
Re #9 - Refactor for small methods
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
349 item.ItemArmourType = armourType; |
85
46ad6f478203
Re #50: Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
350 race.AddEquipmentItem(item); |
55
9080366031c0
Re #9 - Refactor for small methods
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
351 return item; |
52 | 352 } |
353 | |
354 private Ability CreateAbilityFromElement(XmlElement elem, Race race) | |
355 { | |
356 string id = elem.GetAttribute("id"); | |
357 string name = elem.GetAttribute("name"); | |
358 Ability ability = new Ability(id, name); | |
359 XmlNode node = elem.SelectSingleNode("description", WarFoundryXmlFactoryUtils.GetNamespaceManager()); | |
360 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
|
361 race.AddAbility(ability); |
52 | 362 return ability; |
363 } | |
364 } | |
365 } |