changeset 297:349e521785c1

Re #338: WarFoundry.API - Save System Data Created IWarFoundryGameSystemSaver Created WarFoundryXmlGameSystemSaver Updated WarFoundrySaver Created GetGameSystemSaver Created SetGameSystemSaver
author Tsudico
date Fri, 07 Jan 2011 03:35:50 +0000
parents ed5e6d92ecd9
children 483b491d18f4
files IBBoard.WarFoundry.API.csproj api/Factories/Xml/WarFoundryXmlGameSystemSaver.cs api/Savers/IWarFoundryGameSystemSaver.cs api/Savers/WarFoundrySaver.cs
diffstat 4 files changed, 192 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/IBBoard.WarFoundry.API.csproj	Sat Dec 18 22:34:32 2010 +0000
+++ b/IBBoard.WarFoundry.API.csproj	Fri Jan 07 03:35:50 2011 +0000
@@ -1,9 +1,9 @@
-<?xml version="1.0" encoding="utf-8"?>
-<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
-    <ProductVersion>9.0.30729</ProductVersion>
+    <ProductVersion>9.0.21022</ProductVersion>
     <SchemaVersion>2.0</SchemaVersion>
     <ProjectGuid>{951E6C7A-7FBA-4F68-9D9E-F48618BB9626}</ProjectGuid>
     <OutputType>Library</OutputType>
@@ -12,11 +12,9 @@
     <AssemblyName>IBBoard.WarFoundry.API</AssemblyName>
     <FileUpgradeFlags>
     </FileUpgradeFlags>
-    <OldToolsVersion>2.0</OldToolsVersion>
+    <OldToolsVersion>3.5</OldToolsVersion>
     <UpgradeBackupLocation>
     </UpgradeBackupLocation>
-    <TargetFrameworkSubset>
-    </TargetFrameworkSubset>
     <PublishUrl>publish\</PublishUrl>
     <Install>true</Install>
     <InstallFrom>Disk</InstallFrom>
@@ -61,6 +59,7 @@
   -->
   <ItemGroup>
     <None Include="COPYING" />
+    <Compile Include="api\Factories\Xml\WarFoundryXmlGameSystemSaver.cs" />
     <Compile Include="api\Objects\ICostedWarFoundryObject.cs" />
     <Compile Include="api\Commands\CreateAndAddUnitCommand.cs" />
     <Compile Include="api\Commands\RemoveUnitCommand.cs" />
@@ -116,6 +115,7 @@
     <Compile Include="api\Requirements\UnitRequirementMinNumber.cs" />
     <Compile Include="api\Requirements\UnitRequiresAtLeastRequirement.cs" />
     <Compile Include="api\Savers\IWarFoundryFileSaver.cs" />
