# HG changeset patch # User IBBoard # Date 1298822053 0 # Node ID f00a57369aaae07be14ee9771d7950c27eb78a80 # Parent 3854c26073c4f1fc148d4d549b9771e086a10f80 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 diff -r 3854c26073c4 -r f00a57369aaa api/AbstractWarFoundryLoader.cs --- 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 objects = factory.CreateObjectsFromFile(file); - - if (objects.Count > 0) - { - AddLoadedObjects(objects, factory); - loaded = true; - } - - return loaded; + return objects.Count > 0; } diff -r 3854c26073c4 -r f00a57369aaa api/Factories/AbstractNativeWarFoundryFactory.cs --- 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 (ICollection collection, ICollection newItems) where NEW_ITEM_TYPE : IWarFoundryObject - { - foreach (IWarFoundryObject obj in newItems) - { - collection.Add(obj); - } - } - protected ICollection CreateArmiesFromFile(ZipFile file) { ICollection dataStreams = GetArmyZipEntries(file); diff -r 3854c26073c4 -r f00a57369aaa api/Factories/AbstractNonNativeFileExtensionWarFoundryFactory.cs --- 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 DoCreateObjectsFromFile (FileInfo file) { - ICollection objects = new List(); + 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 objects = new List(); + + if (obj != null) + { + objects.Add(obj); + } + return objects; } } diff -r 3854c26073c4 -r f00a57369aaa api/Factories/AbstractWarFoundryFactory.cs --- 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 : IWarFoundryFactory { + public event SingleArgMethodInvoker GameSystemLoaded; + + public event SingleArgMethodInvoker RaceLoaded; + + public event SingleArgMethodInvoker 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 @@ /// /// Reads the data from the supplied converted 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. /// /// /// An object of the converted for the file to load data from @@ -146,5 +153,29 @@ /// A of s that were loaded from the file object /// protected abstract ICollection 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); + } + } } } diff -r 3854c26073c4 -r f00a57369aaa api/Factories/DummyWarFoundryFactory.cs --- 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 @@ /// public class DummyWarFoundryFactory : IWarFoundryFactory { + public event SingleArgMethodInvoker GameSystemLoaded; + + public event SingleArgMethodInvoker RaceLoaded; + + public event SingleArgMethodInvoker ArmyLoaded; + public DummyWarFoundryFactory() { //Public constructor diff -r 3854c26073c4 -r f00a57369aaa api/Factories/IWarFoundryFactory.cs --- 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 GameSystemLoaded; + + event SingleArgMethodInvoker RaceLoaded; + + event SingleArgMethodInvoker ArmyLoaded; + /// /// Completes the loading of an object if it is loaded in stages. /// @@ -64,7 +70,9 @@ bool CanHandleFileAsArmy(FileInfo file); /// - /// 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 if the file is supported by the Factory but the content is invalid. /// ///