diff API/Factories/Xml/WarFoundryXmlRaceFactory.cs @ 377:c3b0b28bad48

Re #353: Handle unloaded units being referenced outside factory * Create interface for race factories * Start to pull out common methods and add "try unit and fall back to factory" method for getting unit types * Make XML Race Factory implement interface * Use new factory
author IBBoard <dev@ibboard.co.uk>
date Mon, 04 Jul 2011 20:02:26 +0000
parents e50682387d63
children ff7f1b319b4e
line wrap: on
line diff
--- a/API/Factories/Xml/WarFoundryXmlRaceFactory.cs	Sat Jul 02 19:58:28 2011 +0000
+++ b/API/Factories/Xml/WarFoundryXmlRaceFactory.cs	Mon Jul 04 20:02:26 2011 +0000
@@ -20,7 +20,7 @@
 	/// <summary>
 	/// A sub-factory for loading WarFoundry Race XML files
 	/// </summary>
-	public class WarFoundryXmlRaceFactory
+	public class WarFoundryXmlRaceFactory : IRaceFactory<XmlDocument, XmlElement>
 	{
 		private Dictionary<Race, XmlDocument> extraData = new Dictionary<Race, XmlDocument>();
 		private WarFoundryXmlLimitParser limitParser = new WarFoundryXmlLimitParser();
@@ -41,9 +41,14 @@
 			XmlDocument extra = null;
 			extraData.TryGetValue(obj, out extra);
 			return extra;
+		}
+
+		public Race CreateRace(XmlElement elem)
+		{
+			return CreateRaceFromElement(elem);
 		}
 		
-		public Race CreateRaceFromElement(ZipFile file, XmlElement elem)
+		public Race CreateRaceFromElement(XmlElement elem)
 		{
 			string id = elem.GetAttribute("id");
 			string subid = elem.GetAttribute("subid");
@@ -106,6 +111,24 @@
 			Category cat = CategoryLoader.CreateFromElement(elem);
 			parentRace.AddCategory(cat);
 			return cat;
+		}
+
+		
+		public UnitType GetUnitType(string id, Race parentRace)
+		{
+			return GetUnitType(id, parentRace, GetExtraData(parentRace));
+		}
+
+		public UnitType GetUnitType(string id, Race parentRace, XmlDocument doc)
+		{
+			UnitType type = parentRace.GetUnitType(id);
+
+			if (type==null)
+			{
+				type = GetUnitTypeFromDocument(doc, id, parentRace);
+			}
+
+			return type;
 		}
 
 		private UnitType GetUnitTypeFromDocument(XmlDocument doc, string id, Race parentRace)
@@ -383,7 +406,7 @@
 
 				if (reqFactory != null) {
 					string data = WarFoundryXmlFactoryUtils.SelectSingleElement(extraData, "race:data").InnerText;
-					UnitRequiresAtLeastNUnitsRequirement req = reqFactory.CreateRequirement(type, data);
+					UnitRequiresAtLeastNUnitsRequirement req = reqFactory.CreateRequirement(type, data, this);
 					type.AddRequirement(req);
 				}
 			}