# HG changeset patch # User IBBoard # Date 1292357827 0 # Node ID 24e7b571f50f507e52ca3db4080ae42310a7c0a8 # Parent 5ed39187b0e3fc8355f1c001ffbb3481b9b4d264 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 diff -r 5ed39187b0e3 -r 24e7b571f50f api/AbstractWarFoundryLoader.cs --- 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 factories; private ICollection nonNativeFactories; private Dictionary> loadedObjects; + public delegate void FileLoadingCompleteDelegate(List failures); + public event MethodInvoker FileLoadingStarted; + public event FileLoadingCompleteDelegate FileLoadingFinished; - + protected AbstractWarFoundryLoader() { directories = new List(); @@ -133,25 +136,34 @@ failedLoads.AddRange(LoadGameSystems(loadableGameSystems)); failedLoads.AddRange(LoadRaces(loadableRaces)); OnFileLoadingFinished(failedLoads); + FinishFileLoad(); return failedLoads; } - + private void OnFileLoadingFinished(List 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 FillLoadableFiles(Dictionary loadableRaces, Dictionary loadableGameSystems) { List fails = new List(); foreach (DirectoryInfo directory in directories) - { + { directory.Refresh(); if (directory.Exists) @@ -171,7 +183,7 @@ private List FillLoadableFilesForDirectory(Dictionary loadableRaces, Dictionary loadableGameSystems, DirectoryInfo directory) { List fails = new List(); - 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 LoadFileWithNonNativeFactories(FileInfo file, out IWarFoundryFactory loadFactory) { ICollection 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 LoadFileWithNativeFactories(FileInfo file, out IWarFoundryFactory loadFactory) { ICollection 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 loadedObjs, IWarFoundryFactory factory) { SimpleSet 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 to store /// 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; } } } diff -r 5ed39187b0e3 -r 24e7b571f50f api/DefaultWarFoundryLoader.cs --- 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 systemsTable; private Dictionary>> 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 @@ /// public Race[] GetRaces(string systemID) { - if (racesTable == null) + if (!loaded && !loading) { LoadFiles(); } @@ -201,7 +209,7 @@ /// 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 @@ /// public string[] GetSystemRaceIDs(string systemID) { - if (racesTable == null) + if (!loaded && !loading) { LoadFiles(); }