Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate api/Factories/Xml/WarFoundryXmlGameSystemFactory.cs @ 107:c4ee96a91018
Re #53: Add XML saver
* Actually add unit elements, since creating them doesn't add them as well!
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Fri, 21 Aug 2009 19:51:24 +0000 |
parents | 2f3cafb69799 |
children | c1caf467dd40 |
rev | line source |
---|---|
52 | 1 // This file (WarFoundryXmlGameSystemFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard |
2 // | |
104
2f3cafb69799
Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents:
67
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. |
52 | 4 |
5 using System; | |
6 using System.Collections.Generic; | |
7 using System.Xml; | |
8 using ICSharpCode.SharpZipLib.Zip; | |
9 using IBBoard.Logging; | |
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 StoreExtraData(system, elem); | |
44 return system; | |
45 } | |
46 | |
47 public void CompleteLoading(GameSystem system) | |
48 { | |
49 if (!WarFoundryXmlFactoryUtils.CanCompleteLoading(system)) | |
50 { | |
51 return; | |
52 } | |
53 | |
54 system.SetAsLoading(); | |
55 XmlDocument extraData = GetExtraData(system); | |
56 LoadCategoriesForSystem(system, extraData); | |
57 XmlElement statsElem = WarFoundryXmlFactoryUtils.SelectSingleElement(extraData, "/system:system/system:sysStatsList"); | |
58 string defaultStatsID = statsElem.GetAttribute("defaultStats"); | |
59 LoadSystemStatsForSystem(system, extraData); | |
60 system.StandardSystemStatsID = defaultStatsID; | |
61 XmlElement systemElement = WarFoundryXmlFactoryUtils.SelectSingleElement(extraData, "/system:system"); | |
62 system.WarnOnError = XmlTools.GetBoolValueFromAttribute(systemElement, "warn"); | |
63 system.AllowAllies = XmlTools.GetBoolValueFromAttribute(systemElement, "allowAllies"); | |
64 LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.ID); | |
65 LogNotifier.DebugFormat(GetType(), "GameSystem with ID {0} default stats: {1}", system.ID, system.StandardSystemStatsID); | |
66 system.SetAsFullyLoaded(); | |
67 } | |
68 | |
69 private void LoadCategoriesForSystem(GameSystem system, XmlNode elem) | |
70 { | |
71 foreach (XmlElement cat in WarFoundryXmlFactoryUtils.SelectNodes(elem, "/system:system/system:categories/cat:cat")) | |
72 { | |
73 system.AddCategory(CreateCategoryFromElement(cat)); | |
74 } | |
75 } | |
76 | |
77 private void LoadSystemStatsForSystem(GameSystem system, XmlNode elem) | |
78 { | |
79 foreach (XmlElement stats in WarFoundryXmlFactoryUtils.SelectNodes(elem, "/system:system/system:sysStatsList/system:sysStats")) | |
80 { | |
81 SystemStats sysStats = CreateSystemStatsFromElement(stats); | |
82 system.AddSystemStats(sysStats); | |
83 } | |
84 } | |
85 | |
86 private SystemStats CreateSystemStatsFromElement(XmlElement elem) | |
87 { | |
67
e6200220ece3
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
88 SystemStats sysStats = new SystemStats(elem.GetAttribute("id")); |
52 | 89 |
53 | 90 foreach (XmlElement slot in WarFoundryXmlFactoryUtils.SelectNodes(elem, "system:sysStat")) |
52 | 91 { |
67
e6200220ece3
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
92 sysStats.AddStatSlot(slot.GetAttribute("name")); |
52 | 93 } |
67
e6200220ece3
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
94 |
e6200220ece3
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
95 return sysStats; |
e6200220ece3
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
96 } |
e6200220ece3
Re #50 - Complete core loading of WarFoundry XML files
IBBoard <dev@ibboard.co.uk>
parents:
53
diff
changeset
|
97 } |
52 | 98 } |