view api/Savers/Xml/WarFoundryXmlFileSaver.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

using System;
using IBBoard.WarFoundry.API.Savers;
using IBBoard.WarFoundry.API.Objects;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using IBBoard.IO;
using IBBoard.Lang;

namespace IBBoard.WarFoundry.API.Savers.Xml
{
	public class WarFoundryXmlFileSaver : IWarFoundryFileSaver
	{
		public WarFoundryXmlFileSaver()
		{
		}

		public bool Save(string path, params WarFoundryLoadedObject[] objects)
		{
			ZipOutputStream zipStream = new ZipOutputStream(new FileStream(path, FileMode.Create));
			AddFiles(zipStream, objects);
			zipStream.Close();
			return true;
		}

		public void AddFiles(ZipOutputStream zipStream, WarFoundryLoadedObject[] objects)
		{
			foreach (WarFoundryLoadedObject obj in objects)
			{
				AddFile(zipStream, obj);
			}
		}

		public void AddFile(ZipOutputStream zipStream, WarFoundryLoadedObject obj)
		{
			ZipEntry entry = new ZipEntry(GetZipEntryName(obj));
			zipStream.PutNextEntry(entry);
			byte[] bytes = GetObjectBytes(obj);
			zipStream.Write(bytes, 0, bytes.Length);
		}

		public byte[] GetObjectBytes(WarFoundryLoadedObject obj)
		{
			string xmlString = "";
			
			if (obj is GameSystem)
			{
				xmlString = GetGameSystemString((GameSystem)obj);
			}
			else if (obj is Army)
			{
				xmlString = GetArmyString((Army)obj);
			}
			
			return StringManipulation.StringToBytes(xmlString);
		}		

		public string GetGameSystemString(GameSystem obj)
		{
			return new WarFoundryXmlGameSystemSaver().CreateXmlString(obj);
		}
		
		public string GetArmyString(Army obj)
		{
			return new WarFoundryXmlArmySaver().CreateXmlString(obj);
		}

		private string GetZipEntryName(WarFoundryLoadedObject obj)
		{
			return obj.ID + GetFileExtension(obj);
		}

		public string GetFileExtension(WarFoundryLoadedObject obj)
		{
			string ext = "";
			
			if (obj is Army)
			{
				ext = ".armyx";
			}
			else if (obj is Race)
			{
				ext = ".racex";
			}
			else if (obj is GameSystem)
			{
				ext = ".systemx";
			}
			
			return ext;
		}
	}
}