changeset 316:40a2df1f629a

Re #324: Add saving of Race and System data to files * Move saver classes out of Factories folder and into Savers folder/namespace
author IBBoard <dev@ibboard.co.uk>
date Sat, 05 Mar 2011 11:52:09 +0000
parents 6cb0fb78b9a6
children 58cd0fa53976
files IBBoard.WarFoundry.API.csproj api/Factories/Xml/WarFoundryXmlArmySaver.cs api/Factories/Xml/WarFoundryXmlGameSystemSaver.cs api/Savers/Xml/WarFoundryXmlArmySaver.cs api/Savers/Xml/WarFoundryXmlGameSystemSaver.cs
diffstat 5 files changed, 342 insertions(+), 337 deletions(-) [+]
line diff
     1.1 --- a/IBBoard.WarFoundry.API.csproj	Mon Feb 28 21:09:20 2011 +0000
     1.2 +++ b/IBBoard.WarFoundry.API.csproj	Sat Mar 05 11:52:09 2011 +0000
     1.3 @@ -62,7 +62,6 @@
     1.4    <ItemGroup>
     1.5      <None Include="app.config" />
     1.6      <None Include="COPYING" />
     1.7 -    <Compile Include="api\Factories\Xml\WarFoundryXmlGameSystemSaver.cs" />
     1.8      <Compile Include="api\Objects\ICostedWarFoundryObject.cs" />
     1.9      <Compile Include="api\Commands\CreateAndAddUnitCommand.cs" />
    1.10      <Compile Include="api\Commands\RemoveUnitCommand.cs" />
    1.11 @@ -186,7 +185,8 @@
    1.12      <Compile Include="api\Savers\IWarFoundryArmySaver.cs" />
    1.13      <Compile Include="api\Savers\IWarFoundryRaceSaver.cs" />
    1.14      <Compile Include="api\Objects\WarFoundryLoadedObject.cs" />
    1.15 -    <Compile Include="api\Factories\Xml\WarFoundryXmlArmySaver.cs" />
    1.16 +    <Compile Include="api\Savers\Xml\WarFoundryXmlArmySaver.cs" />
    1.17 +    <Compile Include="api\Savers\Xml\WarFoundryXmlGameSystemSaver.cs" />
    1.18    </ItemGroup>
    1.19    <ItemGroup>
    1.20      <Reference Include="System.Xml" />
    1.21 @@ -243,4 +243,9 @@
    1.22        </Properties>
    1.23      </MonoDevelop>
    1.24    </ProjectExtensions>
    1.25 +  <ItemGroup>
    1.26 +    <Folder Include="api\Factories\Xml\" />
    1.27 +    <Folder Include="api\Factories\Xml\" />
    1.28 +    <Folder Include="api\Savers\Xml\" />
    1.29 +  </ItemGroup>
    1.30  </Project>
    1.31 \ No newline at end of file
     2.1 --- a/api/Factories/Xml/WarFoundryXmlArmySaver.cs	Mon Feb 28 21:09:20 2011 +0000
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,186 +0,0 @@
     2.4 -// This file (WarFoundryXmlSaver.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2008, 2009 IBBoard.
     2.5 -//
     2.6 -// 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.
     2.7 -
     2.8 -using System;
     2.9 -using System.Collections.Generic;
    2.10 -using System.IO;
    2.11 -using System.Xml;
    2.12 -using System.Xml.Schema;
    2.13 -using IBBoard.Lang;
    2.14 -using IBBoard.Xml;
    2.15 -using IBBoard.WarFoundry.API.Factories.Xml.Zip;
    2.16 -using IBBoard.WarFoundry.API.Objects;
    2.17 -using IBBoard.WarFoundry.API.Savers;
    2.18 -using IBBoard.WarFoundry.API.Util;
    2.19 -using ICSharpCode.SharpZipLib.Zip;
    2.20 -
    2.21 -namespace IBBoard.WarFoundry.API.Factories.Xml
    2.22 -{
    2.23 -	public class WarFoundryXmlArmySaver : IWarFoundryArmySaver
    2.24 -	{
    2.25 -		public const string ARMY_FILE_EXTENSION = ".army";
    2.26 -		
    2.27 -		public bool Save(Army toSave, string savePath)
    2.28 -		{
    2.29 -			bool success = false;
    2.30 -			ZipFile file = null;
    2.31 -			
    2.32 -			if (!savePath.EndsWith(ARMY_FILE_EXTENSION))
    2.33 -			{
    2.34 -				savePath = savePath + ARMY_FILE_EXTENSION;
    2.35 -			}
    2.36 -
    2.37 -			try
    2.38 -			{
    2.39 -				file = ZipFile.Create(savePath);
    2.40 -				file.BeginUpdate();
    2.41 -				file.Add(new StringZipEntrySource(CreateXmlString(toSave)), "data.armyx");
    2.42 -				file.CommitUpdate();
    2.43 -				success = true;
    2.44 -			}
    2.45 -			finally
    2.46 -			{
    2.47 -				if (file != null)
    2.48 -				{
    2.49 -					file.Close();
    2.50 -				}
    2.51 -			}
    2.52 -
    2.53 -			return success;
    2.54 -		}
    2.55 -
    2.56 -		private string CreateXmlString(WarFoundryObject toSave)
    2.57 -		{
    2.58 -			string xmlString = "";
    2.59 -
    2.60 -			if (toSave is Army)
    2.61 -			{
    2.62 -				xmlString = CreateArmyXmlString((Army)toSave);
    2.63 -			}
    2.64 -
    2.65 -			return xmlString;
    2.66 -		}
    2.67 -
    2.68 -		private string CreateArmyXmlString(Army toSave)
    2.69 -		{
    2.70 -			XmlDocument doc = new XmlDocument();
    2.71 -			XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null);
    2.72 -			doc.AppendChild(declaration);
    2.73 -			XmlSchema schema = new XmlSchema();
    2.74 -			schema.Namespaces.Add("", "http://ibboard.co.uk/warfoundry/army");
    2.75 -			schema.Namespaces.Add("core", "http://ibboard.co.uk/warfoundry/core");
    2.76 -			doc.Schemas.Add(schema);
    2.77 -			XmlElement root = doc.CreateElement("army");
    2.78 -			root.SetAttribute("xmlns", "http://ibboard.co.uk/warfoundry/army");
    2.79 -			root.SetAttribute("xmlns:core", "http://ibboard.co.uk/warfoundry/core");
    2.80 -			doc.AppendChild(root);
    2.81 -			root.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(toSave.ID));
    2.82 -			root.SetAttribute("name", toSave.Name);
    2.83 -			//Don't convert system and race to ID format as they could be stored in non-XML file formats
    2.84 -			//If they are in XML files then they'll already be valid
    2.85 -			root.SetAttribute("system", toSave.GameSystem.ID);
    2.86 -			root.SetAttribute("race", toSave.Race.ID);
    2.87 -			root.SetAttribute("maxPoints", toSave.MaxPoints.ToString());
    2.88 -			XmlElement units = doc.CreateElement("units");
    2.89 -			root.AppendChild(units);
    2.90 -			
    2.91 -			foreach (Unit unit in toSave.GetUnits())
    2.92 -			{
    2.93 -				units.AppendChild(CreateUnitElement(unit, doc));
    2.94 -			}
    2.95 -			
    2.96 -			return doc.OuterXml;
    2.97 -		}
    2.98 -
    2.99 -		private XmlElement CreateUnitElement(Unit unit, XmlDocument doc)
   2.100 -		{
   2.101 -			XmlElement unitElem = doc.CreateElement("unit");
   2.102 -			unitElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(unit.ID));
   2.103 -			unitElem.SetAttribute("unitName", (unit.HasDefaultName() ? "" : unit.Name));
   2.104 -			unitElem.SetAttribute("unitType", unit.UnitType.ID);
   2.105 -			unitElem.SetAttribute("size", unit.Size.ToString());
   2.106 -			
   2.107 -			if (!unit.Race.Equals(unit.Army.Race))
   2.108 -			{
   2.109 -				unitElem.SetAttribute("race", unit.Race.ID);
   2.110 -			}
   2.111 -			
   2.112 -			Category unitCategory = unit.Category.Category;
   2.113 -			if (!unit.UnitType.MainCategory.Equals(unitCategory))
   2.114 -			{
   2.115 -				unitElem.SetAttribute("category", unitCategory.ID);
   2.116 -			}
   2.117 -
   2.118 -			XmlElement equipmentElem = CreateEquipmentItemsElement(unit, doc);
   2.119 -
   2.120 -			if (equipmentElem != null)
   2.121 -			{
   2.122 -				unitElem.AppendChild(equipmentElem);
   2.123 -			}
   2.124 -			
   2.125 -			XmlElement containedElem = CreateContainedUnitsElement(unit, doc);
   2.126 -
   2.127 -			if (containedElem != null)
   2.128 -			{
   2.129 -				unitElem.AppendChild(containedElem);
   2.130 -			}
   2.131 -			
   2.132 -			return unitElem;
   2.133 -		}
   2.134 -
   2.135 -		private XmlElement CreateEquipmentItemsElement(Unit unit, XmlDocument doc)
   2.136 -		{
   2.137 -			UnitEquipmentItem[] equipItems = unit.GetEquipment();
   2.138 -			int equipItemCount = equipItems.Length;
   2.139 -			XmlElement equipmentElem = null;
   2.140 -
   2.141 -			if (equipItemCount > 0)
   2.142 -			{
   2.143 -				equipmentElem = doc.CreateElement("equipment");
   2.144 -				
   2.145 -				for (int i = 0; i < equipItemCount; i++)
   2.146 -				{
   2.147 -					equipmentElem.AppendChild(CreateEquipmentElement(equipItems[i], unit, doc));
   2.148 -				}
   2.149 -			}
   2.150 -
   2.151 -			return equipmentElem;
   2.152 -		}
   2.153 -
   2.154 -		private XmlElement CreateEquipmentElement(UnitEquipmentItem item, Unit unit, XmlDocument doc)
   2.155 -		{
   2.156 -			XmlElement equipmentItemElem = doc.CreateElement("equipItem");
   2.157 -			equipmentItemElem.SetAttribute("id", item.ID);
   2.158 -			equipmentItemElem.SetAttribute("amount", UnitEquipmentUtil.GetEquipmentAmount(unit, item).ToString());
   2.159 -			equipmentItemElem.SetAttribute("amountType", UnitEquipmentUtil.GetEquipmentAmountIsRatio(unit, item) ? "ratio" : "fixed");
   2.160 -			return equipmentItemElem;
   2.161 -		}
   2.162 -		
   2.163 -		private XmlElement CreateContainedUnitsElement(Unit unit, XmlDocument doc)
   2.164 -		{
   2.165 -			Unit[] containedUnits = unit.ContainedUnits;
   2.166 -			int containedCount = containedUnits.Length;
   2.167 -			XmlElement containedElem = null;
   2.168 -
   2.169 -			if (containedCount > 0)
   2.170 -			{
   2.171 -				containedElem = doc.CreateElement("contained");
   2.172 -				
   2.173 -				for (int i = 0; i < containedCount; i++)
   2.174 -				{
   2.175 -					containedElem.AppendChild(CreateContainedUnitElement(containedUnits[i], doc));
   2.176 -				}
   2.177 -			}
   2.178 -
   2.179 -			return containedElem;
   2.180 -		}
   2.181 -
   2.182 -		private XmlElement CreateContainedUnitElement(Unit unit,  XmlDocument doc)
   2.183 -		{
   2.184 -			XmlElement containedUnitElem = doc.CreateElement("containedUnit");
   2.185 -			containedUnitElem.SetAttribute("containedID", XmlTools.GetAsciiXmlIdForString(unit.ID));
   2.186 -			return containedUnitElem;
   2.187 -		}
   2.188 -	}
   2.189 -}
     3.1 --- a/api/Factories/Xml/WarFoundryXmlGameSystemSaver.cs	Mon Feb 28 21:09:20 2011 +0000
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,149 +0,0 @@
     3.4 -// This file (WarFoundryXmlGameSystemSaver.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2008, 2009 IBBoard.
     3.5 -//
     3.6 -// 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.
     3.7 -
     3.8 -using System;
     3.9 -using System.Collections.Generic;
    3.10 -using System.IO;
    3.11 -using System.Xml;
    3.12 -using System.Xml.Schema;
    3.13 -using IBBoard.Lang;
    3.14 -using IBBoard.Xml;
    3.15 -using IBBoard.WarFoundry.API.Factories.Xml.Zip;
    3.16 -using IBBoard.WarFoundry.API.Objects;
    3.17 -using IBBoard.WarFoundry.API.Savers;
    3.18 -using IBBoard.WarFoundry.API.Util;
    3.19 -using ICSharpCode.SharpZipLib.Zip;
    3.20 -
    3.21 -namespace IBBoard.WarFoundry.API.Factories.Xml
    3.22 -{
    3.23 -	public class WarFoundryXmlGameSystemSaver : IWarFoundryGameSystemSaver
    3.24 -	{
    3.25 -		public const string GAMESYSTEM_FILE_EXTENSION = ".system";
    3.26 -
    3.27 -		public bool Save(GameSystem toSave, string savePath)
    3.28 -		{
    3.29 -			bool success = false;
    3.30 -			ZipFile file = null;
    3.31 -
    3.32 -			if (!savePath.EndsWith(GAMESYSTEM_FILE_EXTENSION))
    3.33 -			{
    3.34 -				savePath = savePath + GAMESYSTEM_FILE_EXTENSION;
    3.35 -			}
    3.36 -
    3.37 -			try
    3.38 -			{
    3.39 -				file = ZipFile.Create(savePath);
    3.40 -				file.BeginUpdate();
    3.41 -				file.Add(new StringZipEntrySource(CreateXmlString(toSave)), "data.systemx");
    3.42 -				file.CommitUpdate();
    3.43 -				success = true;
    3.44 -			}
    3.45 -			finally
    3.46 -			{
    3.47 -				if (file != null)
    3.48 -				{
    3.49 -					file.Close();
    3.50 -				}
    3.51 -			}
    3.52 -
    3.53 -			return success;
    3.54 -		}
    3.55 -
    3.56 -		private string CreateXmlString(WarFoundryObject toSave)
    3.57 -		{
    3.58 -			string xmlString = "";
    3.59 -
    3.60 -			if (toSave is GameSystem)
    3.61 -			{
    3.62 -				xmlString = CreateGameSystemXmlString((GameSystem)toSave);
    3.63 -			}
    3.64 -
    3.65 -			return xmlString;
    3.66 -		}
    3.67 -
    3.68 -		private string CreateGameSystemXmlString(GameSystem toSave)
    3.69 -		{
    3.70 -			XmlDocument doc = new XmlDocument();
    3.71 -			XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null);
    3.72 -			doc.AppendChild(declaration);
    3.73 -			XmlSchema schema = new XmlSchema();
    3.74 -			schema.Namespaces.Add("", "http://ibboard.co.uk/warfoundry/system");
    3.75 -			schema.Namespaces.Add("cats", "http://ibboard.co.uk/warfoundry/cats");
    3.76 -			doc.Schemas.Add(schema);
    3.77 -			XmlElement root = doc.CreateElement("system");
    3.78 -			root.SetAttribute("xmlns", "http://ibboard.co.uk/warfoundry/system");
    3.79 -			root.SetAttribute("xmlns:cats", "http://ibboard.co.uk/warfoundry/cats");
    3.80 -			doc.AppendChild(root);
    3.81 -			root.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(toSave.ID));
    3.82 -			root.SetAttribute("name", toSave.Name);
    3.83 -			root.SetAttribute("defaultArmySize", toSave.SystemArmyDefaultSize.ToString());
    3.84 -			root.SetAttribute("warn", toSave.WarnOnError.ToString().ToLowerInvariant());
    3.85 -			root.SetAttribute("allowAllies", toSave.AllowAllies.ToString().ToLowerInvariant());
    3.86 -			XmlElement cats = doc.CreateElement("categories");
    3.87 -			root.AppendChild(cats);
    3.88 -
    3.89 -			foreach (Category cat in toSave.Categories)
    3.90 -			{
    3.91 -				cats.AppendChild(CreateCategoryElement(cat, doc));
    3.92 -			}
    3.93 -
    3.94 -			XmlElement sysStatsList = doc.CreateElement("sysStatsList");
    3.95 -			sysStatsList.SetAttribute("defaultStats", XmlTools.GetAsciiXmlIdForString(toSave.StandardSystemStatsID));
    3.96 -			root.AppendChild(sysStatsList);
    3.97 -
    3.98 -			foreach(SystemStats stats in toSave.SystemStats)
    3.99 -			{
   3.100 -				sysStatsList.AppendChild(CreateSystemStatsElement(stats, doc));
   3.101 -			}
   3.102 -
   3.103 -			return doc.OuterXml;
   3.104 -		}
   3.105 -
   3.106 -		private XmlElement CreateCategoryElement(Category cat, XmlDocument doc)
   3.107 -		{
   3.108 -			XmlElement catElem = doc.CreateElement("cats:cat");
   3.109 -			catElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(cat.ID));
   3.110 -			catElem.SetAttribute("name", (cat.HasDefaultName() ? "" : cat.Name));
   3.111 -			if (cat.MinimumPoints > 0)
   3.112 -			{
   3.113 -				catElem.SetAttribute("minPoints", cat.MaximumPercentage.ToString());
   3.114 -			}
   3.115 -			if (cat.MaximumPoints < 100)
   3.116 -			{
   3.117 -				catElem.SetAttribute("maxPoints", cat.MaximumPercentage.ToString());
   3.118 -			}
   3.119 -			if(cat.MinimumPercentage > 0)
   3.120 -			{
   3.121 -				catElem.SetAttribute("minPercentage", cat.MaximumPercentage.ToString());
   3.122 -			}
   3.123 -			if(cat.MaximumPercentage < 100)
   3.124 -			{
   3.125 -				catElem.SetAttribute("maxPercentage", cat.MaximumPercentage.ToString());
   3.126 -			}
   3.127 -
   3.128 -			return catElem;
   3.129 -		}
   3.130 -
   3.131 -		private XmlElement CreateSystemStatsElement(SystemStats stats, XmlDocument doc)
   3.132 -		{
   3.133 -			XmlElement statsElem = doc.CreateElement("sysStats");
   3.134 -			statsElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(stats.ID));
   3.135 -
   3.136 -			foreach(StatSlot stat in stats.StatSlots)
   3.137 -			{
   3.138 -				statsElem.AppendChild(CreateSystemStatElement(stat, doc));
   3.139 -			}
   3.140 -
   3.141 -			return statsElem;
   3.142 -		}
   3.143 -
   3.144 -		private XmlElement CreateSystemStatElement(StatSlot stat, XmlDocument doc)
   3.145 -		{
   3.146 -			XmlElement statElem = doc.CreateElement("sysStat");
   3.147 -			statElem.SetAttribute("name", stat.Name);
   3.148 -
   3.149 -			return statElem;
   3.150 -		}
   3.151 -	}
   3.152 -}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/api/Savers/Xml/WarFoundryXmlArmySaver.cs	Sat Mar 05 11:52:09 2011 +0000
     4.3 @@ -0,0 +1,186 @@
     4.4 +// This file (WarFoundryXmlSaver.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2008, 2009 IBBoard.
     4.5 +//
     4.6 +// 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.
     4.7 +
     4.8 +using System;
     4.9 +using System.Collections.Generic;
    4.10 +using System.IO;
    4.11 +using System.Xml;
    4.12 +using System.Xml.Schema;
    4.13 +using IBBoard.Lang;
    4.14 +using IBBoard.Xml;
    4.15 +using IBBoard.WarFoundry.API.Factories.Xml.Zip;
    4.16 +using IBBoard.WarFoundry.API.Objects;
    4.17 +using IBBoard.WarFoundry.API.Savers;
    4.18 +using IBBoard.WarFoundry.API.Util;
    4.19 +using ICSharpCode.SharpZipLib.Zip;
    4.20 +
    4.21 +namespace IBBoard.WarFoundry.API.Savers.Xml
    4.22 +{
    4.23 +	public class WarFoundryXmlArmySaver : IWarFoundryArmySaver
    4.24 +	{
    4.25 +		public const string ARMY_FILE_EXTENSION = ".army";
    4.26 +		
    4.27 +		public bool Save(Army toSave, string savePath)
    4.28 +		{
    4.29 +			bool success = false;
    4.30 +			ZipFile file = null;
    4.31 +			
    4.32 +			if (!savePath.EndsWith(ARMY_FILE_EXTENSION))
    4.33 +			{
    4.34 +				savePath = savePath + ARMY_FILE_EXTENSION;
    4.35 +			}
    4.36 +
    4.37 +			try
    4.38 +			{
    4.39 +				file = ZipFile.Create(savePath);
    4.40 +				file.BeginUpdate();
    4.41 +				file.Add(new StringZipEntrySource(CreateXmlString(toSave)), "data.armyx");
    4.42 +				file.CommitUpdate();
    4.43 +				success = true;
    4.44 +			}
    4.45 +			finally
    4.46 +			{
    4.47 +				if (file != null)
    4.48 +				{
    4.49 +					file.Close();
    4.50 +				}
    4.51 +			}
    4.52 +
    4.53 +			return success;
    4.54 +		}
    4.55 +
    4.56 +		private string CreateXmlString(WarFoundryObject toSave)
    4.57 +		{
    4.58 +			string xmlString = "";
    4.59 +
    4.60 +			if (toSave is Army)
    4.61 +			{
    4.62 +				xmlString = CreateArmyXmlString((Army)toSave);
    4.63 +			}
    4.64 +
    4.65 +			return xmlString;
    4.66 +		}
    4.67 +
    4.68 +		private string CreateArmyXmlString(Army toSave)
    4.69 +		{
    4.70 +			XmlDocument doc = new XmlDocument();
    4.71 +			XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null);
    4.72 +			doc.AppendChild(declaration);
    4.73 +			XmlSchema schema = new XmlSchema();
    4.74 +			schema.Namespaces.Add("", "http://ibboard.co.uk/warfoundry/army");
    4.75 +			schema.Namespaces.Add("core", "http://ibboard.co.uk/warfoundry/core");
    4.76 +			doc.Schemas.Add(schema);
    4.77 +			XmlElement root = doc.CreateElement("army");
    4.78 +			root.SetAttribute("xmlns", "http://ibboard.co.uk/warfoundry/army");
    4.79 +			root.SetAttribute("xmlns:core", "http://ibboard.co.uk/warfoundry/core");
    4.80 +			doc.AppendChild(root);
    4.81 +			root.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(toSave.ID));
    4.82 +			root.SetAttribute("name", toSave.Name);
    4.83 +			//Don't convert system and race to ID format as they could be stored in non-XML file formats
    4.84 +			//If they are in XML files then they'll already be valid
    4.85 +			root.SetAttribute("system", toSave.GameSystem.ID);
    4.86 +			root.SetAttribute("race", toSave.Race.ID);
    4.87 +			root.SetAttribute("maxPoints", toSave.MaxPoints.ToString());
    4.88 +			XmlElement units = doc.CreateElement("units");
    4.89 +			root.AppendChild(units);
    4.90 +			
    4.91 +			foreach (Unit unit in toSave.GetUnits())
    4.92 +			{
    4.93 +				units.AppendChild(CreateUnitElement(unit, doc));
    4.94 +			}
    4.95 +			
    4.96 +			return doc.OuterXml;
    4.97 +		}
    4.98 +
    4.99 +		private XmlElement CreateUnitElement(Unit unit, XmlDocument doc)
   4.100 +		{
   4.101 +			XmlElement unitElem = doc.CreateElement("unit");
   4.102 +			unitElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(unit.ID));
   4.103 +			unitElem.SetAttribute("unitName", (unit.HasDefaultName() ? "" : unit.Name));
   4.104 +			unitElem.SetAttribute("unitType", unit.UnitType.ID);
   4.105 +			unitElem.SetAttribute("size", unit.Size.ToString());
   4.106 +			
   4.107 +			if (!unit.Race.Equals(unit.Army.Race))
   4.108 +			{
   4.109 +				unitElem.SetAttribute("race", unit.Race.ID);
   4.110 +			}
   4.111 +			
   4.112 +			Category unitCategory = unit.Category.Category;
   4.113 +			if (!unit.UnitType.MainCategory.Equals(unitCategory))
   4.114 +			{
   4.115 +				unitElem.SetAttribute("category", unitCategory.ID);
   4.116 +			}
   4.117 +
   4.118 +			XmlElement equipmentElem = CreateEquipmentItemsElement(unit, doc);
   4.119 +
   4.120 +			if (equipmentElem != null)
   4.121 +			{
   4.122 +				unitElem.AppendChild(equipmentElem);
   4.123 +			}
   4.124 +			
   4.125 +			XmlElement containedElem = CreateContainedUnitsElement(unit, doc);
   4.126 +
   4.127 +			if (containedElem != null)
   4.128 +			{
   4.129 +				unitElem.AppendChild(containedElem);
   4.130 +			}
   4.131 +			
   4.132 +			return unitElem;
   4.133 +		}
   4.134 +
   4.135 +		private XmlElement CreateEquipmentItemsElement(Unit unit, XmlDocument doc)
   4.136 +		{
   4.137 +			UnitEquipmentItem[] equipItems = unit.GetEquipment();
   4.138 +			int equipItemCount = equipItems.Length;
   4.139 +			XmlElement equipmentElem = null;
   4.140 +
   4.141 +			if (equipItemCount > 0)
   4.142 +			{
   4.143 +				equipmentElem = doc.CreateElement("equipment");
   4.144 +				
   4.145 +				for (int i = 0; i < equipItemCount; i++)
   4.146 +				{
   4.147 +					equipmentElem.AppendChild(CreateEquipmentElement(equipItems[i], unit, doc));
   4.148 +				}
   4.149 +			}
   4.150 +
   4.151 +			return equipmentElem;
   4.152 +		}
   4.153 +
   4.154 +		private XmlElement CreateEquipmentElement(UnitEquipmentItem item, Unit unit, XmlDocument doc)
   4.155 +		{
   4.156 +			XmlElement equipmentItemElem = doc.CreateElement("equipItem");
   4.157 +			equipmentItemElem.SetAttribute("id", item.ID);
   4.158 +			equipmentItemElem.SetAttribute("amount", UnitEquipmentUtil.GetEquipmentAmount(unit, item).ToString());
   4.159 +			equipmentItemElem.SetAttribute("amountType", UnitEquipmentUtil.GetEquipmentAmountIsRatio(unit, item) ? "ratio" : "fixed");
   4.160 +			return equipmentItemElem;
   4.161 +		}
   4.162 +		
   4.163 +		private XmlElement CreateContainedUnitsElement(Unit unit, XmlDocument doc)
   4.164 +		{
   4.165 +			Unit[] containedUnits = unit.ContainedUnits;
   4.166 +			int containedCount = containedUnits.Length;
   4.167 +			XmlElement containedElem = null;
   4.168 +
   4.169 +			if (containedCount > 0)
   4.170 +			{
   4.171 +				containedElem = doc.CreateElement("contained");
   4.172 +				
   4.173 +				for (int i = 0; i < containedCount; i++)
   4.174 +				{
   4.175 +					containedElem.AppendChild(CreateContainedUnitElement(containedUnits[i], doc));
   4.176 +				}
   4.177 +			}
   4.178 +
   4.179 +			return containedElem;
   4.180 +		}
   4.181 +
   4.182 +		private XmlElement CreateContainedUnitElement(Unit unit,  XmlDocument doc)
   4.183 +		{
   4.184 +			XmlElement containedUnitElem = doc.CreateElement("containedUnit");
   4.185 +			containedUnitElem.SetAttribute("containedID", XmlTools.GetAsciiXmlIdForString(unit.ID));
   4.186 +			return containedUnitElem;
   4.187 +		}
   4.188 +	}
   4.189 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/api/Savers/Xml/WarFoundryXmlGameSystemSaver.cs	Sat Mar 05 11:52:09 2011 +0000
     5.3 @@ -0,0 +1,149 @@
     5.4 +// This file (WarFoundryXmlGameSystemSaver.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2008, 2009 IBBoard.
     5.5 +//
     5.6 +// 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.
     5.7 +
     5.8 +using System;
     5.9 +using System.Collections.Generic;
    5.10 +using System.IO;
    5.11 +using System.Xml;
    5.12 +using System.Xml.Schema;
    5.13 +using IBBoard.Lang;
    5.14 +using IBBoard.Xml;
    5.15 +using IBBoard.WarFoundry.API.Factories.Xml.Zip;
    5.16 +using IBBoard.WarFoundry.API.Objects;
    5.17 +using IBBoard.WarFoundry.API.Savers;
    5.18 +using IBBoard.WarFoundry.API.Util;
    5.19 +using ICSharpCode.SharpZipLib.Zip;
    5.20 +
    5.21 +namespace IBBoard.WarFoundry.API.Savers.Xml
    5.22 +{
    5.23 +	public class WarFoundryXmlGameSystemSaver : IWarFoundryGameSystemSaver
    5.24 +	{
    5.25 +		public const string GAMESYSTEM_FILE_EXTENSION = ".system";
    5.26 +
    5.27 +		public bool Save(GameSystem toSave, string savePath)
    5.28 +		{
    5.29 +			bool success = false;
    5.30 +			ZipFile file = null;
    5.31 +
    5.32 +			if (!savePath.EndsWith(GAMESYSTEM_FILE_EXTENSION))
    5.33 +			{
    5.34 +				savePath = savePath + GAMESYSTEM_FILE_EXTENSION;
    5.35 +			}
    5.36 +
    5.37 +			try
    5.38 +			{
    5.39 +				file = ZipFile.Create(savePath);
    5.40 +				file.BeginUpdate();
    5.41 +				file.Add(new StringZipEntrySource(CreateXmlString(toSave)), "data.systemx");
    5.42 +				file.CommitUpdate();
    5.43 +				success = true;
    5.44 +			}
    5.45 +			finally
    5.46 +			{
    5.47 +				if (file != null)
    5.48 +				{
    5.49 +					file.Close();
    5.50 +				}
    5.51 +			}
    5.52 +
    5.53 +			return success;
    5.54 +		}
    5.55 +
    5.56 +		private string CreateXmlString(WarFoundryObject toSave)
    5.57 +		{
    5.58 +			string xmlString = "";
    5.59 +
    5.60 +			if (toSave is GameSystem)
    5.61 +			{
    5.62 +				xmlString = CreateGameSystemXmlString((GameSystem)toSave);
    5.63 +			}
    5.64 +
    5.65 +			return xmlString;
    5.66 +		}
    5.67 +
    5.68 +		private string CreateGameSystemXmlString(GameSystem toSave)
    5.69 +		{
    5.70 +			XmlDocument doc = new XmlDocument();
    5.71 +			XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null);
    5.72 +			doc.AppendChild(declaration);
    5.73 +			XmlSchema schema = new XmlSchema();
    5.74 +			schema.Namespaces.Add("", "http://ibboard.co.uk/warfoundry/system");
    5.75 +			schema.Namespaces.Add("cats", "http://ibboard.co.uk/warfoundry/cats");
    5.76 +			doc.Schemas.Add(schema);
    5.77 +			XmlElement root = doc.CreateElement("system");
    5.78 +			root.SetAttribute("xmlns", "http://ibboard.co.uk/warfoundry/system");
    5.79 +			root.SetAttribute("xmlns:cats", "http://ibboard.co.uk/warfoundry/cats");
    5.80 +			doc.AppendChild(root);
    5.81 +			root.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(toSave.ID));
    5.82 +			root.SetAttribute("name", toSave.Name);
    5.83 +			root.SetAttribute("defaultArmySize", toSave.SystemArmyDefaultSize.ToString());
    5.84 +			root.SetAttribute("warn", toSave.WarnOnError.ToString().ToLowerInvariant());
    5.85 +			root.SetAttribute("allowAllies", toSave.AllowAllies.ToString().ToLowerInvariant());
    5.86 +			XmlElement cats = doc.CreateElement("categories");
    5.87 +			root.AppendChild(cats);
    5.88 +
    5.89 +			foreach (Category cat in toSave.Categories)
    5.90 +			{
    5.91 +				cats.AppendChild(CreateCategoryElement(cat, doc));
    5.92 +			}
    5.93 +
    5.94 +			XmlElement sysStatsList = doc.CreateElement("sysStatsList");
    5.95 +			sysStatsList.SetAttribute("defaultStats", XmlTools.GetAsciiXmlIdForString(toSave.StandardSystemStatsID));
    5.96 +			root.AppendChild(sysStatsList);
    5.97 +
    5.98 +			foreach(SystemStats stats in toSave.SystemStats)
    5.99 +			{
   5.100 +				sysStatsList.AppendChild(CreateSystemStatsElement(stats, doc));
   5.101 +			}
   5.102 +
   5.103 +			return doc.OuterXml;
   5.104 +		}
   5.105 +
   5.106 +		private XmlElement CreateCategoryElement(Category cat, XmlDocument doc)
   5.107 +		{
   5.108 +			XmlElement catElem = doc.CreateElement("cats:cat");
   5.109 +			catElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(cat.ID));
   5.110 +			catElem.SetAttribute("name", (cat.HasDefaultName() ? "" : cat.Name));
   5.111 +			if (cat.MinimumPoints > 0)
   5.112 +			{
   5.113 +				catElem.SetAttribute("minPoints", cat.MaximumPercentage.ToString());
   5.114 +			}
   5.115 +			if (cat.MaximumPoints < 100)
   5.116 +			{
   5.117 +				catElem.SetAttribute("maxPoints", cat.MaximumPercentage.ToString());
   5.118 +			}
   5.119 +			if(cat.MinimumPercentage > 0)
   5.120 +			{
   5.121 +				catElem.SetAttribute("minPercentage", cat.MaximumPercentage.ToString());
   5.122 +			}
   5.123 +			if(cat.MaximumPercentage < 100)
   5.124 +			{
   5.125 +				catElem.SetAttribute("maxPercentage", cat.MaximumPercentage.ToString());
   5.126 +			}
   5.127 +
   5.128 +			return catElem;
   5.129 +		}
   5.130 +
   5.131 +		private XmlElement CreateSystemStatsElement(SystemStats stats, XmlDocument doc)
   5.132 +		{
   5.133 +			XmlElement statsElem = doc.CreateElement("sysStats");
   5.134 +			statsElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(stats.ID));
   5.135 +
   5.136 +			foreach(StatSlot stat in stats.StatSlots)
   5.137 +			{
   5.138 +				statsElem.AppendChild(CreateSystemStatElement(stat, doc));
   5.139 +			}
   5.140 +
   5.141 +			return statsElem;
   5.142 +		}
   5.143 +
   5.144 +		private XmlElement CreateSystemStatElement(StatSlot stat, XmlDocument doc)
   5.145 +		{
   5.146 +			XmlElement statElem = doc.CreateElement("sysStat");
   5.147 +			statElem.SetAttribute("name", stat.Name);
   5.148 +
   5.149 +			return statElem;
   5.150 +		}
   5.151 +	}
   5.152 +}