diff api/Objects/Race.cs @ 8:613bc5eaac59

Re #9 - Make WarFoundry loading granular * Remove specific staged loading classes * Rework category loading for GameSystem and Race to make it use AddCategory(Category) method * Promote staged loading from Native Factory to all Factories level * Refactor XML Factory to use smaller methods Also removed some commented code that isn't used any more
author IBBoard <dev@ibboard.co.uk>
date Sun, 04 Jan 2009 19:24:13 +0000
parents 520818033bb6
children 5a1df00b0359
line wrap: on
line diff
--- a/api/Objects/Race.cs	Sun Jan 04 13:12:55 2009 +0000
+++ b/api/Objects/Race.cs	Sun Jan 04 19:24:13 2009 +0000
@@ -27,7 +27,7 @@
 
 namespace IBBoard.WarFoundry.API.Objects
 {
-	public class Race : WarFoundryObject
+	public class Race : WarFoundryStagedLoadingObject
 	{		
 		public static string SYSTEM_DEFAULT_RACE_ID = "GameDefault"; 
 		
@@ -35,17 +35,16 @@
 		private string systemID;
 		private GameSystem system;
 		private Dictionary<Category, Dictionary<string, UnitType>> unitTypesByCat;
-		private Dictionary<string, UnitType> unitTypes;
-		private Dictionary<string, EquipmentItem> equipment;
-		private Dictionary<string, Ability> abilities;
-		private Category[] cats;
-		private FileInfo sourceFile;
+		private Dictionary<string, UnitType> unitTypes = new Dictionary<string,UnitType>();
+		private Dictionary<string, EquipmentItem> equipment = new Dictionary<string,EquipmentItem>();
+		private Dictionary<string, Ability> abilities = new Dictionary<string,Ability>();
+		private Dictionary<string, Category> categories = new Dictionary<string,Category>();
 		
-		public Race(string raceID, string raceName, string gameSystemID) : this(raceID, "", raceName, gameSystemID)
+		public Race(string raceID, string raceName, string gameSystemID, IWarFoundryFactory creatingFactory) : this(raceID, "", raceName, gameSystemID, creatingFactory)
 		{
 		}
 		
-		public Race(string raceID, string raceSubID, string raceName, string gameSystemID) : base(raceID + (raceSubID!="" ? "_"+raceSubID : ""), raceName)
+		public Race(string raceID, string raceSubID, string raceName, string gameSystemID, IWarFoundryFactory creatingFactory) : base(raceID + (raceSubID!="" ? "_"+raceSubID : ""), raceName, creatingFactory)
 		{
 			subID = (raceSubID == null ? "" : raceSubID);
 			systemID = gameSystemID;
@@ -55,12 +54,6 @@
 		{
 			get { return subID; }
 			set { subID = (value == null ? "" : value.Trim()); }
-		}
-		
-		public FileInfo SourceFile
-		{
-			get { return sourceFile; }
-			set { sourceFile = value; }
 		}
 
 		public GameSystem GameSystem
@@ -83,6 +76,11 @@
 				
 				system = value;
 			}
+		}
+		
+		public void AddCategory(Category cat)
+		{
+			categories[cat.ID] = cat;
 		}
 
 		/// <summary>
@@ -96,74 +94,51 @@
 		/// </returns>
 		public Category GetCategory(string id)
 		{
-			Category[] categories = RaceCategories;
+			EnsureFullyLoaded();
 			Category cat = null;
-		
-			for (int i = 0; i<categories.Length; i++)
-			{
-				if (categories[i].ID == id)
-				{
-					cat = categories[i];
-					break;
-				}
+			categories.TryGetValue(id, out cat);
+			
+			if (cat == null)
+			{
+				cat = GameSystem.GetCategory(id);
 			}
-			
+						
 			return cat;
 		}
 
-		/// <summary>
-		/// Gets a category based on its index within the list. Ordering is defined by the game system definition or by the race's overrides.
-		/// </summary>
-		/// <param name="index">
-		/// A <see cref="System.Int32"/>
-		/// </param>
-		/// <returns>
-		/// A <see cref="Category"/>
-		/// </returns>
-		public virtual Category GetCategory(int index)
-		{
-			if (cats == null)
-			{
-				return GameSystem.GetCategory(index);
-			}
-			else
-			{
-				return Categories[index];
-			}
-		}
-		
 		public Category[] Categories
 		{
 			get 
-			{ 
-				return RaceCategories;
-			}
-			
-			set 
-			{
-				RaceOverrideCategories = value;
+			{ 
+				EnsureFullyLoaded();
+				Category[] cats;
+				
+				if (!HasCategoryOverrides())
+				{
+					cats = GameSystem.Categories;
+				}
+				else
+				{
+					cats = DictionaryToArrayConverter.Convert<string, Category>(categories);
+				}
+				
+				return cats;
 			}
+		}
+
+		public bool HasCategoryOverrides()
+		{
+			return categories.Count > 0;
 		}
 		
-		/*private virtual Category[] GetCategories()
-		{
-			if (cats==null)
-			{
-				return GameSystem.Categories;
-			}
-			else
-			{
-				return cats; 
-			}
-		}*/
-		
 		public void SetEquipmentItems(Dictionary<string, EquipmentItem> items)
 		{
 			equipment = items;
 		}
 
-		public virtual EquipmentItem GetEquipmentItem(string id)
-		{
+		public  EquipmentItem GetEquipmentItem(string id)
+		{
+			EnsureFullyLoaded();
 			return (EquipmentItem)equipment[id];
 		}
 		
@@ -177,25 +152,42 @@
 			}
 			
 			return items;
-		}
-
-		public virtual bool HasCategoryOverrides()
-		{
-			return cats!=null;
 		}
 		
 		public void SetUnitTypes(Dictionary<string, UnitType> types)
 		{
 			unitTypes = types;
+			unitTypesByCat = null;
 		}
 
 		public UnitType[] GetUnitTypes(Category cat)
 		{
 			if (unitTypesByCat==null)
 			{				
-				unitTypesByCat = new Dictionary<Category,Dictionary<string,UnitType>>();
+				BuildUnitTypesByCategoryCache();
+			}
+
+			Dictionary<string, UnitType> unitTypesDictionary;
+			unitTypesByCat.TryGetValue(cat, out unitTypesDictionary);
+			UnitType[] unitTypesArray;
+			
+			if (unitTypesDictionary == null)
+			{
+				unitTypesArray = new UnitType[0];
+			}
+			else
+			{
+				unitTypesArray = DictionaryToArrayConverter.Convert<string, UnitType>(unitTypesDictionary);
+			}
+			
+			return unitTypesArray;
+		}
+		
+		private void BuildUnitTypesByCategoryCache()
+		{
+			unitTypesByCat = new Dictionary<Category,Dictionary<string,UnitType>>();
 				
-				foreach (Category category in RaceCategories)
+				foreach (Category category in Categories)
 				{ 
 					unitTypesByCat.Add(category, new Dictionary<string, UnitType>());
 				}
@@ -213,18 +205,6 @@
 
 					catUnitTypes.Add(unit.ID, unit);
 				}
-			}
-
-			ICollection<UnitType> col = unitTypesByCat[cat].Values;
-			UnitType[] toRet = new UnitType[col.Count];
-			int i = 0;
-
-			foreach (UnitType type in col)
-			{
-				toRet[i++] = type;
-			}
-
-			return toRet;
 		}
 
 		public UnitType GetUnitType(string id)
@@ -255,50 +235,6 @@
 			abilities.TryGetValue(id, out ability);
 			return ability;
 		}
-		
-		protected Category[] RaceCategories
-		{
-			get
-			{
-				Category[] cats = RaceOverrideCategories;
-				
-				if (cats == null)
-				{
-					//No overrides, so load system categories
-					cats = GameSystem.Categories;
-				}
-				
-				return cats;
-			}
-		}
-		
-		protected virtual Category[] RaceOverrideCategories
-		{
-			get
-			{
-				return RaceRawOverrideCategories;
-			}
-			set
-			{
-				RaceRawOverrideCategories = value;
-			}
-		}
-		
-		protected Category[] RaceRawOverrideCategories
-		{
-			get { return cats; }
-			set
-			{
-				if (value!=null && value.Length>0)
-				{
-					cats = value;
-				}
-				else
-				{
-					cats = null;
-				}
-			}
-		}
 		
 		protected virtual Dictionary<string, UnitType> RaceUnitTypes
 		{