Mercurial > repos > IBBoard.WarFoundry.API
comparison API/Factories/Xml/WarFoundryXmlArmyParser.cs @ 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 | 3c4a6403a88c |
children |
comparison
equal
deleted
inserted
replaced
495:55b39514cbf8 | 496:00d6cf940c3c |
---|---|
60 } | 60 } |
61 | 61 |
62 private void LoadUnits() | 62 private void LoadUnits() |
63 { | 63 { |
64 units = new Dictionary<string, Unit>(); | 64 units = new Dictionary<string, Unit>(); |
65 Dictionary<string, ICollection<string>> containedUnits = new Dictionary<string, ICollection<string>>(); | |
65 | 66 |
66 foreach (XmlElement unitElem in WarFoundryXmlFactoryUtils.SelectNodes(elem, "/army:army/army:units/army:unit")) | 67 foreach (XmlElement unitElem in WarFoundryXmlFactoryUtils.SelectNodes(elem, "/army:army/army:units/army:unit")) |
67 { | 68 { |
68 string id = unitElem.GetAttribute("id"); | 69 string id = unitElem.GetAttribute("id"); |
69 | 70 |
86 if (cat == null) | 87 if (cat == null) |
87 { | 88 { |
88 cat = unitType.MainCategory; | 89 cat = unitType.MainCategory; |
89 } | 90 } |
90 | 91 |
92 XmlNodeList containedElems = WarFoundryXmlFactoryUtils.SelectNodes(unitElem, "army:contained/army:containedUnit"); | |
93 | |
94 if (containedElems.Count > 0) | |
95 { | |
96 containedUnits[id] = GetContainedIDs(containedElems); | |
97 } | |
98 | |
99 | |
91 Unit unit = new Unit(id, name, size, unitType, army.GetCategory(cat)); | 100 Unit unit = new Unit(id, name, size, unitType, army.GetCategory(cat)); |
92 army.AddUnit(unit, cat); | 101 army.AddUnit(unit, cat); |
93 units.Add(id, unit); | 102 units.Add(id, unit); |
94 | 103 |
95 LoadUnitEquipment(unitElem, unit); | 104 LoadUnitEquipment(unitElem, unit); |
96 } | 105 } |
97 else | 106 else |
98 { | 107 { |
99 throw new InvalidFileException("Duplicate unit ID found in army file: "+id); | 108 throw new InvalidFileException("Duplicate unit ID found in army file: " + id); |
100 } | 109 } |
101 } | 110 } |
111 | |
112 AddParenting(containedUnits); | |
113 } | |
114 | |
115 private ICollection<string> GetContainedIDs(XmlNodeList containedElems) | |
116 { | |
117 ICollection<string> ids = new List<string>(); | |
118 | |
119 foreach (XmlElement elem in containedElems) | |
120 { | |
121 ids.Add(elem.GetAttribute("containedID")); | |
122 } | |
123 | |
124 return ids; | |
102 } | 125 } |
103 | 126 |
104 private void LoadUnitEquipment(XmlElement unitElem, Unit unit) | 127 private void LoadUnitEquipment(XmlElement unitElem, Unit unit) |
105 { | 128 { |
106 foreach (XmlElement elem in WarFoundryXmlFactoryUtils.SelectNodes(unitElem, "army:equipment/army:equipItem")) | 129 foreach (XmlElement elem in WarFoundryXmlFactoryUtils.SelectNodes(unitElem, "army:equipment/army:equipItem")) |
121 unit.SetEquipmentRatio(item, amount); | 144 unit.SetEquipmentRatio(item, amount); |
122 } | 145 } |
123 else | 146 else |
124 { | 147 { |
125 //amount should be a whole number, so do type-cast rounding | 148 //amount should be a whole number, so do type-cast rounding |
126 unit.SetEquipmentAmount(item, (int) amount); | 149 unit.SetEquipmentAmount(item, (int)amount); |
150 } | |
151 } | |
152 } | |
153 | |
154 private void AddParenting(Dictionary<string, ICollection<string>> containedUnits) | |
155 { | |
156 foreach (KeyValuePair<string, ICollection<string>> parenting in containedUnits) | |
157 { | |
158 Unit parent = units[parenting.Key]; | |
159 | |
160 foreach (string childID in parenting.Value) | |
161 { | |
162 parent.AddContainedUnit(units[childID]); | |
127 } | 163 } |
128 } | 164 } |
129 } | 165 } |
130 } | 166 } |
131 } | 167 } |