# HG changeset patch # User IBBoard # Date 1346509706 -3600 # Node ID 00d6cf940c3cade1ac6c442bb254e5f8e0737cd4 # Parent 55b39514cbf8dcd80948517c5c065c628e17f71e Re #420: Saved army does not save "contained" structure * Add loading of nesting from .army files diff -r 55b39514cbf8 -r 00d6cf940c3c API/Factories/Xml/WarFoundryXmlArmyParser.cs --- 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(); + Dictionary> containedUnits = new Dictionary>(); 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 GetContainedIDs(XmlNodeList containedElems) + { + ICollection ids = new List(); + + 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> containedUnits) + { + foreach (KeyValuePair> parenting in containedUnits) + { + Unit parent = units[parenting.Key]; + + foreach (string childID in parenting.Value) + { + parent.AddContainedUnit(units[childID]); } } }