comparison API/Factories/AbstractNativeWarFoundryFactory.cs @ 443:86725e88052e

Re #380: WarFoundry chokes on zips written by Mac OS X * Add "is dot-file" check to behave like Linux and Mac and ignore files with name ".something" Also: * Fix load failure message argument order * Save some regex checks by making sure zip entry is a file first
author IBBoard <dev@ibboard.co.uk>
date Sun, 04 Dec 2011 20:40:31 +0000
parents 2a36ebb7b6a9
children cbeee87dc2d3
comparison
equal deleted inserted replaced
442:5ac76de8ce62 443:86725e88052e
27 protected AbstractNativeWarFoundryFactory() 27 protected AbstractNativeWarFoundryFactory()
28 { 28 {
29 //Do nothing - just make the constructor non-public 29 //Do nothing - just make the constructor non-public
30 } 30 }
31 31
32 protected override ZipFile GetFileAsSupportedType (FileInfo file) 32 protected override ZipFile GetFileAsSupportedType(FileInfo file)
33 { 33 {
34 ZipFile zip = null; 34 ZipFile zip = null;
35 string ext = file.Extension.ToLower(); 35 string ext = file.Extension.ToLower();
36 36
37 if (ext == ".race" || ext == ".army" || ext == ".system") 37 if (ext == ".race" || ext == ".army" || ext == ".system")
145 ICollection<ZipEntry> dataStreams = GetArmyZipEntries(file); 145 ICollection<ZipEntry> dataStreams = GetArmyZipEntries(file);
146 ICollection<Army> armies = new List<Army>(); 146 ICollection<Army> armies = new List<Army>();
147 147
148 foreach (ZipEntry entry in dataStreams) 148 foreach (ZipEntry entry in dataStreams)
149 { 149 {
150 if (IsDotFile(entry))
151 {
152 continue;
153 }
154
150 using (Stream dataStream = file.GetInputStream(entry)) 155 using (Stream dataStream = file.GetInputStream(entry))
151 { 156 {
152 armies.Add(CreateArmyFromStream(file, dataStream)); 157 armies.Add(CreateArmyFromStream(file, dataStream));
153 } 158 }
154 } 159 }
155 160
156 return armies; 161 return armies;
162 }
163
164 private bool IsDotFile(ZipEntry entry)
165 {
166 string name = entry.Name;
167 int slashIdx = name.LastIndexOf('/');
168 return (slashIdx != -1 && slashIdx == name.LastIndexOf("/.")) || (name.Length > 0 && name[0] == '.');
157 } 169 }
158 170
159 protected abstract ICollection<ZipEntry> GetArmyZipEntries(ZipFile file); 171 protected abstract ICollection<ZipEntry> GetArmyZipEntries(ZipFile file);
160 protected abstract Army CreateArmyFromStream(ZipFile file, Stream dataStream); 172 protected abstract Army CreateArmyFromStream(ZipFile file, Stream dataStream);
161 173
163 { 175 {
164 ICollection<ZipEntry> dataStreams = GetRaceZipEntries(file); 176 ICollection<ZipEntry> dataStreams = GetRaceZipEntries(file);
165 ICollection<Race> races = new List<Race>(); 177 ICollection<Race> races = new List<Race>();
166 178
167 foreach (ZipEntry entry in dataStreams) 179 foreach (ZipEntry entry in dataStreams)
168 { 180 {
181 if (IsDotFile(entry))
182 {
183 continue;
184 }
185
169 using (Stream dataStream = file.GetInputStream(entry)) 186 using (Stream dataStream = file.GetInputStream(entry))
170 { 187 {
171 races.Add(CreateRaceFromStream(file, dataStream)); 188 races.Add(CreateRaceFromStream(file, dataStream));
172 } 189 }
173 } 190 }
183 ICollection<ZipEntry> dataStreams = GetGameSystemZipEntries(file); 200 ICollection<ZipEntry> dataStreams = GetGameSystemZipEntries(file);
184 ICollection<GameSystem> systems = new List<GameSystem>(); 201 ICollection<GameSystem> systems = new List<GameSystem>();
185 202
186 foreach (ZipEntry entry in dataStreams) 203 foreach (ZipEntry entry in dataStreams)
187 { 204 {
205 if (IsDotFile(entry))
206 {
207 continue;
208 }
209
188 using (Stream dataStream = file.GetInputStream(entry)) 210 using (Stream dataStream = file.GetInputStream(entry))
189 { 211 {
190 systems.Add(CreateGameSystemFromStream(file, dataStream)); 212 systems.Add(CreateGameSystemFromStream(file, dataStream));
191 } 213 }
192 } 214 }