annotate api/Factories/Xml/WarFoundryXmlArmyParser.cs @ 155:df61d26c23fb

Re #178: "crash" when loading old .army file with equipment tags that are "old" * Try to tidy up the exception messages - ideally they'll end up translated and localised
author IBBoard <dev@ibboard.co.uk>
date Mon, 28 Sep 2009 19:48:37 +0000
parents 52e8c3cdde10
children d1068f4b6d1c
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;
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 {
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
45 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
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 {
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
53 throw new RequiredDataMissingException(file.Name, "Race", raceID);
116
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
0cdc0b07fe11 Re #54: Add army loading
IBBoard <dev@ibboard.co.uk>
parents: 117
diff changeset
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 {
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
76 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
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
0cdc0b07fe11 Re #54: Add army loading
IBBoard <dev@ibboard.co.uk>
parents: 117
diff changeset
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
0cdc0b07fe11 Re #54: Add army loading
IBBoard <dev@ibboard.co.uk>
parents: 117
diff changeset
85
121
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
86 LoadUnitEquipment(unitElem, unit);
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
87 }
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
88 }
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
89 }
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
90
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
91 private void LoadUnitEquipment(XmlElement unitElem, Unit unit)
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
92 {
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
93 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
94 {
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
95 string equipID = elem.GetAttribute("id");
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
96 bool isCustom = XmlTools.GetBoolValueFromAttribute(elem, "isCustomEquipment");
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
97
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
98 if (!isCustom)
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
99 {
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
100 UnitEquipmentItem item = unit.UnitType.GetEquipmentItem(equipID);
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 if (item == null)
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
103 {
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
104 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
105 }
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
106
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
107 double amount = XmlTools.GetDoubleValueFromAttribute(elem, "amount");
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
108 string equipTypeString = elem.GetAttribute("amountType");
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
109
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
110 if (equipTypeString == "ratio")
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
111 {
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
112 unit.SetEquipmentRatio(item, amount);
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
113 }
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
114 else
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
115 {
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
116 //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
117 unit.SetEquipmentAmount(item, (int) amount);
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 else
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
121 {
53654e938982 Re #54: Add army file loading
IBBoard <dev@ibboard.co.uk>
parents: 119
diff changeset
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 }