annotate api/Factories/Xml/WarFoundryXmlFactory.cs @ 48:b49372dd8afa

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