changeset 312:3854c26073c4

Re #253: Allow multiple data files in a single zip * Rebuild file loading to start to allow multiple files in a native file
author IBBoard <dev@ibboard.co.uk>
date Sat, 26 Feb 2011 20:15:12 +0000
parents 5434e648379c
children f00a57369aaa
files api/Factories/AbstractNativeWarFoundryFactory.cs api/Factories/Xml/WarFoundryXmlFactory.cs
diffstat 2 files changed, 75 insertions(+), 51 deletions(-) [+]
line diff
     1.1 --- a/api/Factories/AbstractNativeWarFoundryFactory.cs	Wed Feb 23 20:44:37 2011 +0000
     1.2 +++ b/api/Factories/AbstractNativeWarFoundryFactory.cs	Sat Feb 26 20:15:12 2011 +0000
     1.3 @@ -76,22 +76,21 @@
     1.4  		
     1.5  		protected override ICollection<IWarFoundryObject> DoCreateObjectsFromFile (ZipFile file)
     1.6  		{
     1.7 -			ICollection<IWarFoundryObject> objects = null;
     1.8 -			IWarFoundryObject obj = null;
     1.9 +			ICollection<IWarFoundryObject> objects = new List<IWarFoundryObject>();
    1.10  
    1.11  			try
    1.12  			{
    1.13  				if (CheckCanFindSystemFileContent(file))
    1.14  				{
    1.15 -					obj = CreateGameSystemFromFile(file);
    1.16 +					AddAll(objects, CreateGameSystemsFromFile(file));
    1.17  				}
    1.18 -				else if (CheckCanFindRaceFileContent(file))
    1.19 +				if (CheckCanFindRaceFileContent(file))
    1.20  				{
    1.21 -					obj = CreateRaceFromFile(file);
    1.22 +					AddAll(objects, CreateRacesFromFile(file));
    1.23  				}
    1.24 -				else if (CheckCanFindArmyFileContent(file))
    1.25 +				if (CheckCanFindArmyFileContent(file))
    1.26  				{
    1.27 -					obj = CreateArmyFromFile(file);
    1.28 +					AddAll(objects, CreateArmiesFromFile(file));
    1.29  				}
    1.30  			}
    1.31  			finally
    1.32 @@ -99,64 +98,72 @@
    1.33  				file.Close();
    1.34  			}
    1.35  			
    1.36 -			if (obj!=null)
    1.37 -			{
    1.38 -				objects = new List<IWarFoundryObject>();
    1.39 -				objects.Add(obj);
    1.40 -			}
    1.41 -			
    1.42  			return objects;
    1.43  		}
    1.44  		
    1.45 -		protected Army CreateArmyFromFile(ZipFile file)
    1.46 +		private void AddAll <NEW_ITEM_TYPE>(ICollection<IWarFoundryObject> collection, ICollection<NEW_ITEM_TYPE> newItems) where NEW_ITEM_TYPE : IWarFoundryObject
    1.47  		{
    1.48 -			Stream dataStream = GetArmyDataStream(file);
    1.49 -
    1.50 -			try
    1.51 +			foreach (IWarFoundryObject obj in newItems)
    1.52  			{
    1.53 -				return CreateArmyFromStream(file, dataStream);
    1.54 -			}
    1.55 -			finally
    1.56 -			{
    1.57 -				dataStream.Close();
    1.58 +				collection.Add(obj);
    1.59  			}
    1.60  		}
    1.61  		
    1.62 -		protected abstract Stream GetArmyDataStream(ZipFile file);
    1.63 +		protected ICollection<Army> CreateArmiesFromFile(ZipFile file)
    1.64 +		{
    1.65 +			ICollection<ZipEntry> dataStreams = GetArmyZipEntries(file);
    1.66 +			ICollection<Army> armies = new List<Army>();
    1.67 +
    1.68 +			foreach (ZipEntry entry in dataStreams)
    1.69 +			{
    1.70 +				using (Stream dataStream = file.GetInputStream(entry))
    1.71 +				{
    1.72 +				armies.Add(CreateArmyFromStream(file, dataStream));
    1.73 +				}
    1.74 +			}
    1.75 +			
    1.76 +			return armies;
    1.77 +		}
    1.78 +		
    1.79 +		protected abstract ICollection<ZipEntry> GetArmyZipEntries(ZipFile file);
    1.80  		protected abstract Army CreateArmyFromStream(ZipFile file, Stream dataStream);
    1.81  		
    1.82 -		protected Race CreateRaceFromFile(ZipFile file)
    1.83 +		protected ICollection<Race> CreateRacesFromFile(ZipFile file)
    1.84  		{
    1.85 -			Stream dataStream = GetRaceDataStream(file);
    1.86 +			ICollection<ZipEntry> dataStreams = GetRaceZipEntries(file);
    1.87 +			ICollection<Race> races = new List<Race>();
    1.88  
    1.89 -			try
    1.90 -			{
    1.91 -				return CreateRaceFromStream(file, dataStream);
    1.92 +			foreach (ZipEntry entry in dataStreams)
    1.93 +			{					
    1.94 +				using (Stream dataStream = file.GetInputStream(entry))
    1.95 +				{
    1.96 +					races.Add(CreateRaceFromStream(file, dataStream));
    1.97 +				}
    1.98  			}
    1.99 -			finally
   1.100 -			{
   1.101 -				dataStream.Close();
   1.102 -			}
   1.103 +			
   1.104 +			return races;
   1.105  		}
   1.106  		
   1.107 -		protected abstract Stream GetRaceDataStream(ZipFile file);
   1.108 +		protected abstract ICollection<ZipEntry> GetRaceZipEntries(ZipFile file);
   1.109  		protected abstract Race CreateRaceFromStream(ZipFile file, Stream dataStream);
   1.110  		
   1.111 -		protected GameSystem CreateGameSystemFromFile(ZipFile file)
   1.112 +		protected ICollection<GameSystem> CreateGameSystemsFromFile(ZipFile file)
   1.113  		{
   1.114 -			Stream dataStream = GetGameSystemDataStream(file);
   1.115 +			ICollection<ZipEntry> dataStreams = GetGameSystemZipEntries(file);
   1.116 +			ICollection<GameSystem> systems = new List<GameSystem>();
   1.117  
   1.118 -			try
   1.119 +			foreach (ZipEntry entry in dataStreams)
   1.120  			{
   1.121 -				return CreateGameSystemFromStream(file, dataStream);
   1.122 +				using (Stream dataStream = file.GetInputStream(entry))
   1.123 +				{
   1.124 +					systems.Add(CreateGameSystemFromStream(file, dataStream));
   1.125 +				}
   1.126  			}
   1.127 -			finally
   1.128 -			{
   1.129 -				dataStream.Close();
   1.130 -			}
   1.131 +			
   1.132 +			return systems;
   1.133  		}
   1.134  		
   1.135 -		protected abstract Stream GetGameSystemDataStream(ZipFile file);
   1.136 +		protected abstract ICollection<ZipEntry> GetGameSystemZipEntries(ZipFile file);
   1.137  		protected abstract GameSystem CreateGameSystemFromStream(ZipFile file, Stream dataStream);
   1.138  		
   1.139  		public override bool Equals (object o)
     2.1 --- a/api/Factories/Xml/WarFoundryXmlFactory.cs	Wed Feb 23 20:44:37 2011 +0000
     2.2 +++ b/api/Factories/Xml/WarFoundryXmlFactory.cs	Sat Feb 26 20:15:12 2011 +0000
     2.3 @@ -17,6 +17,7 @@
     2.4  using IBBoard.WarFoundry.API.Requirements;
     2.5  using IBBoard.WarFoundry.API.Objects;
     2.6  using ICSharpCode.SharpZipLib.Zip;
     2.7 +using System.Text.RegularExpressions;
     2.8  
     2.9  namespace IBBoard.WarFoundry.API.Factories.Xml
    2.10  {
    2.11 @@ -64,22 +65,38 @@
    2.12  		
    2.13  		protected override bool CheckCanFindArmyFileContent(ZipFile file)
    2.14  		{
    2.15 -			return file.FindEntry("data.armyx", true) > -1;
    2.16 +			return FindEntries(file, "*.armyx").Count > 0;
    2.17  		}
    2.18  		
    2.19  		protected override bool CheckCanFindSystemFileContent(ZipFile file)
    2.20  		{
    2.21 -			return file.FindEntry("data.systemx", true) > -1;
    2.22 +			return FindEntries(file, "*.systemx").Count > 0;
    2.23  		}
    2.24  		
    2.25  		protected override bool CheckCanFindRaceFileContent(ZipFile file)
    2.26  		{
    2.27 -			return file.FindEntry("data.racex", true) > -1;
    2.28 +			return FindEntries(file, "*.racex").Count > 0;
    2.29  		}
    2.30  		
    2.31 -		protected override Stream GetArmyDataStream(ZipFile file)
    2.32 +		protected override ICollection<ZipEntry> GetArmyZipEntries(ZipFile file)
    2.33  		{
    2.34 -			return file.GetInputStream(file.FindEntry("data.armyx", true));
    2.35 +			return FindEntries(file, "*.armyx");
    2.36 +		}
    2.37 +		
    2.38 +		private ICollection<ZipEntry> FindEntries(ZipFile file, string wildcardPattern)
    2.39 +		{
    2.40 +			Regex re = new Regex("^" + Regex.Escape(wildcardPattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$", RegexOptions.IgnoreCase | RegexOptions.Singleline);
    2.41 +			ICollection<ZipEntry> entries = new List<ZipEntry>();
    2.42 +				
    2.43 +			foreach (ZipEntry entry in file)
    2.44 +			{
    2.45 +				if (re.IsMatch(entry.Name))
    2.46 +				{
    2.47 +					entries.Add(entry);
    2.48 +				}
    2.49 +			}
    2.50 +			
    2.51 +			return entries;
    2.52  		}
    2.53  		
    2.54  		protected override Army CreateArmyFromStream (ZipFile file, Stream dataStream)
    2.55 @@ -102,9 +119,9 @@
    2.56  			return elem;
    2.57  		}
    2.58  
    2.59 -		protected override Stream GetGameSystemDataStream (ZipFile file)
    2.60 +		protected override ICollection<ZipEntry> GetGameSystemZipEntries(ZipFile file)
    2.61  		{
    2.62 -			return file.GetInputStream(file.FindEntry("data.systemx", true));
    2.63 +			return FindEntries(file, "*.systemx");
    2.64  		}
    2.65  		
    2.66  		protected override GameSystem CreateGameSystemFromStream (ZipFile file, Stream dataStream)
    2.67 @@ -114,9 +131,9 @@
    2.68  			return gameSystemFactory.CreateSystemFromElement(file, elem);
    2.69  		}
    2.70  		
    2.71 -		protected override Stream GetRaceDataStream (ZipFile file)
    2.72 +		protected override ICollection<ZipEntry> GetRaceZipEntries(ZipFile file)
    2.73  		{
    2.74 -			return file.GetInputStream(file.FindEntry("data.racex", true));
    2.75 +			return FindEntries(file, "*.racex");
    2.76  		}
    2.77  		
    2.78  		protected override Race CreateRaceFromStream (ZipFile file, Stream dataStream)