view api/Factories/Xml/WarFoundryXmlSaver.cs @ 108:2060f23abee9

Re #53: Create XML saver * Add saving of unit equipment * Add extra property to DTD to indicate whether equipment amount is fixed or a ratio
author IBBoard <dev@ibboard.co.uk>
date Fri, 21 Aug 2009 20:12:27 +0000
parents c4ee96a91018
children 45f1db6356e9
line wrap: on
line source

// This file (WarFoundryXmlSaver.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2008, 2009 IBBoard.
//
// 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.

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using IBBoard.Lang;
using IBBoard.WarFoundry.API.Factories.Xml.Zip;
using IBBoard.WarFoundry.API.Objects;
using IBBoard.WarFoundry.API.Savers;
using ICSharpCode.SharpZipLib.Zip;

namespace IBBoard.WarFoundry.API.Factories.Xml
{
	public class WarFoundryXmlSaver : IWarFoundryFileSaver
	{
		public bool Save(Army toSave, string savePath)
		{
			bool success = false;
			ZipFile file = null;

			try
			{
				file = ZipFile.Create(savePath);
				file.BeginUpdate();
				file.Add(new StringZipEntrySource(CreateXmlString(toSave)), "data.army");
				file.CommitUpdate();
				success = true;
			}
			finally
			{
				if (file != null)
				{
					file.Close();
				}
			}

			return success;
		}

		private string CreateXmlString(WarFoundryObject toSave)
		{
			string xmlString = "";

			if (toSave is Army)
			{
				xmlString = CreateArmyXmlString((Army)toSave);
			}

			return xmlString;
		}

		private string CreateArmyXmlString(Army toSave)
		{
			/*
<army id="12345" name="Sample Army" system="sampleSystem" race="Empire" maxPoints="500">
	<units>
		<unit id="unit1" unitType="Empire1" unitName="General Eustace" size="1">
			<equipment>
				<equipItem id="equip1" amount="1"/>
			</equipment>
		</unit>
		<unit id="unit2" unitType="Empire2" unitName="First Swordsmen" size="20">
			<equipment>
				<equipItem id="equip1" amount="1"/>
				<equipItem id="equip2" amount="1"/>
			</equipment>
		</unit>
		<unit id="unit3" unitType="Empire2" unitName="First Greatswords" size="15">
			<equipment>
				<equipItem id="equip3" amount="1"/>
			</equipment>
		</unit>
	</units>
</army>
*/
			XmlDocument doc = new XmlDocument();
			XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null);
			doc.AppendChild(declaration);
			XmlSchema schema = new XmlSchema();
			schema.Namespaces.Add("xmlns", "http://ibboard.co.uk/warfoundry/army");
			schema.Namespaces.Add("xmlns:core", "http://ibboard.co.uk/warfoundry/core");
			doc.Schemas.Add(schema);
			XmlElement root = doc.CreateElement("army");
			doc.AppendChild(root);
			root.SetAttribute("id", toSave.ID);
			root.SetAttribute("name", toSave.Name);
			root.SetAttribute("system", toSave.GameSystem.ID);
			root.SetAttribute("race", toSave.Race.ID);
			root.SetAttribute("maxPoints", toSave.MaxPoints.ToString());
			XmlElement units = doc.CreateElement("units");
			root.AppendChild(units);
			
			foreach (Unit unit in toSave.GetUnits())
			{
				units.AppendChild(CreateUnitElement(unit, doc));
				
			}
			
			return doc.OuterXml;
		}

		private XmlElement CreateUnitElement(Unit unit, XmlDocument doc)
		{
			XmlElement unitElem = doc.CreateElement("unit");
			unitElem.SetAttribute("id", unit.ID);
			unitElem.SetAttribute("unitName", unit.Name);
			unitElem.SetAttribute("unitType", unit.UnitType.ID);
			unitElem.SetAttribute("size", unit.Size.ToString());
			
			if (!unit.Race.Equals(unit.Army.Race))
			{
				unitElem.SetAttribute("race", unit.Race.ID);
			}

			XmlElement equipmentElem = CreateEquipmentItemsElement(unit, doc);

			if (equipmentElem != null)
			{
				unitElem.AppendChild(equipmentElem);
			}
			
			return unitElem;
		}

		private XmlElement CreateEquipmentItemsElement(Unit unit, XmlDocument doc)
		{
			UnitEquipmentItem[] equipItems = unit.GetEquipment();
			int equipItemCount = equipItems.Length;
			XmlElement equipmentElem = null;

			if (equipItemCount > 0)
			{
				equipmentElem = doc.CreateElement("equipment");
				
				for (int i = 0; i < equipItemCount; i++)
				{
					equipmentElem.AppendChild(CreateEquipmentElement(equipItems[i], unit, doc));
				}
			}

			return equipmentElem;
		}

		private XmlElement CreateEquipmentElement(UnitEquipmentItem item, Unit unit, XmlDocument doc)
		{
			XmlElement equipmentItemElem = doc.CreateElement("equipItem");
			equipmentItemElem.SetAttribute("id", item.EquipmentItemID);
			equipmentItemElem.SetAttribute("amount", unit.GetEquipmentAmount(item).ToString());
			equipmentItemElem.SetAttribute("amountType", unit.GetEquipmentAmountIsRatio(item) ? "ratio" : "fixed");
			return equipmentItemElem;
		}
	}
}