changeset 116:2010a7e8bf9a

Re #54: Add Army support to WarFoundryFactory * Separate out army parsing to a separate class to make unit loading easier
author IBBoard <dev@ibboard.co.uk>
date Sat, 22 Aug 2009 19:42:04 +0000
parents d0c60b3204c1
children 093ee2da0f6e
files IBBoard.WarFoundry.API.csproj api/Factories/Xml/WarFoundryXmlArmyFactory.cs api/Factories/Xml/WarFoundryXmlArmyParser.cs
diffstat 3 files changed, 61 insertions(+), 21 deletions(-) [+]
line diff
     1.1 --- a/IBBoard.WarFoundry.API.csproj	Sat Aug 22 19:41:15 2009 +0000
     1.2 +++ b/IBBoard.WarFoundry.API.csproj	Sat Aug 22 19:42:04 2009 +0000
     1.3 @@ -134,6 +134,7 @@
     1.4      <Compile Include="api\Commands\ReplaceUnitEquipmentWithRatioAmountItemCommand.cs" />
     1.5      <Compile Include="api\Factories\Xml\Zip\StringZipEntrySource.cs" />
     1.6      <Compile Include="api\Factories\RequiredDataMissingException.cs" />
     1.7 +    <Compile Include="api\Factories\Xml\WarFoundryXmlArmyParser.cs" />
     1.8    </ItemGroup>
     1.9    <ItemGroup>
    1.10      <Content Include="libs\ICSharpCode.SharpZipLib.dll" />
     2.1 --- a/api/Factories/Xml/WarFoundryXmlArmyFactory.cs	Sat Aug 22 19:41:15 2009 +0000
     2.2 +++ b/api/Factories/Xml/WarFoundryXmlArmyFactory.cs	Sat Aug 22 19:42:04 2009 +0000
     2.3 @@ -17,27 +17,7 @@
     2.4  	{			
     2.5  		public Army CreateArmyFromElement(ZipFile file, XmlElement elem)
     2.6  		{
     2.7 -			string name = elem.GetAttribute("name");
     2.8 -			string systemID = elem.GetAttribute("system");
     2.9 -			GameSystem system = WarFoundryLoader.GetDefault().GetGameSystem(systemID);
    2.10 -			
    2.11 -			if (system == null)
    2.12 -			{
    2.13 -				throw new RequiredDataMissingException(file.Name, "gameSystem", systemID);
    2.14 -			}
    2.15 -			
    2.16 -			string raceID = elem.GetAttribute("race");
    2.17 -			Race race = WarFoundryLoader.GetDefault().GetRace(system, raceID);
    2.18 -			
    2.19 -			if (race == null)
    2.20 -			{
    2.21 -				throw new RequiredDataMissingException(file.Name, "race", raceID);
    2.22 -			}
    2.23 -			
    2.24 -			int points = XmlTools.GetIntValueFromAttribute(elem, "maxPoints");			
    2.25 -			Army army = new Army(race, name, points, file);
    2.26 -			//TODO: Complete loading of army
    2.27 -			return army;
    2.28 +			return new WarFoundryXmlArmyParser(file, elem).GetArmy();
    2.29  		}
    2.30  	}
    2.31  }
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/api/Factories/Xml/WarFoundryXmlArmyParser.cs	Sat Aug 22 19:42:04 2009 +0000
     3.3 @@ -0,0 +1,59 @@
     3.4 +//  This file (WarFoundryXmlArmyParser.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard
     3.5 +// 
     3.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.
     3.7 +
     3.8 +using System;
     3.9 +using System.Xml;
    3.10 +using IBBoard.Xml;
    3.11 +using ICSharpCode.SharpZipLib.Zip;
    3.12 +using IBBoard.WarFoundry.API.Objects;
    3.13 +
    3.14 +namespace IBBoard.WarFoundry.API.Factories.Xml
    3.15 +{
    3.16 +	public class WarFoundryXmlArmyParser
    3.17 +	{
    3.18 +		private ZipFile file;
    3.19 +		private XmlElement elem;
    3.20 +		private Army army;
    3.21 +		
    3.22 +		public WarFoundryXmlArmyParser(ZipFile file, XmlElement elem)
    3.23 +		{
    3.24 +			this.file = file;
    3.25 +			this.elem = elem;
    3.26 +		}
    3.27 +
    3.28 +		public Army GetArmy()
    3.29 +		{
    3.30 +			if (army == null)
    3.31 +			{
    3.32 +				ParseArmy();
    3.33 +			}
    3.34 +
    3.35 +			return army;
    3.36 +		}
    3.37 +
    3.38 +		private void ParseArmy()
    3.39 +		{
    3.40 +			string name = elem.GetAttribute("name");
    3.41 +			string systemID = elem.GetAttribute("system");
    3.42 +			GameSystem system = WarFoundryLoader.GetDefault().GetGameSystem(systemID);
    3.43 +			
    3.44 +			if (system == null)
    3.45 +			{
    3.46 +				throw new RequiredDataMissingException(file.Name, "gameSystem", systemID);
    3.47 +			}
    3.48 +			
    3.49 +			string raceID = elem.GetAttribute("race");
    3.50 +			Race race = WarFoundryLoader.GetDefault().GetRace(system, raceID);
    3.51 +			
    3.52 +			if (race == null)
    3.53 +			{
    3.54 +				throw new RequiredDataMissingException(file.Name, "race", raceID);
    3.55 +			}
    3.56 +			
    3.57 +			int points = XmlTools.GetIntValueFromAttribute(elem, "maxPoints");			
    3.58 +			army = new Army(race, name, points, file);
    3.59 +			//TODO: Complete loading of army
    3.60 +		}
    3.61 +	}
    3.62 +}