changeset 52:64ef178c18aa

Re #10 - Refactor for readability * Break WarFoundryXMLFactory out in to GameSystem, Race and Army factories * Create factory utils classes with methods from WarFoundryXMLFactory for getting node lists etc
author IBBoard <dev@ibboard.co.uk>
date Mon, 30 Mar 2009 19:44:03 +0000
parents b271a2252758
children 1b35eed503ef
files IBBoard.WarFoundry.API.csproj api/Factories/Xml/AbstractStagedLoadedSubFactory.cs api/Factories/Xml/WarFoundryXmlArmyFactory.cs api/Factories/Xml/WarFoundryXmlFactory.cs api/Factories/Xml/WarFoundryXmlFactoryUtils.cs api/Factories/Xml/WarFoundryXmlGameSystemFactory.cs api/Factories/Xml/WarFoundryXmlRaceFactory.cs
diffstat 7 files changed, 521 insertions(+), 383 deletions(-) [+]
line diff
     1.1 --- a/IBBoard.WarFoundry.API.csproj	Sat Mar 28 21:00:35 2009 +0000
     1.2 +++ b/IBBoard.WarFoundry.API.csproj	Mon Mar 30 19:44:03 2009 +0000
     1.3 @@ -120,6 +120,11 @@
     1.4      <Compile Include="api\WarFoundryCore.cs" />
     1.5      <Compile Include="api\WarFoundryLoader.cs" />
     1.6      <Compile Include="AssemblyInfo.cs" />
     1.7 +    <Compile Include="api\Factories\Xml\WarFoundryXmlGameSystemFactory.cs" />
     1.8 +    <Compile Include="api\Factories\Xml\WarFoundryXmlRaceFactory.cs" />
     1.9 +    <Compile Include="api\Factories\Xml\WarFoundryXmlArmyFactory.cs" />
    1.10 +    <Compile Include="api\Factories\Xml\WarFoundryXmlFactoryUtils.cs" />
    1.11 +    <Compile Include="api\Factories\Xml\AbstractStagedLoadedSubFactory.cs" />
    1.12    </ItemGroup>
    1.13    <ItemGroup>
    1.14      <Content Include="libs\ICSharpCode.SharpZipLib.dll" />
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/api/Factories/Xml/AbstractStagedLoadedSubFactory.cs	Mon Mar 30 19:44:03 2009 +0000
     2.3 @@ -0,0 +1,34 @@
     2.4 +//  This file (AbstractStagedLoadedSubFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard
     2.5 +// 
     2.6 +//  The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
     2.7 +// 
     2.8 +
     2.9 +using System;
    2.10 +using System.Xml;
    2.11 +using IBBoard.Xml;
    2.12 +using IBBoard.WarFoundry.API.Objects;
    2.13 +
    2.14 +namespace IBBoard.WarFoundry.API.Factories.Xml
    2.15 +{
    2.16 +	public class AbstractStagedLoadedSubFactory
    2.17 +	{
    2.18 +		protected WarFoundryXmlFactory mainFactory;
    2.19 +		
    2.20 +		protected AbstractStagedLoadedSubFactory(WarFoundryXmlFactory factory)
    2.21 +		{
    2.22 +			mainFactory = factory;
    2.23 +		}
    2.24 +		
    2.25 +		protected Category CreateCategoryFromElement(XmlElement elem)
    2.26 +		{
    2.27 +			string id = elem.GetAttribute("id");
    2.28 +			string name = elem.GetAttribute("name");
    2.29 +			Category cat = new Category(id, name);
    2.30 +			cat.MaximumPercentage = XmlTools.GetIntValueFromAttribute(elem, "maxPercentage");
    2.31 +			cat.MinimumPercentage = XmlTools.GetIntValueFromAttribute(elem, "minPercentage");
    2.32 +			cat.MaximumPoints = XmlTools.GetIntValueFromAttribute(elem, "maxPoints");
    2.33 +			cat.MinimumPoints = XmlTools.GetIntValueFromAttribute(elem, "minPoints");
    2.34 +			return cat;
    2.35 +		}	
    2.36 +	}
    2.37 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/api/Factories/Xml/WarFoundryXmlArmyFactory.cs	Mon Mar 30 19:44:03 2009 +0000
     3.3 @@ -0,0 +1,32 @@
     3.4 +//  This file (WarFoundryXmlArmyFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard
     3.5 +// 
     3.6 +//  The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
     3.7 +// 
     3.8 +
     3.9 +using System;
    3.10 +using System.Xml;
    3.11 +using IBBoard.Xml;
    3.12 +using ICSharpCode.SharpZipLib.Zip;
    3.13 +using IBBoard.WarFoundry.API.Objects;
    3.14 +
    3.15 +namespace IBBoard.WarFoundry.API.Factories.Xml
    3.16 +{
    3.17 +	/// <summary>
    3.18 +	/// A sub-factory for loading WarFoundry Army XML files
    3.19 +	/// </summary>
    3.20 +	public class WarFoundryXmlArmyFactory
    3.21 +	{			
    3.22 +		public Army CreateArmyFromElement(ZipFile file, XmlElement elem)
    3.23 +		{
    3.24 +			string name = elem.GetAttribute("name");
    3.25 +			string systemID = elem.GetAttribute("gameSystem");
    3.26 +			GameSystem system = WarFoundryLoader.GetDefault().GetGameSystem(systemID);
    3.27 +			string raceID = elem.GetAttribute("race");
    3.28 +			Race race = WarFoundryLoader.GetDefault().GetRace(system, raceID);
    3.29 +			int points = XmlTools.GetIntValueFromAttribute(elem, "maxPoints");			
    3.30 +			Army army = new Army(race, name, points, file);
    3.31 +			//TODO: Complete loading of army
    3.32 +			return army;
    3.33 +		}
    3.34 +	}
    3.35 +}
     4.1 --- a/api/Factories/Xml/WarFoundryXmlFactory.cs	Sat Mar 28 21:00:35 2009 +0000
     4.2 +++ b/api/Factories/Xml/WarFoundryXmlFactory.cs	Mon Mar 30 19:44:03 2009 +0000
     4.3 @@ -26,9 +26,9 @@
     4.4  	public class WarFoundryXmlFactory : AbstractNativeWarFoundryFactory
     4.5  	{
     4.6  		private static WarFoundryXmlFactory factory;
     4.7 -		private XmlReaderSettings settings;
     4.8 -		private XmlNamespaceManager nsManager;
     4.9 -		private Dictionary<IWarFoundryObject, XmlDocument> extraData = new Dictionary<IWarFoundryObject, XmlDocument>();
    4.10 +		private WarFoundryXmlGameSystemFactory gameSystemFactory;
    4.11 +		private WarFoundryXmlRaceFactory raceFactory;
    4.12 +		private WarFoundryXmlArmyFactory armyFactory;
    4.13  
    4.14  		public static AbstractNativeWarFoundryFactory GetFactory()
    4.15  		{
    4.16 @@ -42,7 +42,9 @@
    4.17  		
    4.18  		private WarFoundryXmlFactory() : base()
    4.19  		{
    4.20 -			//Hide constructor
    4.21 +			gameSystemFactory = new WarFoundryXmlGameSystemFactory(this);
    4.22 +			raceFactory = new WarFoundryXmlRaceFactory(this);
    4.23 +			armyFactory = new WarFoundryXmlArmyFactory();
    4.24  		}
    4.25  		
    4.26  		protected override bool CheckCanFindArmyFileContent(ZipFile file)
    4.27 @@ -68,12 +70,12 @@
    4.28  		protected override Army CreateArmyFromStream (ZipFile file, Stream dataStream)
    4.29  		{
    4.30  			XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.ARMY_ELEMENT);
    4.31 -			return CreateArmyFromElement(file, elem);
    4.32 +			return armyFactory.CreateArmyFromElement(file, elem);
    4.33  		}
    4.34  		
    4.35  		private XmlElement GetRootElementFromStream(Stream stream, WarFoundryXmlElementName elementName)
    4.36  		{
    4.37 -			XmlDocument doc = CreateXmlDocumentFromStream(stream);			
    4.38 +			XmlDocument doc = WarFoundryXmlFactoryUtils.CreateXmlDocumentFromStream(stream);			
    4.39  			XmlElement elem = (XmlElement)doc.LastChild;
    4.40  			
    4.41  			if (!elem.LocalName.Equals(elementName.Value))
    4.42 @@ -83,91 +85,6 @@
    4.43  			
    4.44  			return elem;
    4.45  		}
    4.46 -		
    4.47 -		private XmlDocument CreateXmlDocumentFromStream(Stream stream)
    4.48 -		{
    4.49 -			XmlDocument doc = new XmlDocument();
    4.50 -			XmlReader reader = XmlReader.Create(stream, GetReaderSettings());
    4.51 -			
    4.52 -			try
    4.53 -			{
    4.54 -				doc.Load(reader);
    4.55 -			}
    4.56 -			//Don't catch XMLSchemaExceptions - let them get thrown out
    4.57 -			finally
    4.58 -			{
    4.59 -				reader.Close();
    4.60 -			}
    4.61 -
    4.62 -			return doc;
    4.63 -		}
    4.64 -		
    4.65 -		/// <summary>
    4.66 -		/// Lazy-getter for XML reader settings. May throw a <see cref="InvalidDataException"/> if there is a problem with the translation schema.
    4.67 -		/// </summary>
    4.68 -		/// <returns>
    4.69 -		/// A <see cref="XmlReaderSettings"/> with the default values for validating the translation document against the translation schema
    4.70 -		/// </returns>
    4.71 -		private XmlReaderSettings GetReaderSettings()
    4.72 -		{
    4.73 -			if (settings == null)
    4.74 -			{
    4.75 -				settings = new XmlReaderSettings();
    4.76 -				settings.ValidationType = ValidationType.Schema;
    4.77 -				settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
    4.78 -				settings.ProhibitDtd = true;
    4.79 -				settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod);
    4.80 -				XmlSchemaSet cache = new XmlSchemaSet();
    4.81 -				string path =  IBBoard.Constants.ExecutablePath + "/dtds/";
    4.82 -				string nsBase = "http://ibboard.co.uk/warfoundry/";
    4.83 -				AddSchemaToCache(cache, nsBase + "core", path + "warfoundry-core.xsd");
    4.84 -				AddSchemaToCache(cache, nsBase + "cats", path + "warfoundry-cats.xsd");
    4.85 -				AddSchemaToCache(cache, nsBase + "race", path + "race.xsd");
    4.86 -				AddSchemaToCache(cache, nsBase + "system", path + "system.xsd");
    4.87 -				AddSchemaToCache(cache, nsBase + "army", path + "army.xsd");
    4.88 -				settings.Schemas.Add(cache);
    4.89 -			}
    4.90 -			
    4.91 -			return settings;
    4.92 -		}
    4.93 -		
    4.94 -		private void ValidationEventMethod(object sender, ValidationEventArgs e)
    4.95 -		{
    4.96 -			throw new InvalidDataException("Problem validating against schema for WarFoundry data: " + e.Exception.Message, e.Exception);
    4.97 -		}
    4.98 -		
    4.99 -		private void AddSchemaToCache(XmlSchemaSet cache, string xmlNamespace, string schemaLocation)
   4.100 -		{
   4.101 -			try
   4.102 -			{
   4.103 -				cache.Add(xmlNamespace, schemaLocation);
   4.104 -			}
   4.105 -			catch (IOException ex)
   4.106 -			{
   4.107 -				LogNotifier.Warn(GetType(), "Problem reading schema: " + ex.Message, ex);
   4.108 -			}
   4.109 -			catch (XmlSchemaException ex)
   4.110 -			{
   4.111 -				LogNotifier.Warn(GetType(), "Problem validating schema for WarFoundry data: " + ex.Message, ex);
   4.112 -			}
   4.113 -			catch (XmlException ex)
   4.114 -			{
   4.115 -				LogNotifier.Warn(GetType(), "Problem reading data for schema: " + ex.Message, ex);
   4.116 -			}
   4.117 -		}
   4.118 -		
   4.119 -		private Army CreateArmyFromElement(ZipFile file, XmlElement elem)
   4.120 -		{
   4.121 -			string name = elem.GetAttribute("name");
   4.122 -			string systemID = elem.GetAttribute("gameSystem");
   4.123 -			GameSystem system = WarFoundryLoader.GetDefault().GetGameSystem(systemID);
   4.124 -			string raceID = elem.GetAttribute("race");
   4.125 -			Race race = WarFoundryLoader.GetDefault().GetRace(system, raceID);
   4.126 -			int points = XmlTools.GetIntValueFromAttribute(elem, "maxPoints");			
   4.127 -			Army army = new Army(race, name, points, file);
   4.128 -			//TODO: Complete loading of army
   4.129 -			return army;
   4.130 -		}
   4.131  
   4.132  		protected override Stream GetGameSystemDataStream (ZipFile file)
   4.133  		{
   4.134 @@ -178,21 +95,7 @@
   4.135  		{
   4.136  			XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.SYSTEM_ELEMENT);
   4.137  			LogNotifier.Debug(GetType(), "Create GameSystem");
   4.138 -			return CreateSystemFromElement(file, elem);
   4.139 -		}
   4.140 -		
   4.141 -		private GameSystem CreateSystemFromElement(ZipFile file, XmlElement elem)
   4.142 -		{
   4.143 -			string id = elem.GetAttribute("id");
   4.144 -			string name = elem.GetAttribute("name");
   4.145 -			GameSystem system = new GameSystem(id, name, this);
   4.146 -			StoreExtraData(system, elem);
   4.147 -			return system;
   4.148 -		}
   4.149 -		
   4.150 -		private void StoreExtraData(WarFoundryStagedLoadingObject wfObject, XmlElement elem)
   4.151 -		{
   4.152 -			extraData[wfObject] = elem.OwnerDocument;
   4.153 +			return gameSystemFactory.CreateSystemFromElement(file, elem);
   4.154  		}
   4.155  		
   4.156  		protected override Stream GetRaceDataStream (ZipFile file)
   4.157 @@ -204,56 +107,7 @@
   4.158  		{
   4.159  			XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.RACE_ELEMENT);
   4.160  			LogNotifier.Debug(GetType(), "Create Race");
   4.161 -			return CreateRaceFromElement(file, elem);
   4.162 -		}
   4.163 -		
   4.164 -		private Race CreateRaceFromElement(ZipFile file, XmlElement elem)
   4.165 -		{
   4.166 -			string id = elem.GetAttribute("id");
   4.167 -			string subid = elem.GetAttribute("subid");
   4.168 -			string systemID = elem.GetAttribute("system");
   4.169 -			string name = elem.GetAttribute("name");
   4.170 -			Race race = new Race(id, subid, name, systemID, this);
   4.171 -			StoreExtraData(race, elem);
   4.172 -			return race;
   4.173 -		}
   4.174 -	
   4.175 -		public XmlDocument GetExtraData(IWarFoundryObject obj)
   4.176 -		{
   4.177 -			XmlDocument extra = null;
   4.178 -			extraData.TryGetValue(obj, out extra);			
   4.179 -			return extra;
   4.180 -		}
   4.181 -		
   4.182 -		private XmlNamespaceManager GetNamespaceManager()
   4.183 -		{
   4.184 -			if (nsManager == null)
   4.185 -			{
   4.186 -				nsManager = new XmlNamespaceManager(new NameTable());
   4.187 -				nsManager.AddNamespace("core", "http://ibboard.co.uk/warfoundry/core");
   4.188 -				nsManager.AddNamespace("cat", "http://ibboard.co.uk/warfoundry/cats");
   4.189 -				nsManager.AddNamespace("race", "http://ibboard.co.uk/warfoundry/race");
   4.190 -				nsManager.AddNamespace("system", "http://ibboard.co.uk/warfoundry/system");
   4.191 -				nsManager.AddNamespace("army", "http://ibboard.co.uk/warfoundry/army");
   4.192 -			}
   4.193 -			
   4.194 -			return nsManager;
   4.195 -		}
   4.196 -		
   4.197 -		private XmlNodeList SelectNodes(XmlNode element, string xpathQuery)
   4.198 -		{
   4.199 -			return element.SelectNodes(xpathQuery, GetNamespaceManager());
   4.200 -		}
   4.201 -		
   4.202 -		private XmlNode SelectSingleNode(XmlNode element, string xpathQuery)
   4.203 -		{
   4.204 -			return element.SelectSingleNode(xpathQuery, GetNamespaceManager());
   4.205 -		}
   4.206 -		
   4.207 -		private XmlElement SelectSingleElement(XmlNode element, string xpathQuery)
   4.208 -		{
   4.209 -			XmlNode node = SelectSingleNode(element, xpathQuery);
   4.210 -			return (node is XmlElement) ? (XmlElement) node : null;
   4.211 +			return raceFactory.CreateRaceFromElement(file, elem);
   4.212  		}
   4.213  
   4.214  		public override void CompleteLoading(IWarFoundryStagedLoadObject obj)
   4.215 @@ -262,237 +116,12 @@
   4.216  							
   4.217  			if (obj is GameSystem)
   4.218  			{
   4.219 -				CompleteLoading((GameSystem)obj);
   4.220 +				gameSystemFactory.CompleteLoading((GameSystem)obj);
   4.221  			}
   4.222  			else if (obj is Race)
   4.223  			{
   4.224 -				CompleteLoading((Race)obj);
   4.225 +				raceFactory.CompleteLoading((Race)obj);
   4.226  			}
   4.227  		}
   4.228 -		
   4.229 -		public void CompleteLoading(GameSystem system)
   4.230 -		{
   4.231 -			if (!CanCompleteLoading(system))
   4.232 -			{
   4.233 -				return;
   4.234 -			}
   4.235 -			
   4.236 -			system.SetAsLoading();			
   4.237 -			XmlDocument extraData = GetExtraData(system);
   4.238 -			LoadCategoriesForSystem(system, extraData);
   4.239 -			XmlElement statsElem = SelectSingleElement(extraData, "/system:system/system:sysStatsList");
   4.240 -			string defaultStatsID = statsElem.GetAttribute("defaultStats");
   4.241 -			LoadSystemStatsForSystem(system, extraData);
   4.242 -			system.StandardSystemStatsID = defaultStatsID;
   4.243 -			XmlElement systemElement = SelectSingleElement(extraData, "/system:system");
   4.244 -			system.WarnOnError = XmlTools.GetBoolValueFromAttribute(systemElement, "warn");
   4.245 -			system.AllowAllies = XmlTools.GetBoolValueFromAttribute(systemElement, "allowAllies");
   4.246 -			LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.ID);
   4.247 -			LogNotifier.DebugFormat(GetType(), "GameSystem with ID {0} default stats: {1}", system.ID, system.StandardSystemStatsID);
   4.248 -			system.SetAsFullyLoaded();
   4.249 -		}
   4.250 -		
   4.251 -		private bool CanCompleteLoading(IWarFoundryStagedLoadObject obj)
   4.252 -		{
   4.253 -			bool canLoad = true;			
   4.254 -			
   4.255 -			if (obj.IsFullyLoaded)
   4.256 -			{
   4.257 -				LogNotifier.DebugFormat(GetType(), "Object of type {0} with ID {1} is already fully loaded", obj.GetType().Name, obj.ID);
   4.258 -				canLoad = false;
   4.259 -			}
   4.260 -			else if (obj.IsLoading)
   4.261 -			{
   4.262 -				LogNotifier.WarnFormat(GetType(), "Object of type {0} with ID {1} is already being loaded", obj.GetType().Name, obj.ID);
   4.263 -				canLoad = false;
   4.264 -			}
   4.265 -			
   4.266 -			return canLoad;
   4.267 -		}
   4.268 -		
   4.269 -		private void LoadCategoriesForSystem(GameSystem system, XmlNode elem)
   4.270 -		{
   4.271 -			foreach (XmlElement cat in SelectNodes(elem, "/system:system/system:categories/cat:cat"))
   4.272 -			{
   4.273 -				system.AddCategory(CreateCategoryFromElement(cat));
   4.274 -			}
   4.275 -		}
   4.276 -		
   4.277 -		private Category CreateCategoryFromElement(XmlElement elem)
   4.278 -		{
   4.279 -			string id = elem.GetAttribute("id");
   4.280 -			string name = elem.GetAttribute("name");
   4.281 -			Category cat = new Category(id, name);
   4.282 -			cat.MaximumPercentage = XmlTools.GetIntValueFromAttribute(elem, "maxPercentage");
   4.283 -			cat.MinimumPercentage = XmlTools.GetIntValueFromAttribute(elem, "minPercentage");
   4.284 -			cat.MaximumPoints = XmlTools.GetIntValueFromAttribute(elem, "maxPoints");
   4.285 -			cat.MinimumPoints = XmlTools.GetIntValueFromAttribute(elem, "minPoints");
   4.286 -			return cat;
   4.287 -		}		
   4.288 -		
   4.289 -		private void LoadSystemStatsForSystem(GameSystem system, XmlNode elem)
   4.290 -		{
   4.291 -			foreach (XmlElement stats in SelectNodes(elem, "/system:system/system:sysStatsList/system:sysStats"))
   4.292 -			{
   4.293 -				SystemStats sysStats = CreateSystemStatsFromElement(stats);
   4.294 -				system.AddSystemStats(sysStats);
   4.295 -			}
   4.296 -		}
   4.297 -		
   4.298 -		private SystemStats CreateSystemStatsFromElement(XmlElement elem)
   4.299 -		{
   4.300 -			List<StatSlot> slots = new List<StatSlot>();
   4.301 -			string id = elem.GetAttribute("id");	
   4.302 -			
   4.303 -			foreach (XmlElement slot in elem.ChildNodes)
   4.304 -			{
   4.305 -				StatSlot statSlot = new StatSlot(slot.GetAttribute("name"));
   4.306 -				slots.Add(statSlot);
   4.307 -			}
   4.308 -			
   4.309 -			return new SystemStats(id, slots.ToArray());
   4.310 -		}
   4.311 -		
   4.312 -		public void CompleteLoading(Race race)
   4.313 -		{
   4.314 -			if (!CanCompleteLoading(race))
   4.315 -			{
   4.316 -				return;
   4.317 -			}
   4.318 -			
   4.319 -			race.SetAsLoading();			
   4.320 -			XmlDocument extraData = GetExtraData(race);
   4.321 -			
   4.322 -			foreach (XmlElement node in SelectNodes(extraData, "/race:race/race:units/race:unit"))
   4.323 -			{
   4.324 -				UnitType type = CreateUnitTypeFromElement(node, race, race.GameSystem);
   4.325 -				race.AddUnitType(type);
   4.326 -			}
   4.327 -			
   4.328 -			foreach (XmlElement node in SelectNodes(extraData, "/race:race/race:categories/cat:cat"))
   4.329 -			{
   4.330 -				race.AddCategory(CreateCategoryFromElement(node));
   4.331 -			}
   4.332 -							
   4.333 -			foreach (XmlElement node  in SelectNodes(extraData, "/race:race/race:equipment/cat:equipmentItem"))
   4.334 -			{
   4.335 -				EquipmentItem item = CreateEquipmentItemFromElement(node, race);
   4.336 -				race.AddEquipmentItem(item);
   4.337 -			}
   4.338 -							
   4.339 -			foreach (XmlElement node  in SelectNodes(extraData, "/race:race/race:abilities/cat:ability"))
   4.340 -			{
   4.341 -				Ability ability = CreateAbilityFromElement(node, race);
   4.342 -				race.AddAbility(ability);
   4.343 -			}
   4.344 -			
   4.345 -			race.SetAsFullyLoaded();
   4.346 -			LogNotifier.DebugFormat(GetType(), "Completed loading of Race with ID {0}", race.ID);
   4.347 -		}
   4.348 -						
   4.349 -		private UnitType CreateUnitTypeFromElement(XmlElement elem, Race parentRace, GameSystem system)
   4.350 -		{
   4.351 -			string id = elem.GetAttribute("id");
   4.352 -			string name = elem.GetAttribute("typeName");
   4.353 -			UnitType type = new UnitType(id, name, parentRace);
   4.354 -			type.MaxNumber = XmlTools.GetIntValueFromAttribute(elem, "maxNum");
   4.355 -			type.MinNumber = XmlTools.GetIntValueFromAttribute(elem, "minNum");
   4.356 -			type.MaxSize = XmlTools.GetIntValueFromAttribute(elem, "maxSize");
   4.357 -			type.MinSize = XmlTools.GetIntValueFromAttribute(elem, "minSize");
   4.358 -			type.BaseSize = XmlTools.GetIntValueFromAttribute(elem, "baseSize");
   4.359 -			type.CostPerTrooper = XmlTools.GetIntValueFromAttribute(elem, "points");
   4.360 -			type.BaseUnitCost = XmlTools.GetIntValueFromAttribute(elem, "unitPoints");
   4.361 -			string mainCatID = elem.GetAttribute("cat");
   4.362 -			Category cat = parentRace.GetCategory(mainCatID);
   4.363 -			
   4.364 -			if (cat == null)
   4.365 -			{
   4.366 -				throw new InvalidDataException(String.Format("Attribute 'cat' of UnitType {0} (value: {1}) did not reference a valid category", id, mainCatID));
   4.367 -			}
   4.368 -			
   4.369 -			type.MainCategory = cat;
   4.370 -			XmlElement statsElement = SelectSingleElement(elem, "/race:race/race:units/race:unit/race:stats");
   4.371 -			type.UnitStats = ParseUnitStats(statsElement, system);
   4.372 -			//TODO: Add unit requirements
   4.373 -			LogNotifier.Debug(GetType(), "Loaded "+type.Name);
   4.374 -			return type;
   4.375 -		}
   4.376 -		
   4.377 -		private Stats ParseUnitStats(XmlElement elem, GameSystem system)
   4.378 -		{
   4.379 -			List<Stat> statsList = new List<Stat>();
   4.380 -			String statsID = elem.GetAttribute("statSet");
   4.381 -			SystemStats statsSet;
   4.382 -			
   4.383 -			if (statsID == "")
   4.384 -			{
   4.385 -				statsSet = system.StandardSystemStats;
   4.386 -			}
   4.387 -			else
   4.388 -			{
   4.389 -				statsSet = system.GetSystemStatsForID(statsID);
   4.390 -			}
   4.391 -			
   4.392 -			Stats stats = new Stats(statsSet);
   4.393 -			
   4.394 -			foreach (XmlElement stat in elem.ChildNodes)
   4.395 -			{
   4.396 -				String statID = stat.GetAttribute("name");
   4.397 -				StatSlot slot = statsSet[statID];
   4.398 -				
   4.399 -				if (slot!=null)
   4.400 -				{
   4.401 -					statsList.Add(new Stat(slot, stat.InnerText));
   4.402 -				}
   4.403 -				else
   4.404 -				{
   4.405 -					throw new InvalidFileException("The stat "+statID+" was not found in stats set "+statsID);
   4.406 -				}
   4.407 -			}
   4.408 -			
   4.409 -			stats.SetStats(statsList);
   4.410 -			
   4.411 -			return stats;
   4.412 -		}
   4.413 -		
   4.414 -		private EquipmentItem CreateEquipmentItemFromElement(XmlElement elem, Race race)
   4.415 -		{
   4.416 -			string id = elem.GetAttribute("id");
   4.417 -			string name = elem.GetAttribute("name");
   4.418 -			double cost = 0, min = 0, max = 0;
   4.419 -			ArmourType armourType;
   4.420 -			
   4.421 -			try
   4.422 -			{
   4.423 -				cost = XmlTools.GetDoubleValueFromAttribute(elem, "cost");
   4.424 -			}
   4.425 -			catch(FormatException ex)
   4.426 -			{
   4.427 -				throw new InvalidFileException("Attribute 'cost' of equipment item "+id+" was not a valid number", ex);
   4.428 -			}			
   4.429 -			
   4.430 -			try
   4.431 -			{
   4.432 -				armourType = (ArmourType)Enum.Parse(typeof(ArmourType), elem.GetAttribute("armourType"));
   4.433 -			}
   4.434 -			catch(ArgumentException ex)
   4.435 -			{
   4.436 -				throw new InvalidFileException("Attribute 'armourType' of equipment "+id+" was not a valid value from the enumeration", ex);
   4.437 -			}
   4.438 -			
   4.439 -			//TODO: Parse equipment stats if there are any
   4.440 -			
   4.441 -			return new EquipmentItem(id, name, cost, min, max, armourType, race);
   4.442 -		}
   4.443 -		
   4.444 -		private Ability CreateAbilityFromElement(XmlElement elem, Race race)
   4.445 -		{
   4.446 -			string id = elem.GetAttribute("id");
   4.447 -			string name = elem.GetAttribute("name");
   4.448 -			Ability ability = new Ability(id, name);
   4.449 -			XmlNode node = elem.SelectSingleNode("description", GetNamespaceManager());
   4.450 -			ability.Description = (node == null) ? "" : node.InnerText;
   4.451 -			return ability;
   4.452 -		}
   4.453  	}
   4.454  }
   4.455 \ No newline at end of file
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/api/Factories/Xml/WarFoundryXmlFactoryUtils.cs	Mon Mar 30 19:44:03 2009 +0000
     5.3 @@ -0,0 +1,144 @@
     5.4 +//  This file (WarFoundryXmlFactoryUtils.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard
     5.5 +// 
     5.6 +//  The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
     5.7 +// 
     5.8 +
     5.9 +using System;
    5.10 +using System.IO;
    5.11 +using System.Xml;
    5.12 +using System.Xml.Schema;
    5.13 +using IBBoard.Logging;
    5.14 +using IBBoard.WarFoundry.API.Objects;
    5.15 +
    5.16 +namespace IBBoard.WarFoundry.API.Factories.Xml
    5.17 +{
    5.18 +	/// <summary>
    5.19 +	/// A collection of useful utility methods for loading WarFoundry data from XML files
    5.20 +	/// </summary>
    5.21 +	public class WarFoundryXmlFactoryUtils
    5.22 +	{
    5.23 +		private static XmlReaderSettings settings;
    5.24 +		private static XmlNamespaceManager nsManager;
    5.25 +		
    5.26 +		public static XmlNodeList SelectNodes(XmlNode element, string xpathQuery)
    5.27 +		{
    5.28 +			return element.SelectNodes(xpathQuery, GetNamespaceManager());
    5.29 +		}
    5.30 +		
    5.31 +		public static XmlNode SelectSingleNode(XmlNode element, string xpathQuery)
    5.32 +		{
    5.33 +			return element.SelectSingleNode(xpathQuery, GetNamespaceManager());
    5.34 +		}
    5.35 +		
    5.36 +		public static XmlElement SelectSingleElement(XmlNode element, string xpathQuery)
    5.37 +		{
    5.38 +			XmlNode node = SelectSingleNode(element, xpathQuery);
    5.39 +			return (node is XmlElement) ? (XmlElement) node : null;
    5.40 +		}
    5.41 +				
    5.42 +		public static XmlNamespaceManager GetNamespaceManager()
    5.43 +		{
    5.44 +			if (nsManager == null)
    5.45 +			{
    5.46 +				nsManager = new XmlNamespaceManager(new NameTable());
    5.47 +				nsManager.AddNamespace("core", "http://ibboard.co.uk/warfoundry/core");
    5.48 +				nsManager.AddNamespace("cat", "http://ibboard.co.uk/warfoundry/cats");
    5.49 +				nsManager.AddNamespace("race", "http://ibboard.co.uk/warfoundry/race");
    5.50 +				nsManager.AddNamespace("system", "http://ibboard.co.uk/warfoundry/system");
    5.51 +				nsManager.AddNamespace("army", "http://ibboard.co.uk/warfoundry/army");
    5.52 +			}
    5.53 +			
    5.54 +			return nsManager;
    5.55 +		}
    5.56 +		
    5.57 +		/// <summary>
    5.58 +		/// Lazy-getter for XML reader settings. May throw a <see cref="InvalidDataException"/> if there is a problem with the translation schema.
    5.59 +		/// </summary>
    5.60 +		/// <returns>
    5.61 +		/// A <see cref="XmlReaderSettings"/> with the default values for validating the translation document against the translation schema
    5.62 +		/// </returns>
    5.63 +		public static XmlReaderSettings GetReaderSettings()
    5.64 +		{
    5.65 +			if (settings == null)
    5.66 +			{
    5.67 +				settings = new XmlReaderSettings();
    5.68 +				settings.ValidationType = ValidationType.Schema;
    5.69 +				settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
    5.70 +				settings.ProhibitDtd = true;
    5.71 +				settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod);
    5.72 +				XmlSchemaSet cache = new XmlSchemaSet();
    5.73 +				string path =  IBBoard.Constants.ExecutablePath + "/dtds/";
    5.74 +				string nsBase = "http://ibboard.co.uk/warfoundry/";
    5.75 +				AddSchemaToCache(cache, nsBase + "core", path + "warfoundry-core.xsd");
    5.76 +				AddSchemaToCache(cache, nsBase + "cats", path + "warfoundry-cats.xsd");
    5.77 +				AddSchemaToCache(cache, nsBase + "race", path + "race.xsd");
    5.78 +				AddSchemaToCache(cache, nsBase + "system", path + "system.xsd");
    5.79 +				AddSchemaToCache(cache, nsBase + "army", path + "army.xsd");
    5.80 +				settings.Schemas.Add(cache);
    5.81 +			}
    5.82 +			
    5.83 +			return settings;
    5.84 +		}
    5.85 +		
    5.86 +		private static void ValidationEventMethod(object sender, ValidationEventArgs e)
    5.87 +		{
    5.88 +			throw new InvalidDataException("Problem validating against schema for WarFoundry data: " + e.Exception.Message, e.Exception);
    5.89 +		}
    5.90 +		
    5.91 +		private static void AddSchemaToCache(XmlSchemaSet cache, string xmlNamespace, string schemaLocation)
    5.92 +		{
    5.93 +			try
    5.94 +			{
    5.95 +				cache.Add(xmlNamespace, schemaLocation);
    5.96 +			}
    5.97 +			catch (IOException ex)
    5.98 +			{
    5.99 +				LogNotifier.Warn(typeof(WarFoundryXmlFactoryUtils), "Problem reading schema: " + ex.Message, ex);
   5.100 +			}
   5.101 +			catch (XmlSchemaException ex)
   5.102 +			{
   5.103 +				LogNotifier.Warn(typeof(WarFoundryXmlFactoryUtils), "Problem validating schema for WarFoundry data: " + ex.Message, ex);
   5.104 +			}
   5.105 +			catch (XmlException ex)
   5.106 +			{
   5.107 +				LogNotifier.Warn(typeof(WarFoundryXmlFactoryUtils), "Problem reading data for schema: " + ex.Message, ex);
   5.108 +			}
   5.109 +		}
   5.110 +		
   5.111 +		public static XmlDocument CreateXmlDocumentFromStream(Stream stream)
   5.112 +		{
   5.113 +			XmlDocument doc = new XmlDocument();
   5.114 +			XmlReader reader = XmlReader.Create(stream, GetReaderSettings());
   5.115 +			
   5.116 +			try
   5.117 +			{
   5.118 +				doc.Load(reader);
   5.119 +			}
   5.120 +			//Don't catch XMLSchemaExceptions - let them get thrown out
   5.121 +			finally
   5.122 +			{
   5.123 +				reader.Close();
   5.124 +			}
   5.125 +
   5.126 +			return doc;
   5.127 +		}
   5.128 +		
   5.129 +		public static bool CanCompleteLoading(IWarFoundryStagedLoadObject obj)
   5.130 +		{
   5.131 +			bool canLoad = true;			
   5.132 +			
   5.133 +			if (obj.IsFullyLoaded)
   5.134 +			{
   5.135 +				LogNotifier.DebugFormat(typeof(WarFoundryXmlFactoryUtils), "Object of type {0} with ID {1} is already fully loaded", obj.GetType().Name, obj.ID);
   5.136 +				canLoad = false;
   5.137 +			}
   5.138 +			else if (obj.IsLoading)
   5.139 +			{
   5.140 +				LogNotifier.WarnFormat(typeof(WarFoundryXmlFactoryUtils), "Object of type {0} with ID {1} is already being loaded", obj.GetType().Name, obj.ID);
   5.141 +				canLoad = false;
   5.142 +			}
   5.143 +			
   5.144 +			return canLoad;
   5.145 +		}
   5.146 +	}
   5.147 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/api/Factories/Xml/WarFoundryXmlGameSystemFactory.cs	Mon Mar 30 19:44:03 2009 +0000
     6.3 @@ -0,0 +1,100 @@
     6.4 +//  This file (WarFoundryXmlGameSystemFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard
     6.5 +// 
     6.6 +//  The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
     6.7 +// 
     6.8 +
     6.9 +using System;
    6.10 +using System.Collections.Generic;
    6.11 +using System.Xml;
    6.12 +using ICSharpCode.SharpZipLib.Zip;
    6.13 +using IBBoard.Logging;
    6.14 +using IBBoard.Xml;
    6.15 +using IBBoard.WarFoundry.API.Objects;
    6.16 +
    6.17 +namespace IBBoard.WarFoundry.API.Factories.Xml
    6.18 +{
    6.19 +	/// <summary>
    6.20 +	/// A sub-factory specifically for loading GameSystems from WarFoundry XML files
    6.21 +	/// </summary>
    6.22 +	public class WarFoundryXmlGameSystemFactory : AbstractStagedLoadedSubFactory
    6.23 +	{	
    6.24 +		private Dictionary<GameSystem, XmlDocument> extraData = new Dictionary<GameSystem, XmlDocument>();
    6.25 +		
    6.26 +		public WarFoundryXmlGameSystemFactory(WarFoundryXmlFactory factory) : base(factory)
    6.27 +		{
    6.28 +		}
    6.29 +		
    6.30 +		private void StoreExtraData(GameSystem wfObject, XmlElement elem)
    6.31 +		{
    6.32 +			extraData[wfObject] = elem.OwnerDocument;
    6.33 +		}
    6.34 +	
    6.35 +		private XmlDocument GetExtraData(GameSystem obj)
    6.36 +		{
    6.37 +			XmlDocument extra = null;
    6.38 +			extraData.TryGetValue(obj, out extra);			
    6.39 +			return extra;
    6.40 +		}
    6.41 +		
    6.42 +		public GameSystem CreateSystemFromElement(ZipFile file, XmlElement elem)
    6.43 +		{
    6.44 +			string id = elem.GetAttribute("id");
    6.45 +			string name = elem.GetAttribute("name");
    6.46 +			GameSystem system = new GameSystem(id, name, mainFactory);
    6.47 +			StoreExtraData(system, elem);
    6.48 +			return system;
    6.49 +		}		
    6.50 +		
    6.51 +		public void CompleteLoading(GameSystem system)
    6.52 +		{
    6.53 +			if (!WarFoundryXmlFactoryUtils.CanCompleteLoading(system))
    6.54 +			{
    6.55 +				return;
    6.56 +			}
    6.57 +			
    6.58 +			system.SetAsLoading();			
    6.59 +			XmlDocument extraData = GetExtraData(system);
    6.60 +			LoadCategoriesForSystem(system, extraData);
    6.61 +			XmlElement statsElem = WarFoundryXmlFactoryUtils.SelectSingleElement(extraData, "/system:system/system:sysStatsList");
    6.62 +			string defaultStatsID = statsElem.GetAttribute("defaultStats");
    6.63 +			LoadSystemStatsForSystem(system, extraData);
    6.64 +			system.StandardSystemStatsID = defaultStatsID;
    6.65 +			XmlElement systemElement = WarFoundryXmlFactoryUtils.SelectSingleElement(extraData, "/system:system");
    6.66 +			system.WarnOnError = XmlTools.GetBoolValueFromAttribute(systemElement, "warn");
    6.67 +			system.AllowAllies = XmlTools.GetBoolValueFromAttribute(systemElement, "allowAllies");
    6.68 +			LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.ID);
    6.69 +			LogNotifier.DebugFormat(GetType(), "GameSystem with ID {0} default stats: {1}", system.ID, system.StandardSystemStatsID);
    6.70 +			system.SetAsFullyLoaded();
    6.71 +		}
    6.72 +		
    6.73 +		private void LoadCategoriesForSystem(GameSystem system, XmlNode elem)
    6.74 +		{
    6.75 +			foreach (XmlElement cat in WarFoundryXmlFactoryUtils.SelectNodes(elem, "/system:system/system:categories/cat:cat"))
    6.76 +			{
    6.77 +				system.AddCategory(CreateCategoryFromElement(cat));
    6.78 +			}
    6.79 +		}	
    6.80 +		
    6.81 +		private void LoadSystemStatsForSystem(GameSystem system, XmlNode elem)
    6.82 +		{
    6.83 +			foreach (XmlElement stats in WarFoundryXmlFactoryUtils.SelectNodes(elem, "/system:system/system:sysStatsList/system:sysStats"))
    6.84 +			{
    6.85 +				SystemStats sysStats = CreateSystemStatsFromElement(stats);
    6.86 +				system.AddSystemStats(sysStats);
    6.87 +			}
    6.88 +		}
    6.89 +		
    6.90 +		private SystemStats CreateSystemStatsFromElement(XmlElement elem)
    6.91 +		{
    6.92 +			List<StatSlot> slots = new List<StatSlot>();
    6.93 +			string id = elem.GetAttribute("id");	
    6.94 +			
    6.95 +			foreach (XmlElement slot in elem.ChildNodes)
    6.96 +			{
    6.97 +				StatSlot statSlot = new StatSlot(slot.GetAttribute("name"));
    6.98 +				slots.Add(statSlot);
    6.99 +			}
   6.100 +			
   6.101 +			return new SystemStats(id, slots.ToArray());
   6.102 +		}	}
   6.103 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/api/Factories/Xml/WarFoundryXmlRaceFactory.cs	Mon Mar 30 19:44:03 2009 +0000
     7.3 @@ -0,0 +1,194 @@
     7.4 +//  This file (WarFoundryXmlRaceFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard
     7.5 +// 
     7.6 +//  The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
     7.7 +// 
     7.8 +
     7.9 +using System;
    7.10 +using System.Collections.Generic;
    7.11 +using System.IO;
    7.12 +using System.Xml;
    7.13 +using IBBoard.Xml;
    7.14 +using IBBoard.IO;
    7.15 +using IBBoard.Logging;
    7.16 +using ICSharpCode.SharpZipLib.Zip;
    7.17 +using IBBoard.WarFoundry.API.Objects;
    7.18 +
    7.19 +namespace IBBoard.WarFoundry.API.Factories.Xml
    7.20 +{
    7.21 +	/// <summary>
    7.22 +	/// A sub-factory for loading WarFoundry Race XML files
    7.23 +	/// </summary>
    7.24 +	public class WarFoundryXmlRaceFactory : AbstractStagedLoadedSubFactory
    7.25 +	{
    7.26 +		private Dictionary<Race, XmlDocument> extraData = new Dictionary<Race, XmlDocument>();
    7.27 +		
    7.28 +		public WarFoundryXmlRaceFactory(WarFoundryXmlFactory factory) : base (factory)
    7.29 +		{
    7.30 +		}
    7.31 +		
    7.32 +		private void StoreExtraData(Race wfObject, XmlElement elem)
    7.33 +		{
    7.34 +			extraData[wfObject] = elem.OwnerDocument;
    7.35 +		}
    7.36 +	
    7.37 +		private XmlDocument GetExtraData(Race obj)
    7.38 +		{
    7.39 +			XmlDocument extra = null;
    7.40 +			extraData.TryGetValue(obj, out extra);			
    7.41 +			return extra;
    7.42 +		}
    7.43 +		
    7.44 +		public Race CreateRaceFromElement(ZipFile file, XmlElement elem)
    7.45 +		{
    7.46 +			string id = elem.GetAttribute("id");
    7.47 +			string subid = elem.GetAttribute("subid");
    7.48 +			string systemID = elem.GetAttribute("system");
    7.49 +			string name = elem.GetAttribute("name");
    7.50 +			Race race = new Race(id, subid, name, systemID, mainFactory);
    7.51 +			StoreExtraData(race, elem);
    7.52 +			return race;
    7.53 +		}
    7.54 +		
    7.55 +		public void CompleteLoading(Race race)
    7.56 +		{
    7.57 +			if (!WarFoundryXmlFactoryUtils.CanCompleteLoading(race))
    7.58 +			{
    7.59 +				return;
    7.60 +			}
    7.61 +			
    7.62 +			race.SetAsLoading();			
    7.63 +			XmlDocument extraData = GetExtraData(race);
    7.64 +			
    7.65 +			foreach (XmlElement node in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:units/race:unit"))
    7.66 +			{
    7.67 +				UnitType type = CreateUnitTypeFromElement(node, race, race.GameSystem);
    7.68 +				race.AddUnitType(type);
    7.69 +			}
    7.70 +			
    7.71 +			foreach (XmlElement node in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:categories/cat:cat"))
    7.72 +			{
    7.73 +				race.AddCategory(CreateCategoryFromElement(node));
    7.74 +			}
    7.75 +							
    7.76 +			foreach (XmlElement node  in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:equipment/cat:equipmentItem"))
    7.77 +			{
    7.78 +				EquipmentItem item = CreateEquipmentItemFromElement(node, race);
    7.79 +				race.AddEquipmentItem(item);
    7.80 +			}
    7.81 +							
    7.82 +			foreach (XmlElement node  in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:abilities/cat:ability"))
    7.83 +			{
    7.84 +				Ability ability = CreateAbilityFromElement(node, race);
    7.85 +				race.AddAbility(ability);
    7.86 +			}
    7.87 +			
    7.88 +			race.SetAsFullyLoaded();
    7.89 +			LogNotifier.DebugFormat(GetType(), "Completed loading of Race with ID {0}", race.ID);
    7.90 +		}
    7.91 +						
    7.92 +		private UnitType CreateUnitTypeFromElement(XmlElement elem, Race parentRace, GameSystem system)
    7.93 +		{
    7.94 +			string id = elem.GetAttribute("id");
    7.95 +			string name = elem.GetAttribute("typeName");
    7.96 +			UnitType type = new UnitType(id, name, parentRace);
    7.97 +			type.MaxNumber = XmlTools.GetIntValueFromAttribute(elem, "maxNum");
    7.98 +			type.MinNumber = XmlTools.GetIntValueFromAttribute(elem, "minNum");
    7.99 +			type.MaxSize = XmlTools.GetIntValueFromAttribute(elem, "maxSize");
   7.100 +			type.MinSize = XmlTools.GetIntValueFromAttribute(elem, "minSize");
   7.101 +			type.BaseSize = XmlTools.GetIntValueFromAttribute(elem, "baseSize");
   7.102 +			type.CostPerTrooper = XmlTools.GetIntValueFromAttribute(elem, "points");
   7.103 +			type.BaseUnitCost = XmlTools.GetIntValueFromAttribute(elem, "unitPoints");
   7.104 +			string mainCatID = elem.GetAttribute("cat");
   7.105 +			Category cat = parentRace.GetCategory(mainCatID);
   7.106 +			
   7.107 +			if (cat == null)
   7.108 +			{
   7.109 +				throw new InvalidDataException(String.Format("Attribute 'cat' of UnitType {0} (value: {1}) did not reference a valid category", id, mainCatID));
   7.110 +			}
   7.111 +			
   7.112 +			type.MainCategory = cat;
   7.113 +			XmlElement statsElement = WarFoundryXmlFactoryUtils.SelectSingleElement(elem, "/race:race/race:units/race:unit/race:stats");
   7.114 +			type.UnitStats = ParseUnitStats(statsElement, system);
   7.115 +			//TODO: Add unit requirements
   7.116 +			LogNotifier.Debug(GetType(), "Loaded "+type.Name);
   7.117 +			return type;
   7.118 +		}
   7.119 +		
   7.120 +		private Stats ParseUnitStats(XmlElement elem, GameSystem system)
   7.121 +		{
   7.122 +			List<Stat> statsList = new List<Stat>();
   7.123 +			String statsID = elem.GetAttribute("statSet");
   7.124 +			SystemStats statsSet;
   7.125 +			
   7.126 +			if (statsID == "")
   7.127 +			{
   7.128 +				statsSet = system.StandardSystemStats;
   7.129 +			}
   7.130 +			else
   7.131 +			{
   7.132 +				statsSet = system.GetSystemStatsForID(statsID);
   7.133 +			}
   7.134 +			
   7.135 +			Stats stats = new Stats(statsSet);
   7.136 +			
   7.137 +			foreach (XmlElement stat in elem.ChildNodes)
   7.138 +			{
   7.139 +				String statID = stat.GetAttribute("name");
   7.140 +				StatSlot slot = statsSet[statID];
   7.141 +				
   7.142 +				if (slot!=null)
   7.143 +				{
   7.144 +					statsList.Add(new Stat(slot, stat.InnerText));
   7.145 +				}
   7.146 +				else
   7.147 +				{
   7.148 +					throw new InvalidFileException("The stat "+statID+" was not found in stats set "+statsID);
   7.149 +				}
   7.150 +			}
   7.151 +			
   7.152 +			stats.SetStats(statsList);
   7.153 +			
   7.154 +			return stats;
   7.155 +		}
   7.156 +		
   7.157 +		private EquipmentItem CreateEquipmentItemFromElement(XmlElement elem, Race race)
   7.158 +		{
   7.159 +			string id = elem.GetAttribute("id");
   7.160 +			string name = elem.GetAttribute("name");
   7.161 +			double cost = 0, min = 0, max = 0;
   7.162 +			ArmourType armourType;
   7.163 +			
   7.164 +			try
   7.165 +			{
   7.166 +				cost = XmlTools.GetDoubleValueFromAttribute(elem, "cost");
   7.167 +			}
   7.168 +			catch(FormatException ex)
   7.169 +			{
   7.170 +				throw new InvalidFileException("Attribute 'cost' of equipment item "+id+" was not a valid number", ex);
   7.171 +			}			
   7.172 +			
   7.173 +			try
   7.174 +			{
   7.175 +				armourType = (ArmourType)Enum.Parse(typeof(ArmourType), elem.GetAttribute("armourType"));
   7.176 +			}
   7.177 +			catch(ArgumentException ex)
   7.178 +			{
   7.179 +				throw new InvalidFileException("Attribute 'armourType' of equipment "+id+" was not a valid value from the enumeration", ex);
   7.180 +			}
   7.181 +			
   7.182 +			//TODO: Parse equipment stats if there are any
   7.183 +			
   7.184 +			return new EquipmentItem(id, name, cost, min, max, armourType, race);
   7.185 +		}
   7.186 +		
   7.187 +		private Ability CreateAbilityFromElement(XmlElement elem, Race race)
   7.188 +		{
   7.189 +			string id = elem.GetAttribute("id");
   7.190 +			string name = elem.GetAttribute("name");
   7.191 +			Ability ability = new Ability(id, name);
   7.192 +			XmlNode node = elem.SelectSingleNode("description", WarFoundryXmlFactoryUtils.GetNamespaceManager());
   7.193 +			ability.Description = (node == null) ? "" : node.InnerText;
   7.194 +			return ability;
   7.195 +		}
   7.196 +	}
   7.197 +}