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 diff
     1.1 --- a/api/AbstractWarFoundryLoader.cs	Mon Dec 13 21:03:02 2010 +0000
     1.2 +++ b/api/AbstractWarFoundryLoader.cs	Tue Dec 14 20:17:07 2010 +0000
     1.3 @@ -22,10 +22,13 @@
     1.4  		private ICollection<INativeWarFoundryFactory> factories;
     1.5  		private ICollection<INonNativeWarFoundryFactory> nonNativeFactories;
     1.6  		private Dictionary<IWarFoundryFactory, SimpleSet<IWarFoundryObject>> loadedObjects;
     1.7 +
     1.8  		public delegate void FileLoadingCompleteDelegate(List<FileLoadFailure> failures);
     1.9 +
    1.10  		public event MethodInvoker FileLoadingStarted;
    1.11 +
    1.12  		public event FileLoadingCompleteDelegate FileLoadingFinished;
    1.13 -		
    1.14 +
    1.15  		protected AbstractWarFoundryLoader()
    1.16  		{
    1.17  			directories = new List<DirectoryInfo>();
    1.18 @@ -133,25 +136,34 @@
    1.19  			failedLoads.AddRange(LoadGameSystems(loadableGameSystems));
    1.20  			failedLoads.AddRange(LoadRaces(loadableRaces));
    1.21  			OnFileLoadingFinished(failedLoads);
    1.22 +			FinishFileLoad();
    1.23  			return failedLoads;
    1.24  		}
    1.25 -		
    1.26 +
    1.27  		private void OnFileLoadingFinished(List<FileLoadFailure> failures)
    1.28  		{
    1.29 -			if (FileLoadingFinished!=null)
    1.30 +			if (FileLoadingFinished != null)
    1.31  			{
    1.32  				FileLoadingFinished(failures);
    1.33  			}
    1.34  		}
    1.35 -		
    1.36 -		protected abstract void PrepareForFileLoad();
    1.37 +
    1.38 +		protected virtual void PrepareForFileLoad()
    1.39 +		{
    1.40 +			//Do nothing special
    1.41 +		}
    1.42 +
    1.43 +		protected virtual void FinishFileLoad()
    1.44 +		{
    1.45 +			//Do nothing special
    1.46 +		}
    1.47  
    1.48  		private List<FileLoadFailure> FillLoadableFiles(Dictionary<FileInfo, IWarFoundryFactory> loadableRaces, Dictionary<FileInfo, IWarFoundryFactory> loadableGameSystems)
    1.49  		{			
    1.50  			List<FileLoadFailure> fails = new List<FileLoadFailure>();
    1.51  			
    1.52  			foreach (DirectoryInfo directory in directories)
    1.53 -			{
    1.54 +			{
    1.55  				directory.Refresh();
    1.56  
    1.57  				if (directory.Exists)
    1.58 @@ -171,7 +183,7 @@
    1.59  		private List<FileLoadFailure> FillLoadableFilesForDirectory(Dictionary<FileInfo, IWarFoundryFactory> loadableRaces, Dictionary<FileInfo, IWarFoundryFactory> loadableGameSystems, DirectoryInfo directory)
    1.60  		{
    1.61  			List<FileLoadFailure> fails = new List<FileLoadFailure>();
    1.62 -			LogNotifier.Debug(GetType(), "Load from "+directory.FullName);
    1.63 +			LogNotifier.Debug(GetType(), "Load from " + directory.FullName);
    1.64  		
    1.65  			foreach (FileInfo file in directory.GetFiles())
    1.66  			{
    1.67 @@ -185,7 +197,7 @@
    1.68  				{
    1.69  					factory = GetRaceLoadingFactoryForFile(file);
    1.70  	
    1.71 -					if (factory!=null)
    1.72 +					if (factory != null)
    1.73  					{
    1.74  						loadableRaces.Add(file, factory);
    1.75  					}
    1.76 @@ -280,7 +292,7 @@
    1.77  					failure = new FileLoadFailure(file, null, ex.Message, null, ex);
    1.78  				}
    1.79  						
    1.80 -				if (failure!=null)
    1.81 +				if (failure != null)
    1.82  				{
    1.83  					fails.Add(failure);
    1.84  					LogNotifier.Warn(GetType(), failure.Message, failure.Exception);
    1.85 @@ -312,7 +324,7 @@
    1.86  					failure = new FileLoadFailure(file, null, ex.Message, null, ex);
    1.87  				}
    1.88  						
    1.89 -				if (failure!=null)
    1.90 +				if (failure != null)
    1.91  				{
    1.92  					fails.Add(failure);
    1.93  					LogNotifier.Warn(GetType(), failure.Message, failure.Exception);
    1.94 @@ -364,10 +376,10 @@
    1.95  			}
    1.96  			catch (InvalidFileException ex)
    1.97  			{
    1.98 -				LogNotifier.Error(GetType(), file.FullName+" failed to load", ex);
    1.99 +				LogNotifier.Error(GetType(), file.FullName + " failed to load", ex);
   1.100  			}
   1.101  				
   1.102 -			if (objs!=null)
   1.103 +			if (objs != null)
   1.104  			{
   1.105  				AddLoadedObjects(objs, loadFactory);
   1.106  			}
   1.107 @@ -378,7 +390,7 @@
   1.108  
   1.109  			return objs;
   1.110  		}
   1.111 -		
   1.112 +
   1.113  		private ICollection<IWarFoundryObject> LoadFileWithNonNativeFactories(FileInfo file, out IWarFoundryFactory loadFactory)
   1.114  		{
   1.115  			ICollection<IWarFoundryObject> objs = null;
   1.116 @@ -386,18 +398,18 @@
   1.117  			
   1.118  			if (nonNativeFactories.Count > 0)
   1.119  			{
   1.120 -				LogNotifier.Debug(GetType(), "Attempting to load "+file.FullName+" as a non-native file");
   1.121 +				LogNotifier.Debug(GetType(), "Attempting to load " + file.FullName + " as a non-native file");
   1.122  				
   1.123  				foreach (INonNativeWarFoundryFactory factory in nonNativeFactories)
   1.124  				{
   1.125  					bool canLoad = factory.CanHandleFileFormat(file);
   1.126 -					LogNotifier.Debug(GetType(), "Load using "+factory.GetType().FullName+"? " + (canLoad ? "yes" : "no"));
   1.127 +					LogNotifier.Debug(GetType(), "Load using " + factory.GetType().FullName + "? " + (canLoad ? "yes" : "no"));
   1.128  					
   1.129  					if (canLoad)
   1.130  					{
   1.131  						objs = factory.CreateObjectsFromFile(file);
   1.132  						
   1.133 -						if (objs!=null)
   1.134 +						if (objs != null)
   1.135  						{
   1.136  							loadFactory = factory;
   1.137  							break;
   1.138 @@ -408,7 +420,7 @@
   1.139  			
   1.140  			return objs;
   1.141  		}
   1.142 -		
   1.143 +
   1.144  		private ICollection<IWarFoundryObject> LoadFileWithNativeFactories(FileInfo file, out IWarFoundryFactory loadFactory)
   1.145  		{
   1.146  			ICollection<IWarFoundryObject> objs = null;
   1.147 @@ -416,7 +428,7 @@
   1.148  			
   1.149  			if (factories.Count > 0)
   1.150  			{
   1.151 -				LogNotifier.Debug(GetType(), "Attempting to load "+file.FullName+" as native file");
   1.152 +				LogNotifier.Debug(GetType(), "Attempting to load " + file.FullName + " as native file");
   1.153  						
   1.154  				foreach (INativeWarFoundryFactory factory in factories)
   1.155  				{
   1.156 @@ -424,7 +436,7 @@
   1.157  					{
   1.158  						objs = factory.CreateObjectsFromFile(file);
   1.159  						
   1.160 -						if (objs!=null)
   1.161 +						if (objs != null)
   1.162  						{
   1.163  							loadFactory = factory;
   1.164  							break;
   1.165 @@ -435,7 +447,7 @@
   1.166  			
   1.167  			return objs;
   1.168  		}
   1.169 -		
   1.170 +
   1.171  		private void AddLoadedObjects(ICollection<IWarFoundryObject> loadedObjs, IWarFoundryFactory factory)
   1.172  		{
   1.173  			SimpleSet<IWarFoundryObject> objs;
   1.174 @@ -465,12 +477,12 @@
   1.175  				}
   1.176  			}
   1.177  		}
   1.178 -		
   1.179 +
   1.180  		protected void StoreGameSystem(GameSystem system)
   1.181  		{
   1.182  			GameSystem existingSystem = GetExistingSystemForSystem(system);
   1.183  			
   1.184 -			if (existingSystem!=null)
   1.185 +			if (existingSystem != null)
   1.186  			{				
   1.187  				if (!system.Equals(existingSystem))
   1.188  				{
   1.189 @@ -502,7 +514,7 @@
   1.190  		/// The loaded <see cref="GameSystem"/> to store
   1.191  		/// </param>
   1.192  		protected abstract void DoStoreGameSystem(GameSystem system);
   1.193 -		
   1.194 +
   1.195  		protected void StoreRace(Race race)
   1.196  		{
   1.197  			if (race.GameSystem == null)
   1.198 @@ -601,7 +613,7 @@
   1.199  			GameSystem[] systems = GetGameSystems();
   1.200  			return GetWarFoundryObjectIDs(systems);
   1.201  		}
   1.202 -		
   1.203 +
   1.204  		protected string[] GetWarFoundryObjectIDs(WarFoundryObject[] objs)
   1.205  		{
   1.206  			int objCount = objs.Length;
   1.207 @@ -629,7 +641,7 @@
   1.208  			Race[] races = GetRaces(system);
   1.209  			return GetWarFoundryObjectIDs(races);
   1.210  		}
   1.211 -		
   1.212 +
   1.213  		public Army LoadArmy(FileInfo file)
   1.214  		{
   1.215  			IWarFoundryFactory factory = GetArmyLoadingFactoryForFile(file);			
   1.216 @@ -645,7 +657,7 @@
   1.217  					{
   1.218  						if (systemCount is Army)
   1.219  						{
   1.220 -							loadedArmy = (Army) systemCount;
   1.221 +							loadedArmy = (Army)systemCount;
   1.222  						}
   1.223  					}
   1.224  				}
     2.1 --- a/api/DefaultWarFoundryLoader.cs	Mon Dec 13 21:03:02 2010 +0000
     2.2 +++ b/api/DefaultWarFoundryLoader.cs	Tue Dec 14 20:17:07 2010 +0000
     2.3 @@ -15,6 +15,8 @@
     2.4  	{
     2.5  		private Dictionary<string, GameSystem> systemsTable;
     2.6  		private Dictionary<string, Dictionary<string, Dictionary<string, Race>>> racesTable; //Keys are: System, Race, SubRace
     2.7 +		private bool loaded = false;
     2.8 +		private bool loading = false;
     2.9  
    2.10  		public DefaultWarFoundryLoader()
    2.11  		{
    2.12 @@ -24,11 +26,17 @@
    2.13  
    2.14  		protected override void PrepareForFileLoad()
    2.15  		{
    2.16 -			//Just set up blank dictionaries for now - may try different and more complex handling in future
    2.17 +			loading = true;
    2.18  			systemsTable.Clear();
    2.19  			racesTable.Clear();
    2.20  		}
    2.21  
    2.22 +		protected override void FinishFileLoad()
    2.23 +		{
    2.24 +			loaded = true;
    2.25 +			loading = false;
    2.26 +		}
    2.27 +
    2.28  		protected override GameSystem GetExistingSystemForSystem(GameSystem system)
    2.29  		{
    2.30  			return DictionaryUtils.GetValue(systemsTable, system.ID.ToLower());
    2.31 @@ -81,7 +89,7 @@
    2.32  
    2.33  		public override GameSystem[] GetGameSystems()
    2.34  		{
    2.35 -			if (systemsTable == null)
    2.36 +			if (!loaded && !loading)
    2.37  			{
    2.38  				LoadFiles();
    2.39  			}
    2.40 @@ -91,7 +99,7 @@
    2.41  
    2.42  		public override GameSystem GetGameSystem(string systemID)
    2.43  		{
    2.44 -			if (systemsTable == null)
    2.45 +			if (!loaded && !loading)
    2.46  			{
    2.47  				LoadFiles();
    2.48  			}
    2.49 @@ -122,7 +130,7 @@
    2.50  		/// </returns>
    2.51  		public Race[] GetRaces(string systemID)
    2.52  		{
    2.53 -			if (racesTable == null)
    2.54 +			if (!loaded && !loading)
    2.55  			{
    2.56  				LoadFiles();
    2.57  			}
    2.58 @@ -201,7 +209,7 @@
    2.59  		/// </returns>
    2.60  		public Race GetRace(string systemID, string raceID, string raceSubID)
    2.61  		{
    2.62 -			if (racesTable == null)
    2.63 +			if (!loaded && !loading)
    2.64  			{
    2.65  				LoadFiles();
    2.66  			}
    2.67 @@ -244,7 +252,7 @@
    2.68  
    2.69  		public override string[] GetGameSystemIDs()
    2.70  		{
    2.71 -			if (systemsTable == null)
    2.72 +			if (!loaded && !loading)
    2.73  			{
    2.74  				LoadFiles();
    2.75  			}
    2.76 @@ -268,7 +276,7 @@
    2.77  		/// </returns>
    2.78  		public string[] GetSystemRaceIDs(string systemID)
    2.79  		{
    2.80 -			if (racesTable == null)
    2.81 +			if (!loaded && !loading)
    2.82  			{
    2.83  				LoadFiles();
    2.84  			}