changeset 151:1d13820b3d96

Fixes #176: Bug when saving recently edited army * Add loaded file cleanup to AbstractWarFoundryFactory * Add override of method with Zip reference closing to WarFoundryXmlFactory WarFoundry now no longer ends up with trailing handles to files, although why they only caused problems in some situations is unknown Also: * Some line ending fixes (curse cross-platform development and different line terminators!)
author IBBoard <dev@ibboard.co.uk>
date Sat, 26 Sep 2009 18:48:36 +0000
parents b36cc4af435b
children 0c0e14f03785
files api/Factories/AbstractNativeWarFoundryFactory.cs api/Factories/AbstractWarFoundryFactory.cs api/Factories/Xml/WarFoundryXmlFactory.cs api/Objects/Unit.cs
diffstat 4 files changed, 83 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/api/Factories/AbstractNativeWarFoundryFactory.cs	Sat Sep 26 10:43:28 2009 +0000
+++ b/api/Factories/AbstractNativeWarFoundryFactory.cs	Sat Sep 26 18:48:36 2009 +0000
@@ -101,8 +101,8 @@
 			{
 				objects = new List<IWarFoundryObject>();
 				objects.Add(obj);
-			}
-
+			}
+
 			file.Close();
 			
 			return objects;
@@ -110,19 +110,19 @@
 		
 		protected Army CreateArmyFromFile(ZipFile file)
 		{
-			Stream dataStream = GetArmyDataStream(file);
-
-			try
-			{
-				return CreateArmyFromStream(file, dataStream);
-			}
-			catch (InvalidFileException ex)
-			{
-				throw new InvalidFileException("Data file " + file.Name + " was not a valid army file", ex);
-			}
-			finally
-			{
-				dataStream.Close();
+			Stream dataStream = GetArmyDataStream(file);
+
+			try
+			{
+				return CreateArmyFromStream(file, dataStream);
+			}
+			catch (InvalidFileException ex)
+			{
+				throw new InvalidFileException("Data file " + file.Name + " was not a valid army file", ex);
+			}
+			finally
+			{
+				dataStream.Close();
 			}
 		}
 		
@@ -130,7 +130,7 @@
 		protected abstract Army CreateArmyFromStream(ZipFile file, Stream dataStream);
 		
 		protected Race CreateRaceFromFile(ZipFile file)
-		{
+		{
 			Stream dataStream = GetRaceDataStream(file);
 
 			try
@@ -139,11 +139,11 @@
 			}
 			catch (InvalidFileException ex)
 			{
-				throw new InvalidFileException("Data file "+file.Name+" was not a valid race file", ex);
-			}
-			finally
-			{
-				dataStream.Close();
+				throw new InvalidFileException("Data file "+file.Name+" was not a valid race file", ex);
+			}
+			finally
+			{
+				dataStream.Close();
 			}
 		}
 		
@@ -151,20 +151,20 @@
 		protected abstract Race CreateRaceFromStream(ZipFile file, Stream dataStream);
 		
 		protected GameSystem CreateGameSystemFromFile(ZipFile file)
-		{
-			Stream dataStream = GetGameSystemDataStream(file);
-
-			try
-			{
-				return CreateGameSystemFromStream(file, dataStream);
-			}
-			catch (InvalidFileException ex)
-			{
-				throw new InvalidFileException("Data file " + file.Name + " was not a valid game system file", ex);
-			}
-			finally
-			{
-				dataStream.Close();
+		{
+			Stream dataStream = GetGameSystemDataStream(file);
+
+			try
+			{
+				return CreateGameSystemFromStream(file, dataStream);
+			}
+			catch (InvalidFileException ex)
+			{
+				throw new InvalidFileException("Data file " + file.Name + " was not a valid game system file", ex);
+			}
+			finally
+			{
+				dataStream.Close();
 			}
 		}
 		
--- a/api/Factories/AbstractWarFoundryFactory.cs	Sat Sep 26 10:43:28 2009 +0000
+++ b/api/Factories/AbstractWarFoundryFactory.cs	Sat Sep 26 18:48:36 2009 +0000
@@ -19,26 +19,59 @@
 			
 		public bool CanHandleFileFormat (FileInfo file)
 		{
-			FILE_TYPE typedFile = GetFileAsSupportedType(file);
-			return typedFile != null && CheckCanHandleFileFormat(typedFile);
+			FILE_TYPE typedFile = GetFileAsSupportedType(file);
+			bool canHandle = typedFile != null && CheckCanHandleFileFormat(typedFile);
+
+			if (typedFile != null)
+			{
+				CleanUpFileAsSupportedType(typedFile);
+			}
+
+			return canHandle;
 		}
 
 		public bool CanHandleFileAsRace(FileInfo file)
 		{
-			FILE_TYPE typedFile = GetFileAsSupportedType(file);
-			return typedFile != null && CheckCanHandleFileAsRace(typedFile);
+			FILE_TYPE typedFile = GetFileAsSupportedType(file);
+			bool canHandle = typedFile != null && CheckCanHandleFileAsRace(typedFile);
+
+			if (typedFile != null)
+			{
+				CleanUpFileAsSupportedType(typedFile);
+			}
+
+			return canHandle;
 		}
 
 		public bool CanHandleFileAsGameSystem(FileInfo file)
 		{
-			FILE_TYPE typedFile = GetFileAsSupportedType(file);
-			return typedFile != null && CheckCanHandleFileAsGameSystem(typedFile);
+			FILE_TYPE typedFile = GetFileAsSupportedType(file);
+			bool canHandle = typedFile != null && CheckCanHandleFileAsGameSystem(typedFile);
+
+			if (typedFile != null)
+			{
+				CleanUpFileAsSupportedType(typedFile);
+			}
+
+			return canHandle;
 		}
 
 		public bool CanHandleFileAsArmy(FileInfo file)
 		{
 			FILE_TYPE typedFile = GetFileAsSupportedType(file);
-			return typedFile != null && CheckCanHandleFileAsArmy(typedFile);
+			bool canHandle = typedFile != null && CheckCanHandleFileAsArmy(typedFile);
+
+			if (typedFile != null)
+			{
+				CleanUpFileAsSupportedType(typedFile);
+			}
+
+			return canHandle;
+		}
+
+		protected virtual void CleanUpFileAsSupportedType(FILE_TYPE typedFile)
+		{
+			//Do nothing by default
 		}
 		
 		/// <summary>
--- a/api/Factories/Xml/WarFoundryXmlFactory.cs	Sat Sep 26 10:43:28 2009 +0000
+++ b/api/Factories/Xml/WarFoundryXmlFactory.cs	Sat Sep 26 18:48:36 2009 +0000
@@ -108,7 +108,12 @@
 		{
 			XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.RACE_ELEMENT);
 			LogNotifier.Debug(GetType(), "Create Race");
-			return raceFactory.CreateRaceFromElement(file, elem);
+			return raceFactory.CreateRaceFromElement(file, elem);
+		}
+
+		protected override void CleanUpFileAsSupportedType(ZipFile typedFile)
+		{
+			typedFile.Close();
 		}
 
 		public override void CompleteLoading(IWarFoundryStagedLoadObject obj)
--- a/api/Objects/Unit.cs	Sat Sep 26 10:43:28 2009 +0000
+++ b/api/Objects/Unit.cs	Sat Sep 26 18:48:36 2009 +0000
@@ -86,11 +86,11 @@
 					if (CanEquipWithItem(unitEquip))
 					{
 						if (unitEquip.IsRatioLimit)
-						{
+						{
 							SetEquipmentRatio(unitEquip, unitEquip.MinPercentage);
 						}
 						else
-						{
+						{
 							SetEquipmentAmount(unitEquip, unitEquip.MinNumber);
 						}
 					}