annotate api/Factories/Xml/WarFoundryXmlFactory.cs @ 42:d0d44434b557

Fixes #45 - Fix XPath problems * Include namespace prefix and namespace manager in SelectNode calls, as per remark at http://msdn.microsoft.com/en-us/library/hcebdtae.aspx * Add SelectNodes method that always adds NamespaceManager * Fix XPath queries Also: * Add Army XSD to settings schema cache * Stop using deprecated Category constructor
author IBBoard <dev@ibboard.co.uk>
date Sun, 22 Mar 2009 16:37:43 +0000
parents 422ddd5fedd1
children d0812d7de39d
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
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
68 if (!elem.Name.Equals(elementName.Value))
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);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
94 string pointsString = elem.GetAttribute("maxPoints");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
95 int points = 0;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
96
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
97 try
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
98 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
99 points = int.Parse(pointsString);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
100 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
101 catch(FormatException)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
102 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
103 throw new FormatException("Attribute 'maxPoints' of army '"+name+"' was not a valid number");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
104 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
105
10
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
106 Army army = new Army(race, name, points, file);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
107 extraData[army] = elem.OwnerDocument;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
108 return army;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
109 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
110
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
111 protected override Stream GetGameSystemDataStream (ZipFile file)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
112 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
113 return file.GetInputStream(file.FindEntry("data.systemx", true));
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
114 }
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 protected override GameSystem CreateGameSystemFromStream (ZipFile file, Stream dataStream)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
117 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
118 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.SYSTEM_ELEMENT);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
119 LogNotifier.Debug(GetType(), "Create GameSystem");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
120 return CreateSystemFromElement(file, elem);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
121 }
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 private GameSystem CreateSystemFromElement(ZipFile file, XmlElement elem)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
124 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
125 string id = elem.GetAttribute("id");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
126 string name = elem.GetAttribute("name");
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
127 GameSystem system = new GameSystem(id, name, this);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
128 extraData[system] = elem.OwnerDocument;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
129 return system;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
130 }
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 protected override Stream GetRaceDataStream (ZipFile file)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
133 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
134 return file.GetInputStream(file.FindEntry("data.racex", true));
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
135 }
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 protected override Race CreateRaceFromStream (ZipFile file, Stream dataStream)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
138 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
139 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.RACE_ELEMENT);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
140 LogNotifier.Debug(GetType(), "Create Race");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
141 return CreateRaceFromElement(file, elem);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
142 }
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 private Race CreateRaceFromElement(ZipFile file, XmlElement elem)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
145 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
146 string id = elem.GetAttribute("id");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
147 string subid = elem.GetAttribute("subid");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
148 string systemID = elem.GetAttribute("system");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
149 string name = elem.GetAttribute("name");
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
150 Race race = new Race(id, subid, name, systemID, this);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
151 extraData[race] = elem.OwnerDocument;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
152 return race;
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
153 }
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
154
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
155 public override void CompleteLoading(IWarFoundryStagedLoadObject obj)
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
156 {
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
157 LogNotifier.DebugFormat(GetType(), "Complete loading of {0} with ID {1}", obj.GetType().Name, obj.ID);
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
158
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
159 if (obj is GameSystem)
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
160 {
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
161 CompleteLoading((GameSystem)obj);
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
162 }
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
163 else if (obj is Race)
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
164 {
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
165 CompleteLoading((Race)obj);
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
166 }
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
167 }
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
168
10
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
169 public XmlNode GetExtraData(IWarFoundryObject obj)
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
170 {
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
171 XmlDocument extra = null;
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
172 extraData.TryGetValue(obj, out extra);
10
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
173 XmlNode node = null;
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
174
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
175 if (extra !=null)
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
176 {
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
177 node = extra.LastChild;
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
178 }
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
179
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
180 return node;
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
181 }
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
182
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
183 public void CompleteLoading(GameSystem system)
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
184 {
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
185 if (system.IsFullyLoaded)
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
186 {
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
187 LogNotifier.DebugFormat(GetType(), "Object of type GameSystem with ID {0} is already fully loaded", system.ID);
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
188 return;
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
189 }
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
190
34
b28be912adab Re #32 - Migrate to schema
IBBoard <dev@ibboard.co.uk>
parents: 32
diff changeset
191 if (system.IsLoading)
b28be912adab Re #32 - Migrate to schema
IBBoard <dev@ibboard.co.uk>
parents: 32
diff changeset
192 {
b28be912adab Re #32 - Migrate to schema
IBBoard <dev@ibboard.co.uk>
parents: 32
diff changeset
193 LogNotifier.WarnFormat(GetType(), "Object of type GameSystem with ID {0} is already being loaded", system.ID);
b28be912adab Re #32 - Migrate to schema
IBBoard <dev@ibboard.co.uk>
parents: 32
diff changeset
194 return;
b28be912adab Re #32 - Migrate to schema
IBBoard <dev@ibboard.co.uk>
parents: 32
diff changeset
195 }
b28be912adab Re #32 - Migrate to schema
IBBoard <dev@ibboard.co.uk>
parents: 32
diff changeset
196
b28be912adab Re #32 - Migrate to schema
IBBoard <dev@ibboard.co.uk>
parents: 32
diff changeset
197 system.SetAsLoading();
b28be912adab Re #32 - Migrate to schema
IBBoard <dev@ibboard.co.uk>
parents: 32
diff changeset
198
42
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
199 XmlNode elem = GetExtraData(system);
10
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
200 LoadCategoriesForSystem(system, elem);
42
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
201 XmlNodeList nodeList = SelectNodes(elem, "/system:system/system:categories");
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
202 XmlNode statsElem = nodeList.Item(0);
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
203 string defaultStatsID = ((XmlElement)statsElem).GetAttribute("defaultStats", "http://ibboard.co.uk/warfoundry/system");
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
204 LoadSystemStatsForSystem(system, elem);
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
205 system.StandardSystemStatsID = defaultStatsID;
10
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
206 LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.Name);
42
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
207 LogNotifier.DebugFormat(GetType(), "GameSystem with ID {0} default stats: {1}", system.Name, system.StandardSystemStatsID);
10
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
208 system.SetAsFullyLoaded();
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
209 }
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
210
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
211 private void LoadCategoriesForSystem(GameSystem system, XmlNode elem)
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
212 {
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
213 WarFoundryObject tempObj;
10
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
214
42
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
215 foreach (XmlElement cat in SelectNodes(elem, "/system:system/system:categories/cat:category"))
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
216 {
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
217 tempObj = CreateObjectFromElement(cat);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
218
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
219 if (tempObj is Category)
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
220 {
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
221 system.AddCategory((Category)tempObj);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
222 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
223 else
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
224 {
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
225 LogNotifier.WarnFormat(GetType(), "Object for string {0} was not of type Category", cat.OuterXml);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
226 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
227 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
228 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
229
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
230 public void CompleteLoading(Race race)
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
231 {
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
232 if (race.IsFullyLoaded)
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
233 {
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
234 LogNotifier.DebugFormat(GetType(), "Object of type Race with ID {0} is already fully loaded", race.ID);
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
235 return;
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
236 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
237
34
b28be912adab Re #32 - Migrate to schema
IBBoard <dev@ibboard.co.uk>
parents: 32
diff changeset
238 if (race.IsLoading)
b28be912adab Re #32 - Migrate to schema
IBBoard <dev@ibboard.co.uk>
parents: 32
diff changeset
239 {
b28be912adab Re #32 - Migrate to schema
IBBoard <dev@ibboard.co.uk>
parents: 32
diff changeset
240 LogNotifier.WarnFormat(GetType(), "Object of type Race with ID {0} is already being loaded", race.ID);
b28be912adab Re #32 - Migrate to schema
IBBoard <dev@ibboard.co.uk>
parents: 32
diff changeset
241 return;
b28be912adab Re #32 - Migrate to schema
IBBoard <dev@ibboard.co.uk>
parents: 32
diff changeset
242 }
b28be912adab Re #32 - Migrate to schema
IBBoard <dev@ibboard.co.uk>
parents: 32
diff changeset
243
b28be912adab Re #32 - Migrate to schema
IBBoard <dev@ibboard.co.uk>
parents: 32
diff changeset
244 race.SetAsLoading();
b28be912adab Re #32 - Migrate to schema
IBBoard <dev@ibboard.co.uk>
parents: 32
diff changeset
245
10
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
246 XmlNode elem = GetExtraData(race);
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
247 XmlNode colNode = elem.FirstChild;
11
5a1df00b0359 Re #9 - Make WarFoundry API load files in small methods
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
248
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
249 foreach (XmlElement node in colNode.ChildNodes)
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
250 {
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
251 UnitType type = CreateUnitTypeFromElement(node, race, race.GameSystem);
11
5a1df00b0359 Re #9 - Make WarFoundry API load files in small methods
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
252 race.AddUnitType(type);
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
253 }
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
254
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
255 colNode = colNode.NextSibling;
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
256
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
257 if (colNode!=null && colNode.Name == WarFoundryXmlElementName.CATEGORIES_ELEMENT.Value)
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
258 {
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
259 foreach (XmlElement node in colNode.ChildNodes)
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
260 {
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
261 race.AddCategory(CreateCategoryFromElement(node));
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
262 }
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
263
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
264 colNode = colNode.NextSibling;
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
265 }
11
5a1df00b0359 Re #9 - Make WarFoundry API load files in small methods
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
266
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
267 if (colNode!=null && colNode.Name == WarFoundryXmlElementName.RACE_EQUIPMENT_ITEMS_ELEMENT.Value)
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
268 {
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
269 foreach (XmlElement node in colNode.ChildNodes)
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
270 {
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
271 EquipmentItem item = CreateEquipmentItemFromElement(node, race);
11
5a1df00b0359 Re #9 - Make WarFoundry API load files in small methods
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
272 race.AddEquipmentItem(item);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
273 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
274 }
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
275
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
276 Dictionary<string, Ability> raceAbilities = new Dictionary<string, Ability>();
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
277 //TODO: Load abilities
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
278 race.SetAbilities(raceAbilities);
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
279 race.SetAsFullyLoaded();
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
280 LogNotifier.DebugFormat(GetType(), "Completed loading of Race with ID {0}", race.Name);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
281 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
282
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
283 protected XmlDocument CreateXmlDocumentFromStream(Stream stream)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
284 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
285 XmlDocument doc = new XmlDocument();
23
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
286 XmlReader reader = XmlReader.Create(stream, GetReaderSettings());
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
287
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
288 try
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
289 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
290 doc.Load(reader);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
291 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
292 //Don't catch XMLSchemaExceptions - let them get thrown out
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
293 finally
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
294 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
295 reader.Close();
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
296 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
297
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
298 return doc;
23
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
299 }
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
300
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
301 /// <summary>
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
302 /// Lazy-getter for XML reader settings. May throw a <see cref="InvalidDataException"/> if there is a problem with the translation schema.
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
303 /// </summary>
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
304 /// <returns>
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
305 /// A <see cref="XmlReaderSettings"/> with the default values for validating the translation document against the translation schema
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
306 /// </returns>
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
307 private XmlReaderSettings GetReaderSettings()
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
308 {
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
309 if (settings == null)
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
310 {
41
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
311 settings = new XmlReaderSettings();
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
312 settings.ValidationType = ValidationType.Schema;
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
313 settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
314 settings.ProhibitDtd = true;
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
315 settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod);
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
316 XmlSchemaSet cache = new XmlSchemaSet();
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
317 string path = IBBoard.Constants.ExecutablePath + "/dtds/";
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
318 string nsBase = "http://ibboard.co.uk/warfoundry/";
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
319 AddSchemaToCache(cache, nsBase + "core", path + "warfoundry-core.xsd");
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
320 AddSchemaToCache(cache, nsBase + "cats", path + "warfoundry-cats.xsd");
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
321 AddSchemaToCache(cache, nsBase + "race", path + "race.xsd");
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
322 AddSchemaToCache(cache, nsBase + "system", path + "system.xsd");
42
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
323 AddSchemaToCache(cache, nsBase + "army", path + "army.xsd");
41
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
324 settings.Schemas.Add(cache);
23
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
325 }
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
326
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
327 return settings;
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
328 }
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
329
41
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
330 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
331 {
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
332 try
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
333 {
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
334 cache.Add(xmlNamespace, schemaLocation);
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
335 }
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
336 catch (IOException ex)
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
337 {
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
338 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
339 }
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
340 catch (XmlSchemaException ex)
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
341 {
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
342 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
343 }
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
344 catch (XmlException ex)
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
345 {
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
346 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
347 }
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
348 }
422ddd5fedd1 Re #48 - Require schemas to validate
IBBoard <dev@ibboard.co.uk>
parents: 40
diff changeset
349
42
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
350 private XmlNamespaceManager GetNamespaceManager()
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
351 {
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
352 if (nsManager == null)
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
353 {
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
354 nsManager = new XmlNamespaceManager(new NameTable());
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
355 nsManager.AddNamespace("core", "http://ibboard.co.uk/warfoundry/core");
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
356 nsManager.AddNamespace("cat", "http://ibboard.co.uk/warfoundry/cats");
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
357 nsManager.AddNamespace("race", "http://ibboard.co.uk/warfoundry/race");
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
358 nsManager.AddNamespace("system", "http://ibboard.co.uk/warfoundry/system");
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
359 nsManager.AddNamespace("army", "http://ibboard.co.uk/warfoundry/army");
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
360 }
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
361
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
362 return nsManager;
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
363 }
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
364
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
365 private XmlNodeList SelectNodes(XmlNode element, string xpathQuery)
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
366 {
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
367 return element.SelectNodes(xpathQuery, GetNamespaceManager());
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
368 }
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
369
23
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
370 private void ValidationEventMethod(object sender, ValidationEventArgs e)
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
371 {
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
372 throw new InvalidDataException("Problem validating against schema for WarFoundry data: " + e.Exception.Message, e.Exception);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
373 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
374
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
375 private WarFoundryObject CreateObjectFromElement(XmlElement elem)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
376 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
377 WarFoundryObject obj = null;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
378 LogNotifier.DebugFormat(GetType(), "Create object for <{0}>", elem.Name);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
379
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
380 if (elem.Name.Equals(WarFoundryXmlElementName.CATEGORY_ELEMENT.Value))
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
381 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
382 LogNotifier.Debug(GetType(), "Create Category");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
383 obj = CreateCategoryFromElement(elem);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
384 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
385 else
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
386 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
387 LogNotifier.Debug(GetType(), "No match");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
388 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
389
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
390 return obj;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
391 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
392
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
393 private Category CreateCategoryFromElement(XmlElement elem)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
394 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
395 string id = elem.GetAttribute("id");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
396 string name = elem.GetAttribute("name");
42
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
397 Category cat = new Category(id, name);
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
398 cat.MinimumPercentage = GetIntValueFromAttribute(elem, "minPercentage");
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
399 cat.MaximumPercentage = GetIntValueFromAttribute(elem, "maxPercentage");
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
400 cat.MinimumPoints = GetIntValueFromAttribute(elem, "minPoints");
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
401 cat.MaximumPoints = GetIntValueFromAttribute(elem, "maxPoints");
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
402 return cat;
9
6ad505b6c36e Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 8
diff changeset
403 }
6ad505b6c36e Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 8
diff changeset
404
10
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
405 private int GetIntValueFromAttribute(XmlElement elem, string attributeName)
9
6ad505b6c36e Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 8
diff changeset
406 {
40
3664eee50390 Fixes #47 - remove magic numbers
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
407 try
39
05c6745cc660 Re #47: Remove magic numbers
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
408 {
40
3664eee50390 Fixes #47 - remove magic numbers
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
409 return int.Parse(elem.GetAttribute(attributeName));
39
05c6745cc660 Re #47: Remove magic numbers
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
410 }
40
3664eee50390 Fixes #47 - remove magic numbers
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
411 catch(FormatException)
3664eee50390 Fixes #47 - remove magic numbers
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
412 {
3664eee50390 Fixes #47 - remove magic numbers
IBBoard <dev@ibboard.co.uk>
parents: 39
diff changeset
413 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
414 }
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
415 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
416
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
417 private UnitType CreateUnitTypeFromElement(XmlElement elem, Race parentRace, GameSystem system)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
418 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
419 string id = elem.GetAttribute("id");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
420 string name = elem.GetAttribute("typeName");
12
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
421 UnitType type = new UnitType(id, name, parentRace);
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
422 type.MinNumber = GetIntValueFromAttribute(elem, "minNum");
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
423 type.MaxNumber = GetIntValueFromAttribute(elem, "maxNum");
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
424 type.MinSize = GetIntValueFromAttribute(elem, "minSize");
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
425 type.MaxSize = GetIntValueFromAttribute(elem, "maxSize");
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
426 //TODO: Add base size
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
427 type.CostPerTrooper = GetIntValueFromAttribute(elem, "points");
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
428 type.BaseUnitCost = GetIntValueFromAttribute(elem, "unitPoints");
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
429 string mainCatID = elem.GetAttribute("cat");
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
430 type.MainCategory = parentRace.GetCategory(mainCatID);
42
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
431 XmlNodeList nodes = SelectNodes(elem, "/race:race/race:units/race:unit/race:stats");
34
b28be912adab Re #32 - Migrate to schema
IBBoard <dev@ibboard.co.uk>
parents: 32
diff changeset
432 XmlNode node = nodes.Item(0);
12
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
433 type.UnitStats = ParseUnitStats((XmlElement)node, system);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
434 //TODO: Add unit requirements
34
b28be912adab Re #32 - Migrate to schema
IBBoard <dev@ibboard.co.uk>
parents: 32
diff changeset
435 LogNotifier.Debug(GetType(), "Loaded "+type.Name);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
436 return type;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
437 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
438
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
439 private Stats ParseUnitStats(XmlElement elem, GameSystem system)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
440 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
441 List<Stat> statsList = new List<Stat>();
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
442 String statsID = elem.GetAttribute("statSet");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
443 SystemStats statsSet;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
444
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
445 if (statsID == "")
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
446 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
447 statsSet = system.StandardSystemStats;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
448 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
449 else
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
450 {
6
150a5669cd7b Re #9 - more granular loading
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
451 statsSet = system.GetSystemStatsForID(statsID);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
452 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
453
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
454 Stats stats = new Stats(statsSet);
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 foreach (XmlElement stat in elem.ChildNodes)
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 String statID = stat.GetAttribute("name");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
459 StatSlot slot = statsSet[statID];
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
460
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
461 if (slot!=null)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
462 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
463 statsList.Add(new Stat(slot, stat.InnerText));
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
464 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
465 else
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
466 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
467 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
468 }
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.SetStats(statsList);
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 return stats;
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
42
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
476 private void LoadSystemStatsForSystem(GameSystem system, XmlNode elem)
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
477 {
42
d0d44434b557 Fixes #45 - Fix XPath problems
IBBoard <dev@ibboard.co.uk>
parents: 41
diff changeset
478 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
479 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
480 SystemStats sysStats = CreateSystemStatsFromElement(stats);
6
150a5669cd7b Re #9 - more granular loading
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
481 system.AddSystemStats(sysStats);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
482 }
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
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
485 private SystemStats CreateSystemStatsFromElement(XmlElement elem)
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 List<StatSlot> slots = new List<StatSlot>();
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
488 string id = elem.GetAttribute("id");
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 foreach (XmlElement slot in elem.ChildNodes)
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 StatSlot statSlot = new StatSlot(slot.GetAttribute("name"));
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
493 slots.Add(statSlot);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
494 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
495
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
496 return new SystemStats(id, slots.ToArray());
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
497 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
498
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
499 private EquipmentItem CreateEquipmentItemFromElement(XmlElement elem, Race race)
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 string id = elem.GetAttribute("id");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
502 string name = elem.GetAttribute("name");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
503 float cost = 0, min = 0, max = 0;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
504 ArmourType armourType;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
505
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
506 try
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
507 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
508 cost = float.Parse(elem.GetAttribute("cost"));
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
509 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
510 catch(FormatException)
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 throw new FormatException("Attribute 'cost' of equipment item "+id+" was not a valid number");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
513 }
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 try
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
516 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
517 min = float.Parse(elem.GetAttribute("min"));
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
518 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
519 catch(FormatException)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
520 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
521 throw new FormatException("Attribute 'min' of equipment item "+id+" was not a valid number");
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
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
524 try
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
525 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
526 max = float.Parse(elem.GetAttribute("max"));
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
527 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
528 catch(FormatException)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
529 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
530 throw new FormatException("Attribute 'max' of equipment item "+id+" was not a valid number");
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
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
533 try
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
534 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
535 armourType = (ArmourType)Enum.Parse(typeof(ArmourType), elem.GetAttribute("armourType"));
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
536 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
537 catch(FormatException)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
538 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
539 throw new InvalidFileException("Attribute 'armourType' of equipment "+id+" was not a valid value from the enumeration");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
540 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
541
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
542 if (elem.ChildNodes.Count>0)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
543 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
544 //It has stats!
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
545 //TODO: Parse equipment stats
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
546 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
547
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
548 return new EquipmentItem(id, name, cost, min, max, armourType, race);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
549 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
550 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
551 }