changeset 496:00d6cf940c3c

Re #420: Saved army does not save "contained" structure * Add loading of nesting from .army files
author IBBoard <dev@ibboard.co.uk>
date Sat, 01 Sep 2012 15:28:26 +0100
parents 55b39514cbf8
children cd367acd7c48
files API/Factories/Xml/WarFoundryXmlArmyParser.cs
diffstat 1 files changed, 38 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/API/Factories/Xml/WarFoundryXmlArmyParser.cs	Sat Sep 01 12:04:03 2012 +0100
+++ b/API/Factories/Xml/WarFoundryXmlArmyParser.cs	Sat Sep 01 15:28:26 2012 +0100
@@ -62,6 +62,7 @@
 		private void LoadUnits()
 		{
 			units = new Dictionary<string, Unit>();
+			Dictionary<string, ICollection<string>> containedUnits = new Dictionary<string, ICollection<string>>();
 
 			foreach (XmlElement unitElem in WarFoundryXmlFactoryUtils.SelectNodes(elem, "/army:army/army:units/army:unit"))
 			{
@@ -88,6 +89,14 @@
 						cat = unitType.MainCategory;
 					}
 
+					XmlNodeList containedElems = WarFoundryXmlFactoryUtils.SelectNodes(unitElem, "army:contained/army:containedUnit");
+
+					if (containedElems.Count > 0)
+					{
+						containedUnits[id] = GetContainedIDs(containedElems);
+					}
+
+
 					Unit unit = new Unit(id, name, size, unitType, army.GetCategory(cat));
 					army.AddUnit(unit, cat);
 					units.Add(id, unit);
@@ -96,9 +105,23 @@
 				}
 				else
 				{
-					throw new InvalidFileException("Duplicate unit ID found in army file: "+id);
+					throw new InvalidFileException("Duplicate unit ID found in army file: " + id);
 				}
 			}
+
+			AddParenting(containedUnits);
+		}
+
+		private ICollection<string> GetContainedIDs(XmlNodeList containedElems)
+		{
+			ICollection<string> ids = new List<string>();
+
+			foreach (XmlElement elem in containedElems)
+			{
+				ids.Add(elem.GetAttribute("containedID"));
+			}
+
+			return ids;
 		}
 
 		private void LoadUnitEquipment(XmlElement unitElem, Unit unit)
@@ -123,7 +146,20 @@
 				else
 				{
 					//amount should be a whole number, so do type-cast rounding
-					unit.SetEquipmentAmount(item, (int) amount);
+					unit.SetEquipmentAmount(item, (int)amount);
+				}
+			}
+		}
+
+		private void AddParenting(Dictionary<string, ICollection<string>> containedUnits)
+		{
+			foreach (KeyValuePair<string, ICollection<string>> parenting in containedUnits)
+			{
+				Unit parent = units[parenting.Key];
+
+				foreach (string childID in parenting.Value)
+				{
+					parent.AddContainedUnit(units[childID]);
 				}
 			}
 		}