Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate 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 |
rev | line source |
---|---|
337
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
1 // This file (WarFoundryXmlArmyParser.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 // |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
3 // 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. |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 using System; |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 using System.Collections.Generic; |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 using System.Xml; |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 using IBBoard.IO; |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 using IBBoard.Xml; |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 using ICSharpCode.SharpZipLib.Zip; |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 using IBBoard.WarFoundry.API.Objects; |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 namespace IBBoard.WarFoundry.API.Factories.Xml |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 { |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 public class WarFoundryXmlArmyParser |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 { |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 private ZipFile file; |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 private XmlElement elem; |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
19 private Army army; |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
20 private Dictionary<String, Unit> units; |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
21 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
22 public WarFoundryXmlArmyParser(ZipFile file, XmlElement elem) |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
23 { |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
24 this.file = file; |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
25 this.elem = elem; |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
26 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
27 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
28 public Army GetArmy() |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
29 { |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
30 if (army == null) |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
31 { |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
32 ParseArmy(); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
33 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
34 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
35 return army; |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
36 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
37 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
38 private void ParseArmy() |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
39 { |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
40 string name = elem.GetAttribute("name"); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
41 string systemID = elem.GetAttribute("system"); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
42 GameSystem system = WarFoundryLoader.GetDefault().GetGameSystem(systemID); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
43 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
44 if (system == null) |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
45 { |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
46 throw new RequiredDataMissingException(file.Name, "Game System", systemID); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
47 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
48 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
49 string raceID = elem.GetAttribute("race"); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
50 Race race = WarFoundryLoader.GetDefault().GetRace(system, raceID); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
51 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
52 if (race == null) |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
53 { |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
54 throw new RequiredDataMissingException(file.Name, "Race", raceID); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
55 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
56 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
57 int points = XmlTools.GetIntValueFromAttribute(elem, "maxPoints"); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
58 army = new Army(race, name, points, file); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
59 LoadUnits(); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
60 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
61 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
62 private void LoadUnits() |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
63 { |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
64 units = new Dictionary<string, Unit>(); |
496
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
65 Dictionary<string, ICollection<string>> containedUnits = new Dictionary<string, ICollection<string>>(); |
337
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
66 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
67 foreach (XmlElement unitElem in WarFoundryXmlFactoryUtils.SelectNodes(elem, "/army:army/army:units/army:unit")) |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
68 { |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
69 string id = unitElem.GetAttribute("id"); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
70 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
71 if (!units.ContainsKey(id)) |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
72 { |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
73 string unitTypeId = unitElem.GetAttribute("unitType"); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
74 UnitType unitType = army.Race.GetUnitType(unitTypeId); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
75 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
76 if (unitType == null) |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
77 { |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
78 throw new RequiredDataMissingException(file.Name, "Unit Type", unitTypeId); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
79 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
80 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
81 string name = unitElem.GetAttribute("unitName"); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
82 int size = XmlTools.GetIntValueFromAttribute(unitElem, "size"); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
83 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
84 string catID = unitElem.GetAttribute("category"); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
85 Category cat = army.Race.GetCategory(catID); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
86 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
87 if (cat == null) |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
88 { |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
89 cat = unitType.MainCategory; |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
90 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
91 |
496
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
92 XmlNodeList containedElems = WarFoundryXmlFactoryUtils.SelectNodes(unitElem, "army:contained/army:containedUnit"); |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
93 |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
94 if (containedElems.Count > 0) |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
95 { |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
96 containedUnits[id] = GetContainedIDs(containedElems); |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
97 } |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
98 |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
99 |
337
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
100 Unit unit = new Unit(id, name, size, unitType, army.GetCategory(cat)); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
101 army.AddUnit(unit, cat); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
102 units.Add(id, unit); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
103 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
104 LoadUnitEquipment(unitElem, unit); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
105 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
106 else |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
107 { |
496
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
108 throw new InvalidFileException("Duplicate unit ID found in army file: " + id); |
337
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
109 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
110 } |
496
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
111 |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
112 AddParenting(containedUnits); |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
113 } |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
114 |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
115 private ICollection<string> GetContainedIDs(XmlNodeList containedElems) |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
116 { |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
117 ICollection<string> ids = new List<string>(); |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
118 |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
119 foreach (XmlElement elem in containedElems) |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
120 { |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
121 ids.Add(elem.GetAttribute("containedID")); |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
122 } |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
123 |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
124 return ids; |
337
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
125 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
126 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
127 private void LoadUnitEquipment(XmlElement unitElem, Unit unit) |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
128 { |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
129 foreach (XmlElement elem in WarFoundryXmlFactoryUtils.SelectNodes(unitElem, "army:equipment/army:equipItem")) |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
130 { |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
131 string equipID = elem.GetAttribute("id"); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
132 UnitEquipmentItem item = unit.UnitType.GetEquipmentItem(equipID); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
133 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
134 if (item == null) |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
135 { |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
136 throw new RequiredDataMissingException(file.Name, "Equipment Item", equipID); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
137 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
138 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
139 double amount = XmlTools.GetDoubleValueFromAttribute(elem, "amount"); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
140 string equipTypeString = elem.GetAttribute("amountType"); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
141 |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
142 if (equipTypeString == "ratio") |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
143 { |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
144 unit.SetEquipmentRatio(item, amount); |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
145 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
146 else |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
147 { |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
148 //amount should be a whole number, so do type-cast rounding |
496
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
149 unit.SetEquipmentAmount(item, (int)amount); |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
150 } |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
151 } |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
152 } |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
153 |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
154 private void AddParenting(Dictionary<string, ICollection<string>> containedUnits) |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
155 { |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
156 foreach (KeyValuePair<string, ICollection<string>> parenting in containedUnits) |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
157 { |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
158 Unit parent = units[parenting.Key]; |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
159 |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
160 foreach (string childID in parenting.Value) |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
161 { |
00d6cf940c3c
Re #420: Saved army does not save "contained" structure
IBBoard <dev@ibboard.co.uk>
parents:
337
diff
changeset
|
162 parent.AddContainedUnit(units[childID]); |
337
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
163 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
164 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
165 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
166 } |
3c4a6403a88c
* Fix capitalisation so that new files are in the namespace
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
167 } |