comparison API/Savers/Xml/WarFoundryXmlGameSystemSaver.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 5b3e3fb11de9
comparison
equal deleted inserted replaced
336:3631c1493c7f 337:3c4a6403a88c
1 // This file (WarFoundryXmlGameSystemSaver.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2008, 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 System.Xml.Schema;
10 using IBBoard.Lang;
11 using IBBoard.Xml;
12 using IBBoard.WarFoundry.API.Factories.Xml.Zip;
13 using IBBoard.WarFoundry.API.Objects;
14 using IBBoard.WarFoundry.API.Savers;
15 using IBBoard.WarFoundry.API.Util;
16 using ICSharpCode.SharpZipLib.Zip;
17
18 namespace IBBoard.WarFoundry.API.Savers.Xml
19 {
20 public class WarFoundryXmlGameSystemSaver
21 {
22 public string CreateXmlString(GameSystem toSave)
23 {
24 XmlDocument doc = new XmlDocument();
25 XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
26 doc.AppendChild(declaration);
27 XmlSchema schema = new XmlSchema();
28 schema.Namespaces.Add("", "http://ibboard.co.uk/warfoundry/system");
29 schema.Namespaces.Add("cats", "http://ibboard.co.uk/warfoundry/cats");
30 doc.Schemas.Add(schema);
31 XmlElement root = doc.CreateElement("system");
32 root.SetAttribute("xmlns", "http://ibboard.co.uk/warfoundry/system");
33 root.SetAttribute("xmlns:cats", "http://ibboard.co.uk/warfoundry/cats");
34 doc.AppendChild(root);
35 root.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(toSave.ID));
36 root.SetAttribute("name", toSave.Name);
37 root.SetAttribute("defaultArmySize", toSave.SystemArmyDefaultSize.ToString());
38 root.SetAttribute("warn", toSave.WarnOnError.ToString().ToLowerInvariant());
39 root.SetAttribute("allowAllies", toSave.AllowAllies.ToString().ToLowerInvariant());
40 XmlElement cats = doc.CreateElement("categories");
41 root.AppendChild(cats);
42
43 foreach (Category cat in toSave.Categories)
44 {
45 cats.AppendChild(CreateCategoryElement(cat, doc));
46 }
47
48 XmlElement sysStatsList = doc.CreateElement("sysStatsList");
49 sysStatsList.SetAttribute("defaultStats", XmlTools.GetAsciiXmlIdForString(toSave.StandardSystemStatsID));
50 root.AppendChild(sysStatsList);
51
52 foreach(SystemStats stats in toSave.SystemStats)
53 {
54 sysStatsList.AppendChild(CreateSystemStatsElement(stats, doc));
55 }
56
57 return doc.OuterXml;
58 }
59
60 private XmlElement CreateCategoryElement(Category cat, XmlDocument doc)
61 {
62 XmlElement catElem = doc.CreateElement("cats:cat");
63 catElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(cat.ID));
64 catElem.SetAttribute("name", (cat.HasDefaultName() ? "" : cat.Name));
65 if (cat.MinimumPoints > 0)
66 {
67 catElem.SetAttribute("minPoints", cat.MaximumPercentage.ToString());
68 }
69 if (cat.MaximumPoints < 100)
70 {
71 catElem.SetAttribute("maxPoints", cat.MaximumPercentage.ToString());
72 }
73 if(cat.MinimumPercentage > 0)
74 {
75 catElem.SetAttribute("minPercentage", cat.MaximumPercentage.ToString());
76 }
77 if(cat.MaximumPercentage < 100)
78 {
79 catElem.SetAttribute("maxPercentage", cat.MaximumPercentage.ToString());
80 }
81
82 return catElem;
83 }
84
85 private XmlElement CreateSystemStatsElement(SystemStats stats, XmlDocument doc)
86 {
87 XmlElement statsElem = doc.CreateElement("sysStats");
88 statsElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(stats.ID));
89
90 foreach(StatSlot stat in stats.StatSlots)
91 {
92 statsElem.AppendChild(CreateSystemStatElement(stat, doc));
93 }
94
95 return statsElem;
96 }
97
98 private XmlElement CreateSystemStatElement(StatSlot stat, XmlDocument doc)
99 {
100 XmlElement statElem = doc.CreateElement("sysStat");
101 statElem.SetAttribute("name", stat.Name);
102
103 return statElem;
104 }
105 }
106 }