Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate api/Factories/Xml/WarFoundryXmlArmyParser.cs @ 128:45a9452579a2
Fixes #50: Complete core loading of WarFoundry XML files
* Add exceptions on invalid data - completes WarFoundry 0.1 loading needs
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Wed, 26 Aug 2009 18:37:55 +0000 |
parents | 52e8c3cdde10 |
children | df61d26c23fb |
rev | line source |
---|---|
116
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
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 |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 // |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
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. |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 using System; |
117
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
6 using System.Collections.Generic; |
116
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 using System.Xml; |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 using IBBoard.Xml; |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 using ICSharpCode.SharpZipLib.Zip; |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 using IBBoard.WarFoundry.API.Objects; |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 namespace IBBoard.WarFoundry.API.Factories.Xml |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 { |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 public class WarFoundryXmlArmyParser |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 { |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 private ZipFile file; |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 private XmlElement elem; |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 private Army army; |
117
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
19 private Dictionary<String, Unit> units; |
116
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
20 |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
21 public WarFoundryXmlArmyParser(ZipFile file, XmlElement elem) |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
22 { |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
23 this.file = file; |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
24 this.elem = elem; |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
25 } |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
26 |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
27 public Army GetArmy() |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
28 { |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
29 if (army == null) |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
30 { |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
31 ParseArmy(); |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
32 } |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
33 |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
34 return army; |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
35 } |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
36 |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
37 private void ParseArmy() |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
38 { |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
39 string name = elem.GetAttribute("name"); |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
40 string systemID = elem.GetAttribute("system"); |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
41 GameSystem system = WarFoundryLoader.GetDefault().GetGameSystem(systemID); |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
42 |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
43 if (system == null) |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
44 { |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
45 throw new RequiredDataMissingException(file.Name, "gameSystem", systemID); |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
46 } |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
47 |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
48 string raceID = elem.GetAttribute("race"); |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
49 Race race = WarFoundryLoader.GetDefault().GetRace(system, raceID); |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
50 |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
51 if (race == null) |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
52 { |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
53 throw new RequiredDataMissingException(file.Name, "race", raceID); |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
54 } |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
55 |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
56 int points = XmlTools.GetIntValueFromAttribute(elem, "maxPoints"); |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
57 army = new Army(race, name, points, file); |
117
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
58 LoadUnits(); |
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
59 } |
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
60 |
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
61 private void LoadUnits() |
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
62 { |
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
63 units = new Dictionary<string, Unit>(); |
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
64 |
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
65 foreach (XmlElement unitElem in WarFoundryXmlFactoryUtils.SelectNodes(elem, "/army:army/army:units/army:unit")) |
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
66 { |
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
67 string id = unitElem.GetAttribute("id"); |
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
68 |
119 | 69 if (!units.ContainsKey(id)) |
117
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
70 { |
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
71 string unitTypeId = unitElem.GetAttribute("unitType"); |
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
72 UnitType unitType = army.Race.GetUnitType(unitTypeId); |
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
73 |
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
74 if (unitType == null) |
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
75 { |
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
76 throw new RequiredDataMissingException(file.Name, "unitType", unitTypeId); |
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
77 } |
123
52e8c3cdde10
Re #127: Unit creation always assigns default equipment
IBBoard <dev@ibboard.co.uk>
parents:
121
diff
changeset
|
78 |
52e8c3cdde10
Re #127: Unit creation always assigns default equipment
IBBoard <dev@ibboard.co.uk>
parents:
121
diff
changeset
|
79 string name = unitElem.GetAttribute("unitName"); |
52e8c3cdde10
Re #127: Unit creation always assigns default equipment
IBBoard <dev@ibboard.co.uk>
parents:
121
diff
changeset
|
80 int size = XmlTools.GetIntValueFromAttribute(unitElem, "size"); |
117
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
81 |
123
52e8c3cdde10
Re #127: Unit creation always assigns default equipment
IBBoard <dev@ibboard.co.uk>
parents:
121
diff
changeset
|
82 Unit unit = new Unit(id, name, size, unitType, army.GetCategory(unitType.MainCategory)); |
119 | 83 army.AddUnit(unit); |
117
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
84 units.Add(id, unit); |
119 | 85 |
121 | 86 LoadUnitEquipment(unitElem, unit); |
87 } | |
88 } | |
89 } | |
90 | |
91 private void LoadUnitEquipment(XmlElement unitElem, Unit unit) | |
92 { | |
93 foreach (XmlElement elem in WarFoundryXmlFactoryUtils.SelectNodes(unitElem, "army:equipment/army:equipItem")) | |
94 { | |
95 string equipID = elem.GetAttribute("id"); | |
96 bool isCustom = XmlTools.GetBoolValueFromAttribute(elem, "isCustomEquipment"); | |
97 | |
98 if (!isCustom) | |
99 { | |
100 UnitEquipmentItem item = unit.UnitType.GetEquipmentItem(equipID); | |
101 | |
102 if (item == null) | |
103 { | |
104 throw new RequiredDataMissingException(file.Name, "equipItem ID", equipID); | |
105 } | |
106 | |
107 double amount = XmlTools.GetDoubleValueFromAttribute(elem, "amount"); | |
108 string equipTypeString = elem.GetAttribute("amountType"); | |
109 | |
110 if (equipTypeString == "ratio") | |
111 { | |
112 unit.SetEquipmentRatio(item, amount); | |
113 } | |
114 else | |
115 { | |
116 //amount should be a whole number, so do type-cast rounding | |
117 unit.SetEquipmentAmount(item, (int) amount); | |
118 } | |
119 } | |
120 else | |
121 { | |
122 //TODO: Load custom equipment | |
117
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
123 } |
093ee2da0f6e
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
116
diff
changeset
|
124 } |
116
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
125 } |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
126 } |
2010a7e8bf9a
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
127 } |