changeset 11:5a1df00b0359

Re #9 - Make WarFoundry API load files in small methods * Add "add unit type" and "add equipment" methods to Race * Deprecate old "set unit types" and "set equipment" methods on Race * Update WarFoundryXmlFactory to use new methods * Create DuplicateItemException for later use
author IBBoard <dev@ibboard.co.uk>
date Sun, 18 Jan 2009 16:24:03 +0000
parents 607c3232d689
children ac232763858b
files api/Factories/Xml/WarFoundryXmlFactory.cs api/Objects/DuplicateItemException.cs api/Objects/Race.cs
diffstat 3 files changed, 80 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/api/Factories/Xml/WarFoundryXmlFactory.cs	Sun Jan 04 20:43:36 2009 +0000
+++ b/api/Factories/Xml/WarFoundryXmlFactory.cs	Sun Jan 18 16:24:03 2009 +0000
@@ -238,13 +238,11 @@
 			
 			XmlNode elem = GetExtraData(race);
 			XmlNode colNode = elem.FirstChild;
-			
-			Dictionary<string, UnitType> unitTypes = new Dictionary<string, UnitType>();
-			
+						
 			foreach (XmlElement node in colNode.ChildNodes)
 			{
 				UnitType type = CreateUnitTypeFromElement(node, race, race.GameSystem);
-				unitTypes.Add(type.ID, type);
+				race.AddUnitType(type);
 			}
 			
 			colNode = colNode.NextSibling;
@@ -258,23 +256,18 @@
 				
 				colNode = colNode.NextSibling;
 			}
-				
-			Dictionary<string, EquipmentItem> raceEquipment = new Dictionary<string, EquipmentItem>();
-			
+							
 			if (colNode!=null && colNode.Name == WarFoundryXmlElementName.RACE_EQUIPMENT_ITEMS_ELEMENT.Value)
 			{
 				foreach (XmlElement node in colNode.ChildNodes)
 				{
 					EquipmentItem item = CreateEquipmentItemFromElement(node, race);
-					raceEquipment.Add(item.ID, item);
+					race.AddEquipmentItem(item);
 				}
 			}
 			
 			Dictionary<string, Ability> raceAbilities = new Dictionary<string, Ability>();
 			//TODO: Load abilities
-			
-			race.SetUnitTypes(unitTypes);
-			race.SetEquipmentItems(raceEquipment);
 			race.SetAbilities(raceAbilities);
 			race.SetAsFullyLoaded();
 			LogNotifier.DebugFormat(GetType(), "Completed loading of Race with ID {0}", race.Name);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/api/Objects/DuplicateItemException.cs	Sun Jan 18 16:24:03 2009 +0000
@@ -0,0 +1,15 @@
+// This file (DuplicateItemException.cs) is a part of [PROJECT NAME] and is copyright 2009 IBBoard.
+//
+// The file and the library/program it is in are licensed under the GNU LGPL license. Please see COPYING.LGPL for more information and the full license.
+
+using System;
+
+namespace IBBoard.WarFoundry
+{
+	public class DuplicateItemException
+	{
+		public DuplicateItemException()
+		{
+		}
+	}
+}
--- a/api/Objects/Race.cs	Sun Jan 04 20:43:36 2009 +0000
+++ b/api/Objects/Race.cs	Sun Jan 18 16:24:03 2009 +0000
@@ -130,10 +130,20 @@
 		{
 			return categories.Count > 0;
 		}
-		
+
+		public void AddEquipmentItem(EquipmentItem item)
+		{
+			//TODO: Throw DuplicateItemException
+			equipment.Add(item.ID, item);
+		}
+
+		[Obsolete("Use AddEquipmentItem method instead")]
 		public void SetEquipmentItems(Dictionary<string, EquipmentItem> items)
 		{
-			equipment = items;
+			foreach (EquipmentItem item in items.Values)
+			{
+				AddEquipmentItem(item);
+			}
 		}
 
 		public  EquipmentItem GetEquipmentItem(string id)
@@ -153,20 +163,25 @@
 			
 			return items;
 		}
-		
+
+		public void AddUnitType(UnitType type)
+		{
+			unitTypes.Add(type.ID, type);
+			CacheUnitType(type);
+		}
+
+		[Obsolete("Use AddUnitType method instead")]
 		public void SetUnitTypes(Dictionary<string, UnitType> types)
 		{
-			unitTypes = types;
-			unitTypesByCat = null;
+			foreach (UnitType type in types.Values)
+			{
+				AddUnitType(type);
+			}
 		}
 
 		public UnitType[] GetUnitTypes(Category cat)
-		{
-			if (unitTypesByCat==null)
-			{				
-				BuildUnitTypesByCategoryCache();
-			}
-
+		{		
+			BuildUnitTypesByCategoryCache();
 			Dictionary<string, UnitType> unitTypesDictionary;
 			unitTypesByCat.TryGetValue(cat, out unitTypesDictionary);
 			UnitType[] unitTypesArray;
@@ -182,34 +197,49 @@
 			
 			return unitTypesArray;
 		}
+
+		private void CacheUnitType(UnitType unit)
+		{
+			BuildUnitTypesByCategoryCache();
+			Dictionary<string,UnitType> catUnitTypes;
+			unitTypesByCat.TryGetValue(unit.MainCategory, out catUnitTypes);
+
+			if (catUnitTypes == null)
+			{
+				throw new InvalidFileException(String.Format("Unit type {0} with name {1} is a unit of an undefined category", unit.ID, unit.Name));
+			}
+
+			catUnitTypes.Add(unit.ID, unit);
+		}
 		
 		private void BuildUnitTypesByCategoryCache()
 		{
+			if (unitTypesByCat == null)
+			{
+				DoBuildUnitTypesByCategoryCache();
+			}
+		}
+
+		private void DoBuildUnitTypesByCategoryCache()
+		{
 			unitTypesByCat = new Dictionary<Category,Dictionary<string,UnitType>>();
 				
-				foreach (Category category in Categories)
-				{ 
-					unitTypesByCat.Add(category, new Dictionary<string, UnitType>());
-				}
-				
-				Dictionary<string,UnitType> catUnitTypes;
-				
-				foreach (UnitType unit in unitTypes.Values)
-				{
-					unitTypesByCat.TryGetValue(unit.MainCategory, out catUnitTypes);
-
-					if (catUnitTypes == null)
-					{
-						throw new InvalidFileException(String.Format("Unit type {0} with name {1} is a unit of an undefined category", unit.ID, unit.Name));
-					}
-
-					catUnitTypes.Add(unit.ID, unit);
-				}
+			foreach (Category category in Categories)
+			{ 
+				unitTypesByCat.Add(category, new Dictionary<string, UnitType>());
+			}
+			
+			foreach (UnitType unit in unitTypes.Values)
+			{
+				CacheUnitType(unit);
+			}
 		}
 
 		public UnitType GetUnitType(string id)
-		{
-			return (UnitType)unitTypes[id];
+		{
+			UnitType type = null;
+			unitTypes.TryGetValue(id, out type);
+			return type;
 		}
 		
 		public List<Ability> GetAbilityList()