changeset 379:04f4c2fea356

Re #351: Add extensible requirement handling method * Extract common IRequirementFactory interface * Drop back to just returning IRequirement from factory to simplify generics * Add initial registration of requirement factories
author IBBoard <dev@ibboard.co.uk>
date Sat, 23 Jul 2011 19:53:42 +0000
parents ff7f1b319b4e
children 7036bf04e955
files API/Factories/Requirement/IRequirementFactory.cs API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs API/Factories/Xml/WarFoundryXmlRaceFactory.cs API/WarFoundryLoader.cs IBBoard.WarFoundry.API.csproj
diffstat 5 files changed, 43 insertions(+), 7 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/API/Factories/Requirement/IRequirementFactory.cs	Sat Jul 23 19:53:42 2011 +0000
     1.3 @@ -0,0 +1,16 @@
     1.4 +// This file (IRequirementFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 IBBoard
     1.5 +// 
     1.6 +// 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.
     1.7 +using System;
     1.8 +using IBBoard.WarFoundry.API.Objects;
     1.9 +using IBBoard.WarFoundry.API.Objects.Requirement;
    1.10 +
    1.11 +namespace IBBoard.WarFoundry.API.Factories.Requirement
    1.12 +{
    1.13 +	public interface IRequirementFactory
    1.14 +	{
    1.15 +		IRequirement CreateRequirement<SOURCE_FILE_TYPE, ENTRY_TYPE>(UnitType type, string data, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory);
    1.16 +		string AppliesToID { get; }
    1.17 +	}
    1.18 +}
    1.19 +
     2.1 --- a/API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs	Sat Jul 16 19:59:04 2011 +0000
     2.2 +++ b/API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs	Sat Jul 23 19:53:42 2011 +0000
     2.3 @@ -7,14 +7,20 @@
     2.4  
     2.5  namespace IBBoard.WarFoundry.API.Factories.Requirement
     2.6  {
     2.7 -	public class UnitRequiresAtLeastNUnitsRequirementFactory
     2.8 +	public class UnitRequiresAtLeastNUnitsRequirementFactory : IRequirementFactory
     2.9  	{
    2.10  		public UnitRequiresAtLeastNUnitsRequirementFactory()
    2.11  		{
    2.12  			//Do nothing special
    2.13  		}
    2.14  
    2.15 -		public UnitRequiresAtLeastNUnitsRequirement CreateRequirement<SOURCE_FILE_TYPE, ENTRY_TYPE>(UnitType type, string data, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory)
    2.16 +		public string AppliesToID {
    2.17 +			get {
    2.18 +				return "RequiresAtLeastNUnits";
    2.19 +			}
    2.20 +		}
    2.21 +
    2.22 +		public IRequirement CreateRequirement<SOURCE_FILE_TYPE, ENTRY_TYPE>(UnitType type, string data, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory)
    2.23  		{
    2.24  			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(type);
    2.25  			Race race = type.Race;
     3.1 --- a/API/Factories/Xml/WarFoundryXmlRaceFactory.cs	Sat Jul 16 19:59:04 2011 +0000
     3.2 +++ b/API/Factories/Xml/WarFoundryXmlRaceFactory.cs	Sat Jul 23 19:53:42 2011 +0000
     3.3 @@ -401,7 +401,7 @@
     3.4  
     3.5  				if (reqFactory != null) {
     3.6  					string data = WarFoundryXmlFactoryUtils.SelectSingleElement(extraData, "race:data").InnerText;
     3.7 -					UnitRequiresAtLeastNUnitsRequirement req = reqFactory.CreateRequirement(type, data, this);
     3.8 +					IRequirement req = reqFactory.CreateRequirement(type, data, this);
     3.9  					type.AddRequirement(req);
    3.10  				}
    3.11  			}
     4.1 --- a/API/WarFoundryLoader.cs	Sat Jul 16 19:59:04 2011 +0000
     4.2 +++ b/API/WarFoundryLoader.cs	Sat Jul 23 19:53:42 2011 +0000
     4.3 @@ -1,16 +1,19 @@
     4.4  // This file (WarFoundryLoader.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2007, 2008, 2009 IBBoard.
     4.5  //
     4.6  // 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.
     4.7 -
     4.8  using System;
     4.9  using ICSharpCode.SharpZipLib.Zip;
    4.10 +using System.Collections.Generic;
    4.11 +using IBBoard.WarFoundry.API.Factories.Requirement;
    4.12 +using IBBoard.WarFoundry.API.Objects.Requirement;
    4.13  
    4.14  namespace IBBoard.WarFoundry.API
    4.15  {
    4.16  	public class WarFoundryLoader
    4.17  	{		
    4.18  		private static AbstractWarFoundryLoader loader;
    4.19 -		
    4.20 +		private static Dictionary<string, IRequirementFactory> requirementFactories = new Dictionary<string, IRequirementFactory>();
    4.21 +
    4.22  		/// <summary>
    4.23  		/// Gets the default <see cref="WarFoundryLoader"/> used to load WarFoundry data files.
    4.24  		/// </summary>
    4.25 @@ -29,12 +32,22 @@
    4.26  		
    4.27  		public static void SetDefault(AbstractWarFoundryLoader newLoader)
    4.28  		{
    4.29 -			loader = newLoader;	
    4.30 +			loader = newLoader;
    4.31  		}
    4.32 -		
    4.33 +
    4.34  		private WarFoundryLoader()
    4.35  		{
    4.36  			//Hide constructor
    4.37  		}
    4.38 +
    4.39 +		public static void RegisterRequirementFactory(IRequirementFactory factory)
    4.40 +		{
    4.41 +			requirementFactories[factory.AppliesToID] = factory;
    4.42 +		}
    4.43 +
    4.44 +		public static IRequirementFactory GetRequirementFactory(string requirementID)
    4.45 +		{
    4.46 +			return DictionaryUtils.GetValue(requirementFactories, requirementID);
    4.47 +		}
    4.48  	}
    4.49  }
     5.1 --- a/IBBoard.WarFoundry.API.csproj	Sat Jul 16 19:59:04 2011 +0000
     5.2 +++ b/IBBoard.WarFoundry.API.csproj	Sat Jul 23 19:53:42 2011 +0000
     5.3 @@ -182,6 +182,7 @@
     5.4      <Compile Include="API\Factories\Requirement\InvalidRequirementException.cs" />
     5.5      <Compile Include="API\Factories\Xml\CategoryLoader.cs" />
     5.6      <Compile Include="API\Factories\IRaceFactory.cs" />
     5.7 +    <Compile Include="API\Factories\Requirement\IRequirementFactory.cs" />
     5.8    </ItemGroup>
     5.9    <ItemGroup>
    5.10      <Reference Include="System.Xml" />