diff api/Factories/Xml/WarFoundryXmlArmyParser.cs @ 117:093ee2da0f6e

Re #54: Add Army support to WarFoundryFactory * Add starts of army unit loading
author IBBoard <dev@ibboard.co.uk>
date Sat, 22 Aug 2009 20:02:29 +0000
parents 2010a7e8bf9a
children 0cdc0b07fe11
line wrap: on
line diff
--- a/api/Factories/Xml/WarFoundryXmlArmyParser.cs	Sat Aug 22 19:42:04 2009 +0000
+++ b/api/Factories/Xml/WarFoundryXmlArmyParser.cs	Sat Aug 22 20:02:29 2009 +0000
@@ -3,6 +3,7 @@
 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
 
 using System;
+using System.Collections.Generic;
 using System.Xml;
 using IBBoard.Xml;
 using ICSharpCode.SharpZipLib.Zip;
@@ -15,6 +16,7 @@
 		private ZipFile file;
 		private XmlElement elem;
 		private Army army;
+		private Dictionary<String, Unit> units;
 		
 		public WarFoundryXmlArmyParser(ZipFile file, XmlElement elem)
 		{
@@ -53,7 +55,32 @@
 			
 			int points = XmlTools.GetIntValueFromAttribute(elem, "maxPoints");			
 			army = new Army(race, name, points, file);
-			//TODO: Complete loading of army
+			LoadUnits();
+		}
+
+		private void LoadUnits()
+		{
+			units = new Dictionary<string, Unit>();
+
+			foreach (XmlElement unitElem in WarFoundryXmlFactoryUtils.SelectNodes(elem, "/army:army/army:units/army:unit"))
+			{
+				string id = unitElem.GetAttribute("id");
+				Unit unit = DictionaryUtils.GetValue(units, id);
+
+				if (unit == null)
+				{
+					string unitTypeId = unitElem.GetAttribute("unitType");
+					UnitType unitType = army.Race.GetUnitType(unitTypeId);
+
+					if (unitType == null)
+					{
+						throw new RequiredDataMissingException(file.Name, "unitType", unitTypeId);
+					}
+
+					unit = new Unit(unitType, army.GetCategory(unitType.MainCategory));
+					units.Add(id, unit);
+				}
+			}
 		}
 	}
 }