Mercurial > repos > IBBoard.WarFoundry.API
annotate api/Factories/AbstractNativeWarFoundryFactory.cs @ 223:fe5a03d73918
Fixes #232: WarFoundry is hiding error details when loading files
* Remove error hiding introduced in r281 for GameSystems that was in from the start for Races (code caught an InvalidFileException and created and threw a new InvalidFileException)
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 14 Dec 2009 20:19:01 +0000 |
parents | 3e287b6b5244 |
children | ca2905c9b225 |
rev | line source |
---|---|
104
2f3cafb69799
Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
1 // This file (AbstractNativeWarFoundryFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2007, 2008, 2009 IBBoard. |
0 | 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 | 4 |
5 using System; | |
82 | 6 using System.IO; |
0 | 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; | |
82 | 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 | |
0 | 25 { |
115 | 26 public static readonly string SYSTEM_ZIP_IDENTIFIER = "WarFoundry_System"; |
27 public static readonly string RACE_ZIP_IDENTIFIER = "WarFoundry_Race"; | |
28 public static readonly string ARMY_ZIP_IDENTIFIER = "WarFoundry_Army"; | |
0 | 29 |
82 | 30 protected AbstractNativeWarFoundryFactory() |
0 | 31 { |
82 | 32 //Do nothing - just make the constructor non-public |
0 | 33 } |
8
613bc5eaac59
Re #9 - Make WarFoundry loading granular
IBBoard <dev@ibboard.co.uk>
parents:
7
diff
changeset
|
34 |
0 | 35 protected override ZipFile GetFileAsSupportedType (FileInfo file) |
36 { | |
37 ZipFile zip = null; | |
38 | |
39 try | |
40 { | |
41 zip = new ZipFile(file.FullName); | |
42 } | |
7
895c8a2378a1
Code cleanup - remove warning about unused exception
IBBoard <dev@ibboard.co.uk>
parents:
0
diff
changeset
|
43 catch(ZipException) |
0 | 44 { |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
45 //Silently dispose as per spec for the method |
0 | 46 } |
27 | 47 catch (IOException) |
48 { | |
49 //Silently dispose as per spec for the method | |
50 } | |
0 | 51 |
52 return zip; | |
53 } | |
54 | |
55 protected override bool CheckCanHandleFileFormat (ZipFile file) | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
56 { |
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
57 return CheckCanHandleFileAsGameSystem(file) || CheckCanHandleFileAsRace(file) || CheckCanHandleFileAsArmy(file); |
0 | 58 } |
59 | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
60 protected override bool CheckCanHandleFileAsGameSystem(ZipFile file) |
0 | 61 { |
62 return file.ZipFileComment.StartsWith(SYSTEM_ZIP_IDENTIFIER) && CheckCanFindSystemFileContent(file); | |
63 } | |
64 | |
65 protected abstract bool CheckCanFindSystemFileContent(ZipFile file); | |
66 | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
67 protected override bool CheckCanHandleFileAsRace(ZipFile file) |
0 | 68 { |
69 return file.ZipFileComment.StartsWith(RACE_ZIP_IDENTIFIER) && CheckCanFindRaceFileContent(file); | |
70 } | |
71 | |
72 protected abstract bool CheckCanFindRaceFileContent(ZipFile file); | |
73 | |
14
0770e5cbba7c
Closes #21 - File loading in order
IBBoard <dev@ibboard.co.uk>
parents:
8
diff
changeset
|
74 protected override bool CheckCanHandleFileAsArmy(ZipFile file) |
0 | 75 { |
76 return file.ZipFileComment.StartsWith(ARMY_ZIP_IDENTIFIER) && CheckCanFindArmyFileContent(file); | |
77 } | |
78 | |
79 protected abstract bool CheckCanFindArmyFileContent(ZipFile file); | |
80 | |
81 protected override ICollection<IWarFoundryObject> DoCreateObjectsFromFile (ZipFile file) | |
82 { | |
83 ICollection<IWarFoundryObject> objects = null; | |
84 string comment = file.ZipFileComment; | |
200 | 85 IWarFoundryObject obj = null; |
86 | |
87 try | |
88 { | |
89 if (comment.StartsWith(SYSTEM_ZIP_IDENTIFIER)) | |
90 { | |
91 obj = CreateGameSystemFromFile(file); | |
92 } | |
93 else if (comment.StartsWith(RACE_ZIP_IDENTIFIER)) | |
94 { | |
95 obj = CreateRaceFromFile(file); | |
96 } | |
97 else if (comment.StartsWith(ARMY_ZIP_IDENTIFIER)) | |
98 { | |
99 obj = CreateArmyFromFile(file); | |
100 } | |
101 } | |
102 finally | |
103 { | |
104 file.Close(); | |
0 | 105 } |
106 | |
107 if (obj!=null) | |
108 { | |
109 objects = new List<IWarFoundryObject>(); | |
110 objects.Add(obj); | |
151
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
111 } |
0 | 112 |
113 return objects; | |
114 } | |
115 | |
116 protected Army CreateArmyFromFile(ZipFile file) | |
117 { | |
151
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
118 Stream dataStream = GetArmyDataStream(file); |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
119 |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
120 try |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
121 { |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
122 return CreateArmyFromStream(file, dataStream); |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
123 } |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
124 finally |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
125 { |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
126 dataStream.Close(); |
148
8e636443aa8e
Re #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
115
diff
changeset
|
127 } |
0 | 128 } |
129 | |
130 protected abstract Stream GetArmyDataStream(ZipFile file); | |
131 protected abstract Army CreateArmyFromStream(ZipFile file, Stream dataStream); | |
132 | |
133 protected Race CreateRaceFromFile(ZipFile file) | |
151
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
134 { |
148
8e636443aa8e
Re #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
115
diff
changeset
|
135 Stream dataStream = GetRaceDataStream(file); |
8e636443aa8e
Re #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
115
diff
changeset
|
136 |
0 | 137 try |
138 { | |
148
8e636443aa8e
Re #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
115
diff
changeset
|
139 return CreateRaceFromStream(file, dataStream); |
0 | 140 } |
151
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
141 finally |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
142 { |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
143 dataStream.Close(); |
0 | 144 } |
145 } | |
146 | |
147 protected abstract Stream GetRaceDataStream(ZipFile file); | |
148 protected abstract Race CreateRaceFromStream(ZipFile file, Stream dataStream); | |
149 | |
150 protected GameSystem CreateGameSystemFromFile(ZipFile file) | |
151
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
151 { |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
152 Stream dataStream = GetGameSystemDataStream(file); |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
153 |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
154 try |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
155 { |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
156 return CreateGameSystemFromStream(file, dataStream); |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
157 } |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
158 finally |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
159 { |
1d13820b3d96
Fixes #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
150
diff
changeset
|
160 dataStream.Close(); |
148
8e636443aa8e
Re #176: Bug when saving recently edited army
IBBoard <dev@ibboard.co.uk>
parents:
115
diff
changeset
|
161 } |
0 | 162 } |
163 | |
164 protected abstract Stream GetGameSystemDataStream(ZipFile file); | |
82 | 165 protected abstract GameSystem CreateGameSystemFromStream(ZipFile file, Stream dataStream); |
0 | 166 |
167 public override bool Equals (object o) | |
168 { | |
169 if (o == this) | |
170 { | |
171 return true; | |
172 } | |
173 else if (o == null || !(this.GetType().Equals(o.GetType()))) | |
174 { | |
175 return false; | |
176 } | |
177 | |
178 return true; | |
179 } | |
180 | |
181 public override int GetHashCode () | |
182 { | |
183 return GetType().FullName.GetHashCode(); | |
82 | 184 } |
185 } | |
186 } |