view api/Factories/AbstractNativeWarFoundryFactory.cs @ 14:0770e5cbba7c

Closes #21 - File loading in order * Reworked LoadFiles to smaller methods for readability (also re #10) and structure * Now determine expected load return before loading then load all "expected GameSystem" before "expected Race" * Make "can load as race/game system/army" methods public in interface Re #22 - Get errored file loading * Created FileLoadFailure class and made LoadFiles return a list of them Also * Some code cleanup * Change to DictionaryUtils calls
author IBBoard <dev@ibboard.co.uk>
date Sun, 25 Jan 2009 14:03:20 +0000
parents 613bc5eaac59
children 306558904c2a
line wrap: on
line source

// WarFoundryXmlFactory.cs
//
//  Copyright (C) 2007 IBBoard
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License version 2.1 of the License as published by the Free
// Software Foundation.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
//
//

using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Collections.Generic;
using System.Text;
using IBBoard;
using IBBoard.IO;
using IBBoard.Lang;
using IBBoard.Logging;
using IBBoard.Xml;
using IBBoard.WarFoundry.API.Objects;
using ICSharpCode.SharpZipLib.Zip;

namespace IBBoard.WarFoundry.API.Factories
{
	/// <summary>
	/// Base abstract class for all factories that load native WarFoundry data.
	/// </summary>
	public abstract class AbstractNativeWarFoundryFactory : AbstractWarFoundryFactory<ZipFile>, INativeWarFoundryFactory
	{
		protected static readonly string SYSTEM_ZIP_IDENTIFIER = "WarFoundry_System";
		protected static readonly string RACE_ZIP_IDENTIFIER = "WarFoundry_Race";
		protected static readonly string ARMY_ZIP_IDENTIFIER = "WarFoundry_Army";
				
		protected AbstractNativeWarFoundryFactory()
		{
			//Do nothing - just make the constructor non-public
		}
				
		protected override ZipFile GetFileAsSupportedType (FileInfo file)
		{
			ZipFile zip = null;
			
			try
			{
				zip = new ZipFile(file.FullName);
			}
			catch(ZipException)
			{
				//Silently dispose as per spec for the method
			}
			
			return zip;
		}
		
		protected override bool CheckCanHandleFileFormat (ZipFile file)
		{	
			return CheckCanHandleFileAsGameSystem(file) || CheckCanHandleFileAsRace(file) || CheckCanHandleFileAsArmy(file); 
		}
		
		protected override bool CheckCanHandleFileAsGameSystem(ZipFile file)
		{
			return file.ZipFileComment.StartsWith(SYSTEM_ZIP_IDENTIFIER) && CheckCanFindSystemFileContent(file);
		}
		
		protected abstract bool CheckCanFindSystemFileContent(ZipFile file);
		
		protected override bool CheckCanHandleFileAsRace(ZipFile file)
		{
			return file.ZipFileComment.StartsWith(RACE_ZIP_IDENTIFIER) && CheckCanFindRaceFileContent(file);
		}
		
		protected abstract bool CheckCanFindRaceFileContent(ZipFile file);
		
		protected override bool CheckCanHandleFileAsArmy(ZipFile file)
		{
			return file.ZipFileComment.StartsWith(ARMY_ZIP_IDENTIFIER) && CheckCanFindArmyFileContent(file);
		}
		
		protected abstract bool CheckCanFindArmyFileContent(ZipFile file);
		
		protected override ICollection<IWarFoundryObject> DoCreateObjectsFromFile (ZipFile file)
		{
			ICollection<IWarFoundryObject> objects = null;
			string comment = file.ZipFileComment;
			IWarFoundryObject obj = null;
				
			if (comment.StartsWith(SYSTEM_ZIP_IDENTIFIER))
			{
				obj = CreateGameSystemFromFile(file);
			}
			else if (comment.StartsWith(RACE_ZIP_IDENTIFIER))
			{
				obj = CreateRaceFromFile(file);
			}
			else if (comment.StartsWith(ARMY_ZIP_IDENTIFIER))
			{
				obj = CreateArmyFromFile(file);
			}
			
			if (obj!=null)
			{
				objects = new List<IWarFoundryObject>();
				objects.Add(obj);
			}
			
			return objects;
		}
		
		protected Army CreateArmyFromFile(ZipFile file)
		{
			return CreateArmyFromStream(file, GetArmyDataStream(file));
		}
		
		protected abstract Stream GetArmyDataStream(ZipFile file);
		protected abstract Army CreateArmyFromStream(ZipFile file, Stream dataStream);
		
		protected Race CreateRaceFromFile(ZipFile file)
		{
			try
			{
				return CreateRaceFromStream(file, GetRaceDataStream(file));
			}
			catch (InvalidFileException ex)
			{
				throw new InvalidFileException("Data file "+file.Name+" was not a valid race file", ex);
			}
		}
		
		protected abstract Stream GetRaceDataStream(ZipFile file);
		protected abstract Race CreateRaceFromStream(ZipFile file, Stream dataStream);
		
		protected GameSystem CreateGameSystemFromFile(ZipFile file)
		{
			return CreateGameSystemFromStream(file, GetGameSystemDataStream(file));
		}
		
		protected abstract Stream GetGameSystemDataStream(ZipFile file);
		protected abstract GameSystem CreateGameSystemFromStream(ZipFile file, Stream dataStream);
		
		public override bool Equals (object o)
		{
			if (o == this)
			{
				return true;
			}
			else if (o == null || !(this.GetType().Equals(o.GetType())))
			{
				return false;
			}
			
			return true;
		}
		
		public override int GetHashCode ()
		{
			return GetType().FullName.GetHashCode();
		}
	}
}