Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate api/Factories/Xml/WarFoundryXmlFactory.cs @ 48:b49372dd8afa
Re #50 - Complete loading of XML files
* Load base points
* Check category exists when loaded
Re #10 - Refactor source code for readability
* Rearrange some methods
* Move storing "extra data" in to a method
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 28 Mar 2009 16:37:27 +0000 |
parents | 85f2b9c3609c |
children | 9d31d063b194 |
rev | line source |
---|---|
15 | 1 // This file (WarFoundryXmlFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard. |
0 | 2 // |
15 | 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. |
0 | 4 |
5 using System; | |
6 using System.IO; | |
7 using System.Xml; | |
8 using System.Xml.Schema; | |
34 | 9 using System.Xml.XPath; |
0 | 10 using System.Collections.Generic; |
11 using System.Text; | |
12 using IBBoard; | |
13 using IBBoard.IO; | |
14 using IBBoard.Lang; | |
15 using IBBoard.Logging; | |
16 using IBBoard.Xml; | |
17 using IBBoard.WarFoundry.API.Requirements; | |
18 using IBBoard.WarFoundry.API.Objects; | |
19 using ICSharpCode.SharpZipLib.Zip; | |
20 | |
21 namespace IBBoard.WarFoundry.API.Factories.Xml | |
22 { | |
23 /// <summary> | |
10 | 24 /// The WarFoundryXmlFactory loads WarFoundry classes from the native "XML in a zip" file format. Files are validated using the schema for the file type, so structurally invalid files should be identified at initial load. |
0 | 25 /// </summary> |
26 public class WarFoundryXmlFactory : AbstractNativeWarFoundryFactory | |
27 { | |
17
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
28 private static WarFoundryXmlFactory factory; |
23
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
29 private XmlReaderSettings settings; |
42 | 30 private XmlNamespaceManager nsManager; |
0 | 31 private Dictionary<IWarFoundryObject, XmlDocument> extraData = new Dictionary<IWarFoundryObject, XmlDocument>(); |
32 | |
17
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
33 public static AbstractNativeWarFoundryFactory GetFactory() |
0 | 34 { |
17
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
35 if (factory == null) |
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
36 { |
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
37 factory = new WarFoundryXmlFactory(); |
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
38 } |
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
39 |
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
40 return factory; |
0 | 41 } |
42 | |
43 protected WarFoundryXmlFactory() : base() | |
44 { | |
37 | 45 //Hide constructor |
0 | 46 } |
47 | |
48 protected override bool CheckCanFindArmyFileContent(ZipFile file) | |
49 { | |
50 return file.FindEntry("data.armyx", true) > -1; | |
51 } | |
52 | |
53 protected override bool CheckCanFindSystemFileContent(ZipFile file) | |
54 { | |
55 return file.FindEntry("data.systemx", true) > -1; | |
56 } | |
57 | |
58 protected override bool CheckCanFindRaceFileContent(ZipFile file) | |
59 { | |
60 return file.FindEntry("data.racex", true) > -1; | |
61 } | |
62 | |
63 protected XmlElement GetRootElementFromStream(Stream stream, WarFoundryXmlElementName elementName) | |
64 { | |
65 XmlDocument doc = CreateXmlDocumentFromStream(stream); | |
66 XmlElement elem = (XmlElement)doc.LastChild; | |
67 | |
43
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
68 if (!elem.LocalName.Equals(elementName.Value)) |
0 | 69 { |
70 throw new InvalidFileException(String.Format("Root element of XML was not valid. Expected {0} but got {1}", elementName.Value, elem.Name)); | |
71 } | |
72 | |
73 return elem; | |
74 } | |
75 | |
76 protected override Stream GetArmyDataStream(ZipFile file) | |
77 { | |
78 return file.GetInputStream(file.FindEntry("data.armyx", true)); | |
79 } | |
80 | |
81 protected override Army CreateArmyFromStream (ZipFile file, Stream dataStream) | |
82 { | |
83 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.ARMY_ELEMENT); | |
84 return CreateArmyFromElement(file, elem); | |
85 } | |
86 | |
87 private Army CreateArmyFromElement(ZipFile file, XmlElement elem) | |
88 { | |
89 string name = elem.GetAttribute("name"); | |
90 string systemID = elem.GetAttribute("gameSystem"); | |
91 GameSystem system = WarFoundryLoader.GetDefault().GetGameSystem(systemID); | |
92 string raceID = elem.GetAttribute("race"); | |
93 Race race = WarFoundryLoader.GetDefault().GetRace(system, raceID); | |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
94 int points = GetIntValueFromAttribute(elem, "maxPoints"); |
10 | 95 Army army = new Army(race, name, points, file); |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
96 StoreExtraData(army, elem); |
0 | 97 return army; |
98 } | |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
99 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
100 private void StoreExtraData(WarFoundryStagedLoadingObject wfObject, XmlElement elem) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
101 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
102 extraData[wfObject] = elem.OwnerDocument; |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
103 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
104 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
105 public XmlDocument GetExtraData(IWarFoundryObject obj) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
106 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
107 XmlDocument extra = null; |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
108 extraData.TryGetValue(obj, out extra); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
109 return extra; |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
110 } |
0 | 111 |
112 protected override Stream GetGameSystemDataStream (ZipFile file) | |
113 { | |
114 return file.GetInputStream(file.FindEntry("data.systemx", true)); | |
115 } | |
116 | |
117 protected override GameSystem CreateGameSystemFromStream (ZipFile file, Stream dataStream) | |
118 { | |
119 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.SYSTEM_ELEMENT); | |
120 LogNotifier.Debug(GetType(), "Create GameSystem"); | |
121 return CreateSystemFromElement(file, elem); | |
122 } | |
123 | |
124 private GameSystem CreateSystemFromElement(ZipFile file, XmlElement elem) | |
125 { | |
126 string id = elem.GetAttribute("id"); | |
127 string name = elem.GetAttribute("name"); | |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
128 GameSystem system = new GameSystem(id, name, this); |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
129 StoreExtraData(system, elem); |
0 | 130 return system; |
131 } | |
132 | |
133 protected override Stream GetRaceDataStream (ZipFile file) | |
134 { | |
135 return file.GetInputStream(file.FindEntry("data.racex", true)); | |
136 } | |
137 | |
138 protected override Race CreateRaceFromStream (ZipFile file, Stream dataStream) | |
139 { | |
140 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.RACE_ELEMENT); | |
141 LogNotifier.Debug(GetType(), "Create Race"); | |
142 return CreateRaceFromElement(file, elem); | |
143 } | |
144 | |
145 private Race CreateRaceFromElement(ZipFile file, XmlElement elem) | |
146 { | |
147 string id = elem.GetAttribute("id"); | |
148 string subid = elem.GetAttribute("subid"); | |
149 string systemID = elem.GetAttribute("system"); | |
150 string name = elem.GetAttribute("name"); | |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
151 Race race = new Race(id, subid, name, systemID, this); |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
152 StoreExtraData(race, elem); |
0 | 153 return race; |
154 } | |
155 | |
156 protected XmlDocument CreateXmlDocumentFromStream(Stream stream) | |
157 { | |
158 XmlDocument doc = new XmlDocument(); | |
23
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
159 XmlReader reader = XmlReader.Create(stream, GetReaderSettings()); |
0 | 160 |
161 try | |
162 { | |
47
85f2b9c3609c
Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
163 doc.Load(reader); |
0 | 164 } |
165 //Don't catch XMLSchemaExceptions - let them get thrown out | |
166 finally | |
167 { | |
168 reader.Close(); | |
169 } | |
170 | |
171 return doc; | |
23
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
172 } |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
173 |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
174 /// <summary> |
23
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
175 /// Lazy-getter for XML reader settings. May throw a <see cref="InvalidDataException"/> if there is a problem with the translation schema. |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
176 /// </summary> |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
177 /// <returns> |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
178 /// A <see cref="XmlReaderSettings"/> with the default values for validating the translation document against the translation schema |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
179 /// </returns> |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
180 private XmlReaderSettings GetReaderSettings() |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
181 { |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
182 if (settings == null) |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
183 { |
41
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
184 settings = new XmlReaderSettings(); |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
185 settings.ValidationType = ValidationType.Schema; |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
186 settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
187 settings.ProhibitDtd = true; |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
188 settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod); |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
189 XmlSchemaSet cache = new XmlSchemaSet(); |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
190 string path = IBBoard.Constants.ExecutablePath + "/dtds/"; |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
191 string nsBase = "http://ibboard.co.uk/warfoundry/"; |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
192 AddSchemaToCache(cache, nsBase + "core", path + "warfoundry-core.xsd"); |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
193 AddSchemaToCache(cache, nsBase + "cats", path + "warfoundry-cats.xsd"); |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
194 AddSchemaToCache(cache, nsBase + "race", path + "race.xsd"); |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
195 AddSchemaToCache(cache, nsBase + "system", path + "system.xsd"); |
42 | 196 AddSchemaToCache(cache, nsBase + "army", path + "army.xsd"); |
41
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
197 settings.Schemas.Add(cache); |
23
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
198 } |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
199 |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
200 return settings; |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
201 } |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
202 |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
203 private void ValidationEventMethod(object sender, ValidationEventArgs e) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
204 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
205 throw new InvalidDataException("Problem validating against schema for WarFoundry data: " + e.Exception.Message, e.Exception); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
206 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
207 |
41
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
208 private void AddSchemaToCache(XmlSchemaSet cache, string xmlNamespace, string schemaLocation) |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
209 { |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
210 try |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
211 { |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
212 cache.Add(xmlNamespace, schemaLocation); |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
213 } |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
214 catch (IOException ex) |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
215 { |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
216 LogNotifier.Warn(GetType(), "Problem reading schema: " + ex.Message, ex); |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
217 } |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
218 catch (XmlSchemaException ex) |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
219 { |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
220 LogNotifier.Warn(GetType(), "Problem validating schema for WarFoundry data: " + ex.Message, ex); |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
221 } |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
222 catch (XmlException ex) |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
223 { |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
224 LogNotifier.Warn(GetType(), "Problem reading data for schema: " + ex.Message, ex); |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
225 } |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
226 } |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
227 |
42 | 228 private XmlNamespaceManager GetNamespaceManager() |
229 { | |
230 if (nsManager == null) | |
231 { | |
232 nsManager = new XmlNamespaceManager(new NameTable()); | |
233 nsManager.AddNamespace("core", "http://ibboard.co.uk/warfoundry/core"); | |
234 nsManager.AddNamespace("cat", "http://ibboard.co.uk/warfoundry/cats"); | |
235 nsManager.AddNamespace("race", "http://ibboard.co.uk/warfoundry/race"); | |
236 nsManager.AddNamespace("system", "http://ibboard.co.uk/warfoundry/system"); | |
237 nsManager.AddNamespace("army", "http://ibboard.co.uk/warfoundry/army"); | |
238 } | |
239 | |
240 return nsManager; | |
241 } | |
242 | |
243 private XmlNodeList SelectNodes(XmlNode element, string xpathQuery) | |
244 { | |
245 return element.SelectNodes(xpathQuery, GetNamespaceManager()); | |
246 } | |
247 | |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
248 private XmlNode SelectSingleNode(XmlNode element, string xpathQuery) |
0 | 249 { |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
250 return element.SelectSingleNode(xpathQuery, GetNamespaceManager()); |
9
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
251 } |
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
252 |
10 | 253 private int GetIntValueFromAttribute(XmlElement elem, string attributeName) |
9
6ad505b6c36e
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
254 { |
40
3664eee50390
Fixes #47 - remove magic numbers
IBBoard <dev@ibboard.co.uk>
parents:
39
diff
changeset
|
255 try |
39 | 256 { |
40
3664eee50390
Fixes #47 - remove magic numbers
IBBoard <dev@ibboard.co.uk>
parents:
39
diff
changeset
|
257 return int.Parse(elem.GetAttribute(attributeName)); |
39 | 258 } |
40
3664eee50390
Fixes #47 - remove magic numbers
IBBoard <dev@ibboard.co.uk>
parents:
39
diff
changeset
|
259 catch(FormatException) |
3664eee50390
Fixes #47 - remove magic numbers
IBBoard <dev@ibboard.co.uk>
parents:
39
diff
changeset
|
260 { |
3664eee50390
Fixes #47 - remove magic numbers
IBBoard <dev@ibboard.co.uk>
parents:
39
diff
changeset
|
261 throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id"))); |
3664eee50390
Fixes #47 - remove magic numbers
IBBoard <dev@ibboard.co.uk>
parents:
39
diff
changeset
|
262 } |
0 | 263 } |
43
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
264 |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
265 private double GetDoubleValueFromAttribute(XmlElement elem, string attributeName) |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
266 { |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
267 double doubleVal = double.NaN; |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
268 string attribValue = elem.GetAttribute(attributeName); |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
269 |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
270 if (attribValue == "INF") |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
271 { |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
272 doubleVal = double.PositiveInfinity; |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
273 } |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
274 else |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
275 { |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
276 try |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
277 { |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
278 return int.Parse(attribValue); |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
279 } |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
280 catch(FormatException) |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
281 { |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
282 throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id"))); |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
283 } |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
284 } |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
285 |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
286 return doubleVal; |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
287 } |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
288 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
289 public override void CompleteLoading(IWarFoundryStagedLoadObject obj) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
290 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
291 LogNotifier.DebugFormat(GetType(), "Complete loading of {0} with ID {1}", obj.GetType().Name, obj.ID); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
292 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
293 if (obj is GameSystem) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
294 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
295 CompleteLoading((GameSystem)obj); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
296 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
297 else if (obj is Race) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
298 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
299 CompleteLoading((Race)obj); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
300 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
301 else if (obj is Army) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
302 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
303 CompleteLoading((Army) obj); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
304 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
305 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
306 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
307 public void CompleteLoading(GameSystem system) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
308 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
309 if (system.IsFullyLoaded) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
310 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
311 LogNotifier.DebugFormat(GetType(), "Object of type GameSystem with ID {0} is already fully loaded", system.ID); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
312 return; |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
313 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
314 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
315 if (system.IsLoading) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
316 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
317 LogNotifier.WarnFormat(GetType(), "Object of type GameSystem with ID {0} is already being loaded", system.ID); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
318 return; |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
319 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
320 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
321 system.SetAsLoading(); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
322 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
323 XmlDocument extraData = GetExtraData(system); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
324 LoadCategoriesForSystem(system, extraData); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
325 XmlElement statsElem = SelectSingleNode(extraData, "/system:system/system:sysStatsList"); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
326 string defaultStatsID = statsElem.GetAttribute("defaultStats"); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
327 LoadSystemStatsForSystem(system, extraData); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
328 system.StandardSystemStatsID = defaultStatsID; |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
329 LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.ID); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
330 LogNotifier.DebugFormat(GetType(), "GameSystem with ID {0} default stats: {1}", system.ID, system.StandardSystemStatsID); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
331 system.SetAsFullyLoaded(); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
332 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
333 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
334 private void LoadCategoriesForSystem(GameSystem system, XmlNode elem) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
335 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
336 WarFoundryObject tempObj; |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
337 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
338 foreach (XmlElement cat in SelectNodes(elem, "/system:system/system:categories/cat:cat")) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
339 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
340 tempObj = CreateObjectFromElement(cat); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
341 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
342 if (tempObj is Category) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
343 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
344 system.AddCategory((Category)tempObj); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
345 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
346 else |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
347 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
348 LogNotifier.WarnFormat(GetType(), "Object for string {0} was not of type Category", cat.OuterXml); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
349 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
350 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
351 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
352 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
353 public void CompleteLoading(Race race) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
354 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
355 if (race.IsFullyLoaded) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
356 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
357 LogNotifier.DebugFormat(GetType(), "Object of type Race with ID {0} is already fully loaded", race.ID); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
358 return; |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
359 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
360 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
361 if (race.IsLoading) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
362 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
363 LogNotifier.WarnFormat(GetType(), "Object of type Race with ID {0} is already being loaded", race.ID); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
364 return; |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
365 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
366 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
367 race.SetAsLoading(); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
368 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
369 XmlDocument extraData = GetExtraData(race); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
370 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
371 foreach (XmlElement node in SelectNodes(extraData, "/race:race/race:units/race:unit")) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
372 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
373 UnitType type = CreateUnitTypeFromElement(node, race, race.GameSystem); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
374 race.AddUnitType(type); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
375 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
376 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
377 foreach (XmlElement node in SelectNodes(extraData, "/race:race/race:categories/cat:cat")) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
378 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
379 race.AddCategory(CreateCategoryFromElement(node)); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
380 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
381 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
382 foreach (XmlElement node in SelectNodes(extraData, "/race:race/race:equipment/cat:equipmentItem")) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
383 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
384 EquipmentItem item = CreateEquipmentItemFromElement(node, race); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
385 race.AddEquipmentItem(item); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
386 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
387 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
388 foreach (XmlElement node in SelectNodes(extraData, "/race:race/race:abilities/cat:ability")) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
389 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
390 Ability ability = CreateAbilityFromElement(node, race); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
391 race.AddAbility(ability); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
392 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
393 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
394 race.SetAsFullyLoaded(); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
395 LogNotifier.DebugFormat(GetType(), "Completed loading of Race with ID {0}", race.ID); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
396 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
397 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
398 private WarFoundryObject CreateObjectFromElement(XmlElement elem) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
399 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
400 WarFoundryObject obj = null; |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
401 LogNotifier.DebugFormat(GetType(), "Create object for <{0}>", elem.Name); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
402 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
403 if (elem.LocalName.Equals(WarFoundryXmlElementName.CATEGORY_ELEMENT.Value)) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
404 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
405 LogNotifier.Debug(GetType(), "Create Category"); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
406 obj = CreateCategoryFromElement(elem); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
407 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
408 else |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
409 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
410 LogNotifier.Debug(GetType(), "No match"); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
411 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
412 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
413 return obj; |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
414 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
415 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
416 private Category CreateCategoryFromElement(XmlElement elem) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
417 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
418 string id = elem.GetAttribute("id"); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
419 string name = elem.GetAttribute("name"); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
420 Category cat = new Category(id, name); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
421 cat.MaximumPercentage = GetIntValueFromAttribute(elem, "maxPercentage"); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
422 cat.MinimumPercentage = GetIntValueFromAttribute(elem, "minPercentage"); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
423 cat.MaximumPoints = GetIntValueFromAttribute(elem, "maxPoints"); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
424 cat.MinimumPoints = GetIntValueFromAttribute(elem, "minPoints"); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
425 return cat; |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
426 } |
0 | 427 |
428 private UnitType CreateUnitTypeFromElement(XmlElement elem, Race parentRace, GameSystem system) | |
429 { | |
430 string id = elem.GetAttribute("id"); | |
431 string name = elem.GetAttribute("typeName"); | |
12
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
11
diff
changeset
|
432 UnitType type = new UnitType(id, name, parentRace); |
45 | 433 type.MaxNumber = GetIntValueFromAttribute(elem, "maxNum"); |
434 type.MinNumber = GetIntValueFromAttribute(elem, "minNum"); | |
435 type.MaxSize = GetIntValueFromAttribute(elem, "maxSize"); | |
436 type.MinSize = GetIntValueFromAttribute(elem, "minSize"); | |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
437 type.BaseSize = GetIntValueFromAttribute(elem, "baseSize"); |
12
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
11
diff
changeset
|
438 type.CostPerTrooper = GetIntValueFromAttribute(elem, "points"); |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
11
diff
changeset
|
439 type.BaseUnitCost = GetIntValueFromAttribute(elem, "unitPoints"); |
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
11
diff
changeset
|
440 string mainCatID = elem.GetAttribute("cat"); |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
441 Category cat = parentRace.GetCategory(mainCatID); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
442 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
443 if (cat == null) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
444 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
445 throw new InvalidDataException(String.Format("Attribute 'cat' of UnitType {0} (value: {1}) did not reference a valid category", id, mainCatID)); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
446 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
447 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
448 type.MainCategory = cat; |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
449 XmlElement statsElement = SelectSingleNodes(elem, "/race:race/race:units/race:unit/race:stats"); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
450 type.UnitStats = ParseUnitStats(statsElement, system); |
0 | 451 //TODO: Add unit requirements |
34 | 452 LogNotifier.Debug(GetType(), "Loaded "+type.Name); |
0 | 453 return type; |
454 } | |
455 | |
456 private Stats ParseUnitStats(XmlElement elem, GameSystem system) | |
457 { | |
458 List<Stat> statsList = new List<Stat>(); | |
459 String statsID = elem.GetAttribute("statSet"); | |
460 SystemStats statsSet; | |
461 | |
462 if (statsID == "") | |
463 { | |
464 statsSet = system.StandardSystemStats; | |
465 } | |
466 else | |
467 { | |
6 | 468 statsSet = system.GetSystemStatsForID(statsID); |
0 | 469 } |
470 | |
471 Stats stats = new Stats(statsSet); | |
472 | |
473 foreach (XmlElement stat in elem.ChildNodes) | |
474 { | |
475 String statID = stat.GetAttribute("name"); | |
476 StatSlot slot = statsSet[statID]; | |
477 | |
478 if (slot!=null) | |
479 { | |
480 statsList.Add(new Stat(slot, stat.InnerText)); | |
481 } | |
482 else | |
483 { | |
484 throw new InvalidFileException("The stat "+statID+" was not found in stats set "+statsID); | |
485 } | |
486 } | |
487 | |
488 stats.SetStats(statsList); | |
489 | |
490 return stats; | |
491 } | |
492 | |
42 | 493 private void LoadSystemStatsForSystem(GameSystem system, XmlNode elem) |
0 | 494 { |
42 | 495 foreach (XmlElement stats in SelectNodes(elem, "/system:system/system:sysStatsList/system:sysStats")) |
0 | 496 { |
497 SystemStats sysStats = CreateSystemStatsFromElement(stats); | |
6 | 498 system.AddSystemStats(sysStats); |
0 | 499 } |
500 } | |
501 | |
502 private SystemStats CreateSystemStatsFromElement(XmlElement elem) | |
503 { | |
504 List<StatSlot> slots = new List<StatSlot>(); | |
505 string id = elem.GetAttribute("id"); | |
506 | |
507 foreach (XmlElement slot in elem.ChildNodes) | |
508 { | |
509 StatSlot statSlot = new StatSlot(slot.GetAttribute("name")); | |
510 slots.Add(statSlot); | |
511 } | |
512 | |
513 return new SystemStats(id, slots.ToArray()); | |
514 } | |
515 | |
516 private EquipmentItem CreateEquipmentItemFromElement(XmlElement elem, Race race) | |
517 { | |
518 string id = elem.GetAttribute("id"); | |
519 string name = elem.GetAttribute("name"); | |
43
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
520 double cost = 0, min = 0, max = 0; |
0 | 521 ArmourType armourType; |
522 | |
523 try | |
524 { | |
43
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
525 cost = GetDoubleValueFromAttribute(elem, "cost"); |
0 | 526 } |
43
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
527 catch(FormatException ex) |
0 | 528 { |
43
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
529 throw new InvalidFileException("Attribute 'cost' of equipment item "+id+" was not a valid number", ex); |
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
530 } |
0 | 531 |
532 try | |
533 { | |
534 armourType = (ArmourType)Enum.Parse(typeof(ArmourType), elem.GetAttribute("armourType")); | |
535 } | |
43
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
536 catch(ArgumentException ex) |
0 | 537 { |
44
db951aad24b9
Re #50 - Complete loading of WarFoundry XML
IBBoard <dev@ibboard.co.uk>
parents:
43
diff
changeset
|
538 throw new InvalidFileException("Attribute 'armourType' of equipment "+id+" was not a valid value from the enumeration"); |
0 | 539 } |
540 | |
44
db951aad24b9
Re #50 - Complete loading of WarFoundry XML
IBBoard <dev@ibboard.co.uk>
parents:
43
diff
changeset
|
541 //TODO: Parse equipment stats if there are any |
0 | 542 |
543 return new EquipmentItem(id, name, cost, min, max, armourType, race); | |
544 } | |
47
85f2b9c3609c
Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
545 |
85f2b9c3609c
Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
546 private Ability CreateAbilityFromElement(XmlElement elem, Race race) |
85f2b9c3609c
Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
547 { |
85f2b9c3609c
Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
548 string id = elem.GetAttribute("id"); |
85f2b9c3609c
Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
549 string name = elem.GetAttribute("name"); |
85f2b9c3609c
Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
550 Ability ability = new Ability(id, name); |
85f2b9c3609c
Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
551 XmlNode node = elem.SelectSingleNode("description", GetNamespaceManager()); |
85f2b9c3609c
Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
552 ability.Description = (node == null) ? "" : node.InnerText; |
85f2b9c3609c
Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
553 return ability; |
85f2b9c3609c
Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
554 } |
0 | 555 } |
556 } |