diff 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
line wrap: on
line diff
--- a/API/Factories/AbstractNativeWarFoundryFactory.cs	Sat Dec 03 20:10:13 2011 +0000
+++ b/API/Factories/AbstractNativeWarFoundryFactory.cs	Sun Dec 04 20:40:31 2011 +0000
@@ -29,22 +29,22 @@
 			//Do nothing - just make the constructor non-public
 		}
 				
-		protected override ZipFile GetFileAsSupportedType (FileInfo file)
+		protected override ZipFile GetFileAsSupportedType(FileInfo file)
 		{
 			ZipFile zip = null;
 			string ext = file.Extension.ToLower();
 
-			if (ext == ".race" || ext == ".army" || ext == ".system")
+			if (ext == ".race" || ext == ".army" || ext == ".system")
 			{
-				try
+				try
 				{
 					zip = new ZipFile(file.FullName);
-				}
-				catch (ZipException)
+				}
+				catch (ZipException)
 				{
 					//Silently dispose as per spec for the method
-				}
-				catch (IOException)
+				}
+				catch (IOException)
 				{
 					//Silently dispose as per spec for the method
 				}
@@ -147,14 +147,26 @@
 
 			foreach (ZipEntry entry in dataStreams)
 			{
+				if (IsDotFile(entry))
+				{
+					continue;
+				}
+
 				using (Stream dataStream = file.GetInputStream(entry))
 				{
-				armies.Add(CreateArmyFromStream(file, dataStream));
+					armies.Add(CreateArmyFromStream(file, dataStream));
 				}
 			}
 			
 			return armies;
 		}
+
+		private bool IsDotFile(ZipEntry entry)
+		{
+			string name = entry.Name;
+			int slashIdx = name.LastIndexOf('/');
+			return (slashIdx != -1 && slashIdx == name.LastIndexOf("/.")) || (name.Length > 0 && name[0] == '.');
+		}
 		
 		protected abstract ICollection<ZipEntry> GetArmyZipEntries(ZipFile file);
 		protected abstract Army CreateArmyFromStream(ZipFile file, Stream dataStream);
@@ -165,7 +177,12 @@
 			ICollection<Race> races = new List<Race>();
 
 			foreach (ZipEntry entry in dataStreams)
-			{					
+			{
+				if (IsDotFile(entry))
+				{
+					continue;
+				}
+
 				using (Stream dataStream = file.GetInputStream(entry))
 				{
 					races.Add(CreateRaceFromStream(file, dataStream));
@@ -185,6 +202,11 @@
 
 			foreach (ZipEntry entry in dataStreams)
 			{
+				if (IsDotFile(entry))
+				{
+					continue;
+				}
+
 				using (Stream dataStream = file.GetInputStream(entry))
 				{
 					systems.Add(CreateGameSystemFromStream(file, dataStream));