comparison API/Factories/Xml/WarFoundryXmlGameSystemFactory.cs @ 337:3c4a6403a88c

* Fix capitalisation so that new files are in the namespace no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 03 Apr 2011 18:50:32 +0000
parents
children e50682387d63
comparison
equal deleted inserted replaced
336:3631c1493c7f 337:3c4a6403a88c
1 // This file (WarFoundryXmlGameSystemFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard
2 //
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.
4
5 using System;
6 using System.Collections.Generic;
7 using System.IO;
8 using System.Xml;
9 using ICSharpCode.SharpZipLib.Zip;
10 using IBBoard.Xml;
11 using IBBoard.WarFoundry.API.Objects;
12
13 namespace IBBoard.WarFoundry.API.Factories.Xml
14 {
15 /// <summary>
16 /// A sub-factory specifically for loading GameSystems from WarFoundry XML files
17 /// </summary>
18 public class WarFoundryXmlGameSystemFactory : AbstractStagedLoadedSubFactory
19 {
20 private Dictionary<GameSystem, XmlDocument> extraData = new Dictionary<GameSystem, XmlDocument>();
21
22 public WarFoundryXmlGameSystemFactory(WarFoundryXmlFactory factory) : base(factory)
23 {
24 }
25
26 private void StoreExtraData(GameSystem wfObject, XmlElement elem)
27 {
28 extraData[wfObject] = elem.OwnerDocument;
29 }
30
31 private XmlDocument GetExtraData(GameSystem obj)
32 {
33 XmlDocument extra = null;
34 extraData.TryGetValue(obj, out extra);
35 return extra;
36 }
37
38 public GameSystem CreateSystemFromElement(ZipFile file, XmlElement elem)
39 {
40 string id = elem.GetAttribute("id");
41 string name = elem.GetAttribute("name");
42 GameSystem system = new GameSystem(id, name, mainFactory);
43 system.SystemArmyDefaultSize = XmlTools.GetIntValueFromAttribute (elem, "defaultArmySize");
44 system.SystemPtsAbbrevSingle = elem.GetAttribute ("defaultPtsAbbreviationSingular");
45 system.SystemPtsAbbrevPlural = elem.GetAttribute ("defaultPtsAbbreviationPlural");
46 system.SystemPtsNameSingle = elem.GetAttribute ("defaultPtsNameSingular");
47 system.SystemPtsNamePlural = elem.GetAttribute ("defaultPtsNamePlural");
48 StoreExtraData(system, elem);
49 return system;
50 }
51
52 public void CompleteLoading(GameSystem system)
53 {
54 if (!WarFoundryXmlFactoryUtils.CanCompleteLoading(system))
55 {
56 return;
57 }
58
59 system.SetAsLoading();
60 XmlDocument extraData = GetExtraData(system);
61 LoadCategoriesForSystem(system, extraData);
62 XmlElement statsElem = WarFoundryXmlFactoryUtils.SelectSingleElement(extraData, "/system:system/system:sysStatsList");
63 string defaultStatsID = statsElem.GetAttribute("defaultStats");
64 LoadSystemStatsForSystem(system, extraData);
65 system.StandardSystemStatsID = defaultStatsID;
66 XmlElement systemElement = WarFoundryXmlFactoryUtils.SelectSingleElement(extraData, "/system:system");
67 system.WarnOnError = XmlTools.GetBoolValueFromAttribute(systemElement, "warn");
68 system.AllowAllies = XmlTools.GetBoolValueFromAttribute(systemElement, "allowAllies");
69 system.SetAsFullyLoaded();
70 }
71
72
73 private void LoadCategoriesForSystem(GameSystem system, XmlNode elem)
74 {
75 foreach (XmlElement cat in WarFoundryXmlFactoryUtils.SelectNodes(elem, "/system:system/system:categories/cat:cat"))
76 {
77 system.AddCategory(CreateCategoryFromElement(cat));
78 }
79 }
80
81 private void LoadSystemStatsForSystem(GameSystem system, XmlNode elem)
82 {
83 foreach (XmlElement stats in WarFoundryXmlFactoryUtils.SelectNodes(elem, "/system:system/system:sysStatsList/system:sysStats"))
84 {
85 SystemStats sysStats = CreateSystemStatsFromElement(stats);
86 system.AddSystemStats(sysStats);
87 }
88 }
89
90 private SystemStats CreateSystemStatsFromElement(XmlElement elem)
91 {
92 SystemStats sysStats = new SystemStats(elem.GetAttribute("id"));
93
94 foreach (XmlElement slot in WarFoundryXmlFactoryUtils.SelectNodes(elem, "system:sysStat"))
95 {
96 sysStats.AddStatSlot(slot.GetAttribute("name"));
97 }
98
99 return sysStats;
100 }
101 }
102 }