Mercurial > repos > IBBoard.WarFoundry.API
changeset 291:24e7b571f50f
Re #318: DefaultWarFoundryLoader throws null ref when loading individual files
* Fix quick fix so that it actually works and loads files - but it is still a quick-fix that needs double-checking
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Tue, 14 Dec 2010 20:17:07 +0000 |
parents | 5ed39187b0e3 |
children | 4dcb038e4f55 |
files | api/AbstractWarFoundryLoader.cs api/DefaultWarFoundryLoader.cs |
diffstat | 2 files changed, 53 insertions(+), 33 deletions(-) [+] |
line wrap: on
line diff
--- a/api/AbstractWarFoundryLoader.cs Mon Dec 13 21:03:02 2010 +0000 +++ b/api/AbstractWarFoundryLoader.cs Tue Dec 14 20:17:07 2010 +0000 @@ -22,10 +22,13 @@ private ICollection<INativeWarFoundryFactory> factories; private ICollection<INonNativeWarFoundryFactory> nonNativeFactories; private Dictionary<IWarFoundryFactory, SimpleSet<IWarFoundryObject>> loadedObjects; + public delegate void FileLoadingCompleteDelegate(List<FileLoadFailure> failures); + public event MethodInvoker FileLoadingStarted; + public event FileLoadingCompleteDelegate FileLoadingFinished; - + protected AbstractWarFoundryLoader() { directories = new List<DirectoryInfo>(); @@ -133,25 +136,34 @@ failedLoads.AddRange(LoadGameSystems(loadableGameSystems)); failedLoads.AddRange(LoadRaces(loadableRaces)); OnFileLoadingFinished(failedLoads); + FinishFileLoad(); return failedLoads; } - + private void OnFileLoadingFinished(List<FileLoadFailure> failures) { - if (FileLoadingFinished!=null) + if (FileLoadingFinished != null) { FileLoadingFinished(failures); } } - - protected abstract void PrepareForFileLoad(); + + protected virtual void PrepareForFileLoad() + { + //Do nothing special + } + + protected virtual void FinishFileLoad() + { + //Do nothing special + } private List<FileLoadFailure> FillLoadableFiles(Dictionary<FileInfo, IWarFoundryFactory> loadableRaces, Dictionary<FileInfo, IWarFoundryFactory> loadableGameSystems) { List<FileLoadFailure> fails = new List<FileLoadFailure>(); foreach (DirectoryInfo directory in directories) - { + { directory.Refresh(); if (directory.Exists) @@ -171,7 +183,7 @@ private List<FileLoadFailure> FillLoadableFilesForDirectory(Dictionary<FileInfo, IWarFoundryFactory> loadableRaces, Dictionary<FileInfo, IWarFoundryFactory> loadableGameSystems, DirectoryInfo directory) { List<FileLoadFailure> fails = new List<FileLoadFailure>(); - LogNotifier.Debug(GetType(), "Load from "+directory.FullName); + LogNotifier.Debug(GetType(), "Load from " + directory.FullName); foreach (FileInfo file in directory.GetFiles()) { @@ -185,7 +197,7 @@ { factory = GetRaceLoadingFactoryForFile(file); - if (factory!=null) + if (factory != null) { loadableRaces.Add(file, factory); } @@ -280,7 +292,7 @@ failure = new FileLoadFailure(file, null, ex.Message, null, ex); } - if (failure!=null) + if (failure != null) { fails.Add(failure); LogNotifier.Warn(GetType(), failure.Message, failure.Exception); @@ -312,7 +324,7 @@ failure = new FileLoadFailure(file, null, ex.Message, null, ex); } - if (failure!=null) + if (failure != null) { fails.Add(failure); LogNotifier.Warn(GetType(), failure.Message, failure.Exception); @@ -364,10 +376,10 @@ } catch (InvalidFileException ex) { - LogNotifier.Error(GetType(), file.FullName+" failed to load", ex); + LogNotifier.Error(GetType(), file.FullName + " failed to load", ex); } - if (objs!=null) + if (objs != null) { AddLoadedObjects(objs, loadFactory); } @@ -378,7 +390,7 @@ return objs; } - + private ICollection<IWarFoundryObject> LoadFileWithNonNativeFactories(FileInfo file, out IWarFoundryFactory loadFactory) { ICollection<IWarFoundryObject> objs = null; @@ -386,18 +398,18 @@ if (nonNativeFactories.Count > 0) { - LogNotifier.Debug(GetType(), "Attempting to load "+file.FullName+" as a non-native file"); + LogNotifier.Debug(GetType(), "Attempting to load " + file.FullName + " as a non-native file"); foreach (INonNativeWarFoundryFactory factory in nonNativeFactories) { bool canLoad = factory.CanHandleFileFormat(file); - LogNotifier.Debug(GetType(), "Load using "+factory.GetType().FullName+"? " + (canLoad ? "yes" : "no")); + LogNotifier.Debug(GetType(), "Load using " + factory.GetType().FullName + "? " + (canLoad ? "yes" : "no")); if (canLoad) { objs = factory.CreateObjectsFromFile(file); - if (objs!=null) + if (objs != null) { loadFactory = factory; break; @@ -408,7 +420,7 @@ return objs; } - + private ICollection<IWarFoundryObject> LoadFileWithNativeFactories(FileInfo file, out IWarFoundryFactory loadFactory) { ICollection<IWarFoundryObject> objs = null; @@ -416,7 +428,7 @@ if (factories.Count > 0) { - LogNotifier.Debug(GetType(), "Attempting to load "+file.FullName+" as native file"); + LogNotifier.Debug(GetType(), "Attempting to load " + file.FullName + " as native file"); foreach (INativeWarFoundryFactory factory in factories) { @@ -424,7 +436,7 @@ { objs = factory.CreateObjectsFromFile(file); - if (objs!=null) + if (objs != null) { loadFactory = factory; break; @@ -435,7 +447,7 @@ return objs; } - + private void AddLoadedObjects(ICollection<IWarFoundryObject> loadedObjs, IWarFoundryFactory factory) { SimpleSet<IWarFoundryObject> objs; @@ -465,12 +477,12 @@ } } } - + protected void StoreGameSystem(GameSystem system) { GameSystem existingSystem = GetExistingSystemForSystem(system); - if (existingSystem!=null) + if (existingSystem != null) { if (!system.Equals(existingSystem)) { @@ -502,7 +514,7 @@ /// The loaded <see cref="GameSystem"/> to store /// </param> protected abstract void DoStoreGameSystem(GameSystem system); - + protected void StoreRace(Race race) { if (race.GameSystem == null) @@ -601,7 +613,7 @@ GameSystem[] systems = GetGameSystems(); return GetWarFoundryObjectIDs(systems); } - + protected string[] GetWarFoundryObjectIDs(WarFoundryObject[] objs) { int objCount = objs.Length; @@ -629,7 +641,7 @@ Race[] races = GetRaces(system); return GetWarFoundryObjectIDs(races); } - + public Army LoadArmy(FileInfo file) { IWarFoundryFactory factory = GetArmyLoadingFactoryForFile(file); @@ -645,7 +657,7 @@ { if (systemCount is Army) { - loadedArmy = (Army) systemCount; + loadedArmy = (Army)systemCount; } } }
--- a/api/DefaultWarFoundryLoader.cs Mon Dec 13 21:03:02 2010 +0000 +++ b/api/DefaultWarFoundryLoader.cs Tue Dec 14 20:17:07 2010 +0000 @@ -15,6 +15,8 @@ { private Dictionary<string, GameSystem> systemsTable; private Dictionary<string, Dictionary<string, Dictionary<string, Race>>> racesTable; //Keys are: System, Race, SubRace + private bool loaded = false; + private bool loading = false; public DefaultWarFoundryLoader() { @@ -24,11 +26,17 @@ protected override void PrepareForFileLoad() { - //Just set up blank dictionaries for now - may try different and more complex handling in future + loading = true; systemsTable.Clear(); racesTable.Clear(); } + protected override void FinishFileLoad() + { + loaded = true; + loading = false; + } + protected override GameSystem GetExistingSystemForSystem(GameSystem system) { return DictionaryUtils.GetValue(systemsTable, system.ID.ToLower()); @@ -81,7 +89,7 @@ public override GameSystem[] GetGameSystems() { - if (systemsTable == null) + if (!loaded && !loading) { LoadFiles(); } @@ -91,7 +99,7 @@ public override GameSystem GetGameSystem(string systemID) { - if (systemsTable == null) + if (!loaded && !loading) { LoadFiles(); } @@ -122,7 +130,7 @@ /// </returns> public Race[] GetRaces(string systemID) { - if (racesTable == null) + if (!loaded && !loading) { LoadFiles(); } @@ -201,7 +209,7 @@ /// </returns> public Race GetRace(string systemID, string raceID, string raceSubID) { - if (racesTable == null) + if (!loaded && !loading) { LoadFiles(); } @@ -244,7 +252,7 @@ public override string[] GetGameSystemIDs() { - if (systemsTable == null) + if (!loaded && !loading) { LoadFiles(); } @@ -268,7 +276,7 @@ /// </returns> public string[] GetSystemRaceIDs(string systemID) { - if (racesTable == null) + if (!loaded && !loading) { LoadFiles(); }