diff api/Objects/Race.cs @ 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 613bc5eaac59
children ad8eaed12e66
line wrap: on
line diff
--- 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()