annotate api/Factories/Xml/WarFoundryXmlArmyParser.cs @ 119:0cdc0b07fe11

Re #54: Add army loading * Add unit to army once it is loaded
author IBBoard <dev@ibboard.co.uk>
date Sun, 23 Aug 2009 10:32:09 +0000
parents 093ee2da0f6e
children 53654e938982
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 {
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
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 {
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 }
093ee2da0f6e Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 116
diff changeset
78
119
0cdc0b07fe11 Re #54: Add army loading
IBBoard <dev@ibboard.co.uk>
parents: 117
diff changeset
79 Unit unit = new Unit(unitType, army.GetCategory(unitType.MainCategory));
0cdc0b07fe11 Re #54: Add army loading
IBBoard <dev@ibboard.co.uk>
parents: 117
diff changeset
80 army.AddUnit(unit);
117
093ee2da0f6e Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 116
diff changeset
81 units.Add(id, unit);
119
0cdc0b07fe11 Re #54: Add army loading
IBBoard <dev@ibboard.co.uk>
parents: 117
diff changeset
82
0cdc0b07fe11 Re #54: Add army loading
IBBoard <dev@ibboard.co.uk>
parents: 117
diff changeset
83 //LoadUnitEquipment(
117
093ee2da0f6e Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 116
diff changeset
84 }
093ee2da0f6e Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 116
diff changeset
85 }
116
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
86 }
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
87 }
2010a7e8bf9a Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
88 }