annotate api/Factories/Xml/WarFoundryXmlFactory.cs @ 23:f9846f896df3

Re #32 - Migrate WarFoundry files to using Schemas * Add missing spaces to Cats and Core XSD * Fix some incorrect namespaces in Race XSD * Copy schemas to output dir on build * Make WarFoundryXmlFactory validate against Schemas * Make WarFoundryLoader handle failed file loads slightly differently so that we can log out as a warning * Correctly structure "simpleContent" sections of Race XSD Still to do: * Work out why Race XSD doesn't like core:nonNegativeDecimal but appears to be fine with core:percentage * Migrate test files to define namespaces and make sure they match the structure
author IBBoard <dev@ibboard.co.uk>
date Thu, 12 Mar 2009 21:35:17 +0000
parents a99d3b8466ba
children 457c9357dd64
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;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 using System.Collections.Generic;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 using System.Text;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 using IBBoard;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 using IBBoard.IO;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 using IBBoard.Lang;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 using IBBoard.Logging;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 using IBBoard.Xml;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 using IBBoard.WarFoundry.API.Requirements;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 using IBBoard.WarFoundry.API.Objects;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 using ICSharpCode.SharpZipLib.Zip;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 namespace IBBoard.WarFoundry.API.Factories.Xml
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 /// <summary>
10
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
23 /// 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
24 /// </summary>
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 public class WarFoundryXmlFactory : AbstractNativeWarFoundryFactory
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 {
17
a99d3b8466ba Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
27 private static WarFoundryXmlFactory factory;
23
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
28 private XmlReaderSettings settings;
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 private Dictionary<IWarFoundryObject, XmlDocument> extraData = new Dictionary<IWarFoundryObject, XmlDocument>();
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 private XmlResolver xmlResolver;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31
17
a99d3b8466ba Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
32 public static AbstractNativeWarFoundryFactory GetFactory()
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 {
17
a99d3b8466ba Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
34 if (factory == null)
a99d3b8466ba Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
35 {
a99d3b8466ba Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
36 factory = new WarFoundryXmlFactory();
a99d3b8466ba Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
37 }
a99d3b8466ba Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
38
a99d3b8466ba Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
39 return factory;
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 }
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 protected WarFoundryXmlFactory() : base()
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 xmlResolver = new IBBXmlResolver(Constants.ExecutablePath);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 }
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 protected override bool CheckCanFindArmyFileContent(ZipFile file)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49 return file.FindEntry("data.armyx", true) > -1;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50 }
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 protected override bool CheckCanFindSystemFileContent(ZipFile file)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54 return file.FindEntry("data.systemx", true) > -1;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 }
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 protected override bool CheckCanFindRaceFileContent(ZipFile file)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59 return file.FindEntry("data.racex", true) > -1;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
60 }
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 protected XmlElement GetRootElementFromStream(Stream stream, WarFoundryXmlElementName elementName)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
63 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
64 XmlDocument doc = CreateXmlDocumentFromStream(stream);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
65 XmlElement elem = (XmlElement)doc.LastChild;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
66
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
67 if (!elem.Name.Equals(elementName.Value))
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
68 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
69 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
70 }
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 return elem;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
73 }
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 protected override Stream GetArmyDataStream(ZipFile file)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
76 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
77 return file.GetInputStream(file.FindEntry("data.armyx", true));
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
78 }
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 protected override Army CreateArmyFromStream (ZipFile file, Stream dataStream)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
81 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
82 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.ARMY_ELEMENT);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
83 return CreateArmyFromElement(file, elem);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
84 }
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 private Army CreateArmyFromElement(ZipFile file, XmlElement elem)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
87 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
88 string name = elem.GetAttribute("name");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
89 string systemID = elem.GetAttribute("gameSystem");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
90 GameSystem system = WarFoundryLoader.GetDefault().GetGameSystem(systemID);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
91 string raceID = elem.GetAttribute("race");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
92 Race race = WarFoundryLoader.GetDefault().GetRace(system, raceID);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
93 string pointsString = elem.GetAttribute("maxPoints");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
94 int points = 0;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
95
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
96 try
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
97 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
98 points = int.Parse(pointsString);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
99 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
100 catch(FormatException)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
101 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
102 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
103 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
104
10
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
105 Army army = new Army(race, name, points, file);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
106 extraData[army] = elem.OwnerDocument;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
107 return army;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
108 }
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 protected override Stream GetGameSystemDataStream (ZipFile file)
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 return file.GetInputStream(file.FindEntry("data.systemx", true));
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
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
115 protected override GameSystem CreateGameSystemFromStream (ZipFile file, Stream dataStream)
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 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.SYSTEM_ELEMENT);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
118 LogNotifier.Debug(GetType(), "Create GameSystem");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
119 return CreateSystemFromElement(file, elem);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
120 }
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 private GameSystem CreateSystemFromElement(ZipFile file, XmlElement elem)
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 string id = elem.GetAttribute("id");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
125 string name = elem.GetAttribute("name");
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
126 GameSystem system = new GameSystem(id, name, this);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
127 extraData[system] = elem.OwnerDocument;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
128 return system;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
129 }
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 protected override Stream GetRaceDataStream (ZipFile file)
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 return file.GetInputStream(file.FindEntry("data.racex", true));
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
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
136 protected override Race CreateRaceFromStream (ZipFile file, Stream dataStream)
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 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.RACE_ELEMENT);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
139 LogNotifier.Debug(GetType(), "Create Race");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
140 return CreateRaceFromElement(file, elem);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
141 }
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 private Race CreateRaceFromElement(ZipFile file, XmlElement elem)
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 string id = elem.GetAttribute("id");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
146 string subid = elem.GetAttribute("subid");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
147 string systemID = elem.GetAttribute("system");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
148 string name = elem.GetAttribute("name");
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
149 Race race = new Race(id, subid, name, systemID, this);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
150 extraData[race] = elem.OwnerDocument;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
151 return race;
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
152 }
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
153
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
154 public override void CompleteLoading(IWarFoundryStagedLoadObject obj)
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
155 {
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
156 LogNotifier.DebugFormat(GetType(), "Complete loading of {0} with ID {1}", obj.GetType().Name, obj.ID);
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
157
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
158 if (obj is GameSystem)
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
159 {
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
160 CompleteLoading((GameSystem)obj);
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
161 }
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
162 else if (obj is Race)
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
163 {
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
164 CompleteLoading((Race)obj);
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
165 }
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
166 }
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
167
10
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
168 public XmlNode GetExtraData(IWarFoundryObject obj)
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
169 {
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
170 XmlDocument extra = null;
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
171 extraData.TryGetValue(obj, out extra);
10
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
172 XmlNode node = null;
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
173
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
174 if (extra !=null)
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
175 {
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
176 node = extra.LastChild;
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
177 }
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 return node;
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
180 }
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
181
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
182 public void CompleteLoading(GameSystem system)
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
183 {
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
184 if (system.IsFullyLoaded)
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
185 {
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
186 LogNotifier.DebugFormat(GetType(), "Object of type GameSystem with ID {0} is already fully loaded", system.ID);
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
187 return;
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
188 }
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
189
10
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
190 XmlNode elem = GetExtraData(system);
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
191 XmlNode catsElem = elem.FirstChild;
10
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
192 LoadCategoriesForSystem(system, elem);
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
193
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
194 XmlElement statsElem = (XmlElement)catsElem.NextSibling;
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
195 LoadSystemStatsFromElement(statsElem, system);
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
196 string defaultStatsID = statsElem.GetAttribute("defaultStats");
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
197
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
198 LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.Name);
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
199 system.StandardSystemStatsID = defaultStatsID;
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
200 system.SetAsFullyLoaded();
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
201 }
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
202
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
203 private void LoadCategoriesForSystem(GameSystem system, XmlNode elem)
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
204 {
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
205 WarFoundryObject tempObj;
10
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
206
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
207 foreach (XmlElement cat in elem.SelectNodes("//"+WarFoundryXmlElementName.CATEGORY_ELEMENT.Value))
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
208 {
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
209 tempObj = CreateObjectFromElement(cat);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
210
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
211 if (tempObj is Category)
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
212 {
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
213 system.AddCategory((Category)tempObj);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
214 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
215 else
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
216 {
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
217 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
218 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
219 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
220 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
221
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
222 public void CompleteLoading(Race race)
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
223 {
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
224 if (race.IsFullyLoaded)
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
225 {
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
226 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
227 return;
0
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
10
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
230 XmlNode elem = GetExtraData(race);
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
231 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
232
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
233 foreach (XmlElement node in colNode.ChildNodes)
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
234 {
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
235 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
236 race.AddUnitType(type);
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
237 }
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
238
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
239 colNode = colNode.NextSibling;
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
240
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
241 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
242 {
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
243 foreach (XmlElement node in colNode.ChildNodes)
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
244 {
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
245 race.AddCategory(CreateCategoryFromElement(node));
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
246 }
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
247
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
248 colNode = colNode.NextSibling;
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
249 }
11
5a1df00b0359 Re #9 - Make WarFoundry API load files in small methods
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
250
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
251 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
252 {
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
253 foreach (XmlElement node in colNode.ChildNodes)
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
254 {
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
255 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
256 race.AddEquipmentItem(item);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
257 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
258 }
8
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
259
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
260 Dictionary<string, Ability> raceAbilities = new Dictionary<string, Ability>();
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
261 //TODO: Load abilities
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
262 race.SetAbilities(raceAbilities);
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
263 race.SetAsFullyLoaded();
613bc5eaac59 Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
264 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
265 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
266
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
267 protected XmlDocument CreateXmlDocumentFromStream(Stream stream)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
268 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
269 XmlDocument doc = new XmlDocument();
23
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
270 XmlReader reader = XmlReader.Create(stream, GetReaderSettings());
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
271
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
272 try
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 doc.Load(reader);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
275 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
276 //Don't catch XMLSchemaExceptions - let them get thrown out
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
277 finally
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
278 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
279 reader.Close();
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
280 }
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 if (doc.ChildNodes.Count!=3)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
283 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
284 throw new InvalidFileException(Translation.GetTranslation("ErrorInvalidXmlFile", "XML file was not a valid XML file. It should contain three child nodes - XML definition, DocType and root node."));
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
285 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
286
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
287 return doc;
23
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
288 }
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
289
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
290 /// <summary>
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
291 /// 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
292 /// </summary>
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
293 /// <returns>
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
294 /// 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
295 /// </returns>
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
296 private XmlReaderSettings GetReaderSettings()
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
297 {
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
298 if (settings == null)
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 try
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
301 {
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
302 settings = new XmlReaderSettings();
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
303 settings.ValidationType = ValidationType.Schema;
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
304 settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
305 settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod);
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
306 XmlSchemaSet cache = new XmlSchemaSet();
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
307 cache.Add("http://ibboard.co.uk/warfoundry/core", IBBoard.Constants.ExecutablePath + "/dtds/warfoundry-core.xsd");
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
308 cache.Add("http://ibboard.co.uk/warfoundry/cats", IBBoard.Constants.ExecutablePath + "/dtds/warfoundry-cats.xsd");
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
309 cache.Add("http://ibboard.co.uk/warfoundry/race", IBBoard.Constants.ExecutablePath + "/dtds/race.xsd");
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
310 cache.Add("http://ibboard.co.uk/warfoundry/system", IBBoard.Constants.ExecutablePath + "/dtds/system.xsd");
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
311 settings.Schemas.Add(cache);
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
312 }
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
313 catch (DirectoryNotFoundException ex)
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
314 {
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
315 throw new InvalidDataException("Problem validating schema for WarFoundry data: " + ex.Message, ex);
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
316 }
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
317 catch (XmlSchemaException ex)
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
318 {
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
319 throw new InvalidDataException("Problem validating schema for WarFoundry data: " + ex.Message, ex);
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
320 }
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
321 catch (XmlException ex)
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
322 {
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
323 throw new InvalidDataException("Problem reading data for schema: " + ex.Message, ex);
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
324 }
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
325 }
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
326
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
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
330 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
331 {
f9846f896df3 Re #32 - Migrate WarFoundry files to using Schemas
IBBoard <dev@ibboard.co.uk>
parents: 17
diff changeset
332 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
333 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
334
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
335 protected XmlDocument CreateXmlDocumentFromString(string xmlString)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
336 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
337 XmlReaderSettings settings = new XmlReaderSettings();
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
338 settings.XmlResolver = xmlResolver;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
339 settings.ValidationType = ValidationType.DTD;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
340 settings.ProhibitDtd = false;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
341 settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
342 XmlReader reader = XmlReader.Create(new MemoryStream(Encoding.UTF8.GetBytes(xmlString)), settings);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
343 XmlDocument doc = new XmlDocument();
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
344
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
345 try
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
346 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
347 doc.Load(reader);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
348 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
349 catch(XmlSchemaException ex)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
350 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
351 throw new InvalidFileException(Translation.GetTranslation("ErrorInvalidXmlString", "Failed to parse invalid XML string"), ex);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
352 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
353 finally
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
354 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
355 //We might not need to make sure the memory stream is closed, but do it just in case
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
356 reader.Close();
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
357 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
358
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
359 if (doc.ChildNodes.Count!=3)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
360 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
361 throw new InvalidFileException(String.Format(Translation.GetTranslation("ErrorInvalidXmlFile", "XML string was not a valid XML file. It should contain three child nodes - XML definition, DocType and root node.")));
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
362 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
363
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
364 return doc;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
365 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
366
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
367 private WarFoundryObject CreateObjectFromElement(XmlElement elem)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
368 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
369 WarFoundryObject obj = null;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
370 LogNotifier.DebugFormat(GetType(), "Create object for <{0}>", elem.Name);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
371
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
372 if (elem.Name.Equals(WarFoundryXmlElementName.CATEGORY_ELEMENT.Value))
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 LogNotifier.Debug(GetType(), "Create Category");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
375 obj = CreateCategoryFromElement(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 else
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
378 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
379 LogNotifier.Debug(GetType(), "No match");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
380 }
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 return obj;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
383 }
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 private Category CreateCategoryFromElement(XmlElement elem)
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 string id = elem.GetAttribute("id");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
388 string name = elem.GetAttribute("name");
9
6ad505b6c36e Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 8
diff changeset
389 int minPc, maxPc, minPts, maxPts, minChoices, maxChoices, baseValue, incValue, incAmount;
6ad505b6c36e Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 8
diff changeset
390 minPc = GetIntValueFromAttribute(elem, "minPercentage");
6ad505b6c36e Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 8
diff changeset
391 maxPc = GetIntValueFromAttribute(elem, "maxPercentage");
6ad505b6c36e Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 8
diff changeset
392 minPts = GetIntValueFromAttribute(elem, "minPoints");
6ad505b6c36e Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 8
diff changeset
393 maxPts = GetIntValueFromAttribute(elem, "maxPoints");
6ad505b6c36e Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 8
diff changeset
394 minChoices = GetIntValueFromAttribute(elem, "minChoices");
6ad505b6c36e Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 8
diff changeset
395 maxChoices = GetIntValueFromAttribute(elem, "maxChoices");
6ad505b6c36e Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 8
diff changeset
396 baseValue = GetIntValueFromAttribute(elem, "baseValue");
6ad505b6c36e Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 8
diff changeset
397 incValue = GetIntValueFromAttribute(elem, "incValue");
6ad505b6c36e Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 8
diff changeset
398 incAmount = GetIntValueFromAttribute(elem, "incAmount");
6ad505b6c36e Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 8
diff changeset
399
6ad505b6c36e Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 8
diff changeset
400 return new Category(id, name, minPts, maxPts, minPc, maxPc, minChoices, maxChoices, baseValue, incValue, incAmount);
6ad505b6c36e Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 8
diff changeset
401 }
6ad505b6c36e Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 8
diff changeset
402
10
607c3232d689 Re #11 - Documentation
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
403 private int GetIntValueFromAttribute(XmlElement elem, string attributeName)
9
6ad505b6c36e Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 8
diff changeset
404 {
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
405 try
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
406 {
9
6ad505b6c36e Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 8
diff changeset
407 return int.Parse(elem.GetAttribute(attributeName));
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
408 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
409 catch(FormatException)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
410 {
9
6ad505b6c36e Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 8
diff changeset
411 throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id")));
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
412 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
413 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
414
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
415 private UnitType CreateUnitTypeFromElement(XmlElement elem, Race parentRace, GameSystem system)
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 string id = elem.GetAttribute("id");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
418 string name = elem.GetAttribute("typeName");
12
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
419 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
420 type.MinNumber = GetIntValueFromAttribute(elem, "minNum");
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
421 type.MaxNumber = GetIntValueFromAttribute(elem, "maxNum");
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
422 type.MinSize = GetIntValueFromAttribute(elem, "minSize");
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
423 type.MaxSize = GetIntValueFromAttribute(elem, "maxSize");
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
424 //TODO: Add base size
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
425 type.CostPerTrooper = GetIntValueFromAttribute(elem, "points");
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
426 type.BaseUnitCost = GetIntValueFromAttribute(elem, "unitPoints");
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 XmlNode node = elem.FirstChild;
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 foreach(XmlElement cat in node.ChildNodes)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
431 {
12
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
432 string catID = cat.GetAttribute("catID");
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
433 type.AddCategory(parentRace.GetCategory(catID));
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
434 }
12
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
435
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
436 string mainCatID = elem.GetAttribute("cat");
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
437 type.MainCategory = parentRace.GetCategory(mainCatID);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
438 node = node.NextSibling;
12
ac232763858b Re #9 - Make WarFoundry API use smaller methods
IBBoard <dev@ibboard.co.uk>
parents: 11
diff changeset
439 type.UnitStats = ParseUnitStats((XmlElement)node, system);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
440 //TODO: Add unit requirements
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
441
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
442 return type;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
443 }
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 private Stats ParseUnitStats(XmlElement elem, GameSystem system)
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 List<Stat> statsList = new List<Stat>();
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
448 String statsID = elem.GetAttribute("statSet");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
449 SystemStats statsSet;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
450
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
451 if (statsID == "")
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 statsSet = system.StandardSystemStats;
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 else
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
456 {
6
150a5669cd7b Re #9 - more granular loading
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
457 statsSet = system.GetSystemStatsForID(statsID);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
458 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
459
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
460 Stats stats = new Stats(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 foreach (XmlElement stat in elem.ChildNodes)
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 String statID = stat.GetAttribute("name");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
465 StatSlot slot = statsSet[statID];
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 if (slot!=null)
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 statsList.Add(new Stat(slot, stat.InnerText));
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 else
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 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
474 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
475 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
476
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
477 stats.SetStats(statsList);
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
478
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
479 return stats;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
480 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
481
6
150a5669cd7b Re #9 - more granular loading
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
482 private void LoadSystemStatsFromElement(XmlElement elem, GameSystem system)
0
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 foreach (XmlElement stats in elem.ChildNodes)
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 SystemStats sysStats = CreateSystemStatsFromElement(stats);
6
150a5669cd7b Re #9 - more granular loading
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
487 system.AddSystemStats(sysStats);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
488 }
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
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
491 private SystemStats CreateSystemStatsFromElement(XmlElement elem)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
492 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
493 List<StatSlot> slots = new List<StatSlot>();
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
494 string id = elem.GetAttribute("id");
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 foreach (XmlElement slot in elem.ChildNodes)
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 StatSlot statSlot = new StatSlot(slot.GetAttribute("name"));
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
499 slots.Add(statSlot);
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 return new SystemStats(id, slots.ToArray());
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
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
505 private EquipmentItem CreateEquipmentItemFromElement(XmlElement elem, Race race)
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 string id = elem.GetAttribute("id");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
508 string name = elem.GetAttribute("name");
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
509 float cost = 0, min = 0, max = 0;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
510 ArmourType armourType;
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 try
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 cost = float.Parse(elem.GetAttribute("cost"));
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 catch(FormatException)
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 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
519 }
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 try
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 min = float.Parse(elem.GetAttribute("min"));
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
524 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
525 catch(FormatException)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
526 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
527 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
528 }
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 try
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 max = float.Parse(elem.GetAttribute("max"));
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 catch(FormatException)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
535 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
536 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
537 }
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 try
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 armourType = (ArmourType)Enum.Parse(typeof(ArmourType), elem.GetAttribute("armourType"));
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 catch(FormatException)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
544 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
545 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
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 if (elem.ChildNodes.Count>0)
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 //It has stats!
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
551 //TODO: Parse equipment stats
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
552 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
553
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
554 return new EquipmentItem(id, name, cost, min, max, armourType, race);
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 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
557 }