comparison api/Factories/AbstractNativeWarFoundryFactory.cs @ 14:0770e5cbba7c

Closes #21 - File loading in order * Reworked LoadFiles to smaller methods for readability (also re #10) and structure * Now determine expected load return before loading then load all "expected GameSystem" before "expected Race" * Make "can load as race/game system/army" methods public in interface Re #22 - Get errored file loading * Created FileLoadFailure class and made LoadFiles return a list of them Also * Some code cleanup * Change to DictionaryUtils calls
author IBBoard <dev@ibboard.co.uk>
date Sun, 25 Jan 2009 14:03:20 +0000
parents 613bc5eaac59
children 306558904c2a
comparison
equal deleted inserted replaced
13:ad8eaed12e66 14:0770e5cbba7c
56 { 56 {
57 zip = new ZipFile(file.FullName); 57 zip = new ZipFile(file.FullName);
58 } 58 }
59 catch(ZipException) 59 catch(ZipException)
60 { 60 {
61 //Silently dispose - we don't support the file and our other methods should handle that 61 //Silently dispose as per spec for the method
62 } 62 }
63 63
64 return zip; 64 return zip;
65 } 65 }
66 66
67 protected override bool CheckCanHandleFileFormat (ZipFile file) 67 protected override bool CheckCanHandleFileFormat (ZipFile file)
68 { 68 {
69 bool canHandle = false; 69 return CheckCanHandleFileAsGameSystem(file) || CheckCanHandleFileAsRace(file) || CheckCanHandleFileAsArmy(file);
70
71 if (file!=null)
72 {
73 canHandle = CheckCanHandleAsSystem(file) || CheckCanHandleAsRace(file) || CheckCanHandleAsArmy(file);
74 }
75
76 return canHandle;
77 } 70 }
78 71
79 protected bool CheckCanHandleAsSystem(ZipFile file) 72 protected override bool CheckCanHandleFileAsGameSystem(ZipFile file)
80 { 73 {
81 return file.ZipFileComment.StartsWith(SYSTEM_ZIP_IDENTIFIER) && CheckCanFindSystemFileContent(file); 74 return file.ZipFileComment.StartsWith(SYSTEM_ZIP_IDENTIFIER) && CheckCanFindSystemFileContent(file);
82 } 75 }
83 76
84 protected abstract bool CheckCanFindSystemFileContent(ZipFile file); 77 protected abstract bool CheckCanFindSystemFileContent(ZipFile file);
85 78
86 protected bool CheckCanHandleAsRace(ZipFile file) 79 protected override bool CheckCanHandleFileAsRace(ZipFile file)
87 { 80 {
88 return file.ZipFileComment.StartsWith(RACE_ZIP_IDENTIFIER) && CheckCanFindRaceFileContent(file); 81 return file.ZipFileComment.StartsWith(RACE_ZIP_IDENTIFIER) && CheckCanFindRaceFileContent(file);
89 } 82 }
90 83
91 protected abstract bool CheckCanFindRaceFileContent(ZipFile file); 84 protected abstract bool CheckCanFindRaceFileContent(ZipFile file);
92 85
93 protected bool CheckCanHandleAsArmy(ZipFile file) 86 protected override bool CheckCanHandleFileAsArmy(ZipFile file)
94 { 87 {
95 return file.ZipFileComment.StartsWith(ARMY_ZIP_IDENTIFIER) && CheckCanFindArmyFileContent(file); 88 return file.ZipFileComment.StartsWith(ARMY_ZIP_IDENTIFIER) && CheckCanFindArmyFileContent(file);
96 } 89 }
97 90
98 protected abstract bool CheckCanFindArmyFileContent(ZipFile file); 91 protected abstract bool CheckCanFindArmyFileContent(ZipFile file);
122 objects.Add(obj); 115 objects.Add(obj);
123 } 116 }
124 117
125 return objects; 118 return objects;
126 } 119 }
127
128
129 /*
130 public override bool CanHandleArmyFileFormat(FileInfo file)
131 {
132 bool canHandle = false;
133
134 try
135 {
136 ZipFile zip = new ZipFile(file.FullName);
137 canHandle = zip.ZipFileComment.StartsWith(ARMY_ZIP_IDENTIFIER) && CanHandleArmyFileFormat(zip);
138 }
139 catch (ZipException)
140 {
141 //Not a valid zip file so we can't handle it
142 }
143
144 return canHandle;
145 }
146
147 public override bool CanHandleRaceFileFormat(FileInfo file)
148 {
149 bool canHandle = false;
150
151 try
152 {
153 ZipFile zip = new ZipFile(file.FullName);
154 canHandle = zip.ZipFileComment.StartsWith(RACE_ZIP_IDENTIFIER) && CanHandleRaceFileFormat(zip);
155 }
156 catch (ZipException)
157 {
158 //Not a valid zip file so we can't handle it
159 }
160
161 return canHandle;
162 }
163
164 public override bool CanHandleSystemFileFormat(FileInfo file)
165 {
166 bool canHandle = false;
167
168 try
169 {
170 ZipFile zip = new ZipFile(file.FullName);
171 canHandle = zip.ZipFileComment.StartsWith(SYSTEM_ZIP_IDENTIFIER) && CanHandleSystemFileFormat(zip);
172 }
173 catch (ZipException)
174 {
175 //Not a valid zip file so we can't handle it
176 }
177
178 return canHandle;
179 }
180
181 public abstract bool CanHandleArmyFileFormat(ZipFile file);
182 public abstract bool CanHandleRaceFileFormat(ZipFile file);
183 public abstract bool CanHandleSystemFileFormat(ZipFile file);
184
185 public override Army CreateArmyFromFile (FileInfo file)
186 {
187 try
188 {
189 ZipFile zip = new ZipFile(file.FullName);
190 return CreateArmyFromFile(zip);
191 }
192 catch(ZipException ex)
193 {
194 throw new InvalidFileException(Translation.GetTranslation("InvalidArmyFileException", "Cannot get Army for file {0} as it was not a recognised Army file", file.Name), ex);
195 }
196 }
197
198 public override Race CreateRaceFromFile (FileInfo file)
199 {
200 try
201 {
202 ZipFile zip = new ZipFile(file.FullName);
203 return CreateRaceFromFile(zip);
204 }
205 catch(ZipException ex)
206 {
207 throw new InvalidFileException(Translation.GetTranslation("InvalidRaceFileException", "Cannot get Race for file {0} as it was not a recognised Race file", file.Name), ex);
208 }
209 }
210
211 public override GameSystem CreateGameSystemFromFile (FileInfo file)
212 {
213 try
214 {
215 ZipFile zip = new ZipFile(file.FullName);
216 return CreateGameSystemFromFile(zip);
217 }
218 catch(ZipException ex)
219 {
220 throw new InvalidFileException(Translation.GetTranslation("InvalidGameSystemFileException", "Cannot get Game System for file {0} as it was not a recognised Race file", file.Name), ex);
221 }
222 }*/
223 120
224 protected Army CreateArmyFromFile(ZipFile file) 121 protected Army CreateArmyFromFile(ZipFile file)
225 { 122 {
226 Army army = CreateArmyFromStream(file, GetArmyDataStream(file)); 123 return CreateArmyFromStream(file, GetArmyDataStream(file));
227 //throw new InvalidFileException(Translation.GetTranslation("InvalidArmyFileException", "Cannot get Army for file {0} as it was not an Army file", file.Name));
228 return army;
229 } 124 }
230 125
231 protected abstract Stream GetArmyDataStream(ZipFile file); 126 protected abstract Stream GetArmyDataStream(ZipFile file);
232 protected abstract Army CreateArmyFromStream(ZipFile file, Stream dataStream); 127 protected abstract Army CreateArmyFromStream(ZipFile file, Stream dataStream);
233 128
234 protected Race CreateRaceFromFile(ZipFile file) 129 protected Race CreateRaceFromFile(ZipFile file)
235 { 130 {
236 try 131 try
237 { 132 {
238 Race race = CreateRaceFromStream(file, GetRaceDataStream(file)); 133 return CreateRaceFromStream(file, GetRaceDataStream(file));
239 //throw new InvalidFileException(Translation.GetTranslation("InvalidRaceFileException", "Cannot get Race for file {0} as it was not a Race file", file.Name));
240 return race;
241 } 134 }
242 catch (InvalidFileException ex) 135 catch (InvalidFileException ex)
243 { 136 {
244 throw new InvalidFileException("Data file "+file.Name+" was not a valid race file", ex); 137 throw new InvalidFileException("Data file "+file.Name+" was not a valid race file", ex);
245 } 138 }
248 protected abstract Stream GetRaceDataStream(ZipFile file); 141 protected abstract Stream GetRaceDataStream(ZipFile file);
249 protected abstract Race CreateRaceFromStream(ZipFile file, Stream dataStream); 142 protected abstract Race CreateRaceFromStream(ZipFile file, Stream dataStream);
250 143
251 protected GameSystem CreateGameSystemFromFile(ZipFile file) 144 protected GameSystem CreateGameSystemFromFile(ZipFile file)
252 { 145 {
253 GameSystem system = CreateGameSystemFromStream(file, GetGameSystemDataStream(file)); 146 return CreateGameSystemFromStream(file, GetGameSystemDataStream(file));
254 //throw new InvalidFileException(Translation.GetTranslation("InvalidGameSystemFileException", "Cannot get Game System for file {0} as it was not a Game System file", file.Name));
255 return system;
256 } 147 }
257 148
258 protected abstract Stream GetGameSystemDataStream(ZipFile file); 149 protected abstract Stream GetGameSystemDataStream(ZipFile file);
259 protected abstract GameSystem CreateGameSystemFromStream(ZipFile file, Stream dataStream); 150 protected abstract GameSystem CreateGameSystemFromStream(ZipFile file, Stream dataStream);
260 151