changeset 109:45f1db6356e9

Fixes #53: Make XML file saver * force file extension * Make sure IDs are in valid XML format * Remove commented example of file * Output contained units
author IBBoard <dev@ibboard.co.uk>
date Sat, 22 Aug 2009 10:51:50 +0000
parents 2060f23abee9
children f0fb96d0cfe9
files api/Factories/Xml/WarFoundryXmlSaver.cs
diffstat 1 files changed, 45 insertions(+), 25 deletions(-) [+]
line diff
     1.1 --- a/api/Factories/Xml/WarFoundryXmlSaver.cs	Fri Aug 21 20:12:27 2009 +0000
     1.2 +++ b/api/Factories/Xml/WarFoundryXmlSaver.cs	Sat Aug 22 10:51:50 2009 +0000
     1.3 @@ -8,6 +8,7 @@
     1.4  using System.Xml;
     1.5  using System.Xml.Schema;
     1.6  using IBBoard.Lang;
     1.7 +using IBBoard.Xml;
     1.8  using IBBoard.WarFoundry.API.Factories.Xml.Zip;
     1.9  using IBBoard.WarFoundry.API.Objects;
    1.10  using IBBoard.WarFoundry.API.Savers;
    1.11 @@ -17,10 +18,17 @@
    1.12  {
    1.13  	public class WarFoundryXmlSaver : IWarFoundryFileSaver
    1.14  	{
    1.15 +		public const string ARMY_FILE_EXTENSION = ".army";
    1.16 +		
    1.17  		public bool Save(Army toSave, string savePath)
    1.18  		{
    1.19  			bool success = false;
    1.20  			ZipFile file = null;
    1.21 +			
    1.22 +			if (!savePath.EndsWith(ARMY_FILE_EXTENSION))
    1.23 +			{
    1.24 +				savePath = savePath + ARMY_FILE_EXTENSION;
    1.25 +			}
    1.26  
    1.27  			try
    1.28  			{
    1.29 @@ -55,28 +63,6 @@
    1.30  
    1.31  		private string CreateArmyXmlString(Army toSave)
    1.32  		{
    1.33 -			/*
    1.34 -<army id="12345" name="Sample Army" system="sampleSystem" race="Empire" maxPoints="500">
    1.35 -	<units>
    1.36 -		<unit id="unit1" unitType="Empire1" unitName="General Eustace" size="1">
    1.37 -			<equipment>
    1.38 -				<equipItem id="equip1" amount="1"/>
    1.39 -			</equipment>
    1.40 -		</unit>
    1.41 -		<unit id="unit2" unitType="Empire2" unitName="First Swordsmen" size="20">
    1.42 -			<equipment>
    1.43 -				<equipItem id="equip1" amount="1"/>
    1.44 -				<equipItem id="equip2" amount="1"/>
    1.45 -			</equipment>
    1.46 -		</unit>
    1.47 -		<unit id="unit3" unitType="Empire2" unitName="First Greatswords" size="15">
    1.48 -			<equipment>
    1.49 -				<equipItem id="equip3" amount="1"/>
    1.50 -			</equipment>
    1.51 -		</unit>
    1.52 -	</units>
    1.53 -</army>
    1.54 -*/
    1.55  			XmlDocument doc = new XmlDocument();
    1.56  			XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null);
    1.57  			doc.AppendChild(declaration);
    1.58 @@ -86,8 +72,10 @@
    1.59  			doc.Schemas.Add(schema);
    1.60  			XmlElement root = doc.CreateElement("army");
    1.61  			doc.AppendChild(root);
    1.62 -			root.SetAttribute("id", toSave.ID);
    1.63 +			root.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(toSave.ID));
    1.64  			root.SetAttribute("name", toSave.Name);
    1.65 +			//Don't convert system and race to ID format as they could be stored in non-XML file formats
    1.66 +			//If they are in XML files then they'll already be valid
    1.67  			root.SetAttribute("system", toSave.GameSystem.ID);
    1.68  			root.SetAttribute("race", toSave.Race.ID);
    1.69  			root.SetAttribute("maxPoints", toSave.MaxPoints.ToString());
    1.70 @@ -97,7 +85,6 @@
    1.71  			foreach (Unit unit in toSave.GetUnits())
    1.72  			{
    1.73  				units.AppendChild(CreateUnitElement(unit, doc));
    1.74 -				
    1.75  			}
    1.76  			
    1.77  			return doc.OuterXml;
    1.78 @@ -106,7 +93,7 @@
    1.79  		private XmlElement CreateUnitElement(Unit unit, XmlDocument doc)
    1.80  		{
    1.81  			XmlElement unitElem = doc.CreateElement("unit");
    1.82 -			unitElem.SetAttribute("id", unit.ID);
    1.83 +			unitElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(unit.ID));
    1.84  			unitElem.SetAttribute("unitName", unit.Name);
    1.85  			unitElem.SetAttribute("unitType", unit.UnitType.ID);
    1.86  			unitElem.SetAttribute("size", unit.Size.ToString());
    1.87 @@ -123,6 +110,13 @@
    1.88  				unitElem.AppendChild(equipmentElem);
    1.89  			}
    1.90  			
    1.91 +			XmlElement containedElem = CreateContainedUnitsElement(unit, doc);
    1.92 +
    1.93 +			if (containedElem != null)
    1.94 +			{
    1.95 +				unitElem.AppendChild(containedElem);
    1.96 +			}
    1.97 +			
    1.98  			return unitElem;
    1.99  		}
   1.100  
   1.101 @@ -153,5 +147,31 @@
   1.102  			equipmentItemElem.SetAttribute("amountType", unit.GetEquipmentAmountIsRatio(item) ? "ratio" : "fixed");
   1.103  			return equipmentItemElem;
   1.104  		}
   1.105 +		
   1.106 +		private XmlElement CreateContainedUnitsElement(Unit unit, XmlDocument doc)
   1.107 +		{
   1.108 +			Unit[] containedUnits = unit.ContainedUnits;
   1.109 +			int containedCount = containedUnits.Length;
   1.110 +			XmlElement containedElem = null;
   1.111 +
   1.112 +			if (containedCount > 0)
   1.113 +			{
   1.114 +				containedElem = doc.CreateElement("contained");
   1.115 +				
   1.116 +				for (int i = 0; i < containedCount; i++)
   1.117 +				{
   1.118 +					containedElem.AppendChild(CreateContainedUnitElement(containedUnits[i], doc));
   1.119 +				}
   1.120 +			}
   1.121 +
   1.122 +			return containedElem;
   1.123 +		}
   1.124 +
   1.125 +		private XmlElement CreateContainedUnitElement(Unit unit,  XmlDocument doc)
   1.126 +		{
   1.127 +			XmlElement containedUnitElem = doc.CreateElement("containedUnit");
   1.128 +			containedUnitElem.SetAttribute("containedID", XmlTools.GetAsciiXmlIdForString(unit.ID));
   1.129 +			return containedUnitElem;
   1.130 +		}
   1.131  	}
   1.132  }