changeset 313:f00a57369aaa

Re #253: Allow multiple data files in a single zip * Add event-based mechanism to allow GameSystem to be registered before Race is loaded from a single zip
author IBBoard <dev@ibboard.co.uk>
date Sun, 27 Feb 2011 15:54:13 +0000
parents 3854c26073c4
children bd5d8bfe18a6
files api/AbstractWarFoundryLoader.cs api/Factories/AbstractNativeWarFoundryFactory.cs api/Factories/AbstractNonNativeFileExtensionWarFoundryFactory.cs api/Factories/AbstractWarFoundryFactory.cs api/Factories/DummyWarFoundryFactory.cs api/Factories/IWarFoundryFactory.cs
diffstat 6 files changed, 87 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/api/AbstractWarFoundryLoader.cs	Sat Feb 26 20:15:12 2011 +0000
+++ b/api/AbstractWarFoundryLoader.cs	Sun Feb 27 15:54:13 2011 +0000
@@ -341,18 +341,11 @@
 
 		private bool LoadObject(FileInfo file, IWarFoundryFactory factory)
 		{
-			bool loaded = false;
-			
 			LogNotifier.DebugFormat(GetType(), "Loading {0} using {1}", file.FullName, factory.GetType().Name);
+			factory.RaceLoaded+= StoreRace;
+			factory.GameSystemLoaded+= StoreGameSystem;
 			ICollection<IWarFoundryObject> objects = factory.CreateObjectsFromFile(file);
-			
-			if (objects.Count > 0)
-			{
-				AddLoadedObjects(objects, factory);
-				loaded = true;
-			}
-
-			return loaded;
+			return objects.Count > 0;
 		}
 		
 		
--- a/api/Factories/AbstractNativeWarFoundryFactory.cs	Sat Feb 26 20:15:12 2011 +0000
+++ b/api/Factories/AbstractNativeWarFoundryFactory.cs	Sun Feb 27 15:54:13 2011 +0000
@@ -82,15 +82,29 @@
 			{
 				if (CheckCanFindSystemFileContent(file))
 				{
-					AddAll(objects, CreateGameSystemsFromFile(file));
+					foreach (GameSystem system in CreateGameSystemsFromFile(file))
+					{
+						OnGameSystemLoaded(system);
+						objects.Add(system);
+					}
 				}
+				
 				if (CheckCanFindRaceFileContent(file))
 				{
-					AddAll(objects, CreateRacesFromFile(file));
+					foreach(Race race in CreateRacesFromFile(file))
+					{
+						OnRaceLoaded(race);
+						objects.Add(race);
+					}
 				}
+				
 				if (CheckCanFindArmyFileContent(file))
 				{
-					AddAll(objects, CreateArmiesFromFile(file));
+					foreach (Army army in CreateArmiesFromFile(file))
+					{
+						OnArmyLoaded(army);
+						objects.Add(army);
+					}
 				}
 			}
 			finally
@@ -101,14 +115,6 @@
 			return objects;
 		}
 		
-		private void AddAll <NEW_ITEM_TYPE>(ICollection<IWarFoundryObject> collection, ICollection<NEW_ITEM_TYPE> newItems) where NEW_ITEM_TYPE : IWarFoundryObject
-		{
-			foreach (IWarFoundryObject obj in newItems)
-			{
-				collection.Add(obj);
-			}
-		}
-		
 		protected ICollection<Army> CreateArmiesFromFile(ZipFile file)
 		{
 			ICollection<ZipEntry> dataStreams = GetArmyZipEntries(file);
--- a/api/Factories/AbstractNonNativeFileExtensionWarFoundryFactory.cs	Sat Feb 26 20:15:12 2011 +0000
+++ b/api/Factories/AbstractNonNativeFileExtensionWarFoundryFactory.cs	Sun Feb 27 15:54:13 2011 +0000
@@ -47,25 +47,38 @@
 		
 		protected override ICollection<IWarFoundryObject> DoCreateObjectsFromFile (FileInfo file)
 		{
-			ICollection<IWarFoundryObject> objects = new List<IWarFoundryObject>();
+			IWarFoundryObject obj = null;
 			
-			if (CheckCanHandleFileAsRace(file))
+			if (CheckCanHandleFileAsGameSystem(file))
 			{
-				objects.Add(CreateRaceFromFile(file));
+				GameSystem gameSystem = CreateGameSystemFromFile (file);
+				OnGameSystemLoaded(gameSystem);
+				obj = gameSystem;
 			}
-			else if (CheckCanHandleFileAsGameSystem(file))
+			else if (CheckCanHandleFileAsRace(file))
 			{
-				objects.Add(CreateGameSystemFromFile(file));
+				Race race = CreateRaceFromFile (file);
+				OnRaceLoaded(race);
+				obj = race;
 			}
 			else if (CheckCanHandleFileAsArmy(file))
 			{
-				objects.Add(CreateArmyFromFile(file));
+				Army army = CreateArmyFromFile (file);
+				OnArmyLoaded(army);
+				obj = army;
 			}
 			else
 			{
 				LogNotifier.Warn(GetType(), "Failed trying to create from "+file.FullName+" - not a Race, Army or GameSystem");
 			}
 			
+			ICollection<IWarFoundryObject> objects = new List<IWarFoundryObject>();
+			
+			if (obj != null)
+			{
+				objects.Add(obj);
+			}
+			
 			return objects;
 		}
 	}
