annotate api/Factories/Xml/WarFoundryXmlSaver.cs @ 150:b36cc4af435b

Re #176: Bug when saving recently edited army * Try to make sure that we clear up more of our open streams Bug seems to be state related in some way since I can only trigger it when loading the file as the first action, but doesn't seem to be related to file loading of other data files since a diagnostic hard-coded "LoadFiles()" call in the FrmMain constructor doesn't change the behaviour
author IBBoard <dev@ibboard.co.uk>
date Sat, 26 Sep 2009 10:43:28 +0000
parents f58051572ec7
children 1b718b67f7f6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
104
2f3cafb69799 Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents: 82
diff changeset
1 // This file (WarFoundryXmlSaver.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2008, 2009 IBBoard.
15
306558904c2a Re #1 - LGPL license all code
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
2 //
104
2f3cafb69799 Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents: 82
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.
15
306558904c2a Re #1 - LGPL license all code
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
4
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 using System;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using System.Collections.Generic;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 using System.IO;
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
8 using System.Xml;
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
9 using System.Xml.Schema;
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 using IBBoard.Lang;
109
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
11 using IBBoard.Xml;
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
12 using IBBoard.WarFoundry.API.Factories.Xml.Zip;
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 using IBBoard.WarFoundry.API.Objects;
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
14 using IBBoard.WarFoundry.API.Savers;
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
15 using ICSharpCode.SharpZipLib.Zip;
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 namespace IBBoard.WarFoundry.API.Factories.Xml
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 {
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
19 public class WarFoundryXmlSaver : IWarFoundryFileSaver
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 {
109
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
21 public const string ARMY_FILE_EXTENSION = ".army";
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
22
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
23 public bool Save(Army toSave, string savePath)
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 {
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
25 bool success = false;
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
26 ZipFile file = null;
109
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
27
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
28 if (!savePath.EndsWith(ARMY_FILE_EXTENSION))
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
29 {
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
30 savePath = savePath + ARMY_FILE_EXTENSION;
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
31 }
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
32
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
33 try
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 {
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
35 file = ZipFile.Create(savePath);
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
36 file.BeginUpdate();
110
f0fb96d0cfe9 Re #53: Create XML army saver
IBBoard <dev@ibboard.co.uk>
parents: 109
diff changeset
37 file.Add(new StringZipEntrySource(CreateXmlString(toSave)), "data.armyx");
115
d0c60b3204c1 Re #53: Add XML army saving
IBBoard <dev@ibboard.co.uk>
parents: 112
diff changeset
38 file.SetComment(AbstractNativeWarFoundryFactory.ARMY_ZIP_IDENTIFIER);
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
39 file.CommitUpdate();
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
40 success = true;
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 }
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
42 finally
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 {
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
44 if (file != null)
82
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
45 {
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
46 file.Close();
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 }
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
49
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
50 return success;
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51 }
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
52
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53 private string CreateXmlString(WarFoundryObject toSave)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54 {
108
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
55 string xmlString = "";
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
56
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
57 if (toSave is Army)
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
58 {
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
59 xmlString = CreateArmyXmlString((Army)toSave);
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
60 }
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
61
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
62 return xmlString;
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
63 }
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
64
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
65 private string CreateArmyXmlString(Army toSave)
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
66 {
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
67 XmlDocument doc = new XmlDocument();
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
68 XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null);
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
69 doc.AppendChild(declaration);
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
70 XmlSchema schema = new XmlSchema();
125
c60fd5664698 Re #53: Create WarFoundry save class to store army as file
IBBoard <dev@ibboard.co.uk>
parents: 122
diff changeset
71 schema.Namespaces.Add("", "http://ibboard.co.uk/warfoundry/army");
c60fd5664698 Re #53: Create WarFoundry save class to store army as file
IBBoard <dev@ibboard.co.uk>
parents: 122
diff changeset
72 schema.Namespaces.Add("core", "http://ibboard.co.uk/warfoundry/core");
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
73 doc.Schemas.Add(schema);
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
74 XmlElement root = doc.CreateElement("army");
112
863518044d38 Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 110
diff changeset
75 root.SetAttribute("xmlns", "http://ibboard.co.uk/warfoundry/army");
863518044d38 Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents: 110
diff changeset
76 root.SetAttribute("xmlns:core", "http://ibboard.co.uk/warfoundry/core");
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
77 doc.AppendChild(root);
109
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
78 root.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(toSave.ID));
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
79 root.SetAttribute("name", toSave.Name);
109
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
80 //Don't convert system and race to ID format as they could be stored in non-XML file formats
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
81 //If they are in XML files then they'll already be valid
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
82 root.SetAttribute("system", toSave.GameSystem.ID);
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
83 root.SetAttribute("race", toSave.Race.ID);
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
84 root.SetAttribute("maxPoints", toSave.MaxPoints.ToString());
106
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
85 XmlElement units = doc.CreateElement("units");
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
86 root.AppendChild(units);
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
87
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
88 foreach (Unit unit in toSave.GetUnits())
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
89 {
108
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
90 units.AppendChild(CreateUnitElement(unit, doc));
106
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
91 }
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
92
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
93 return doc.OuterXml;
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
94 }
108
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
95
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
96 private XmlElement CreateUnitElement(Unit unit, XmlDocument doc)
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
97 {
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
98 XmlElement unitElem = doc.CreateElement("unit");
109
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
99 unitElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(unit.ID));
137
f58051572ec7 Fixes #128: Don't save out default names
IBBoard <dev@ibboard.co.uk>
parents: 125
diff changeset
100 unitElem.SetAttribute("unitName", (unit.HasDefaultName() ? "" : unit.Name));
108
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
101 unitElem.SetAttribute("unitType", unit.UnitType.ID);
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
102 unitElem.SetAttribute("size", unit.Size.ToString());
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
103
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
104 if (!unit.Race.Equals(unit.Army.Race))
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
105 {
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
106 unitElem.SetAttribute("race", unit.Race.ID);
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
107 }
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
108
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
109 XmlElement equipmentElem = CreateEquipmentItemsElement(unit, doc);
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
110
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
111 if (equipmentElem != null)
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
112 {
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
113 unitElem.AppendChild(equipmentElem);
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
114 }
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
115
109
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
116 XmlElement containedElem = CreateContainedUnitsElement(unit, doc);
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
117
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
118 if (containedElem != null)
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
119 {
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
120 unitElem.AppendChild(containedElem);
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
121 }
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
122
108
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
123 return unitElem;
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
124 }
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
125
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
126 private XmlElement CreateEquipmentItemsElement(Unit unit, XmlDocument doc)
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
127 {
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
128 UnitEquipmentItem[] equipItems = unit.GetEquipment();
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
129 int equipItemCount = equipItems.Length;
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
130 XmlElement equipmentElem = null;
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
131
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
132 if (equipItemCount > 0)
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
133 {
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
134 equipmentElem = doc.CreateElement("equipment");
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
135
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
136 for (int i = 0; i < equipItemCount; i++)
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
137 {
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
138 equipmentElem.AppendChild(CreateEquipmentElement(equipItems[i], unit, doc));
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
139 }
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
140 }
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
141
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
142 return equipmentElem;
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
143 }
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
144
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
145 private XmlElement CreateEquipmentElement(UnitEquipmentItem item, Unit unit, XmlDocument doc)
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
146 {
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
147 XmlElement equipmentItemElem = doc.CreateElement("equipItem");
122
571eee2b7b30 Re #54: Add army loading
IBBoard <dev@ibboard.co.uk>
parents: 115
diff changeset
148 equipmentItemElem.SetAttribute("id", item.ID);
108
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
149 equipmentItemElem.SetAttribute("amount", unit.GetEquipmentAmount(item).ToString());
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
150 equipmentItemElem.SetAttribute("amountType", unit.GetEquipmentAmountIsRatio(item) ? "ratio" : "fixed");
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
151 return equipmentItemElem;
2060f23abee9 Re #53: Create XML saver
IBBoard <dev@ibboard.co.uk>
parents: 107
diff changeset
152 }
109
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
153
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
154 private XmlElement CreateContainedUnitsElement(Unit unit, XmlDocument doc)
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
155 {
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
156 Unit[] containedUnits = unit.ContainedUnits;
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
157 int containedCount = containedUnits.Length;
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
158 XmlElement containedElem = null;
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
159
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
160 if (containedCount > 0)
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
161 {
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
162 containedElem = doc.CreateElement("contained");
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
163
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
164 for (int i = 0; i < containedCount; i++)
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
165 {
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
166 containedElem.AppendChild(CreateContainedUnitElement(containedUnits[i], doc));
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
167 }
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
168 }
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
169
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
170 return containedElem;
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
171 }
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
172
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
173 private XmlElement CreateContainedUnitElement(Unit unit, XmlDocument doc)
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
174 {
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
175 XmlElement containedUnitElem = doc.CreateElement("containedUnit");
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
176 containedUnitElem.SetAttribute("containedID", XmlTools.GetAsciiXmlIdForString(unit.ID));
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
177 return containedUnitElem;
45f1db6356e9 Fixes #53: Make XML file saver
IBBoard <dev@ibboard.co.uk>
parents: 108
diff changeset
178 }
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
179 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
180 }