annotate api/Factories/Xml/WarFoundryXmlGameSystemFactory.cs @ 53:1b35eed503ef

Closes #13 - Migrate to XPath * Remove elem.ChildNodes calls and use XPath to make sure we're getting the nodes we think we should
author IBBoard <dev@ibboard.co.uk>
date Wed, 01 Apr 2009 19:20:27 +0000
parents 64ef178c18aa
children e6200220ece3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
52
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 // This file (WarFoundryXmlGameSystemFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 //
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 //
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using System;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 using System.Collections.Generic;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 using System.Xml;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 using ICSharpCode.SharpZipLib.Zip;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 using IBBoard.Logging;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 using IBBoard.Xml;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 using IBBoard.WarFoundry.API.Objects;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 namespace IBBoard.WarFoundry.API.Factories.Xml
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 /// <summary>
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 /// A sub-factory specifically for loading GameSystems from WarFoundry XML files
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 /// </summary>
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 public class WarFoundryXmlGameSystemFactory : AbstractStagedLoadedSubFactory
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 private Dictionary<GameSystem, XmlDocument> extraData = new Dictionary<GameSystem, XmlDocument>();
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 public WarFoundryXmlGameSystemFactory(WarFoundryXmlFactory factory) : base(factory)
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 private void StoreExtraData(GameSystem wfObject, XmlElement elem)
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 extraData[wfObject] = elem.OwnerDocument;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 private XmlDocument GetExtraData(GameSystem obj)
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 XmlDocument extra = null;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 extraData.TryGetValue(obj, out extra);
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 return extra;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 public GameSystem CreateSystemFromElement(ZipFile file, XmlElement elem)
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 string id = elem.GetAttribute("id");
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 string name = elem.GetAttribute("name");
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 GameSystem system = new GameSystem(id, name, mainFactory);
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 StoreExtraData(system, elem);
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 return system;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 public void CompleteLoading(GameSystem system)
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50 if (!WarFoundryXmlFactoryUtils.CanCompleteLoading(system))
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
52 return;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 system.SetAsLoading();
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
56 XmlDocument extraData = GetExtraData(system);
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 LoadCategoriesForSystem(system, extraData);
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 XmlElement statsElem = WarFoundryXmlFactoryUtils.SelectSingleElement(extraData, "/system:system/system:sysStatsList");
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59 string defaultStatsID = statsElem.GetAttribute("defaultStats");
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
60 LoadSystemStatsForSystem(system, extraData);
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
61 system.StandardSystemStatsID = defaultStatsID;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62 XmlElement systemElement = WarFoundryXmlFactoryUtils.SelectSingleElement(extraData, "/system:system");
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
63 system.WarnOnError = XmlTools.GetBoolValueFromAttribute(systemElement, "warn");
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
64 system.AllowAllies = XmlTools.GetBoolValueFromAttribute(systemElement, "allowAllies");
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
65 LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.ID);
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
66 LogNotifier.DebugFormat(GetType(), "GameSystem with ID {0} default stats: {1}", system.ID, system.StandardSystemStatsID);
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
67 system.SetAsFullyLoaded();
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
68 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
69
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
70 private void LoadCategoriesForSystem(GameSystem system, XmlNode elem)
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
71 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
72 foreach (XmlElement cat in WarFoundryXmlFactoryUtils.SelectNodes(elem, "/system:system/system:categories/cat:cat"))
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
73 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
74 system.AddCategory(CreateCategoryFromElement(cat));
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
75 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
76 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
77
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
78 private void LoadSystemStatsForSystem(GameSystem system, XmlNode elem)
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
79 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
80 foreach (XmlElement stats in WarFoundryXmlFactoryUtils.SelectNodes(elem, "/system:system/system:sysStatsList/system:sysStats"))
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
81 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
82 SystemStats sysStats = CreateSystemStatsFromElement(stats);
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
83 system.AddSystemStats(sysStats);
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
84 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
85 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
86
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
87 private SystemStats CreateSystemStatsFromElement(XmlElement elem)
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
88 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
89 List<StatSlot> slots = new List<StatSlot>();
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
90 string id = elem.GetAttribute("id");
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
91
53
1b35eed503ef Closes #13 - Migrate to XPath
IBBoard <dev@ibboard.co.uk>
parents: 52
diff changeset
92 foreach (XmlElement slot in WarFoundryXmlFactoryUtils.SelectNodes(elem, "system:sysStat"))
52
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
93 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
94 StatSlot statSlot = new StatSlot(slot.GetAttribute("name"));
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
95 slots.Add(statSlot);
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
96 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
97
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
98 return new SystemStats(id, slots.ToArray());
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
99 } }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
100 }