annotate api/Factories/Xml/WarFoundryXmlArmyParser.cs @ 274:4ef761279ea2

Re #290: Migrate to using key/keyref definitions instead of ID/IDREF type * Fix ability description loading by adding namespace alias * Make use of utility method to help
author IBBoard <dev@ibboard.co.uk>
date Sat, 19 Jun 2010 19:07:52 +0000
parents 35dc06030355
children cd657faa0c05
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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;
238
d1068f4b6d1c Fixes #141: Check IDs of units on loading army
IBBoard <dev@ibboard.co.uk>
parents: 155
diff changeset
8 using IBBoard.IO;
116
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 using IBBoard.Xml;
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 using ICSharpCode.SharpZipLib.Zip;
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 using IBBoard.WarFoundry.API.Objects;
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 namespace IBBoard.WarFoundry.API.Factories.Xml
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 {
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 public class WarFoundryXmlArmyParser
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 {
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 private ZipFile file;
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 private XmlElement elem;
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 private Army army;
117
093ee2da0f6e Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 116
diff changeset
20 private Dictionary<String, Unit> units;
116
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 public WarFoundryXmlArmyParser(ZipFile file, XmlElement elem)
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 {
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 this.file = file;
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 this.elem = elem;
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
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 public Army GetArmy()
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 {
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 if (army == null)
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 {
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 ParseArmy();
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
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 return army;
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
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 private void ParseArmy()
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 {
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 string name = elem.GetAttribute("name");
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 string systemID = elem.GetAttribute("system");
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 GameSystem system = WarFoundryLoader.GetDefault().GetGameSystem(systemID);
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 if (system == null)
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 {
155
df61d26c23fb Re #178: "crash" when loading old .army file with equipment tags that are "old"
IBBoard <dev@ibboard.co.uk>
parents: 123
diff changeset
46 throw new RequiredDataMissingException(file.Name, "Game System", systemID);
116
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
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49 string raceID = elem.GetAttribute("race");
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50 Race race = WarFoundryLoader.GetDefault().GetRace(system, raceID);
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
52 if (race == null)
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53 {
155
df61d26c23fb Re #178: "crash" when loading old .army file with equipment tags that are "old"
IBBoard <dev@ibboard.co.uk>
parents: 123
diff changeset
54 throw new RequiredDataMissingException(file.Name, "Race", raceID);
116
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
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 int points = XmlTools.GetIntValueFromAttribute(elem, "maxPoints");
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 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
59 LoadUnits();
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
093ee2da0f6e Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 116
diff changeset
62 private void LoadUnits()
093ee2da0f6e Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 116
diff changeset
63 {
093ee2da0f6e Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 116
diff changeset
64 units = new Dictionary<string, Unit>();
093ee2da0f6e Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 116
diff changeset
65
093ee2da0f6e Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 116
diff changeset
66 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
67 {
093ee2da0f6e Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 116
diff changeset
68 string id = unitElem.GetAttribute("id");
093ee2da0f6e Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 116
diff changeset
69
119
0cdc0b07fe11 Re #54: Add army loading
IBBoard <dev@ibboard.co.uk>
parents: 117
diff changeset
70 if (!units.ContainsKey(id))
117
093ee2da0f6e Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 116
diff changeset
71 {
093ee2da0f6e Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 116
diff changeset
72 string unitTypeId = unitElem.GetAttribute("unitType");
093ee2da0f6e Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 116
diff changeset
73 UnitType unitType = army.Race.GetUnitType(unitTypeId);
093ee2da0f6e Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 116
diff changeset
74
093ee2da0f6e Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 116
diff changeset
75 if (unitType == null)
093ee2da0f6e Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 116
diff changeset
76 {
155
df61d26c23fb Re #178: "crash" when loading old .army file with equipment tags that are "old"
IBBoard <dev@ibboard.co.uk>
parents: 123
diff changeset
77 throw new RequiredDataMissingException(file.Name, "Unit Type", unitTypeId);
117
093ee2da0f6e Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 116
diff changeset
78 }
123
52e8c3cdde10 Re #127: Unit creation always assigns default equipment
IBBoard <dev@ibboard.co.uk>
parents: 121
diff changeset
79
52e8c3cdde10 Re #127: Unit creation always assigns default equipment
IBBoard <dev@ibboard.co.uk>
parents: 121
diff changeset
80 string name = unitElem.GetAttribute("unitName");
52e8c3cdde10 Re #127: Unit creation always assigns default equipment
IBBoard <dev@ibboard.co.uk>
parents: 121
diff changeset
81 int size = XmlTools.GetIntValueFromAttribute(unitElem, "size");
259
35dc06030355 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 238
diff changeset
82
35dc06030355 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 238
diff changeset
83 string catID = unitElem.GetAttribute("category");
35dc06030355 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 238
diff changeset
84 Category cat = army.Race.GetCategory(catID);
35dc06030355 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 238
diff changeset
85
35dc06030355 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 238
diff changeset
86 if (cat == null)
35dc06030355 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 238
diff changeset
87 {
35dc06030355 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 238
diff changeset
88 cat = unitType.MainCategory;
35dc06030355 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 238
diff changeset
89 }
117
093ee2da0f6e Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 116
diff changeset
90
259
35dc06030355 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 238
diff changeset
91 Unit unit = new Unit(id, name, size, unitType, army.GetCategory(cat));
35dc06030355 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 238
diff changeset
92 army.AddUnit(unit, cat);
117
093ee2da0f6e Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 116
diff changeset
93 units.Add(id, unit);
119
0cdc0b07fe11 Re #54: Add army loading
IBBoard <dev@ibboard.co.uk>
parents: 117
diff changeset
94
121
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
95 LoadUnitEquipment(unitElem, unit);
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
96 }
238
d1068f4b6d1c Fixes #141: Check IDs of units on loading army
IBBoard <dev@ibboard.co.uk>
parents: 155
diff changeset
97 else
d1068f4b6d1c Fixes #141: Check IDs of units on loading army
IBBoard <dev@ibboard.co.uk>
parents: 155
diff changeset
98 {
d1068f4b6d1c Fixes #141: Check IDs of units on loading army
IBBoard <dev@ibboard.co.uk>
parents: 155
diff changeset
99 throw new InvalidFileException("Duplicate unit ID found in army file: "+id);
d1068f4b6d1c Fixes #141: Check IDs of units on loading army
IBBoard <dev@ibboard.co.uk>
parents: 155
diff changeset
100 }
121
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
101 }
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
102 }
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
103
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
104 private void LoadUnitEquipment(XmlElement unitElem, Unit unit)
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
105 {
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
106 foreach (XmlElement elem in WarFoundryXmlFactoryUtils.SelectNodes(unitElem, "army:equipment/army:equipItem"))
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
107 {
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
108 string equipID = elem.GetAttribute("id");
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
109 bool isCustom = XmlTools.GetBoolValueFromAttribute(elem, "isCustomEquipment");
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
110
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
111 if (!isCustom)
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
112 {
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
113 UnitEquipmentItem item = unit.UnitType.GetEquipmentItem(equipID);
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
114
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
115 if (item == null)
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
116 {
155
df61d26c23fb Re #178: "crash" when loading old .army file with equipment tags that are "old"
IBBoard <dev@ibboard.co.uk>
parents: 123
diff changeset
117 throw new RequiredDataMissingException(file.Name, "Equipment Item", equipID);
121
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
118 }
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
119
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
120 double amount = XmlTools.GetDoubleValueFromAttribute(elem, "amount");
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
121 string equipTypeString = elem.GetAttribute("amountType");
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
122
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
123 if (equipTypeString == "ratio")
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
124 {
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
125 unit.SetEquipmentRatio(item, amount);
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
126 }
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
127 else
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
128 {
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
129 //amount should be a whole number, so do type-cast rounding
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
130 unit.SetEquipmentAmount(item, (int) amount);
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
131 }
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
132 }
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
133 else
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
134 {
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
135 //TODO: Load custom equipment
117
093ee2da0f6e Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 116
diff changeset
136 }
093ee2da0f6e Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 116
diff changeset
137 }
116
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
138 }
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
139 }
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
140 }