changeset 47:85f2b9c3609c

Re #13 - Use XPath for file loading * Replace loading of race unit types, categories and equipment with XPath method * Add loading of abilities using XPath Also: * Add implementation for Ability * Add AddAbility method to Race * Alter some logging so that "for ID" is followed by ID not name
author IBBoard <dev@ibboard.co.uk>
date Thu, 26 Mar 2009 20:49:42 +0000
parents a5855fcd75ab
children b49372dd8afa
files api/Factories/Xml/WarFoundryXmlFactory.cs api/Objects/Ability.cs api/Objects/Race.cs
diffstat 3 files changed, 56 insertions(+), 27 deletions(-) [+]
line diff
     1.1 --- a/api/Factories/Xml/WarFoundryXmlFactory.cs	Mon Mar 23 20:57:07 2009 +0000
     1.2 +++ b/api/Factories/Xml/WarFoundryXmlFactory.cs	Thu Mar 26 20:49:42 2009 +0000
     1.3 @@ -203,8 +203,8 @@
     1.4  			string defaultStatsID = ((XmlElement)statsElem).GetAttribute("defaultStats");
     1.5  			LoadSystemStatsForSystem(system, elem);
     1.6  			system.StandardSystemStatsID = defaultStatsID;
     1.7 -			LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.Name);
     1.8 -			LogNotifier.DebugFormat(GetType(), "GameSystem with ID {0} default stats: {1}", system.Name, system.StandardSystemStatsID);
     1.9 +			LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.ID);
    1.10 +			LogNotifier.DebugFormat(GetType(), "GameSystem with ID {0} default stats: {1}", system.ID, system.StandardSystemStatsID);
    1.11  			system.SetAsFullyLoaded();
    1.12  		}
    1.13  		
    1.14 @@ -244,40 +244,32 @@
    1.15  			race.SetAsLoading();
    1.16  			
    1.17  			XmlNode elem = GetExtraData(race);
    1.18 -			XmlNode colNode = elem.FirstChild;
    1.19 -						
    1.20 -			foreach (XmlElement node in colNode.ChildNodes)
    1.21 +			
    1.22 +			foreach (XmlElement node in SelectNodes(elem, "/race:race/race:units/race:unit"))
    1.23  			{
    1.24  				UnitType type = CreateUnitTypeFromElement(node, race, race.GameSystem);
    1.25  				race.AddUnitType(type);
    1.26  			}
    1.27  			
    1.28 -			colNode = colNode.NextSibling;
    1.29 -			
    1.30 -			if (colNode!=null && colNode.Name == WarFoundryXmlElementName.CATEGORIES_ELEMENT.Value)
    1.31 +			foreach (XmlElement node in SelectNodes(elem, "/race:race/race:categories/cat:cat"))
    1.32  			{
    1.33 -				foreach (XmlElement node in colNode.ChildNodes)
    1.34 -				{
    1.35 -					race.AddCategory(CreateCategoryFromElement(node));
    1.36 -				}
    1.37 -				
    1.38 -				colNode = colNode.NextSibling;
    1.39 +				race.AddCategory(CreateCategoryFromElement(node));
    1.40  			}
    1.41  							
    1.42 -			if (colNode!=null && colNode.Name == WarFoundryXmlElementName.RACE_EQUIPMENT_ITEMS_ELEMENT.Value)
    1.43 +			foreach (XmlElement node  in SelectNodes(elem, "/race:race/race:equipment/cat:equipmentItem"))
    1.44  			{
    1.45 -				foreach (XmlElement node in colNode.ChildNodes)
    1.46 -				{
    1.47 -					EquipmentItem item = CreateEquipmentItemFromElement(node, race);
    1.48 -					race.AddEquipmentItem(item);
    1.49 -				}
    1.50 +				EquipmentItem item = CreateEquipmentItemFromElement(node, race);
    1.51 +				race.AddEquipmentItem(item);
    1.52 +			}
    1.53 +							
    1.54 +			foreach (XmlElement node  in SelectNodes(elem, "/race:race/race:abilities/cat:ability"))
    1.55 +			{
    1.56 +				Ability ability = CreateAbilityFromElement(node, race);
    1.57 +				race.AddAbility(ability);
    1.58  			}
    1.59  			
    1.60 -			Dictionary<string, Ability> raceAbilities = new Dictionary<string, Ability>();
    1.61 -			//TODO: Load abilities
    1.62 -			race.SetAbilities(raceAbilities);
    1.63  			race.SetAsFullyLoaded();
    1.64 -			LogNotifier.DebugFormat(GetType(), "Completed loading of Race with ID {0}", race.Name);
    1.65 +			LogNotifier.DebugFormat(GetType(), "Completed loading of Race with ID {0}", race.ID);
    1.66  		}
    1.67  		
    1.68  		protected XmlDocument CreateXmlDocumentFromStream(Stream stream)
    1.69 @@ -287,7 +279,7 @@
    1.70  			
    1.71  			try
    1.72  			{
    1.73 -				doc.Load(reader);			
    1.74 +				doc.Load(reader);
    1.75  			}
    1.76  			//Don't catch XMLSchemaExceptions - let them get thrown out
    1.77  			finally
    1.78 @@ -549,5 +541,15 @@
    1.79  			
    1.80  			return new EquipmentItem(id, name, cost, min, max, armourType, race);
    1.81  		}
    1.82 +		
    1.83 +		private Ability CreateAbilityFromElement(XmlElement elem, Race race)
    1.84 +		{
    1.85 +			string id = elem.GetAttribute("id");
    1.86 +			string name = elem.GetAttribute("name");
    1.87 +			Ability ability = new Ability(id, name);
    1.88 +			XmlNode node = elem.SelectSingleNode("description", GetNamespaceManager());
    1.89 +			ability.Description = (node == null) ? "" : node.InnerText;
    1.90 +			return ability;
    1.91 +		}
    1.92  	}
    1.93  }
    1.94 \ No newline at end of file
     2.1 --- a/api/Objects/Ability.cs	Mon Mar 23 20:57:07 2009 +0000
     2.2 +++ b/api/Objects/Ability.cs	Thu Mar 26 20:49:42 2009 +0000
     2.3 @@ -6,10 +6,27 @@
     2.4  
     2.5  namespace IBBoard.WarFoundry.API.Objects
     2.6  {
     2.7 +	/// <summary>
     2.8 +	/// An Ability is a special rule that a UnitType has, made up of an ability name and a description.
     2.9 +	/// </summary>
    2.10  	public class Ability : WarFoundryObject
    2.11  	{
    2.12 -		public Ability()
    2.13 +		private string description;
    2.14 +		
    2.15 +		public Ability(String id, String name) : base(id, name)
    2.16  		{
    2.17  		}
    2.18 +		
    2.19 +		public string Description
    2.20 +		{
    2.21 +			get { return description; }
    2.22 +			set
    2.23 +			{
    2.24 +				if (value!=null)
    2.25 +				{
    2.26 +					description = value.Trim();
    2.27 +				}
    2.28 +			}
    2.29 +		}
    2.30  	}
    2.31  }
     3.1 --- a/api/Objects/Race.cs	Mon Mar 23 20:57:07 2009 +0000
     3.2 +++ b/api/Objects/Race.cs	Thu Mar 26 20:49:42 2009 +0000
     3.3 @@ -238,9 +238,19 @@
     3.4  			return items;
     3.5  		}
     3.6  		
     3.7 +		public void AddAbility(Ability newAbility)
     3.8 +		{
     3.9 +			//TODO: Throw DuplicateItemException
    3.10 +			abilities.Add(newAbility.ID, newAbility);
    3.11 +		}
    3.12 +
    3.13 +		[Obsolete("Use AddAbility method instead")]
    3.14  		public void SetAbilities(Dictionary<string, Ability> newAbilities)
    3.15  		{
    3.16 -			abilities = newAbilities;
    3.17 +			foreach (Ability ability in newAbilities.Values)
    3.18 +			{
    3.19 +				AddAbility(ability);
    3.20 +			}
    3.21  		}
    3.22  				
    3.23  		public Ability GetAbility(string id)