changeset 417:2a36ebb7b6a9

Re #358: Handle factory.CreateObjectsFromFile where GetFileAsSupportedType returns null * Refactor out methods for file loading * Add "if zip file not null" check around newly refactored method
author IBBoard <dev@ibboard.co.uk>
date Tue, 06 Sep 2011 20:36:06 +0100
parents c70973b65090
children 6798a59c1b49
files API/Factories/AbstractNativeWarFoundryFactory.cs
diffstat 1 files changed, 52 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/API/Factories/AbstractNativeWarFoundryFactory.cs	Sun Sep 04 20:43:44 2011 +0100
+++ b/API/Factories/AbstractNativeWarFoundryFactory.cs	Tue Sep 06 20:36:06 2011 +0100
@@ -15,6 +15,7 @@
 using IBBoard.Xml;
 using IBBoard.WarFoundry.API.Objects;
 using ICSharpCode.SharpZipLib.Zip;
+using IBBoard.Collections;
 
 namespace IBBoard.WarFoundry.API.Factories
 {
@@ -31,9 +32,9 @@
 		protected override ZipFile GetFileAsSupportedType (FileInfo file)
 		{
 			ZipFile zip = null;
-			string ext = file.Extension;
+			string ext = file.Extension.ToLower();
 
-			if (ext == "race" || ext == "army" || ext == "system")
+			if (ext == ".race" || ext == ".army" || ext == ".system")
 			{
 				try
 				{
@@ -81,41 +82,62 @@
 		{
 			ICollection<IWarFoundryObject> objects = new List<IWarFoundryObject>();
 
+			if (file != null)
+			{
+				DoCreateObjectsFromZip(file, objects);
+			}
+
+			return objects;
+		}
+
+		private void DoCreateObjectsFromZip(ZipFile file, ICollection<IWarFoundryObject> objects)
+		{
 			try
 			{
-				if (CheckCanFindSystemFileContent(file))
-				{
-					foreach (GameSystem system in CreateGameSystemsFromFile(file))
-					{
-						OnGameSystemLoaded(system);
-						objects.Add(system);
-					}
-				}
-				
-				if (CheckCanFindRaceFileContent(file))
-				{
-					foreach(Race race in CreateRacesFromFile(file))
-					{
-						OnRaceLoaded(race);
-						objects.Add(race);
-					}
-				}
-				
-				if (CheckCanFindArmyFileContent(file))
-				{
-					foreach (Army army in CreateArmiesFromFile(file))
-					{
-						OnArmyLoaded(army);
-						objects.Add(army);
-					}
-				}
+				CreateSystemObjectsFromZip (file, objects);
+				CreateRaceObjectsFromZip (file, objects);
+				CreateArmyObjectsFromZip (file, objects);
 			}
 			finally
 			{
 				file.Close();
 			}
-			
-			return objects;
+		}
+
+		private void CreateSystemObjectsFromZip (ZipFile file, ICollection<IWarFoundryObject> objects)
+		{
+			if (CheckCanFindSystemFileContent(file))
+			{
+				foreach (GameSystem system in CreateGameSystemsFromFile(file))
+				{
+					OnGameSystemLoaded(system);
+					objects.Add(system);
+				}
+			}
+		}
+
+		void CreateRaceObjectsFromZip (ZipFile file, ICollection<IWarFoundryObject> objects)
+		{
+			if (CheckCanFindRaceFileContent(file))
+			{
+				foreach(Race race in CreateRacesFromFile(file))
+				{
+					OnRaceLoaded(race);
+					objects.Add(race);
+				}
+			}
+		}
+
+		void CreateArmyObjectsFromZip (ZipFile file, ICollection<IWarFoundryObject> objects)
+		{
+			if (CheckCanFindArmyFileContent(file))
+			{
+				foreach (Army army in CreateArmiesFromFile(file))
+				{
+					OnArmyLoaded(army);
+					objects.Add(army);
+				}
+			}
 		}
 		
 		protected ICollection<Army> CreateArmiesFromFile(ZipFile file)