Mercurial > repos > IBDev-IBBoard.WarFoundry.API
comparison API/Factories/AbstractNativeWarFoundryFactory.cs @ 337:3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
no-open-ticket
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 03 Apr 2011 18:50:32 +0000 |
parents | |
children | c70973b65090 |
comparison
equal
deleted
inserted
replaced
336:3631c1493c7f | 337:3c4a6403a88c |
---|---|
1 // This file (AbstractNativeWarFoundryFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2007, 2008, 2009 IBBoard. | |
2 // | |
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. | |
4 | |
5 using System; | |
6 using System.IO; | |
7 using System.Xml; | |
8 using System.Xml.Schema; | |
9 using System.Collections.Generic; | |
10 using System.Text; | |
11 using IBBoard; | |
12 using IBBoard.IO; | |
13 using IBBoard.Lang; | |
14 using IBBoard.Logging; | |
15 using IBBoard.Xml; | |
16 using IBBoard.WarFoundry.API.Objects; | |
17 using ICSharpCode.SharpZipLib.Zip; | |
18 | |
19 namespace IBBoard.WarFoundry.API.Factories | |
20 { | |
21 /// <summary> | |
22 /// Base abstract class for all factories that load native WarFoundry data. | |
23 /// </summary> | |
24 public abstract class AbstractNativeWarFoundryFactory : AbstractWarFoundryFactory<ZipFile>, INativeWarFoundryFactory | |
25 { | |
26 protected AbstractNativeWarFoundryFactory() | |
27 { | |
28 //Do nothing - just make the constructor non-public | |
29 } | |
30 | |
31 protected override ZipFile GetFileAsSupportedType (FileInfo file) | |
32 { | |
33 ZipFile zip = null; | |
34 | |
35 try | |
36 { | |
37 zip = new ZipFile(file.FullName); | |
38 } | |
39 catch(ZipException) | |
40 { | |
41 //Silently dispose as per spec for the method | |
42 } | |
43 catch (IOException) | |
44 { | |
45 //Silently dispose as per spec for the method | |
46 } | |
47 | |
48 return zip; | |
49 } | |
50 | |
51 protected override bool CheckCanHandleFileFormat (ZipFile file) | |
52 { | |
53 return CheckCanHandleFileAsGameSystem(file) || CheckCanHandleFileAsRace(file) || CheckCanHandleFileAsArmy(file); | |
54 } | |
55 | |
56 protected override bool CheckCanHandleFileAsGameSystem(ZipFile file) | |
57 { | |
58 return CheckCanFindSystemFileContent(file); | |
59 } | |
60 | |
61 protected abstract bool CheckCanFindSystemFileContent(ZipFile file); | |
62 | |
63 protected override bool CheckCanHandleFileAsRace(ZipFile file) | |
64 { | |
65 return CheckCanFindRaceFileContent(file); | |
66 } | |
67 | |
68 protected abstract bool CheckCanFindRaceFileContent(ZipFile file); | |
69 | |
70 protected override bool CheckCanHandleFileAsArmy(ZipFile file) | |
71 { | |
72 return CheckCanFindArmyFileContent(file); | |
73 } | |
74 | |
75 protected abstract bool CheckCanFindArmyFileContent(ZipFile file); | |
76 | |
77 protected override ICollection<IWarFoundryObject> DoCreateObjectsFromFile (ZipFile file) | |
78 { | |
79 ICollection<IWarFoundryObject> objects = new List<IWarFoundryObject>(); | |
80 | |
81 try | |
82 { | |
83 if (CheckCanFindSystemFileContent(file)) | |
84 { | |
85 foreach (GameSystem system in CreateGameSystemsFromFile(file)) | |
86 { | |
87 OnGameSystemLoaded(system); | |
88 objects.Add(system); | |
89 } | |
90 } | |
91 | |
92 if (CheckCanFindRaceFileContent(file)) | |
93 { | |
94 foreach(Race race in CreateRacesFromFile(file)) | |
95 { | |
96 OnRaceLoaded(race); | |
97 objects.Add(race); | |
98 } | |
99 } | |
100 | |
101 if (CheckCanFindArmyFileContent(file)) | |
102 { | |
103 foreach (Army army in CreateArmiesFromFile(file)) | |
104 { | |
105 OnArmyLoaded(army); | |
106 objects.Add(army); | |
107 } | |
108 } | |
109 } | |
110 finally | |
111 { | |
112 file.Close(); | |
113 } | |
114 | |
115 return objects; | |
116 } | |
117 | |
118 protected ICollection<Army> CreateArmiesFromFile(ZipFile file) | |
119 { | |
120 ICollection<ZipEntry> dataStreams = GetArmyZipEntries(file); | |
121 ICollection<Army> armies = new List<Army>(); | |
122 | |
123 foreach (ZipEntry entry in dataStreams) | |
124 { | |
125 using (Stream dataStream = file.GetInputStream(entry)) | |
126 { | |
127 armies.Add(CreateArmyFromStream(file, dataStream)); | |
128 } | |
129 } | |
130 | |
131 return armies; | |
132 } | |
133 | |
134 protected abstract ICollection<ZipEntry> GetArmyZipEntries(ZipFile file); | |
135 protected abstract Army CreateArmyFromStream(ZipFile file, Stream dataStream); | |
136 | |
137 protected ICollection<Race> CreateRacesFromFile(ZipFile file) | |
138 { | |
139 ICollection<ZipEntry> dataStreams = GetRaceZipEntries(file); | |
140 ICollection<Race> races = new List<Race>(); | |
141 | |
142 foreach (ZipEntry entry in dataStreams) | |
143 { | |
144 using (Stream dataStream = file.GetInputStream(entry)) | |
145 { | |
146 races.Add(CreateRaceFromStream(file, dataStream)); | |
147 } | |
148 } | |
149 | |
150 return races; | |
151 } | |
152 | |
153 protected abstract ICollection<ZipEntry> GetRaceZipEntries(ZipFile file); | |
154 protected abstract Race CreateRaceFromStream(ZipFile file, Stream dataStream); | |
155 | |
156 protected ICollection<GameSystem> CreateGameSystemsFromFile(ZipFile file) | |
157 { | |
158 ICollection<ZipEntry> dataStreams = GetGameSystemZipEntries(file); | |
159 ICollection<GameSystem> systems = new List<GameSystem>(); | |
160 | |
161 foreach (ZipEntry entry in dataStreams) | |
162 { | |
163 using (Stream dataStream = file.GetInputStream(entry)) | |
164 { | |
165 systems.Add(CreateGameSystemFromStream(file, dataStream)); | |
166 } | |
167 } | |
168 | |
169 return systems; | |
170 } | |
171 | |
172 protected abstract ICollection<ZipEntry> GetGameSystemZipEntries(ZipFile file); | |
173 protected abstract GameSystem CreateGameSystemFromStream(ZipFile file, Stream dataStream); | |
174 | |
175 public override bool Equals (object o) | |
176 { | |
177 if (o == this) | |
178 { | |
179 return true; | |
180 } | |
181 else if (o == null || !(this.GetType().Equals(o.GetType()))) | |
182 { | |
183 return false; | |
184 } | |
185 | |
186 return true; | |
187 } | |
188 | |
189 public override int GetHashCode () | |
190 { | |
191 return GetType().FullName.GetHashCode(); | |
192 } | |
193 } | |
194 } |