changeset 226:c931684f9024

Re #234: Invalid data file doesn't stop load * Add internal methods to remove game systems or races that fail to load * Add error handling on XML CompleteLoading so that game system or race is removed, but make sure not to affect error
author IBBoard <dev@ibboard.co.uk>
date Sat, 19 Dec 2009 15:40:50 +0000
parents 0d5a17f33a36
children bf4f04f385d0
files api/Factories/Xml/WarFoundryXmlFactory.cs api/WarFoundryLoader.cs
diffstat 2 files changed, 78 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/api/Factories/Xml/WarFoundryXmlFactory.cs	Thu Dec 17 20:52:22 2009 +0000
+++ b/api/Factories/Xml/WarFoundryXmlFactory.cs	Sat Dec 19 15:40:50 2009 +0000
@@ -118,16 +118,43 @@
 
 		public override void CompleteLoading(IWarFoundryStagedLoadObject obj)
 		{			
-			LogNotifier.DebugFormat(GetType(), "Complete loading of {0} with ID {1}", obj.GetType().Name, obj.ID);
-							
-			if (obj is GameSystem)
-			{
-				gameSystemFactory.CompleteLoading((GameSystem)obj);
-			}
-			else if (obj is Race)
-			{
-				raceFactory.CompleteLoading((Race)obj);
-			}
+			LogNotifier.DebugFormat(GetType(), "Complete loading of {0} with ID {1}", obj.GetType().Name, obj.ID);
+
+				if (obj is GameSystem)
+				{
+					CompleteLoadingGameSystem((GameSystem) obj);
+				}
+				else if (obj is Race)
+				{
+					CompleteLoadingRace((Race) obj);
+				}
+		}
+
+		private void CompleteLoadingRace(Race race)
+		{
+			try
+			{
+				raceFactory.CompleteLoading(race);
+			}
+			catch (InvalidFileException ex)
+			{
+				WarFoundryLoader.GetDefault().RemoveRace(race);
+				throw;
+			}
+		}
+
+		private void CompleteLoadingGameSystem(GameSystem system)
+		{
+			try
+			{
+			gameSystemFactory.CompleteLoading(system);
+
+		}
+		catch (InvalidFileException ex)
+		{
+			WarFoundryLoader.GetDefault().RemoveGameSystem(system);
+			throw;
+		}
 		}
 	}
 }
\ No newline at end of file
--- a/api/WarFoundryLoader.cs	Thu Dec 17 20:52:22 2009 +0000
+++ b/api/WarFoundryLoader.cs	Sat Dec 19 15:40:50 2009 +0000
@@ -588,6 +588,15 @@
 			GameSystem system;
 			systemsTable.TryGetValue(systemID.ToLower(), out system);
 			return system;
+		}
+
+		/// <summary>
+		/// Removes a loaded <see cref="GameSystem"/>. Used when a GameSystem fails to complete loading
+		/// </summary>
+		/// <param name="system">The GameSystem to remove</param>
+		internal void RemoveGameSystem(GameSystem system)
+		{
+			systemsTable.Remove(system.ID.ToLower());
 		}
 
 		/// <summary>
@@ -727,26 +736,39 @@
 			}
 			
 			Race race = null;
-			
-			systemID = systemID.ToLower();
-			raceID = raceID.ToLower();
-			raceSubID = raceSubID.ToLower();
-
-			Dictionary<string, Dictionary<string, Race>> races;	
-			racesTable.TryGetValue(systemID, out races);
-
-			if (races!=null)
-			{
-				Dictionary<string, Race> subraces;
-				races.TryGetValue(raceID, out subraces);
-
-				if (subraces!=null)
-				{
-					subraces.TryGetValue(raceSubID, out race);
-				}
+			
+			Dictionary<string, Race> subraces = GetRaceTable(systemID, raceID);
+
+			if (subraces != null)
+			{
+				subraces.TryGetValue(raceSubID.ToLower(), out race);
 			}
 			
-			return race;
+			return race;
+		}
+
+		private Dictionary<string, Race> GetRaceTable(string systemID, string raceID)
+		{
+			Dictionary<string, Dictionary<string, Race>> races;
+			racesTable.TryGetValue(systemID.ToLower(), out races);
+			Dictionary<string, Race> subraces = null;
+
+			if (races != null)
+			{
+				races.TryGetValue(raceID.ToLower(), out subraces);
+			}
+
+			return subraces;
+		}
+
+		internal void RemoveRace(Race race)
+		{
+			Dictionary<string, Race> subraces = GetRaceTable(race.GameSystem.ID, race.ID);
+
+			if (subraces != null)
+			{
+				subraces.Remove(race.SubID.ToLower());
+			}
 		}
 
 		/// <summary>
@@ -873,6 +895,6 @@
 			}
 
 			return loadingFactory;
-		}
+		}
 	}
 }