+    <Compile Include="api\Savers\IWarFoundryGameSystemSaver.cs" />
     <Compile Include="api\Savers\WarFoundrySaver.cs" />
     <Compile Include="api\Util\UnitEquipmentUtil.cs" />
     <Compile Include="api\WarFoundryCore.cs" />
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/api/Factories/Xml/WarFoundryXmlGameSystemSaver.cs	Fri Jan 07 03:35:50 2011 +0000
@@ -0,0 +1,149 @@
+// This file (WarFoundryXmlGameSystemSaver.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.Factories.Xml
+{
+	public class WarFoundryXmlGameSystemSaver : IWarFoundryGameSystemSaver
+	{
+		public const string GAMESYSTEM_FILE_EXTENSION = ".system";
+
+		public bool Save(GameSystem toSave, string savePath)
+		{
+			bool success = false;
+			ZipFile file = null;
+
+			if (!savePath.EndsWith(GAMESYSTEM_FILE_EXTENSION))
+			{
+				savePath = savePath + GAMESYSTEM_FILE_EXTENSION;
+			}
+
+			try
+			{
+				file = ZipFile.Create(savePath);
+				file.BeginUpdate();
+				file.Add(new StringZipEntrySource(CreateXmlString(toSave)), "data.systemx");
+				file.CommitUpdate();
+				success = true;
+			}
+			finally
+			{
+				if (file != null)
+				{
+					file.Close();
+				}
+			}
+
+			return success;
+		}
+
+		private string CreateXmlString(WarFoundryObject toSave)
+		{
+			string xmlString = "";
+
+			if (toSave is GameSystem)
+			{
+				xmlString = CreateGameSystemXmlString((GameSystem)toSave);
+			}
+
+			return xmlString;
+		}
+
+		private string CreateGameSystemXmlString(GameSystem toSave)
+		{
+			XmlDocument doc = new XmlDocument();
+			XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null);
+			doc.AppendChild(declaration);
+			XmlSchema schema = new XmlSchema();
+			schema.Namespaces.Add("", "http://ibboard.co.uk/warfoundry/system");
+			schema.Namespaces.Add("cats", "http://ibboard.co.uk/warfoundry/cats");
+			doc.Schemas.Add(schema);
+			XmlElement root = doc.CreateElement("system");
+			root.SetAttribute("xmlns", "http://ibboard.co.uk/warfoundry/system");
+			root.SetAttribute("xmlns:cats", "http://ibboard.co.uk/warfoundry/cats");
+			doc.AppendChild(root);
+			root.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(toSave.ID));
+			root.SetAttribute("name", toSave.Name);
+			root.SetAttribute("defaultArmySize", toSave.SystemArmyDefaultSize.ToString());
+			root.SetAttribute("warn", toSave.WarnOnError.ToString().ToLowerInvariant());
+			root.SetAttribute("allowAllies", toSave.AllowAllies.ToString().ToLowerInvariant());
+			XmlElement cats = doc.CreateElement("categories");
+			root.AppendChild(cats);
+
+			foreach (Category cat in toSave.Categories)
+			{
+				cats.AppendChild(CreateCategoryElement(cat, doc));
+			}
+
+			XmlElement sysStatsList = doc.CreateElement("sysStatsList");
+			sysStatsList.SetAttribute("defaultStats", XmlTools.GetAsciiXmlIdForString(toSave.StandardSystemStatsID));
+			root.AppendChild(sysStatsList);
+
+			foreach(SystemStats stats in toSave.SystemStats)
+			{
+				sysStatsList.AppendChild(CreateSystemStatsElement(stats, doc));
+			}
+
+			return doc.OuterXml;
+		}
+
+		private XmlElement CreateCategoryElement(Category cat, XmlDocument doc)
+		{
+			XmlElement catElem = doc.CreateElement("cats:cat");
+			catElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(cat.ID));
+			catElem.SetAttribute("name", (cat.HasDefaultName() ? "" : cat.Name));
+			if (cat.MinimumPoints > 0)
+			{
+				catElem.SetAttribute("minPoints", cat.MaximumPercentage.ToString());
+			}
+			if (cat.MaximumPoints < 100)
+			{
+				catElem.SetAttribute("maxPoints", cat.MaximumPercentage.ToString());
+			}
+			if(cat.MinimumPercentage > 0)
+			{
+				catElem.SetAttribute("minPercentage", cat.MaximumPercentage.ToString());
+			}
+			if(cat.MaximumPercentage < 100)
+			{
+				catElem.SetAttribute("maxPercentage", cat.MaximumPercentage.ToString());
+			}
+
+			return catElem;
+		}
+
+		private XmlElement CreateSystemStatsElement(SystemStats stats, XmlDocument doc)
+		{
+			XmlElement statsElem = doc.CreateElement("sysStats");
+			statsElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(stats.ID));
+
+			foreach(StatSlot stat in stats.StatSlots)
+			{
+				statsElem.AppendChild(CreateSystemStatElement(stat, doc));
+			}
+
+			return statsElem;
+		}
+
+		private XmlElement CreateSystemStatElement(StatSlot stat, XmlDocument doc)
+		{
+			XmlElement statElem = doc.CreateElement("sysStat");
+			statElem.SetAttribute("name", stat.Name);
+
+			return statElem;
+		}
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/api/Savers/IWarFoundryGameSystemSaver.cs	Fri Jan 07 03:35:50 2011 +0000
@@ -0,0 +1,26 @@
+// This file (IWarFoundrySystemSaver.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 IBBoard.WarFoundry.API.Objects;
+
+namespace IBBoard.WarFoundry.API.Savers
+{
+	public interface IWarFoundryGameSystemSaver
+	{
+		/// <summary>
+		/// Saves a <see cref="GameSystem"/> to a file on disk.
+		/// </summary>
+		/// <param name="system">
+		/// The <see cref="GameSystem"/> to save
+		/// </param>
+		/// <param name="path">
+		/// The path to save the system to
+		/// </param>
+		/// <returns>
+		/// TRUE if saving succedes, else FALSE
+		/// </returns>
+		bool Save(GameSystem system, string path);
+	}
+}
\ No newline at end of file
--- a/api/Savers/WarFoundrySaver.cs	Sat Dec 18 22:34:32 2010 +0000
+++ b/api/Savers/WarFoundrySaver.cs	Fri Jan 07 03:35:50 2011 +0000
@@ -9,6 +9,7 @@
 	public class WarFoundrySaver
 	{
 		private static IWarFoundryFileSaver fileSaver;
+		private static IWarFoundryGameSystemSaver systemSaver;
 		
 		public static IWarFoundryFileSaver GetSaver()
 		{
@@ -19,5 +20,15 @@
 		{
 			fileSaver = newFileSaver;
 		}
+
+		public static IWarFoundryGameSystemSaver GetGameSystemSaver()
+		{
+			return systemSaver;
+		}
+
+		public static void SetGameSystemSaver(IWarFoundryGameSystemSaver newGameSystemSaver)
+		{
+			systemSaver = newGameSystemSaver;
+		}
 	}
 }