view API/Factories/AbstractNativeWarFoundryFactory.cs @ 463:cbeee87dc2d3

Re #58: Remove LogNotifier from API * Remove LogNotifier from API - mostly unnecessary logging Also: * Formatting auto-corrected * LoadFile() "try...catch {silently dispose}" removed. Code shouldn't be throwing those errors and needs to be handled elsewhere if it does
author IBBoard <dev@ibboard.co.uk>
date Sat, 17 Mar 2012 20:02:32 +0000
parents 86725e88052e
children 1ed2f3ab5e35
line wrap: on
line source

// This file (AbstractNativeWarFoundryFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2007, 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.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.Xml;
using IBBoard.WarFoundry.API.Objects;
using ICSharpCode.SharpZipLib.Zip;
using IBBoard.Collections;

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 AbstractNativeWarFoundryFactory()
		{
			//Do nothing - just make the constructor non-public
		}
				
		protected override ZipFile GetFileAsSupportedType(FileInfo file)
		{
			ZipFile zip = null;
			string ext = file.Extension.ToLower();

			if (ext == ".race" || ext == ".army" || ext == ".system")
			{
				try
				{
					zip = new ZipFile(file.FullName);
				}
				catch (ZipException)
				{
					//Silently dispose as per spec for the method
				}
				catch (IOException)
				{
					//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 CheckCanFindSystemFileContent(file);
		}
		
		protected abstract bool CheckCanFindSystemFileContent(ZipFile file);
		
		protected override bool CheckCanHandleFileAsRace(ZipFile file)
		{
			return CheckCanFindRaceFileContent(file);
		}
		
		protected abstract bool CheckCanFindRaceFileContent(ZipFile file);
		
		protected override bool CheckCanHandleFileAsArmy(ZipFile file)
		{
			return CheckCanFindArmyFileContent(file);
		}
		
		protected abstract bool CheckCanFindArmyFileContent(ZipFile file);
		
		protected override ICollection<IWarFoundryObject> DoCreateObjectsFromFile(ZipFile file)
		{
			ICollection<IWarFoundryObject> objects = new List<IWarFoundryObject>();

			if (file != null)
			{
				DoCreateObjectsFromZip(file, objects);
			}

			return objects;
		}

		private void DoCreateObjectsFromZip(ZipFile file, ICollection<IWarFoundryObject> objects)
		{
			try
			{
				CreateSystemObjectsFromZip(file, objects);
				CreateRaceObjectsFromZip(file, objects);
				CreateArmyObjectsFromZip(file, objects);
			}
			finally
			{
				file.Close();
			}
		}

		private void CreateSystemObjectsFromZip(ZipFile file, ICollection<IWarFoundryObject> objects)
		{
			if (CheckCanFindSystemFileContent(file))
			{
				foreach (GameSystem system in CreateGameSystemsFromFile(file))
				{
					OnGameSystemLoaded(system);
					objects.Add(system);
				}
			}
		}

		void CreateRaceObjectsFromZip(ZipFile file, ICollection<IWarFoundryObject> objects)
		{
			if (CheckCanFindRaceFileContent(file))
			{
				foreach (Race race in CreateRacesFromFile(file))
				{
					OnRaceLoaded(race);
					objects.Add(race);
				}
			}
		}

		void CreateArmyObjectsFromZip(ZipFile file, ICollection<IWarFoundryObject> objects)
		{
			if (CheckCanFindArmyFileContent(file))
			{
				foreach (Army army in CreateArmiesFromFile(file))
				{
					OnArmyLoaded(army);
					objects.Add(army);
				}
			}
		}
		
		protected ICollection<Army> CreateArmiesFromFile(ZipFile file)
		{
			ICollection<ZipEntry> dataStreams = GetArmyZipEntries(file);
			ICollection<Army> armies = new List<Army>();

			foreach (ZipEntry entry in dataStreams)
			{
				if (IsDotFile(entry))
				{
					continue;
				}

				using (Stream dataStream = file.GetInputStream(entry))
				{
					armies.Add(CreateArmyFromStream(file, dataStream));
				}
			}
			
			return armies;
		}

		private bool IsDotFile(ZipEntry entry)
		{
			string name = entry.Name;
			int slashIdx = name.LastIndexOf('/');
			return (slashIdx != -1 && slashIdx == name.LastIndexOf("/.")) || (name.Length > 0 && name[0] == '.');
		}
		
		protected abstract ICollection<ZipEntry> GetArmyZipEntries(ZipFile file);

		protected abstract Army CreateArmyFromStream(ZipFile file, Stream dataStream);
		
		protected ICollection<Race> CreateRacesFromFile(ZipFile file)
		{
			ICollection<ZipEntry> dataStreams = GetRaceZipEntries(file);
			ICollection<Race> races = new List<Race>();

			foreach (ZipEntry entry in dataStreams)
			{
				if (IsDotFile(entry))
				{
					continue;
				}

				using (Stream dataStream = file.GetInputStream(entry))
				{
					races.Add(CreateRaceFromStream(file, dataStream));
				}
			}
			
			return races;
		}
		
		protected abstract ICollection<ZipEntry> GetRaceZipEntries(ZipFile file);

		protected abstract Race CreateRaceFromStream(ZipFile file, Stream dataStream);
		
		protected ICollection<GameSystem> CreateGameSystemsFromFile(ZipFile file)
		{
			ICollection<ZipEntry> dataStreams = GetGameSystemZipEntries(file);
			ICollection<GameSystem> systems = new List<GameSystem>();

			foreach (ZipEntry entry in dataStreams)
			{
				if (IsDotFile(entry))
				{
					continue;
				}

				using (Stream dataStream = file.GetInputStream(entry))
				{
					systems.Add(CreateGameSystemFromStream(file, dataStream));
				}
			}
			
			return systems;
		}
		
		protected abstract ICollection<ZipEntry> GetGameSystemZipEntries(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();
		}
	}
}