Mercurial > repos > IBBoard.WarFoundry.API
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 wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/API/Factories/IRaceFactory.cs Mon Jul 04 20:02:26 2011 +0000 @@ -0,0 +1,20 @@ +// This file (IRaceFactory.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; +using IBBoard.WarFoundry.API.Objects; + +namespace IBBoard.WarFoundry.API.Factories +{ + /// <summary> + /// 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. + /// </summary> + public interface IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> + { + Race CreateRace(ENTRY_TYPE entry); + void CompleteLoading(Race race); + UnitType GetUnitType(string id, Race parentRace); + UnitType GetUnitType(string id, Race parentRace, SOURCE_FILE_TYPE src); + } +} +
--- a/API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs Sat Jul 02 19:58:28 2011 +0000 +++ b/API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactory.cs Mon Jul 04 20:02:26 2011 +0000 @@ -14,21 +14,21 @@ //Do nothing special } - public UnitRequiresAtLeastNUnitsRequirement CreateRequirement(UnitType type, string data) + public UnitRequiresAtLeastNUnitsRequirement CreateRequirement<SOURCE_FILE_TYPE, ENTRY_TYPE>(UnitType type, string data, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory) { UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(type); Race race = type.Race; - AddRequirements(req, race, data); + AddRequirements(req, race, data, raceFactory); return req; } - private void AddRequirements(UnitRequiresAtLeastNUnitsRequirement req, Race race, string data) + private void AddRequirements<SOURCE_FILE_TYPE, ENTRY_TYPE>(UnitRequiresAtLeastNUnitsRequirement req, Race race, string data, IRaceFactory<SOURCE_FILE_TYPE, ENTRY_TYPE> raceFactory) { foreach (string requirement in data.Split('|')) { string[] requirementParts = requirement.Split(':'); string unitID = requirementParts[0]; - UnitType unitType = race.GetUnitType(unitID); + UnitType unitType = raceFactory.GetUnitType(unitID, race); if (unitType == null) {
--- a/API/Factories/Xml/WarFoundryXmlFactory.cs Sat Jul 02 19:58:28 2011 +0000 +++ b/API/Factories/Xml/WarFoundryXmlFactory.cs Mon Jul 04 20:02:26 2011 +0000 @@ -139,7 +139,7 @@ { XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.RACE_ELEMENT); LogNotifier.Debug(GetType(), "Create Race"); - return raceFactory.CreateRaceFromElement(file, elem); + return raceFactory.CreateRaceFromElement(elem); } protected override void CleanUpFileAsSupportedType(ZipFile typedFile)
--- a/API/Factories/Xml/WarFoundryXmlRaceFactory.cs Sat Jul 02 19:58:28 2011 +0000 +++ b/API/Factories/Xml/WarFoundryXmlRaceFactory.cs Mon Jul 04 20:02:26 2011 +0000 @@ -20,7 +20,7 @@ /// <summary> /// A sub-factory for loading WarFoundry Race XML files /// </summary> - public class WarFoundryXmlRaceFactory + public class WarFoundryXmlRaceFactory : IRaceFactory<XmlDocument, XmlElement> { private Dictionary<Race, XmlDocument> extraData = new Dictionary<Race, XmlDocument>(); private WarFoundryXmlLimitParser limitParser = new WarFoundryXmlLimitParser(); @@ -41,9 +41,14 @@ XmlDocument extra = null; extraData.TryGetValue(obj, out extra); return extra; + } + + public Race CreateRace(XmlElement elem) + { + return CreateRaceFromElement(elem); } - public Race CreateRaceFromElement(ZipFile file, XmlElement elem) + public Race CreateRaceFromElement(XmlElement elem) { string id = elem.GetAttribute("id"); string subid = elem.GetAttribute("subid"); @@ -106,6 +111,24 @@ Category cat = CategoryLoader.CreateFromElement(elem); parentRace.AddCategory(cat); return cat; + } + + + public UnitType GetUnitType(string id, Race parentRace) + { + return GetUnitType(id, parentRace, GetExtraData(parentRace)); + } + + public UnitType GetUnitType(string id, Race parentRace, XmlDocument doc) + { + UnitType type = parentRace.GetUnitType(id); + + if (type==null) + { + type = GetUnitTypeFromDocument(doc, id, parentRace); + } + + return type; } private UnitType GetUnitTypeFromDocument(XmlDocument doc, string id, Race parentRace) @@ -383,7 +406,7 @@ if (reqFactory != null) { string data = WarFoundryXmlFactoryUtils.SelectSingleElement(extraData, "race:data").InnerText; - UnitRequiresAtLeastNUnitsRequirement req = reqFactory.CreateRequirement(type, data); + UnitRequiresAtLeastNUnitsRequirement req = reqFactory.CreateRequirement(type, data, this); type.AddRequirement(req); } }
--- a/IBBoard.WarFoundry.API.csproj Sat Jul 02 19:58:28 2011 +0000 +++ b/IBBoard.WarFoundry.API.csproj Mon Jul 04 20:02:26 2011 +0000 @@ -181,6 +181,7 @@ <Compile Include="API\Factories\Requirement\UnitRequiresAtLeastNUnitsRequirementFactory.cs" /> <Compile Include="API\Factories\Requirement\InvalidRequirementException.cs" /> <Compile Include="API\Factories\Xml\CategoryLoader.cs" /> + <Compile Include="API\Factories\IRaceFactory.cs" /> </ItemGroup> <ItemGroup> <Reference Include="System.Xml" />