Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate api/Savers/Xml/WarFoundryXmlGameSystemSaver.cs @ 325:e0580a009e75
Re #324: Add saving of Race and System data to files
* Remove extra interfaces, as their replacements would be implementation specific and the break-down is now only a clean coding issue
* Strip out extra code from Army and GameSystem saver
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 12 Mar 2011 20:32:08 +0000 |
parents | 3e9b0603afad |
children |
rev | line source |
---|---|
316
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
1 // This file (WarFoundryXmlGameSystemSaver.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2008, 2009 IBBoard. |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 // |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
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. |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 using System; |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 using System.Collections.Generic; |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 using System.IO; |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 using System.Xml; |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 using System.Xml.Schema; |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 using IBBoard.Lang; |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 using IBBoard.Xml; |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 using IBBoard.WarFoundry.API.Factories.Xml.Zip; |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 using IBBoard.WarFoundry.API.Objects; |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 using IBBoard.WarFoundry.API.Savers; |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 using IBBoard.WarFoundry.API.Util; |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 using ICSharpCode.SharpZipLib.Zip; |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 namespace IBBoard.WarFoundry.API.Savers.Xml |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
19 { |
325
e0580a009e75
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
322
diff
changeset
|
20 public class WarFoundryXmlGameSystemSaver |
316
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
21 { |
325
e0580a009e75
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
322
diff
changeset
|
22 public string CreateXmlString(GameSystem toSave) |
316
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
23 { |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
24 XmlDocument doc = new XmlDocument(); |
322
3e9b0603afad
Re #338: WarFoundry.API - Save System Data
IBBoard <dev@ibboard.co.uk>
parents:
316
diff
changeset
|
25 XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null); |
316
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
26 doc.AppendChild(declaration); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
27 XmlSchema schema = new XmlSchema(); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
28 schema.Namespaces.Add("", "http://ibboard.co.uk/warfoundry/system"); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
29 schema.Namespaces.Add("cats", "http://ibboard.co.uk/warfoundry/cats"); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
30 doc.Schemas.Add(schema); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
31 XmlElement root = doc.CreateElement("system"); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
32 root.SetAttribute("xmlns", "http://ibboard.co.uk/warfoundry/system"); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
33 root.SetAttribute("xmlns:cats", "http://ibboard.co.uk/warfoundry/cats"); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
34 doc.AppendChild(root); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
35 root.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(toSave.ID)); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
36 root.SetAttribute("name", toSave.Name); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
37 root.SetAttribute("defaultArmySize", toSave.SystemArmyDefaultSize.ToString()); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
38 root.SetAttribute("warn", toSave.WarnOnError.ToString().ToLowerInvariant()); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
39 root.SetAttribute("allowAllies", toSave.AllowAllies.ToString().ToLowerInvariant()); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
40 XmlElement cats = doc.CreateElement("categories"); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
41 root.AppendChild(cats); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
42 |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
43 foreach (Category cat in toSave.Categories) |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
44 { |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
45 cats.AppendChild(CreateCategoryElement(cat, doc)); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
46 } |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
47 |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
48 XmlElement sysStatsList = doc.CreateElement("sysStatsList"); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
49 sysStatsList.SetAttribute("defaultStats", XmlTools.GetAsciiXmlIdForString(toSave.StandardSystemStatsID)); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
50 root.AppendChild(sysStatsList); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
51 |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
52 foreach(SystemStats stats in toSave.SystemStats) |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
53 { |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
54 sysStatsList.AppendChild(CreateSystemStatsElement(stats, doc)); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
55 } |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
56 |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
57 return doc.OuterXml; |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
58 } |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
59 |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
60 private XmlElement CreateCategoryElement(Category cat, XmlDocument doc) |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
61 { |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
62 XmlElement catElem = doc.CreateElement("cats:cat"); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
63 catElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(cat.ID)); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
64 catElem.SetAttribute("name", (cat.HasDefaultName() ? "" : cat.Name)); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
65 if (cat.MinimumPoints > 0) |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
66 { |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
67 catElem.SetAttribute("minPoints", cat.MaximumPercentage.ToString()); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
68 } |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
69 if (cat.MaximumPoints < 100) |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
70 { |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
71 catElem.SetAttribute("maxPoints", cat.MaximumPercentage.ToString()); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
72 } |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
73 if(cat.MinimumPercentage > 0) |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
74 { |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
75 catElem.SetAttribute("minPercentage", cat.MaximumPercentage.ToString()); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
76 } |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
77 if(cat.MaximumPercentage < 100) |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
78 { |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
79 catElem.SetAttribute("maxPercentage", cat.MaximumPercentage.ToString()); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
80 } |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
81 |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
82 return catElem; |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
83 } |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
84 |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
85 private XmlElement CreateSystemStatsElement(SystemStats stats, XmlDocument doc) |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
86 { |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
87 XmlElement statsElem = doc.CreateElement("sysStats"); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
88 statsElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(stats.ID)); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
89 |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
90 foreach(StatSlot stat in stats.StatSlots) |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
91 { |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
92 statsElem.AppendChild(CreateSystemStatElement(stat, doc)); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
93 } |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
94 |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
95 return statsElem; |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
96 } |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
97 |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
98 private XmlElement CreateSystemStatElement(StatSlot stat, XmlDocument doc) |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
99 { |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
100 XmlElement statElem = doc.CreateElement("sysStat"); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
101 statElem.SetAttribute("name", stat.Name); |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
102 |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
103 return statElem; |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
104 } |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
105 } |
40a2df1f629a
Re #324: Add saving of Race and System data to files
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
106 } |