Mercurial > repos > snowblizz-super-API-ideas
annotate api/Factories/Xml/WarFoundryXmlFactory.cs @ 51:b271a2252758
Re #50 - Fully load XML files
* Load remaining GameSystem attributes
* Add "AllowAllies" property to GameSystem class
* Add "is loading" methods to "staged loading" interface
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 28 Mar 2009 21:00:35 +0000 |
parents | bb6b993b98bf |
children | 64ef178c18aa |
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 | |
50
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
43 private WarFoundryXmlFactory() : base() |
0 | 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 | |
50
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
63 protected override Stream GetArmyDataStream(ZipFile file) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
64 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
65 return file.GetInputStream(file.FindEntry("data.armyx", true)); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
66 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
67 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
68 protected override Army CreateArmyFromStream (ZipFile file, Stream dataStream) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
69 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
70 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.ARMY_ELEMENT); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
71 return CreateArmyFromElement(file, elem); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
72 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
73 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
74 private XmlElement GetRootElementFromStream(Stream stream, WarFoundryXmlElementName elementName) |
0 | 75 { |
76 XmlDocument doc = CreateXmlDocumentFromStream(stream); | |
77 XmlElement elem = (XmlElement)doc.LastChild; | |
78 | |
43
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
79 if (!elem.LocalName.Equals(elementName.Value)) |
0 | 80 { |
81 throw new InvalidFileException(String.Format("Root element of XML was not valid. Expected {0} but got {1}", elementName.Value, elem.Name)); | |
82 } | |
83 | |
84 return elem; | |
85 } | |
86 | |
50
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
87 private XmlDocument CreateXmlDocumentFromStream(Stream stream) |
0 | 88 { |
89 XmlDocument doc = new XmlDocument(); | |
23
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
90 XmlReader reader = XmlReader.Create(stream, GetReaderSettings()); |
50
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
91 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
92 try |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
93 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
94 doc.Load(reader); |
0 | 95 } |
50
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
96 //Don't catch XMLSchemaExceptions - let them get thrown out |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
97 finally |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
98 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
99 reader.Close(); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
100 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
101 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
102 return doc; |
23
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
103 } |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
104 |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
105 /// <summary> |
23
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
106 /// 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
|
107 /// </summary> |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
108 /// <returns> |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
109 /// 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
|
110 /// </returns> |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
111 private XmlReaderSettings GetReaderSettings() |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
112 { |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
113 if (settings == null) |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
114 { |
41
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
115 settings = new XmlReaderSettings(); |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
116 settings.ValidationType = ValidationType.Schema; |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
117 settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
118 settings.ProhibitDtd = true; |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
119 settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod); |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
120 XmlSchemaSet cache = new XmlSchemaSet(); |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
121 string path = IBBoard.Constants.ExecutablePath + "/dtds/"; |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
122 string nsBase = "http://ibboard.co.uk/warfoundry/"; |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
123 AddSchemaToCache(cache, nsBase + "core", path + "warfoundry-core.xsd"); |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
124 AddSchemaToCache(cache, nsBase + "cats", path + "warfoundry-cats.xsd"); |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
125 AddSchemaToCache(cache, nsBase + "race", path + "race.xsd"); |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
126 AddSchemaToCache(cache, nsBase + "system", path + "system.xsd"); |
42 | 127 AddSchemaToCache(cache, nsBase + "army", path + "army.xsd"); |
41
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
128 settings.Schemas.Add(cache); |
23
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
129 } |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
130 |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
131 return settings; |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
132 } |
f9846f896df3
Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents:
17
diff
changeset
|
133 |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
134 private void ValidationEventMethod(object sender, ValidationEventArgs e) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
135 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
136 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
|
137 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
138 |
41
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
139 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
|
140 { |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
141 try |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
142 { |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
143 cache.Add(xmlNamespace, schemaLocation); |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
144 } |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
145 catch (IOException ex) |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
146 { |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
147 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
|
148 } |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
149 catch (XmlSchemaException ex) |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
150 { |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
151 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
|
152 } |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
153 catch (XmlException ex) |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
154 { |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
155 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
|
156 } |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
157 } |
422ddd5fedd1
Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents:
40
diff
changeset
|
158 |
50
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
159 private Army CreateArmyFromElement(ZipFile file, XmlElement elem) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
160 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
161 string name = elem.GetAttribute("name"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
162 string systemID = elem.GetAttribute("gameSystem"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
163 GameSystem system = WarFoundryLoader.GetDefault().GetGameSystem(systemID); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
164 string raceID = elem.GetAttribute("race"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
165 Race race = WarFoundryLoader.GetDefault().GetRace(system, raceID); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
166 int points = XmlTools.GetIntValueFromAttribute(elem, "maxPoints"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
167 Army army = new Army(race, name, points, file); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
168 //TODO: Complete loading of army |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
169 return army; |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
170 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
171 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
172 protected override Stream GetGameSystemDataStream (ZipFile file) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
173 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
174 return file.GetInputStream(file.FindEntry("data.systemx", true)); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
175 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
176 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
177 protected override GameSystem CreateGameSystemFromStream (ZipFile file, Stream dataStream) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
178 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
179 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.SYSTEM_ELEMENT); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
180 LogNotifier.Debug(GetType(), "Create GameSystem"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
181 return CreateSystemFromElement(file, elem); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
182 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
183 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
184 private GameSystem CreateSystemFromElement(ZipFile file, XmlElement elem) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
185 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
186 string id = elem.GetAttribute("id"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
187 string name = elem.GetAttribute("name"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
188 GameSystem system = new GameSystem(id, name, this); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
189 StoreExtraData(system, elem); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
190 return system; |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
191 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
192 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
193 private void StoreExtraData(WarFoundryStagedLoadingObject wfObject, XmlElement elem) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
194 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
195 extraData[wfObject] = elem.OwnerDocument; |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
196 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
197 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
198 protected override Stream GetRaceDataStream (ZipFile file) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
199 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
200 return file.GetInputStream(file.FindEntry("data.racex", true)); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
201 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
202 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
203 protected override Race CreateRaceFromStream (ZipFile file, Stream dataStream) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
204 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
205 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.RACE_ELEMENT); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
206 LogNotifier.Debug(GetType(), "Create Race"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
207 return CreateRaceFromElement(file, elem); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
208 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
209 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
210 private Race CreateRaceFromElement(ZipFile file, XmlElement elem) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
211 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
212 string id = elem.GetAttribute("id"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
213 string subid = elem.GetAttribute("subid"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
214 string systemID = elem.GetAttribute("system"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
215 string name = elem.GetAttribute("name"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
216 Race race = new Race(id, subid, name, systemID, this); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
217 StoreExtraData(race, elem); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
218 return race; |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
219 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
220 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
221 public XmlDocument GetExtraData(IWarFoundryObject obj) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
222 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
223 XmlDocument extra = null; |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
224 extraData.TryGetValue(obj, out extra); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
225 return extra; |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
226 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
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 } |
49
9d31d063b194
Re #10 - Refactor source code for readability
IBBoard <dev@ibboard.co.uk>
parents:
48
diff
changeset
|
252 |
9d31d063b194
Re #10 - Refactor source code for readability
IBBoard <dev@ibboard.co.uk>
parents:
48
diff
changeset
|
253 private XmlElement SelectSingleElement(XmlNode element, string xpathQuery) |
9d31d063b194
Re #10 - Refactor source code for readability
IBBoard <dev@ibboard.co.uk>
parents:
48
diff
changeset
|
254 { |
9d31d063b194
Re #10 - Refactor source code for readability
IBBoard <dev@ibboard.co.uk>
parents:
48
diff
changeset
|
255 XmlNode node = SelectSingleNode(element, xpathQuery); |
9d31d063b194
Re #10 - Refactor source code for readability
IBBoard <dev@ibboard.co.uk>
parents:
48
diff
changeset
|
256 return (node is XmlElement) ? (XmlElement) node : null; |
9d31d063b194
Re #10 - Refactor source code for readability
IBBoard <dev@ibboard.co.uk>
parents:
48
diff
changeset
|
257 } |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
258 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
259 public override void CompleteLoading(IWarFoundryStagedLoadObject obj) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
260 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
261 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
|
262 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
263 if (obj is GameSystem) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
264 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
265 CompleteLoading((GameSystem)obj); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
266 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
267 else if (obj is Race) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
268 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
269 CompleteLoading((Race)obj); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
270 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
271 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
272 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
273 public void CompleteLoading(GameSystem system) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
274 { |
50
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
275 if (!CanCompleteLoading(system)) |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
276 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
277 return; |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
278 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
279 |
50
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
280 system.SetAsLoading(); |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
281 XmlDocument extraData = GetExtraData(system); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
282 LoadCategoriesForSystem(system, extraData); |
49
9d31d063b194
Re #10 - Refactor source code for readability
IBBoard <dev@ibboard.co.uk>
parents:
48
diff
changeset
|
283 XmlElement statsElem = SelectSingleElement(extraData, "/system:system/system:sysStatsList"); |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
284 string defaultStatsID = statsElem.GetAttribute("defaultStats"); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
285 LoadSystemStatsForSystem(system, extraData); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
286 system.StandardSystemStatsID = defaultStatsID; |
51 | 287 XmlElement systemElement = SelectSingleElement(extraData, "/system:system"); |
288 system.WarnOnError = XmlTools.GetBoolValueFromAttribute(systemElement, "warn"); | |
289 system.AllowAllies = XmlTools.GetBoolValueFromAttribute(systemElement, "allowAllies"); | |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
290 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
|
291 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
|
292 system.SetAsFullyLoaded(); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
293 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
294 |
50
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
295 private bool CanCompleteLoading(IWarFoundryStagedLoadObject obj) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
296 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
297 bool canLoad = true; |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
298 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
299 if (obj.IsFullyLoaded) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
300 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
301 LogNotifier.DebugFormat(GetType(), "Object of type {0} with ID {1} is already fully loaded", obj.GetType().Name, obj.ID); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
302 canLoad = false; |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
303 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
304 else if (obj.IsLoading) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
305 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
306 LogNotifier.WarnFormat(GetType(), "Object of type {0} with ID {1} is already being loaded", obj.GetType().Name, obj.ID); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
307 canLoad = false; |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
308 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
309 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
310 return canLoad; |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
311 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
312 |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
313 private void LoadCategoriesForSystem(GameSystem system, XmlNode elem) |
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 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
|
316 { |
50
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
317 system.AddCategory(CreateCategoryFromElement(cat)); |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
318 } |
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 |
50
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
321 private Category CreateCategoryFromElement(XmlElement elem) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
322 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
323 string id = elem.GetAttribute("id"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
324 string name = elem.GetAttribute("name"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
325 Category cat = new Category(id, name); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
326 cat.MaximumPercentage = XmlTools.GetIntValueFromAttribute(elem, "maxPercentage"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
327 cat.MinimumPercentage = XmlTools.GetIntValueFromAttribute(elem, "minPercentage"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
328 cat.MaximumPoints = XmlTools.GetIntValueFromAttribute(elem, "maxPoints"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
329 cat.MinimumPoints = XmlTools.GetIntValueFromAttribute(elem, "minPoints"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
330 return cat; |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
331 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
332 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
333 private void LoadSystemStatsForSystem(GameSystem system, XmlNode elem) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
334 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
335 foreach (XmlElement stats in SelectNodes(elem, "/system:system/system:sysStatsList/system:sysStats")) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
336 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
337 SystemStats sysStats = CreateSystemStatsFromElement(stats); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
338 system.AddSystemStats(sysStats); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
339 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
340 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
341 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
342 private SystemStats CreateSystemStatsFromElement(XmlElement elem) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
343 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
344 List<StatSlot> slots = new List<StatSlot>(); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
345 string id = elem.GetAttribute("id"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
346 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
347 foreach (XmlElement slot in elem.ChildNodes) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
348 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
349 StatSlot statSlot = new StatSlot(slot.GetAttribute("name")); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
350 slots.Add(statSlot); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
351 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
352 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
353 return new SystemStats(id, slots.ToArray()); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
354 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
355 |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
356 public void CompleteLoading(Race race) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
357 { |
51 | 358 if (!CanCompleteLoading(race)) |
48
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 return; |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
361 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
362 |
51 | 363 race.SetAsLoading(); |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
364 XmlDocument extraData = GetExtraData(race); |
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 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
|
367 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
368 UnitType type = CreateUnitTypeFromElement(node, race, race.GameSystem); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
369 race.AddUnitType(type); |
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 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
372 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
|
373 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
374 race.AddCategory(CreateCategoryFromElement(node)); |
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:equipment/cat:equipmentItem")) |
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 EquipmentItem item = CreateEquipmentItemFromElement(node, race); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
380 race.AddEquipmentItem(item); |
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 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
383 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
|
384 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
385 Ability ability = CreateAbilityFromElement(node, race); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
386 race.AddAbility(ability); |
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 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
389 race.SetAsFullyLoaded(); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
390 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
|
391 } |
0 | 392 |
393 private UnitType CreateUnitTypeFromElement(XmlElement elem, Race parentRace, GameSystem system) | |
394 { | |
395 string id = elem.GetAttribute("id"); | |
396 string name = elem.GetAttribute("typeName"); | |
12
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
11
diff
changeset
|
397 UnitType type = new UnitType(id, name, parentRace); |
50
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
398 type.MaxNumber = XmlTools.GetIntValueFromAttribute(elem, "maxNum"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
399 type.MinNumber = XmlTools.GetIntValueFromAttribute(elem, "minNum"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
400 type.MaxSize = XmlTools.GetIntValueFromAttribute(elem, "maxSize"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
401 type.MinSize = XmlTools.GetIntValueFromAttribute(elem, "minSize"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
402 type.BaseSize = XmlTools.GetIntValueFromAttribute(elem, "baseSize"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
403 type.CostPerTrooper = XmlTools.GetIntValueFromAttribute(elem, "points"); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
404 type.BaseUnitCost = XmlTools.GetIntValueFromAttribute(elem, "unitPoints"); |
12
ac232763858b
Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents:
11
diff
changeset
|
405 string mainCatID = elem.GetAttribute("cat"); |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
406 Category cat = parentRace.GetCategory(mainCatID); |
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 if (cat == null) |
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 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
|
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 type.MainCategory = cat; |
49
9d31d063b194
Re #10 - Refactor source code for readability
IBBoard <dev@ibboard.co.uk>
parents:
48
diff
changeset
|
414 XmlElement statsElement = SelectSingleElement(elem, "/race:race/race:units/race:unit/race:stats"); |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
415 type.UnitStats = ParseUnitStats(statsElement, system); |
0 | 416 //TODO: Add unit requirements |
34 | 417 LogNotifier.Debug(GetType(), "Loaded "+type.Name); |
0 | 418 return type; |
419 } | |
420 | |
421 private Stats ParseUnitStats(XmlElement elem, GameSystem system) | |
422 { | |
423 List<Stat> statsList = new List<Stat>(); | |
424 String statsID = elem.GetAttribute("statSet"); | |
425 SystemStats statsSet; | |
426 | |
427 if (statsID == "") | |
428 { | |
429 statsSet = system.StandardSystemStats; | |
430 } | |
431 else | |
432 { | |
6 | 433 statsSet = system.GetSystemStatsForID(statsID); |
0 | 434 } |
435 | |
436 Stats stats = new Stats(statsSet); | |
437 | |
438 foreach (XmlElement stat in elem.ChildNodes) | |
439 { | |
440 String statID = stat.GetAttribute("name"); | |
441 StatSlot slot = statsSet[statID]; | |
442 | |
443 if (slot!=null) | |
444 { | |
445 statsList.Add(new Stat(slot, stat.InnerText)); | |
446 } | |
447 else | |
448 { | |
449 throw new InvalidFileException("The stat "+statID+" was not found in stats set "+statsID); | |
450 } | |
451 } | |
452 | |
453 stats.SetStats(statsList); | |
454 | |
455 return stats; | |
456 } | |
457 | |
458 private EquipmentItem CreateEquipmentItemFromElement(XmlElement elem, Race race) | |
459 { | |
460 string id = elem.GetAttribute("id"); | |
461 string name = elem.GetAttribute("name"); | |
43
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
462 double cost = 0, min = 0, max = 0; |
0 | 463 ArmourType armourType; |
464 | |
465 try | |
466 { | |
50
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
467 cost = XmlTools.GetDoubleValueFromAttribute(elem, "cost"); |
0 | 468 } |
43
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
469 catch(FormatException ex) |
0 | 470 { |
43
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
471 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
|
472 } |
0 | 473 |
474 try | |
475 { | |
476 armourType = (ArmourType)Enum.Parse(typeof(ArmourType), elem.GetAttribute("armourType")); | |
477 } | |
43
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
478 catch(ArgumentException ex) |
0 | 479 { |
50
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
480 throw new InvalidFileException("Attribute 'armourType' of equipment "+id+" was not a valid value from the enumeration", ex); |
0 | 481 } |
482 | |
44
db951aad24b9
Re #50 - Complete loading of WarFoundry XML
IBBoard <dev@ibboard.co.uk>
parents:
43
diff
changeset
|
483 //TODO: Parse equipment stats if there are any |
0 | 484 |
485 return new EquipmentItem(id, name, cost, min, max, armourType, race); | |
486 } | |
47
85f2b9c3609c
Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
487 |
85f2b9c3609c
Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
488 private Ability CreateAbilityFromElement(XmlElement elem, Race race) |
85f2b9c3609c
Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
489 { |
85f2b9c3609c
Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
490 string id = elem.GetAttribute("id"); |
85f2b9c3609c
Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
491 string name = elem.GetAttribute("name"); |
85f2b9c3609c
Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
492 Ability ability = new Ability(id, name); |
85f2b9c3609c
Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
493 XmlNode node = elem.SelectSingleNode("description", GetNamespaceManager()); |
85f2b9c3609c
Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
494 ability.Description = (node == null) ? "" : node.InnerText; |
85f2b9c3609c
Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
495 return ability; |
85f2b9c3609c
Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
496 } |
0 | 497 } |
498 } |