# HG changeset patch # User IBBoard # Date 1309618785 0 # Node ID 13793f3a2a2e50e3b4549cc0a5377c338efc1616 # Parent e10630f39ec2d708763a5cc11f6e9cb93dbf9cc5 Re #351: Add extensible requirement handling method * Add initial conditional to fetching of factory * Exception if content isn't correct diff -r e10630f39ec2 -r 13793f3a2a2e API/Factories/Requirement/InvalidRequirementException.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/API/Factories/Requirement/InvalidRequirementException.cs Sat Jul 02 14:59:45 2011 +0000 @@ -0,0 +1,15 @@ +// This file (InvalidRequirementException.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 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; + +namespace IBBoard.WarFoundry.API.Factories.Requirement +{ + public class InvalidRequirementException : Exception + { + public InvalidRequirementException(string message) : base(message) + { + } + } +} + diff -r e10630f39ec2 -r 13793f3a2a2e API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs --- a/API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs Tue Jun 14 14:27:14 2011 +0000 +++ b/API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs Sat Jul 02 14:59:45 2011 +0000 @@ -27,13 +27,20 @@ foreach (string requirement in data.Split('|')) { string[] requirementParts = requirement.Split(':'); + UnitType unitType = race.GetUnitType(requirementParts[0]); + + if (unitType == null) + { + throw new InvalidRequirementException(String.Format("Invalid unit type '{0}' for 'Requires at least N units' requirement", requirementParts[0])); + } + if (requirementParts.Length == 2) { - req.AddUnitTypeRequirement(race.GetUnitType(requirementParts[0]), Int32.Parse(requirementParts[1])); + req.AddUnitTypeRequirement(unitType, Int32.Parse(requirementParts[1])); } else { - req.AddUnitTypeRequirement(race.GetUnitType(requirementParts[0])); + req.AddUnitTypeRequirement(unitType); } } } diff -r e10630f39ec2 -r 13793f3a2a2e API/Factories/Xml/WarFoundryXmlRaceFactory.cs --- a/API/Factories/Xml/WarFoundryXmlRaceFactory.cs Tue Jun 14 14:27:14 2011 +0000 +++ b/API/Factories/Xml/WarFoundryXmlRaceFactory.cs Sat Jul 02 14:59:45 2011 +0000 @@ -388,9 +388,17 @@ } } - UnitRequiresAtLeastNUnitsRequirementFactory GetRequirementFactory (string name) + private UnitRequiresAtLeastNUnitsRequirementFactory GetRequirementFactory (string name) { - return new UnitRequiresAtLeastNUnitsRequirementFactory(); + + UnitRequiresAtLeastNUnitsRequirementFactory factory = null; + + if ("RequiresAtLeastNUnits".Equals(name)) + { + factory = new UnitRequiresAtLeastNUnitsRequirementFactory(); + } + + return factory; } private void LoadExtraDataForUnitType(XmlElement elem, UnitType type) diff -r e10630f39ec2 -r 13793f3a2a2e IBBoard.WarFoundry.API.csproj --- a/IBBoard.WarFoundry.API.csproj Tue Jun 14 14:27:14 2011 +0000 +++ b/IBBoard.WarFoundry.API.csproj Sat Jul 02 14:59:45 2011 +0000 @@ -180,6 +180,7 @@ +