Mercurial > repos > snowblizz-super-API-ideas
annotate api/Factories/Xml/WarFoundryXmlFactory.cs @ 234:06b4beb3e156 WarFoundry_v0.1beta6_Winforms
Re #228: Crash with missing abilityID
* Add helper methods to get various supporting factories
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Thu, 24 Dec 2009 19:59:38 +0000 |
parents | c931684f9024 |
children | d1068f4b6d1c |
rev | line source |
---|---|
104
2f3cafb69799
Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
1 // This file (WarFoundryXmlFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2007, 2008, 2009 IBBoard. |
0 | 2 // |
104
2f3cafb69799
Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. |
0 | 4 |
5 using System; | |
82 | 6 using System.IO; |
0 | 7 using System.Xml; |
8 using System.Xml.Schema; | |
34 | 9 using System.Xml.XPath; |
0 | 10 using System.Collections.Generic; |
11 using System.Text; | |
12 using IBBoard; | |
13 using IBBoard.IO; | |
14 using IBBoard.Lang; | |
15 using IBBoard.Logging; | |
16 using IBBoard.Xml; | |
17 using IBBoard.WarFoundry.API.Requirements; | |
18 using IBBoard.WarFoundry.API.Objects; | |
82 | 19 using ICSharpCode.SharpZipLib.Zip; |
20 | |
21 namespace IBBoard.WarFoundry.API.Factories.Xml | |
22 { | |
23 /// <summary> | |
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. | |
25 /// </summary> | |
26 public class WarFoundryXmlFactory : AbstractNativeWarFoundryFactory | |
0 | 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; |
52
64ef178c18aa
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
51
diff
changeset
|
29 private WarFoundryXmlGameSystemFactory gameSystemFactory; |
64ef178c18aa
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
51
diff
changeset
|
30 private WarFoundryXmlRaceFactory raceFactory; |
64ef178c18aa
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
51
diff
changeset
|
31 private WarFoundryXmlArmyFactory armyFactory; |
0 | 32 |
234
06b4beb3e156
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
226
diff
changeset
|
33 public static WarFoundryXmlFactory GetFactory() |
0 | 34 { |
17
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
35 if (factory == null) |
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
36 { |
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
37 factory = new WarFoundryXmlFactory(); |
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
38 } |
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
39 |
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
40 return factory; |
0 | 41 } |
42 | |
82 | 43 private WarFoundryXmlFactory() : base() |
0 | 44 { |
52
64ef178c18aa
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
51
diff
changeset
|
45 gameSystemFactory = new WarFoundryXmlGameSystemFactory(this); |
64ef178c18aa
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
51
diff
changeset
|
46 raceFactory = new WarFoundryXmlRaceFactory(this); |
82 | 47 armyFactory = new WarFoundryXmlArmyFactory(); |
0 | 48 } |
49 | |
234
06b4beb3e156
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
226
diff
changeset
|
50 public WarFoundryXmlGameSystemFactory GetSystemFactory() |
06b4beb3e156
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
226
diff
changeset
|
51 { |
06b4beb3e156
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
226
diff
changeset
|
52 return gameSystemFactory; |
06b4beb3e156
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
226
diff
changeset
|
53 } |
06b4beb3e156
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
226
diff
changeset
|
54 |
06b4beb3e156
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
226
diff
changeset
|
55 public WarFoundryXmlRaceFactory GetRaceFactory() |
06b4beb3e156
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
226
diff
changeset
|
56 { |
06b4beb3e156
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
226
diff
changeset
|
57 return raceFactory; |
06b4beb3e156
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
226
diff
changeset
|
58 } |
06b4beb3e156
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
226
diff
changeset
|
59 |
06b4beb3e156
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
226
diff
changeset
|
60 public WarFoundryXmlArmyFactory GetArmyFactory() |
06b4beb3e156
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
226
diff
changeset
|
61 { |
06b4beb3e156
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
226
diff
changeset
|
62 return armyFactory; |
06b4beb3e156
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
226
diff
changeset
|
63 } |
06b4beb3e156
Re #228: Crash with missing abilityID
IBBoard <dev@ibboard.co.uk>
parents:
226
diff
changeset
|
64 |
0 | 65 protected override bool CheckCanFindArmyFileContent(ZipFile file) |
66 { | |
67 return file.FindEntry("data.armyx", true) > -1; | |
68 } | |
69 | |
70 protected override bool CheckCanFindSystemFileContent(ZipFile file) | |
71 { | |
72 return file.FindEntry("data.systemx", true) > -1; | |
73 } | |
74 | |
75 protected override bool CheckCanFindRaceFileContent(ZipFile file) | |
76 { | |
77 return file.FindEntry("data.racex", true) > -1; | |
78 } | |
79 | |
50
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
80 protected override Stream GetArmyDataStream(ZipFile file) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
81 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
82 return file.GetInputStream(file.FindEntry("data.armyx", true)); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
83 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
84 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
85 protected override Army CreateArmyFromStream (ZipFile file, Stream dataStream) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
86 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
87 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.ARMY_ELEMENT); |
52
64ef178c18aa
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
51
diff
changeset
|
88 return armyFactory.CreateArmyFromElement(file, elem); |
50
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
89 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
90 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
91 private XmlElement GetRootElementFromStream(Stream stream, WarFoundryXmlElementName elementName) |
0 | 92 { |
135 | 93 XmlDocument doc = WarFoundryXmlFactoryUtils.CreateXmlDocumentFromStream(stream); |
199
70ba3bee0c2e
Fixes #207: WarFoundry still keeps file handles on failure
IBBoard <dev@ibboard.co.uk>
parents:
151
diff
changeset
|
94 |
0 | 95 XmlElement elem = (XmlElement)doc.LastChild; |
96 | |
43
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
97 if (!elem.LocalName.Equals(elementName.Value)) |
0 | 98 { |
99 throw new InvalidFileException(String.Format("Root element of XML was not valid. Expected {0} but got {1}", elementName.Value, elem.Name)); | |
100 } | |
101 | |
102 return elem; | |
103 } | |
50
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
104 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
105 protected override Stream GetGameSystemDataStream (ZipFile file) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
106 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
107 return file.GetInputStream(file.FindEntry("data.systemx", true)); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
108 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
109 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
110 protected override GameSystem CreateGameSystemFromStream (ZipFile file, Stream dataStream) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
111 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
112 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.SYSTEM_ELEMENT); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
113 LogNotifier.Debug(GetType(), "Create GameSystem"); |
52
64ef178c18aa
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
51
diff
changeset
|
114 return gameSystemFactory.CreateSystemFromElement(file, elem); |
50
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
115 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
116 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
117 protected override Stream GetRaceDataStream (ZipFile file) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
118 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
119 return file.GetInputStream(file.FindEntry("data.racex", true)); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
120 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
121 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
122 protected override Race CreateRaceFromStream (ZipFile file, Stream dataStream) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
123 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
124 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.RACE_ELEMENT); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
125 LogNotifier.Debug(GetType(), "Create Race"); |
199
70ba3bee0c2e
Fixes #207: WarFoundry still keeps file handles on failure
IBBoard <dev@ibboard.co.uk>
parents:
151
diff
changeset
|
126 return raceFactory.CreateRaceFromElement(file, elem); |
70ba3bee0c2e
Fixes #207: WarFoundry still keeps file handles on failure
IBBoard <dev@ibboard.co.uk>
parents:
151
diff
changeset
|
127 } |
70ba3bee0c2e
Fixes #207: WarFoundry still keeps file handles on failure
IBBoard <dev@ibboard.co.uk>
parents:
151
diff
changeset
|
128 |
70ba3bee0c2e
Fixes #207: WarFoundry still keeps file handles on failure
IBBoard <dev@ibboard.co.uk>
parents:
151
diff
changeset
|
129 protected override void CleanUpFileAsSupportedType(ZipFile typedFile) |
70ba3bee0c2e
Fixes #207: WarFoundry still keeps file handles on failure
IBBoard <dev@ibboard.co.uk>
parents:
151
diff
changeset
|
130 { |
70ba3bee0c2e
Fixes #207: WarFoundry still keeps file handles on failure
IBBoard <dev@ibboard.co.uk>
parents:
151
diff
changeset
|
131 typedFile.Close(); |
49
9d31d063b194
Re #10 - Refactor source code for readability
IBBoard <dev@ibboard.co.uk>
parents:
48
diff
changeset
|
132 } |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
133 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
134 public override void CompleteLoading(IWarFoundryStagedLoadObject obj) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
135 { |
226
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
136 LogNotifier.DebugFormat(GetType(), "Complete loading of {0} with ID {1}", obj.GetType().Name, obj.ID); |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
137 |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
138 if (obj is GameSystem) |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
139 { |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
140 CompleteLoadingGameSystem((GameSystem) obj); |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
141 } |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
142 else if (obj is Race) |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
143 { |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
144 CompleteLoadingRace((Race) obj); |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
145 } |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
146 } |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
147 |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
148 private void CompleteLoadingRace(Race race) |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
149 { |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
150 try |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
151 { |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
152 raceFactory.CompleteLoading(race); |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
153 } |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
154 catch (InvalidFileException ex) |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
155 { |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
156 WarFoundryLoader.GetDefault().RemoveRace(race); |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
157 throw; |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
158 } |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
159 } |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
160 |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
161 private void CompleteLoadingGameSystem(GameSystem system) |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
162 { |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
163 try |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
164 { |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
165 gameSystemFactory.CompleteLoading(system); |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
166 |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
167 } |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
168 catch (InvalidFileException ex) |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
169 { |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
170 WarFoundryLoader.GetDefault().RemoveGameSystem(system); |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
171 throw; |
c931684f9024
Re #234: Invalid data file doesn't stop load
IBBoard <dev@ibboard.co.uk>
parents:
199
diff
changeset
|
172 } |
47
85f2b9c3609c
Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
173 } |
82 | 174 } |
0 | 175 } |