comparison api/Factories/Xml/WarFoundryXmlGameSystemSaver.cs @ 297:349e521785c1

Re #338: WarFoundry.API - Save System Data Created IWarFoundryGameSystemSaver Created WarFoundryXmlGameSystemSaver Updated WarFoundrySaver Created GetGameSystemSaver Created SetGameSystemSaver
author Tsudico
date Fri, 07 Jan 2011 03:35:50 +0000
parents
children
comparison
equal deleted inserted replaced
296:ed5e6d92ecd9 297:349e521785c1
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.Factories.Xml
19 {
20 public class WarFoundryXmlGameSystemSaver : IWarFoundryGameSystemSaver
21 {
22 public const string GAMESYSTEM_FILE_EXTENSION = ".system";
23
24 public bool Save(GameSystem toSave, string savePath)
25 {
26 bool success = false;
27 ZipFile file = null;
28
29 if (!savePath.EndsWith(GAMESYSTEM_FILE_EXTENSION))
30 {
31 savePath = savePath + GAMESYSTEM_FILE_EXTENSION;
32 }
33
34 try
35 {
36 file = ZipFile.Create(savePath);
37 file.BeginUpdate();
38 file.Add(new StringZipEntrySource(CreateXmlString(toSave)), "data.systemx");
39 file.CommitUpdate();
40 success = true;
41 }
42 finally
43 {
44 if (file != null)
45 {
46 file.Close();
47 }
48 }
49
50 return success;
51 }
52
53 private string CreateXmlString(WarFoundryObject toSave)
54 {
55 string xmlString = "";
56
57 if (toSave is GameSystem)
58 {
59 xmlString = CreateGameSystemXmlString((GameSystem)toSave);
60 }
61
62 return xmlString;
63 }
64
65 private string CreateGameSystemXmlString(GameSystem toSave)
66 {
67 XmlDocument doc = new XmlDocument();
68 XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null);
69 doc.AppendChild(declaration);
70 XmlSchema schema = new XmlSchema();
71 schema.Namespaces.Add("", "http://ibboard.co.uk/warfoundry/system");
72 schema.Namespaces.Add("cats", "http://ibboard.co.uk/warfoundry/cats");
73 doc.Schemas.Add(schema);
74 XmlElement root = doc.CreateElement("system");
75 root.SetAttribute("xmlns", "http://ibboard.co.uk/warfoundry/system");
76 root.SetAttribute("xmlns:cats", "http://ibboard.co.uk/warfoundry/cats");
77 doc.AppendChild(root);
78 root.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(toSave.ID));
79 root.SetAttribute("name", toSave.Name);
80 root.SetAttribute("defaultArmySize", toSave.SystemArmyDefaultSize.ToString());
81 root.SetAttribute("warn", toSave.WarnOnError.ToString().ToLowerInvariant());
82 root.SetAttribute("allowAllies", toSave.AllowAllies.ToString().ToLowerInvariant());
83 XmlElement cats = doc.CreateElement("categories");
84 root.AppendChild(cats);
85
86 foreach (Category cat in toSave.Categories)
87 {
88 cats.AppendChild(CreateCategoryElement(cat, doc));
89 }
90
91 XmlElement sysStatsList = doc.CreateElement("sysStatsList");
92 sysStatsList.SetAttribute("defaultStats", XmlTools.GetAsciiXmlIdForString(toSave.StandardSystemStatsID));
93 root.AppendChild(sysStatsList);
94
95 foreach(SystemStats stats in toSave.SystemStats)
96 {
97 sysStatsList.AppendChild(CreateSystemStatsElement(stats, doc));
98 }
99
100 return doc.OuterXml;
101 }
102
103 private XmlElement CreateCategoryElement(Category cat, XmlDocument doc)
104 {
105 XmlElement catElem = doc.CreateElement("cats:cat");
106 catElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(cat.ID));
107 catElem.SetAttribute("name", (cat.HasDefaultName() ? "" : cat.Name));
108 if (cat.MinimumPoints > 0)
109 {
110 catElem.SetAttribute("minPoints", cat.MaximumPercentage.ToString());
111 }
112 if (cat.MaximumPoints < 100)
113 {
114 catElem.SetAttribute("maxPoints", cat.MaximumPercentage.ToString());
115 }
116 if(cat.MinimumPercentage > 0)
117 {
118 catElem.SetAttribute("minPercentage", cat.MaximumPercentage.ToString());
119 }
120 if(cat.MaximumPercentage < 100)
121 {
122 catElem.SetAttribute("maxPercentage", cat.MaximumPercentage.ToString());
123 }
124
125 return catElem;
126 }
127
128 private XmlElement CreateSystemStatsElement(SystemStats stats, XmlDocument doc)
129 {
130 XmlElement statsElem = doc.CreateElement("sysStats");
131 statsElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(stats.ID));
132
133 foreach(StatSlot stat in stats.StatSlots)
134 {
135 statsElem.AppendChild(CreateSystemStatElement(stat, doc));
136 }
137
138 return statsElem;
139 }
140
141 private XmlElement CreateSystemStatElement(StatSlot stat, XmlDocument doc)
142 {
143 XmlElement statElem = doc.CreateElement("sysStat");
144 statElem.SetAttribute("name", stat.Name);
145
146 return statElem;
147 }
148 }
149 }