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 diff
     1.1 --- a/api/Factories/AbstractNativeWarFoundryFactory.cs	Sat Sep 26 10:43:28 2009 +0000
     1.2 +++ b/api/Factories/AbstractNativeWarFoundryFactory.cs	Sat Sep 26 18:48:36 2009 +0000
     1.3 @@ -101,8 +101,8 @@
     1.4  			{
     1.5  				objects = new List<IWarFoundryObject>();
     1.6  				objects.Add(obj);
     1.7 -			}
     1.8 -
     1.9 +			}
    1.10 +
    1.11  			file.Close();
    1.12  			
    1.13  			return objects;
    1.14 @@ -110,19 +110,19 @@
    1.15  		
    1.16  		protected Army CreateArmyFromFile(ZipFile file)
    1.17  		{
    1.18 -			Stream dataStream = GetArmyDataStream(file);
    1.19 -
    1.20 -			try
    1.21 -			{
    1.22 -				return CreateArmyFromStream(file, dataStream);
    1.23 -			}
    1.24 -			catch (InvalidFileException ex)
    1.25 -			{
    1.26 -				throw new InvalidFileException("Data file " + file.Name + " was not a valid army file", ex);
    1.27 -			}
    1.28 -			finally
    1.29 -			{
    1.30 -				dataStream.Close();
    1.31 +			Stream dataStream = GetArmyDataStream(file);
    1.32 +
    1.33 +			try
    1.34 +			{
    1.35 +				return CreateArmyFromStream(file, dataStream);
    1.36 +			}
    1.37 +			catch (InvalidFileException ex)
    1.38 +			{
    1.39 +				throw new InvalidFileException("Data file " + file.Name + " was not a valid army file", ex);
    1.40 +			}
    1.41 +			finally
    1.42 +			{
    1.43 +				dataStream.Close();
    1.44  			}
    1.45  		}
    1.46  		
    1.47 @@ -130,7 +130,7 @@
    1.48  		protected abstract Army CreateArmyFromStream(ZipFile file, Stream dataStream);
    1.49  		
    1.50  		protected Race CreateRaceFromFile(ZipFile file)
    1.51 -		{
    1.52 +		{
    1.53  			Stream dataStream = GetRaceDataStream(file);
    1.54  
    1.55  			try
    1.56 @@ -139,11 +139,11 @@
    1.57  			}
    1.58  			catch (InvalidFileException ex)
    1.59  			{
    1.60 -				throw new InvalidFileException("Data file "+file.Name+" was not a valid race file", ex);
    1.61 -			}
    1.62 -			finally
    1.63 -			{
    1.64 -				dataStream.Close();
    1.65 +				throw new InvalidFileException("Data file "+file.Name+" was not a valid race file", ex);
    1.66 +			}
    1.67 +			finally
    1.68 +			{
    1.69 +				dataStream.Close();
    1.70  			}
    1.71  		}
    1.72  		
    1.73 @@ -151,20 +151,20 @@
    1.74  		protected abstract Race CreateRaceFromStream(ZipFile file, Stream dataStream);
    1.75  		
    1.76  		protected GameSystem CreateGameSystemFromFile(ZipFile file)
    1.77 -		{
    1.78 -			Stream dataStream = GetGameSystemDataStream(file);
    1.79 -
    1.80 -			try
    1.81 -			{
    1.82 -				return CreateGameSystemFromStream(file, dataStream);
    1.83 -			}
    1.84 -			catch (InvalidFileException ex)
    1.85 -			{
    1.86 -				throw new InvalidFileException("Data file " + file.Name + " was not a valid game system file", ex);
    1.87 -			}
    1.88 -			finally
    1.89 -			{
    1.90 -				dataStream.Close();
    1.91 +		{
    1.92 +			Stream dataStream = GetGameSystemDataStream(file);
    1.93 +
    1.94 +			try
    1.95 +			{
    1.96 +				return CreateGameSystemFromStream(file, dataStream);
    1.97 +			}
    1.98 +			catch (InvalidFileException ex)
    1.99 +			{
   1.100 +				throw new InvalidFileException("Data file " + file.Name + " was not a valid game system file", ex);
   1.101 +			}
   1.102 +			finally
   1.103 +			{
   1.104 +				dataStream.Close();
   1.105  			}
   1.106  		}
   1.107  		
     2.1 --- a/api/Factories/AbstractWarFoundryFactory.cs	Sat Sep 26 10:43:28 2009 +0000
     2.2 +++ b/api/Factories/AbstractWarFoundryFactory.cs	Sat Sep 26 18:48:36 2009 +0000
     2.3 @@ -19,26 +19,59 @@
     2.4  			
     2.5  		public bool CanHandleFileFormat (FileInfo file)
     2.6  		{
     2.7 -			FILE_TYPE typedFile = GetFileAsSupportedType(file);
     2.8 -			return typedFile != null && CheckCanHandleFileFormat(typedFile);
     2.9 +			FILE_TYPE typedFile = GetFileAsSupportedType(file);
    2.10 +			bool canHandle = typedFile != null && CheckCanHandleFileFormat(typedFile);
    2.11 +
    2.12 +			if (typedFile != null)
    2.13 +			{
    2.14 +				CleanUpFileAsSupportedType(typedFile);
    2.15 +			}
    2.16 +
    2.17 +			return canHandle;
    2.18  		}
    2.19  
    2.20  		public bool CanHandleFileAsRace(FileInfo file)
    2.21  		{
    2.22 -			FILE_TYPE typedFile = GetFileAsSupportedType(file);
    2.23 -			return typedFile != null && CheckCanHandleFileAsRace(typedFile);
    2.24 +			FILE_TYPE typedFile = GetFileAsSupportedType(file);
    2.25 +			bool canHandle = typedFile != null && CheckCanHandleFileAsRace(typedFile);
    2.26 +
    2.27 +			if (typedFile != null)
    2.28 +			{
    2.29 +				CleanUpFileAsSupportedType(typedFile);
    2.30 +			}
    2.31 +
    2.32 +			return canHandle;
    2.33  		}
    2.34  
    2.35  		public bool CanHandleFileAsGameSystem(FileInfo file)
    2.36  		{
    2.37 -			FILE_TYPE typedFile = GetFileAsSupportedType(file);
    2.38 -			return typedFile != null && CheckCanHandleFileAsGameSystem(typedFile);
    2.39 +			FILE_TYPE typedFile = GetFileAsSupportedType(file);
    2.40 +			bool canHandle = typedFile != null && CheckCanHandleFileAsGameSystem(typedFile);
    2.41 +
    2.42 +			if (typedFile != null)
    2.43 +			{
    2.44 +				CleanUpFileAsSupportedType(typedFile);
    2.45 +			}
    2.46 +
    2.47 +			return canHandle;
    2.48  		}
    2.49  
    2.50  		public bool CanHandleFileAsArmy(FileInfo file)
    2.51  		{
    2.52  			FILE_TYPE typedFile = GetFileAsSupportedType(file);
    2.53 -			return typedFile != null && CheckCanHandleFileAsArmy(typedFile);
    2.54 +			bool canHandle = typedFile != null && CheckCanHandleFileAsArmy(typedFile);
    2.55 +
    2.56 +			if (typedFile != null)
    2.57 +			{
    2.58 +				CleanUpFileAsSupportedType(typedFile);
    2.59 +			}
    2.60 +
    2.61 +			return canHandle;
    2.62 +		}
    2.63 +
    2.64 +		protected virtual void CleanUpFileAsSupportedType(FILE_TYPE typedFile)
    2.65 +		{
    2.66 +			//Do nothing by default
    2.67  		}
    2.68  		
    2.69  		/// <summary>
     3.1 --- a/api/Factories/Xml/WarFoundryXmlFactory.cs	Sat Sep 26 10:43:28 2009 +0000
     3.2 +++ b/api/Factories/Xml/WarFoundryXmlFactory.cs	Sat Sep 26 18:48:36 2009 +0000
     3.3 @@ -108,7 +108,12 @@
     3.4  		{
     3.5  			XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.RACE_ELEMENT);
     3.6  			LogNotifier.Debug(GetType(), "Create Race");
     3.7 -			return raceFactory.CreateRaceFromElement(file, elem);
     3.8 +			return raceFactory.CreateRaceFromElement(file, elem);
     3.9 +		}
    3.10 +
    3.11 +		protected override void CleanUpFileAsSupportedType(ZipFile typedFile)
    3.12 +		{
    3.13 +			typedFile.Close();
    3.14  		}
    3.15  
    3.16  		public override void CompleteLoading(IWarFoundryStagedLoadObject obj)
     4.1 --- a/api/Objects/Unit.cs	Sat Sep 26 10:43:28 2009 +0000
     4.2 +++ b/api/Objects/Unit.cs	Sat Sep 26 18:48:36 2009 +0000
     4.3 @@ -86,11 +86,11 @@
     4.4  					if (CanEquipWithItem(unitEquip))
     4.5  					{
     4.6  						if (unitEquip.IsRatioLimit)
     4.7 -						{
     4.8 +						{
     4.9  							SetEquipmentRatio(unitEquip, unitEquip.MinPercentage);
    4.10  						}
    4.11  						else
    4.12 -						{
    4.13 +						{
    4.14  							SetEquipmentAmount(unitEquip, unitEquip.MinNumber);
    4.15  						}
    4.16  					}