390
|
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 int defaultarmysize = XmlTools.GetIntValueFromAttribute(elem,"defaultArmySize");
|
|
44 system.SystemArmyDefaultSize = defaultarmysize;
|
|
45 StoreExtraData(system, elem);
|
|
46 return system;
|
|
47
|
|
48 }
|
|
49
|
|
50 public void CompleteLoading(GameSystem system)
|
|
51 {
|
|
52 if (!WarFoundryXmlFactoryUtils.CanCompleteLoading(system))
|
|
53 {
|
|
54 return;
|
|
55 }
|
|
56
|
|
57 system.SetAsLoading();
|
|
58 XmlDocument extraData = GetExtraData(system);
|
|
59 LoadCategoriesForSystem(system, extraData);
|
|
60 XmlElement statsElem = WarFoundryXmlFactoryUtils.SelectSingleElement(extraData, "/system:system/system:sysStatsList");
|
|
61 string defaultStatsID = statsElem.GetAttribute("defaultStats");
|
|
62 LoadSystemStatsForSystem(system, extraData);
|
|
63 system.StandardSystemStatsID = defaultStatsID;
|
|
64 XmlElement systemElement = WarFoundryXmlFactoryUtils.SelectSingleElement(extraData, "/system:system");
|
|
65 system.WarnOnError = XmlTools.GetBoolValueFromAttribute(systemElement, "warn");
|
|
66 system.AllowAllies = XmlTools.GetBoolValueFromAttribute(systemElement, "allowAllies");
|
|
67 system.SetAsFullyLoaded();
|
|
68 }
|
|
69
|
|
70
|
|
71 private void LoadCategoriesForSystem(GameSystem system, XmlNode elem)
|
|
72 {
|
|
73 foreach (XmlElement cat in WarFoundryXmlFactoryUtils.SelectNodes(elem, "/system:system/system:categories/cat:cat"))
|
|
74 {
|
|
75 system.AddCategory(CreateCategoryFromElement(cat));
|
|
76 }
|
|
77 }
|
|
78
|
|
79 private void LoadSystemStatsForSystem(GameSystem system, XmlNode elem)
|
|
80 {
|
|
81 foreach (XmlElement stats in WarFoundryXmlFactoryUtils.SelectNodes(elem, "/system:system/system:sysStatsList/system:sysStats"))
|
|
82 {
|
|
83 SystemStats sysStats = CreateSystemStatsFromElement(stats);
|
|
84 system.AddSystemStats(sysStats);
|
|
85 }
|
|
86 }
|
|
87
|
|
88 private SystemStats CreateSystemStatsFromElement(XmlElement elem)
|
|
89 {
|
|
90 SystemStats sysStats = new SystemStats(elem.GetAttribute("id"));
|
|
91
|
|
92 foreach (XmlElement slot in WarFoundryXmlFactoryUtils.SelectNodes(elem, "system:sysStat"))
|
|
93 {
|
|
94 sysStats.AddStatSlot(slot.GetAttribute("name"));
|
|
95 }
|
|
96
|
|
97 return sysStats;
|
|
98 }
|
|
99 }
|
|
100 }
|