0
|
1 // WarFoundryXmlFactory.cs
|
|
2 //
|
|
3 // Copyright (C) 2007 IBBoard
|
|
4 //
|
|
5 // This library is free software; you can redistribute it and/or
|
|
6 // modify it under the terms of the GNU Lesser General Public
|
|
7 // License version 2.1 of the License as published by the Free
|
|
8 // Software Foundation.
|
|
9 //
|
|
10 // This library is distributed in the hope that it will be useful,
|
|
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13 // Lesser General Public License for more details.
|
|
14 //
|
|
15 // You should have received a copy of the GNU Lesser General Public
|
|
16 // License along with this library; if not, write to the Free Software
|
|
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
18 //
|
|
19 //
|
|
20
|
|
21 using System;
|
|
22 using System.IO;
|
|
23 using System.Xml;
|
|
24 using System.Xml.Schema;
|
|
25 using System.Collections.Generic;
|
|
26 using System.Text;
|
|
27 using IBBoard;
|
|
28 using IBBoard.IO;
|
|
29 using IBBoard.Lang;
|
|
30 using IBBoard.Logging;
|
|
31 using IBBoard.Xml;
|
|
32 using IBBoard.WarFoundry.API.Objects;
|
|
33 using ICSharpCode.SharpZipLib.Zip;
|
|
34
|
|
35 namespace IBBoard.WarFoundry.API.Factories
|
|
36 {
|
|
37 /// <summary>
|
|
38 /// Base abstract class for all factories that load native WarFoundry data.
|
|
39 /// </summary>
|
|
40 public abstract class AbstractNativeWarFoundryFactory : AbstractWarFoundryFactory<ZipFile>, INativeWarFoundryFactory
|
|
41 {
|
|
42 protected static readonly string SYSTEM_ZIP_IDENTIFIER = "WarFoundry_System";
|
|
43 protected static readonly string RACE_ZIP_IDENTIFIER = "WarFoundry_Race";
|
|
44 protected static readonly string ARMY_ZIP_IDENTIFIER = "WarFoundry_Army";
|
|
45
|
|
46 protected AbstractNativeWarFoundryFactory()
|
|
47 {
|
|
48 //Do nothing - just make the constructor non-public
|
|
49 }
|
|
50
|
|
51 public abstract void CompleteLoading(IWarFoundryStagedLoadObject obj);
|
|
52
|
|
53 protected override ZipFile GetFileAsSupportedType (FileInfo file)
|
|
54 {
|
|
55 ZipFile zip = null;
|
|
56
|
|
57 try
|
|
58 {
|
|
59 zip = new ZipFile(file.FullName);
|
|
60 }
|
|
61 catch(ZipException ex)
|
|
62 {
|
|
63 //Silently dispose - we don't support the file and our other methods should handle that
|
|
64 }
|
|
65
|
|
66 return zip;
|
|
67 }
|
|
68
|
|
69 protected override bool CheckCanHandleFileFormat (ZipFile file)
|
|
70 {
|
|
71 bool canHandle = false;
|
|
72
|
|
73 if (file!=null)
|
|
74 {
|
|
75 canHandle = CheckCanHandleAsSystem(file) || CheckCanHandleAsRace(file) || CheckCanHandleAsArmy(file);
|
|
76 }
|
|
77
|
|
78 return canHandle;
|
|
79 }
|
|
80
|
|
81 protected bool CheckCanHandleAsSystem(ZipFile file)
|
|
82 {
|
|
83 return file.ZipFileComment.StartsWith(SYSTEM_ZIP_IDENTIFIER) && CheckCanFindSystemFileContent(file);
|
|
84 }
|
|
85
|
|
86 protected abstract bool CheckCanFindSystemFileContent(ZipFile file);
|
|
87
|
|
88 protected bool CheckCanHandleAsRace(ZipFile file)
|
|
89 {
|
|
90 return file.ZipFileComment.StartsWith(RACE_ZIP_IDENTIFIER) && CheckCanFindRaceFileContent(file);
|
|
91 }
|
|
92
|
|
93 protected abstract bool CheckCanFindRaceFileContent(ZipFile file);
|
|
94
|
|
95 protected bool CheckCanHandleAsArmy(ZipFile file)
|
|
96 {
|
|
97 return file.ZipFileComment.StartsWith(ARMY_ZIP_IDENTIFIER) && CheckCanFindArmyFileContent(file);
|
|
98 }
|
|
99
|
|
100 protected abstract bool CheckCanFindArmyFileContent(ZipFile file);
|
|
101
|
|
102 protected override ICollection<IWarFoundryObject> DoCreateObjectsFromFile (ZipFile file)
|
|
103 {
|
|
104 ICollection<IWarFoundryObject> objects = null;
|
|
105 string comment = file.ZipFileComment;
|
|
106 IWarFoundryObject obj = null;
|
|
107
|
|
108 if (comment.StartsWith(SYSTEM_ZIP_IDENTIFIER))
|
|
109 {
|
|
110 obj = CreateGameSystemFromFile(file);
|
|
111 }
|
|
112 else if (comment.StartsWith(RACE_ZIP_IDENTIFIER))
|
|
113 {
|
|
114 obj = CreateRaceFromFile(file);
|
|
115 }
|
|
116 else if (comment.StartsWith(ARMY_ZIP_IDENTIFIER))
|
|
117 {
|
|
118 obj = CreateArmyFromFile(file);
|
|
119 }
|
|
120
|
|
121 if (obj!=null)
|
|
122 {
|
|
123 objects = new List<IWarFoundryObject>();
|
|
124 objects.Add(obj);
|
|
125 }
|
|
126
|
|
127 return objects;
|
|
128 }
|
|
129
|
|
130
|
|
131 /*
|
|
132 public override bool CanHandleArmyFileFormat(FileInfo file)
|
|
133 {
|
|
134 bool canHandle = false;
|
|
135
|
|
136 try
|
|
137 {
|
|
138 ZipFile zip = new ZipFile(file.FullName);
|
|
139 canHandle = zip.ZipFileComment.StartsWith(ARMY_ZIP_IDENTIFIER) && CanHandleArmyFileFormat(zip);
|
|
140 }
|
|
141 catch (ZipException)
|
|
142 {
|
|
143 //Not a valid zip file so we can't handle it
|
|
144 }
|
|
145
|
|
146 return canHandle;
|
|
147 }
|
|
148
|
|
149 public override bool CanHandleRaceFileFormat(FileInfo file)
|
|
150 {
|
|
151 bool canHandle = false;
|
|
152
|
|
153 try
|
|
154 {
|
|
155 ZipFile zip = new ZipFile(file.FullName);
|
|
156 canHandle = zip.ZipFileComment.StartsWith(RACE_ZIP_IDENTIFIER) && CanHandleRaceFileFormat(zip);
|
|
157 }
|
|
158 catch (ZipException)
|
|
159 {
|
|
160 //Not a valid zip file so we can't handle it
|
|
161 }
|
|
162
|
|
163 return canHandle;
|
|
164 }
|
|
165
|
|
166 public override bool CanHandleSystemFileFormat(FileInfo file)
|
|
167 {
|
|
168 bool canHandle = false;
|
|
169
|
|
170 try
|
|
171 {
|
|
172 ZipFile zip = new ZipFile(file.FullName);
|
|
173 canHandle = zip.ZipFileComment.StartsWith(SYSTEM_ZIP_IDENTIFIER) && CanHandleSystemFileFormat(zip);
|
|
174 }
|
|
175 catch (ZipException)
|
|
176 {
|
|
177 //Not a valid zip file so we can't handle it
|
|
178 }
|
|
179
|
|
180 return canHandle;
|
|
181 }
|
|
182
|
|
183 public abstract bool CanHandleArmyFileFormat(ZipFile file);
|
|
184 public abstract bool CanHandleRaceFileFormat(ZipFile file);
|
|
185 public abstract bool CanHandleSystemFileFormat(ZipFile file);
|
|
186
|
|
187 public override Army CreateArmyFromFile (FileInfo file)
|
|
188 {
|
|
189 try
|
|
190 {
|
|
191 ZipFile zip = new ZipFile(file.FullName);
|
|
192 return CreateArmyFromFile(zip);
|
|
193 }
|
|
194 catch(ZipException ex)
|
|
195 {
|
|
196 throw new InvalidFileException(Translation.GetTranslation("InvalidArmyFileException", "Cannot get Army for file {0} as it was not a recognised Army file", file.Name), ex);
|
|
197 }
|
|
198 }
|
|
199
|
|
200 public override Race CreateRaceFromFile (FileInfo file)
|
|
201 {
|
|
202 try
|
|
203 {
|
|
204 ZipFile zip = new ZipFile(file.FullName);
|
|
205 return CreateRaceFromFile(zip);
|
|
206 }
|
|
207 catch(ZipException ex)
|
|
208 {
|
|
209 throw new InvalidFileException(Translation.GetTranslation("InvalidRaceFileException", "Cannot get Race for file {0} as it was not a recognised Race file", file.Name), ex);
|
|
210 }
|
|
211 }
|
|
212
|
|
213 public override GameSystem CreateGameSystemFromFile (FileInfo file)
|
|
214 {
|
|
215 try
|
|
216 {
|
|
217 ZipFile zip = new ZipFile(file.FullName);
|
|
218 return CreateGameSystemFromFile(zip);
|
|
219 }
|
|
220 catch(ZipException ex)
|
|
221 {
|
|
222 throw new InvalidFileException(Translation.GetTranslation("InvalidGameSystemFileException", "Cannot get Game System for file {0} as it was not a recognised Race file", file.Name), ex);
|
|
223 }
|
|
224 }*/
|
|
225
|
|
226 protected Army CreateArmyFromFile(ZipFile file)
|
|
227 {
|
|
228 Army army = CreateArmyFromStream(file, GetArmyDataStream(file));
|
|
229 //throw new InvalidFileException(Translation.GetTranslation("InvalidArmyFileException", "Cannot get Army for file {0} as it was not an Army file", file.Name));
|
|
230 return army;
|
|
231 }
|
|
232
|
|
233 protected abstract Stream GetArmyDataStream(ZipFile file);
|
|
234 protected abstract Army CreateArmyFromStream(ZipFile file, Stream dataStream);
|
|
235
|
|
236 protected Race CreateRaceFromFile(ZipFile file)
|
|
237 {
|
|
238 try
|
|
239 {
|
|
240 Race race = CreateRaceFromStream(file, GetRaceDataStream(file));
|
|
241 //throw new InvalidFileException(Translation.GetTranslation("InvalidRaceFileException", "Cannot get Race for file {0} as it was not a Race file", file.Name));
|
|
242 return race;
|
|
243 }
|
|
244 catch (InvalidFileException ex)
|
|
245 {
|
|
246 throw new InvalidFileException("Data file "+file.Name+" was not a valid race file", ex);
|
|
247 }
|
|
248 }
|
|
249
|
|
250 protected abstract Stream GetRaceDataStream(ZipFile file);
|
|
251 protected abstract Race CreateRaceFromStream(ZipFile file, Stream dataStream);
|
|
252
|
|
253 protected GameSystem CreateGameSystemFromFile(ZipFile file)
|
|
254 {
|
|
255 GameSystem system = CreateGameSystemFromStream(file, GetGameSystemDataStream(file));
|
|
256 //throw new InvalidFileException(Translation.GetTranslation("InvalidGameSystemFileException", "Cannot get Game System for file {0} as it was not a Game System file", file.Name));
|
|
257 return system;
|
|
258 }
|
|
259
|
|
260 protected abstract Stream GetGameSystemDataStream(ZipFile file);
|
|
261 protected abstract GameSystem CreateGameSystemFromStream(ZipFile file, Stream dataStream);
|
|
262
|
|
263 public override bool Equals (object o)
|
|
264 {
|
|
265 if (o == this)
|
|
266 {
|
|
267 return true;
|
|
268 }
|
|
269 else if (o == null || !(this.GetType().Equals(o.GetType())))
|
|
270 {
|
|
271 return false;
|
|
272 }
|
|
273
|
|
274 return true;
|
|
275 }
|
|
276
|
|
277 public override int GetHashCode ()
|
|
278 {
|
|
279 return GetType().FullName.GetHashCode();
|
|
280 }
|
|
281 }
|
|
282 }
|