annotate api/Factories/Xml/WarFoundryXmlFactory.cs @ 82:3ea0ab04352b

* Fix line terminators no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sat, 27 Jun 2009 18:59:49 +0000
parents 64ef178c18aa
children 2f3cafb69799
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;
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 {
52
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 51
diff changeset
78 XmlDocument doc = WarFoundryXmlFactoryUtils.CreateXmlDocumentFromStream(stream);
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
79 XmlElement elem = (XmlElement)doc.LastChild;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
80
43
d0812d7de39d Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents: 42
diff changeset
81 if (!elem.LocalName.Equals(elementName.Value))
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
82 {
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
83 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
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 return elem;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
87 }
50
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
88
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
89 protected override Stream GetGameSystemDataStream (ZipFile file)
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 return file.GetInputStream(file.FindEntry("data.systemx", true));
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
92 }
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 protected override GameSystem CreateGameSystemFromStream (ZipFile file, Stream dataStream)
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
95 {
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
96 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.SYSTEM_ELEMENT);
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
97 LogNotifier.Debug(GetType(), "Create GameSystem");
52
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 51
diff changeset
98 return gameSystemFactory.CreateSystemFromElement(file, elem);
50
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
99 }
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 protected override Stream GetRaceDataStream (ZipFile file)
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
102 {
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
103 return file.GetInputStream(file.FindEntry("data.racex", true));
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
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
106 protected override Race CreateRaceFromStream (ZipFile file, Stream dataStream)
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
107 {
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
108 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.RACE_ELEMENT);
bb6b993b98bf Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 49
diff changeset
109 LogNotifier.Debug(GetType(), "Create Race");
52
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 51
diff changeset
110 return raceFactory.CreateRaceFromElement(file, elem);
49
9d31d063b194 Re #10 - Refactor source code for readability
IBBoard <dev@ibboard.co.uk>
parents: 48
diff changeset
111 }
48
b49372dd8afa Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents: 47
diff changeset
112
b49372dd8afa Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents: 47
diff changeset
113 public override void CompleteLoading(IWarFoundryStagedLoadObject obj)
b49372dd8afa Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents: 47
diff changeset
114 {
b49372dd8afa Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents: 47
diff changeset
115 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
116
b49372dd8afa Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents: 47
diff changeset
117 if (obj is GameSystem)
b49372dd8afa Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents: 47
diff changeset
118 {
52
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 51
diff changeset
119 gameSystemFactory.CompleteLoading((GameSystem)obj);
48
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 else if (obj is Race)
b49372dd8afa Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents: 47
diff changeset
122 {
52
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents: 51
diff changeset
123 raceFactory.CompleteLoading((Race)obj);
48
b49372dd8afa Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents: 47
diff changeset
124 }
47
85f2b9c3609c Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents: 45
diff changeset
125 }
82
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 52
diff changeset
126 }
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
127 }