annotate api/Factories/Xml/WarFoundryXmlFactory.cs @ 213:c6713a1b4c0d

* Line ending changes no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sat, 21 Nov 2009 20:06:36 +0000
parents 70ba3bee0c2e
children c931684f9024
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
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
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;
82
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 52
diff changeset
6 using System.IO;
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 using System.Xml;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 using System.Xml.Schema;
34
b28be912adab Re #32 - Migrate to schema
IBBoard <dev@ibboard.co.uk>
parents: 32
diff changeset
9 using System.Xml.XPath;
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 using System.Collections.Generic;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 using System.Text;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 using IBBoard;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 using IBBoard.IO;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 using IBBoard.Lang;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 using IBBoard.Logging;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 using IBBoard.Xml;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 using IBBoard.WarFoundry.API.Requirements;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 using IBBoard.WarFoundry.API.Objects;
82
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 52
diff changeset
19 using ICSharpCode.SharpZipLib.Zip;
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 52
diff changeset
20
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 52
diff changeset
21 namespace IBBoard.WarFoundry.API.Factories.Xml
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 52
diff changeset
22 {
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 52
diff changeset
23 /// <summary>
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 52
diff changeset
24 /// The WarFoundryXmlFactory loads WarFoundry classes from the native "XML in a zip" file format. Files are validated using the schema for the file type, so structurally invalid files should be identified at initial load.
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 52
diff changeset
25 /// </summary>
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 52
diff changeset
26 public class WarFoundryXmlFactory : AbstractNativeWarFoundryFactory
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 {
17
a99d3b8466ba Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
28 private static WarFoundryXmlFactory factory;
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
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32
17
a99d3b8466ba Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
33 public static AbstractNativeWarFoundryFactory GetFactory()
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 {
17
a99d3b8466ba Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
35 if (factory == null)
a99d3b8466ba Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
36 {
a99d3b8466ba Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
37 factory = new WarFoundryXmlFactory();
a99d3b8466ba Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
38 }
a99d3b8466ba Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
39
a99d3b8466ba Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
40 return factory;
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42
82
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 52
diff changeset
43 private WarFoundryXmlFactory() : base()
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
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
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 52
diff changeset
47 armyFactory = new WarFoundryXmlArmyFactory();
0
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
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50 protected override bool CheckCanFindArmyFileContent(ZipFile file)
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 return file.FindEntry("data.armyx", true) > -1;
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
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 protected override bool CheckCanFindSystemFileContent(ZipFile file)
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 return file.FindEntry("data.systemx", true) > -1;
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
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
60 protected override bool CheckCanFindRaceFileContent(ZipFile file)
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 return file.FindEntry("data.racex", true) > -1;
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
50
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
65 protected override Stream GetArmyDataStream(ZipFile file)
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
66 {
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
67 return file.GetInputStream(file.FindEntry("data.armyx", true));
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
68 }
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
69
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
70 protected override Army CreateArmyFromStream (ZipFile file, Stream dataStream)
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
71 {
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
72 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.ARMY_ELEMENT);
52
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 51
diff changeset
73 return armyFactory.CreateArmyFromElement(file, elem);
50
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
74 }
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
75
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
76 private XmlElement GetRootElementFromStream(Stream stream, WarFoundryXmlElementName elementName)
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
77 {
135
a37cdcbcad14 * Line ending change
IBBoard <dev@ibboard.co.uk>
parents: 126
diff changeset
78 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
79
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
80 XmlElement elem = (XmlElement)doc.LastChild;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
81
43
d0812d7de39d Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents: 42
diff changeset
82 if (!elem.LocalName.Equals(elementName.Value))
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
83 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
84 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
85 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
86
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
87 return elem;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
88 }
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 protected override Stream GetGameSystemDataStream (ZipFile file)
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
91 {
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
92 return file.GetInputStream(file.FindEntry("data.systemx", true));
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
93 }
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
94
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
95 protected override GameSystem CreateGameSystemFromStream (ZipFile file, Stream dataStream)
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
96 {
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
97 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.SYSTEM_ELEMENT);
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
98 LogNotifier.Debug(GetType(), "Create GameSystem");
52
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 51
diff changeset
99 return gameSystemFactory.CreateSystemFromElement(file, elem);
50
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
100 }
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
101
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
102 protected override Stream GetRaceDataStream (ZipFile file)
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
103 {
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
104 return file.GetInputStream(file.FindEntry("data.racex", true));
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
105 }
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 protected override Race CreateRaceFromStream (ZipFile file, Stream dataStream)
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 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.RACE_ELEMENT);
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
110 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
111 return raceFactory.CreateRaceFromElement(file, elem);
70ba3bee0c2e Fixes #207: WarFoundry still keeps file handles on failure
IBBoard <dev@ibboard.co.uk>
parents: 151
diff changeset
112 }
70ba3bee0c2e Fixes #207: WarFoundry still keeps file handles on failure
IBBoard <dev@ibboard.co.uk>
parents: 151
diff changeset
113
70ba3bee0c2e Fixes #207: WarFoundry still keeps file handles on failure
IBBoard <dev@ibboard.co.uk>
parents: 151
diff changeset
114 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
115 {
70ba3bee0c2e Fixes #207: WarFoundry still keeps file handles on failure
IBBoard <dev@ibboard.co.uk>
parents: 151
diff changeset
116 typedFile.Close();
49
9d31d063b194 Re #10 - Refactor source code for readability
IBBoard <dev@ibboard.co.uk>
parents: 48
diff changeset
117 }
48
b49372dd8afa Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents: 47
diff changeset
118
b49372dd8afa Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents: 47
diff changeset
119 public override void CompleteLoading(IWarFoundryStagedLoadObject obj)
b49372dd8afa Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents: 47
diff changeset
120 {
b49372dd8afa Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents: 47
diff changeset
121 LogNotifier.DebugFormat(GetType(), "Complete loading of {0} with ID {1}", obj.GetType().Name, obj.ID);
b49372dd8afa Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents: 47
diff changeset
122
b49372dd8afa Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents: 47
diff changeset
123 if (obj is GameSystem)
b49372dd8afa Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents: 47
diff changeset
124 {
52
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 51
diff changeset
125 gameSystemFactory.CompleteLoading((GameSystem)obj);
48
b49372dd8afa Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents: 47
diff changeset
126 }
b49372dd8afa Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents: 47
diff changeset
127 else if (obj is Race)
b49372dd8afa Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents: 47
diff changeset
128 {
52
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 51
diff changeset
129 raceFactory.CompleteLoading((Race)obj);
48
b49372dd8afa Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents: 47
diff changeset
130 }
47
85f2b9c3609c Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents: 45
diff changeset
131 }
82
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 52
diff changeset
132 }
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
133 }