changeset 111:b5ae022ce7b8

Re #54: Add army loading support * Add method to loader to try to load an army from a specific file
author IBBoard <dev@ibboard.co.uk>
date Sat, 22 Aug 2009 15:34:25 +0000
parents f0fb96d0cfe9
children 863518044d38
files api/WarFoundryLoader.cs
diffstat 1 files changed, 49 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/api/WarFoundryLoader.cs	Sat Aug 22 14:20:24 2009 +0000
+++ b/api/WarFoundryLoader.cs	Sat Aug 22 15:34:25 2009 +0000
@@ -811,5 +811,54 @@
 				return keys;
 			}
 		}
+		
+		public Army LoadArmy(FileInfo file)
+		{
+			IWarFoundryFactory factory = GetArmyLoadingFactoryForFile(file);
+			ICollection<IWarFoundryObject> objs = factory.CreateObjectsFromFile(file);
+			
+			Army loadedArmy = null;
+			
+			if (objs.Count == 1)
+			{
+				foreach (IWarFoundryObject obj in objs)
+				{
+					if (obj is Army)
+					{
+						loadedArmy = (Army) obj;
+					}
+				}
+			}
+			
+			return loadedArmy;
+		}
+
+		private IWarFoundryFactory GetArmyLoadingFactoryForFile(FileInfo file)
+		{
+			IWarFoundryFactory loadingFactory = null;
+			
+			foreach (INonNativeWarFoundryFactory factory in nonNativeFactories)
+			{
+				if (factory.CanHandleFileAsArmy(file))
+				{
+					loadingFactory = factory;
+					break;
+				}
+			}
+
+			if (loadingFactory == null)
+			{
+				foreach (INativeWarFoundryFactory factory in factories)
+				{
+					if (factory.CanHandleFileAsArmy(file))
+					{
+						loadingFactory = factory;
+						break;
+					}
+				}
+			}
+
+			return loadingFactory;
+		}
 	}
 }