view api/Savers/Xml/WarFoundryXmlArmySaver.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 8a64b36d36b8
children
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.Xml;
using IBBoard.WarFoundry.API.Factories.Xml.Zip;
using IBBoard.WarFoundry.API.Objects;
using IBBoard.WarFoundry.API.Savers;
using IBBoard.WarFoundry.API.Util;
using ICSharpCode.SharpZipLib.Zip;

namespace IBBoard.WarFoundry.API.Savers.Xml
{
	public class WarFoundryXmlArmySaver
	{
		public string CreateXmlString(Army toSave)
		{
			XmlDocument doc = new XmlDocument();
			XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
			doc.AppendChild(declaration);
			XmlSchema schema = new XmlSchema();
			schema.Namespaces.Add("", "http://ibboard.co.uk/warfoundry/army");
			schema.Namespaces.Add("core", "http://ibboard.co.uk/warfoundry/core");
			doc.Schemas.Add(schema);
			XmlElement root = doc.CreateElement("army");
			root.SetAttribute("xmlns", "http://ibboard.co.uk/warfoundry/army");
			root.SetAttribute("xmlns:core", "http://ibboard.co.uk/warfoundry/core");
			doc.AppendChild(root);
			root.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(toSave.ID));
			root.SetAttribute("name", toSave.Name);
			//Don't convert system and race to ID format as they could be stored in non-XML file formats
			//If they are in XML files then they'll already be valid
			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", XmlTools.GetAsciiXmlIdForString(unit.ID));
			unitElem.SetAttribute("unitName", (unit.HasDefaultName() ? "" : 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);
			}
			
			Category unitCategory = unit.Category.Category;
			if (!unit.UnitType.MainCategory.Equals(unitCategory))
			{
				unitElem.SetAttribute("category", unitCategory.ID);
			}

			XmlElement equipmentElem = CreateEquipmentItemsElement(unit, doc);

			if (equipmentElem != null)
			{
				unitElem.AppendChild(equipmentElem);
			}
			
			XmlElement containedElem = CreateContainedUnitsElement(unit, doc);

			if (containedElem != null)
			{
				unitElem.AppendChild(containedElem);
			}
			
			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.ID);
			equipmentItemElem.SetAttribute("amount", UnitEquipmentUtil.GetEquipmentAmount(unit, item).ToString());
			equipmentItemElem.SetAttribute("amountType", UnitEquipmentUtil.GetEquipmentAmountIsRatio(unit, item) ? "ratio" : "fixed");
			return equipmentItemElem;
		}
		
		private XmlElement CreateContainedUnitsElement(Unit unit, XmlDocument doc)
		{
			Unit[] containedUnits = unit.ContainedUnits;
			int containedCount = containedUnits.Length;
			XmlElement containedElem = null;

			if (containedCount > 0)
			{
				containedElem = doc.CreateElement("contained");
				
				for (int i = 0; i < containedCount; i++)
				{
					containedElem.AppendChild(CreateContainedUnitElement(containedUnits[i], doc));
				}
			}

			return containedElem;
		}

		private XmlElement CreateContainedUnitElement(Unit unit,  XmlDocument doc)
		{
			XmlElement containedUnitElem = doc.CreateElement("containedUnit");
			containedUnitElem.SetAttribute("containedID", XmlTools.GetAsciiXmlIdForString(unit.ID));
			return containedUnitElem;
		}
	}
}