changeset 377:c3b0b28bad48

Re #353: Handle unloaded units being referenced outside factory * Create interface for race factories * Start to pull out common methods and add "try unit and fall back to factory" method for getting unit types * Make XML Race Factory implement interface * Use new factory
author IBBoard <dev@ibboard.co.uk>
date Mon, 04 Jul 2011 20:02:26 +0000
parents e50682387d63
children ff7f1b319b4e
files API/Factories/IRaceFactory.cs API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs API/Factories/Xml/WarFoundryXmlFactory.cs API/Factories/Xml/WarFoundryXmlRaceFactory.cs IBBoard.WarFoundry.API.csproj
diffstat 5 files changed, 52 insertions(+), 8 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/API/Factories/IRaceFactory.cs	Mon Jul 04 20:02:26 2011 +0000
     1.3 @@ -0,0 +1,20 @@
     1.4 +// This file (IRaceFactory.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 +
    1.10 +namespace IBBoard.WarFoundry.API.Factories
    1.11 +{
    1.12 +	/// <summary>
    1.13 +	/// Interface for factory that creates races. SOURCE_FILE_TYPE is the class of the source 'file' or object. ENTRY_TYPE is the class of the object that data is loaded from.
    1.14 +	/// </summary>
    1.15 +	public interface IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE>
    1.16 +	{
    1.17 +		Race CreateRace(ENTRY_TYPE entry);
    1.18 +		void CompleteLoading(Race race);
    1.19 +		UnitType GetUnitType(string id, Race parentRace);
    1.20 +		UnitType GetUnitType(string id, Race parentRace, SOURCE_FILE_TYPE src);
    1.21 +	}
    1.22 +}
    1.23 +
     2.1 --- a/API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs	Sat Jul 02 19:58:28 2011 +0000
     2.2 +++ b/API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs	Mon Jul 04 20:02:26 2011 +0000
     2.3 @@ -14,21 +14,21 @@
     2.4  			//Do nothing special
     2.5  		}
     2.6  
     2.7 -		public UnitRequiresAtLeastNUnitsRequirement CreateRequirement(UnitType type, string data)
     2.8 +		public UnitRequiresAtLeastNUnitsRequirement CreateRequirement<SOURCE_FILE_TYPE, ENTRY_TYPE>(UnitType type, string data, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory)
     2.9  		{
    2.10  			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(type);
    2.11  			Race race = type.Race;
    2.12 -			AddRequirements(req, race, data);
    2.13 +			AddRequirements(req, race, data, raceFactory);
    2.14  			return req;
    2.15  		}
    2.16  
    2.17 -		private void AddRequirements(UnitRequiresAtLeastNUnitsRequirement req, Race race, string data)
    2.18 +		private void AddRequirements<SOURCE_FILE_TYPE, ENTRY_TYPE>(UnitRequiresAtLeastNUnitsRequirement req, Race race, string data, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory)
    2.19  		{
    2.20  			foreach (string requirement in data.Split('|'))
    2.21  			{
    2.22  				string[] requirementParts = requirement.Split(':');
    2.23  				string unitID = requirementParts[0];
    2.24 -				UnitType unitType = race.GetUnitType(unitID);
    2.25 +				UnitType unitType = raceFactory.GetUnitType(unitID, race);
    2.26  
    2.27  				if (unitType == null)
    2.28  				{
     3.1 --- a/API/Factories/Xml/WarFoundryXmlFactory.cs	Sat Jul 02 19:58:28 2011 +0000
     3.2 +++ b/API/Factories/Xml/WarFoundryXmlFactory.cs	Mon Jul 04 20:02:26 2011 +0000
     3.3 @@ -139,7 +139,7 @@
     3.4  		{
     3.5  			XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.RACE_ELEMENT);
     3.6  			LogNotifier.Debug(GetType(), "Create Race");
     3.7 -			return raceFactory.CreateRaceFromElement(file, elem);
     3.8 +			return raceFactory.CreateRaceFromElement(elem);
     3.9  		}
    3.10  
    3.11  		protected override void CleanUpFileAsSupportedType(ZipFile typedFile)
     4.1 --- a/API/Factories/Xml/WarFoundryXmlRaceFactory.cs	Sat Jul 02 19:58:28 2011 +0000
     4.2 +++ b/API/Factories/Xml/WarFoundryXmlRaceFactory.cs	Mon Jul 04 20:02:26 2011 +0000
     4.3 @@ -20,7 +20,7 @@
     4.4  	/// <summary>
     4.5  	/// A sub-factory for loading WarFoundry Race XML files
     4.6  	/// </summary>
     4.7 -	public class WarFoundryXmlRaceFactory
     4.8 +	public class WarFoundryXmlRaceFactory : IRaceFactory<XmlDocument, XmlElement>
     4.9  	{
    4.10  		private Dictionary<Race, XmlDocument> extraData = new Dictionary<Race, XmlDocument>();
    4.11  		private WarFoundryXmlLimitParser limitParser = new WarFoundryXmlLimitParser();
    4.12 @@ -41,9 +41,14 @@
    4.13  			XmlDocument extra = null;
    4.14  			extraData.TryGetValue(obj, out extra);
    4.15  			return extra;
    4.16 +		}
    4.17 +
    4.18 +		public Race CreateRace(XmlElement elem)
    4.19 +		{
    4.20 +			return CreateRaceFromElement(elem);
    4.21  		}
    4.22  		
    4.23 -		public Race CreateRaceFromElement(ZipFile file, XmlElement elem)
    4.24 +		public Race CreateRaceFromElement(XmlElement elem)
    4.25  		{
    4.26  			string id = elem.GetAttribute("id");
    4.27  			string subid = elem.GetAttribute("subid");
    4.28 @@ -106,6 +111,24 @@
    4.29  			Category cat = CategoryLoader.CreateFromElement(elem);
    4.30  			parentRace.AddCategory(cat);
    4.31  			return cat;
    4.32 +		}
    4.33 +
    4.34 +		
    4.35 +		public UnitType GetUnitType(string id, Race parentRace)
    4.36 +		{
    4.37 +			return GetUnitType(id, parentRace, GetExtraData(parentRace));
    4.38 +		}
    4.39 +
    4.40 +		public UnitType GetUnitType(string id, Race parentRace, XmlDocument doc)
    4.41 +		{
    4.42 +			UnitType type = parentRace.GetUnitType(id);
    4.43 +
    4.44 +			if (type==null)
    4.45 +			{
    4.46 +				type = GetUnitTypeFromDocument(doc, id, parentRace);
    4.47 +			}
    4.48 +
    4.49 +			return type;
    4.50  		}
    4.51  
    4.52  		private UnitType GetUnitTypeFromDocument(XmlDocument doc, string id, Race parentRace)
    4.53 @@ -383,7 +406,7 @@
    4.54  
    4.55  				if (reqFactory != null) {
    4.56  					string data = WarFoundryXmlFactoryUtils.SelectSingleElement(extraData, "race:data").InnerText;
    4.57 -					UnitRequiresAtLeastNUnitsRequirement req = reqFactory.CreateRequirement(type, data);
    4.58 +					UnitRequiresAtLeastNUnitsRequirement req = reqFactory.CreateRequirement(type, data, this);
    4.59  					type.AddRequirement(req);
    4.60  				}
    4.61  			}
     5.1 --- a/IBBoard.WarFoundry.API.csproj	Sat Jul 02 19:58:28 2011 +0000
     5.2 +++ b/IBBoard.WarFoundry.API.csproj	Mon Jul 04 20:02:26 2011 +0000
     5.3 @@ -181,6 +181,7 @@
     5.4      <Compile Include="API\Factories\Requirement\UnitRequiresAtLeastNUnitsRequirementFactory.cs" />
     5.5      <Compile Include="API\Factories\Requirement\InvalidRequirementException.cs" />
     5.6      <Compile Include="API\Factories\Xml\CategoryLoader.cs" />
     5.7 +    <Compile Include="API\Factories\IRaceFactory.cs" />
     5.8    </ItemGroup>
     5.9    <ItemGroup>
    5.10      <Reference Include="System.Xml" />