view API/Savers/Xml/WarFoundryXmlGameSystemSaver.cs @ 337:3c4a6403a88c

* Fix capitalisation so that new files are in the namespace no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 03 Apr 2011 18:50:32 +0000
parents
children 5b3e3fb11de9
line wrap: on
line source

// 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.Savers.Xml
{
	public class WarFoundryXmlGameSystemSaver
	{
		public string CreateXmlString(GameSystem 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/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;
		}
	}
}