diff api/Factories/AbstractNativeWarFoundryFactory.cs @ 0:520818033bb6

Initial commit of WarFoundry code
author IBBoard <dev@ibboard.co.uk>
date Fri, 19 Dec 2008 15:57:51 +0000
parents
children 895c8a2378a1
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/api/Factories/AbstractNativeWarFoundryFactory.cs	Fri Dec 19 15:57:51 2008 +0000
@@ -0,0 +1,282 @@
+// 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
+		}
+		
+		public abstract void CompleteLoading(IWarFoundryStagedLoadObject obj);
+		
+		protected override ZipFile GetFileAsSupportedType (FileInfo file)
+		{
+			ZipFile zip = null;
+			
+			try
+			{
+				zip = new ZipFile(file.FullName);
+			}
+			catch(ZipException ex)
+			{
+				//Silently dispose - we don't support the file and our other methods should handle that
+			}
+			
+			return zip;
+		}
+		
+		protected override bool CheckCanHandleFileFormat (ZipFile file)
+		{
+			bool canHandle = false;
+			
+			if (file!=null)
+			{			
+				canHandle = CheckCanHandleAsSystem(file) || CheckCanHandleAsRace(file) || CheckCanHandleAsArmy(file); 
+			}
+			
+			return canHandle;
+		}
+		
+		protected bool CheckCanHandleAsSystem(ZipFile file)
+		{
+			return file.ZipFileComment.StartsWith(SYSTEM_ZIP_IDENTIFIER) && CheckCanFindSystemFileContent(file);
+		}
+		
+		protected abstract bool CheckCanFindSystemFileContent(ZipFile file);
+		
+		protected bool CheckCanHandleAsRace(ZipFile file)
+		{
+			return file.ZipFileComment.StartsWith(RACE_ZIP_IDENTIFIER) && CheckCanFindRaceFileContent(file);
+		}
+		
+		protected abstract bool CheckCanFindRaceFileContent(ZipFile file);
+		
+		protected bool CheckCanHandleAsArmy(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;
+		}
+
+		
+/*	
+		public override bool CanHandleArmyFileFormat(FileInfo file)
+		{
+			bool canHandle = false;
+			
+			try
+			{
+				ZipFile zip = new ZipFile(file.FullName);
+				canHandle = zip.ZipFileComment.StartsWith(ARMY_ZIP_IDENTIFIER) && CanHandleArmyFileFormat(zip);
+			}
+			catch (ZipException)
+			{
+				//Not a valid zip file so we can't handle it
+			}
+			
+			return canHandle;
+		}
+		
+		public override bool CanHandleRaceFileFormat(FileInfo file)
+		{
+			bool canHandle = false;
+			
+			try
+			{
+				ZipFile zip = new ZipFile(file.FullName);
+				canHandle = zip.ZipFileComment.StartsWith(RACE_ZIP_IDENTIFIER) && CanHandleRaceFileFormat(zip);
+			}
+			catch (ZipException)
+			{
+				//Not a valid zip file so we can't handle it
+			}
+			
+			return canHandle;
+		}
+		
+		public override bool CanHandleSystemFileFormat(FileInfo file)
+		{
+			bool canHandle = false;
+			
+			try
+			{
+				ZipFile zip = new ZipFile(file.FullName);
+				canHandle = zip.ZipFileComment.StartsWith(SYSTEM_ZIP_IDENTIFIER) && CanHandleSystemFileFormat(zip);
+			}
+			catch (ZipException)
+			{
+				//Not a valid zip file so we can't handle it
+			}
+			
+			return canHandle;
+		}
+		
+		public abstract bool CanHandleArmyFileFormat(ZipFile file);
+		public abstract bool CanHandleRaceFileFormat(ZipFile file);
+		public abstract bool CanHandleSystemFileFormat(ZipFile file);
+		
+		public override Army CreateArmyFromFile (FileInfo file)
+		{
+			try
+			{
+				ZipFile zip = new ZipFile(file.FullName);
+				return CreateArmyFromFile(zip);
+			}
+			catch(ZipException ex)
+			{
+				throw new InvalidFileException(Translation.GetTranslation("InvalidArmyFileException", "Cannot get Army for file {0} as it was not a recognised Army file", file.Name), ex);
+			}
+		}
+				
+		public override Race CreateRaceFromFile (FileInfo file)
+		{
+			try
+			{
+				ZipFile zip = new ZipFile(file.FullName);
+				return CreateRaceFromFile(zip);
+			}
+			catch(ZipException ex)
+			{
+				throw new InvalidFileException(Translation.GetTranslation("InvalidRaceFileException", "Cannot get Race for file {0} as it was not a recognised Race file", file.Name), ex);
+			}
+		}
+				
+		public override GameSystem CreateGameSystemFromFile (FileInfo file)
+		{
+			try
+			{
+				ZipFile zip = new ZipFile(file.FullName);
+				return CreateGameSystemFromFile(zip);
+			}
+			catch(ZipException ex)
+			{
+				throw new InvalidFileException(Translation.GetTranslation("InvalidGameSystemFileException", "Cannot get Game System for file {0} as it was not a recognised Race file", file.Name), ex);
+			}
+		}*/
+		
+		protected Army CreateArmyFromFile(ZipFile file)
+		{
+			Army army = CreateArmyFromStream(file, GetArmyDataStream(file));
+			//throw new InvalidFileException(Translation.GetTranslation("InvalidArmyFileException", "Cannot get Army for file {0} as it was not an Army file", file.Name));
+			return army;
+		}
+		
+		protected abstract Stream GetArmyDataStream(ZipFile file);
+		protected abstract Army CreateArmyFromStream(ZipFile file, Stream dataStream);
+		
+		protected Race CreateRaceFromFile(ZipFile file)
+		{
+			try
+			{
+				Race race = CreateRaceFromStream(file, GetRaceDataStream(file));			
+				//throw new InvalidFileException(Translation.GetTranslation("InvalidRaceFileException", "Cannot get Race for file {0} as it was not a Race file", file.Name));
+				return race;
+			}
+			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)
+		{
+			GameSystem system = CreateGameSystemFromStream(file, GetGameSystemDataStream(file));		
+			//throw new InvalidFileException(Translation.GetTranslation("InvalidGameSystemFileException", "Cannot get Game System for file {0} as it was not a Game System file", file.Name));
+			return system;
+		}
+		
+		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();
+		}
+	}
+}