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