--- a/api/Factories/AbstractWarFoundryFactory.cs	Sat Feb 26 20:15:12 2011 +0000
+++ b/api/Factories/AbstractWarFoundryFactory.cs	Sun Feb 27 15:54:13 2011 +0000
@@ -11,12 +11,18 @@
 {
 	public abstract class AbstractWarFoundryFactory<FILE_TYPE> : IWarFoundryFactory
 	{		
+		public event SingleArgMethodInvoker<GameSystem> GameSystemLoaded;
+		
+		public event SingleArgMethodInvoker<Race> RaceLoaded;
+		
+		public event SingleArgMethodInvoker<Army> ArmyLoaded;
+		
 		public virtual void CompleteLoading(IWarFoundryStagedLoadObject obj)
 		{
 			//Pretend we've fully loaded, as this will probably be standard for non-native factories and some native factories
 			obj.SetAsFullyLoaded();
 		}
-			
+
 		public bool CanHandleFileFormat (FileInfo file)
 		{
 			FILE_TYPE typedFile = GetFileAsSupportedType(file);
@@ -138,6 +144,7 @@
 		
 		/// <summary>
 		/// Reads the data from the supplied converted <see cref="FILE_TYPE"/> object and returns it as a collection of loadable objects.
+		/// In addition, the method fires the appropriate XxxLoaded event for each object created for asynchronous use.
 		/// </summary>
 		/// <param name="file">
 		/// An object of the converted <see cref="FILE_TYPE"/> for the file to load data from
@@ -146,5 +153,29 @@
 		/// A <see cref="ICollection`1"/> of <see cref="IWarFoundryObject"/>s that were loaded from the file object
 		/// </returns>
 		protected abstract ICollection<IWarFoundryObject> DoCreateObjectsFromFile(FILE_TYPE file);
+		
+		protected void OnGameSystemLoaded(GameSystem system)
+		{
+			if (GameSystemLoaded != null)
+			{
+				GameSystemLoaded(system);
+			}
+		}
+		
+		protected void OnRaceLoaded(Race race)
+		{
+			if (RaceLoaded != null)
+			{
+				RaceLoaded(race);
+			}
+		}
+		
+		protected void OnArmyLoaded(Army army)
+		{
+			if (ArmyLoaded != null)
+			{
+				ArmyLoaded(army);
+			}
+		}
 	}
 }
--- a/api/Factories/DummyWarFoundryFactory.cs	Sat Feb 26 20:15:12 2011 +0000
+++ b/api/Factories/DummyWarFoundryFactory.cs	Sun Feb 27 15:54:13 2011 +0000
@@ -14,6 +14,12 @@
 	///</summary>
 	public class DummyWarFoundryFactory : IWarFoundryFactory
 	{
+		public event SingleArgMethodInvoker<GameSystem> GameSystemLoaded;
+
+		public event SingleArgMethodInvoker<Race> RaceLoaded;
+
+		public event SingleArgMethodInvoker<Army> ArmyLoaded;
+		
 		public DummyWarFoundryFactory()
 		{
 			//Public constructor
--- a/api/Factories/IWarFoundryFactory.cs	Sat Feb 26 20:15:12 2011 +0000
+++ b/api/Factories/IWarFoundryFactory.cs	Sun Feb 27 15:54:13 2011 +0000
@@ -10,7 +10,13 @@
 namespace IBBoard.WarFoundry.API.Factories
 {
 	public interface IWarFoundryFactory
-	{
+	{		
+		event SingleArgMethodInvoker<GameSystem> GameSystemLoaded;
+		
+		event SingleArgMethodInvoker<Race> RaceLoaded;
+		
+		event SingleArgMethodInvoker<Army> ArmyLoaded;
+		
 		/// <summary>
 		/// Completes the loading of an object if it is loaded in stages.
 		/// </summary>
@@ -64,7 +70,9 @@
 		bool CanHandleFileAsArmy(FileInfo file);
 		
 		/// <summary>
-		/// Reads the data from the supplied file and returns it as a collection of loadable objects.
+		/// Reads the data from the supplied file and returns it as a collection of loadable objects. In addition, it fires the appropriate XxxLoaded event
+		/// for each object loaded for asynchronous use.
+		/// 
 		/// May throw a <see cref=" IBBoard.IO.InvalidFileException"/> if the file is supported by the Factory but the content is invalid.
 		/// </summary>
 		/// <param name="file">