changeset 6:150a5669cd7b

Re #9 - more granular loading * Remove SystemStatsSet class so that other classes don't know the internals of how GameSystem stores its stats (cleaner code principle) * Make XML loader each stats set and add to the game system * Add methods to GameSystem to remove use of SystemStatsSet and hide internal handling * Add methods to add SystemStats to GameSystem
author IBBoard <dev@ibboard.co.uk>
date Sun, 04 Jan 2009 12:13:59 +0000
parents b9346894319c
children 895c8a2378a1
files IBBoard.WarFoundry.API.mdp api/Factories/Xml/WarFoundryXmlFactory.cs api/Objects/GameSystem.cs api/Objects/SystemStatsSet.cs dtds/translation.dtd
diffstat 5 files changed, 33 insertions(+), 68 deletions(-) [+]
line wrap: on
line diff
--- a/IBBoard.WarFoundry.API.mdp	Fri Dec 26 12:45:32 2008 +0000
+++ b/IBBoard.WarFoundry.API.mdp	Sun Jan 04 12:13:59 2009 +0000
@@ -62,7 +62,6 @@
     <File name="api/Objects/Stats.cs" subtype="Code" buildaction="Compile" />
     <File name="api/Objects/StatSlot.cs" subtype="Code" buildaction="Compile" />
     <File name="api/Objects/SystemStats.cs" subtype="Code" buildaction="Compile" />
-    <File name="api/Objects/SystemStatsSet.cs" subtype="Code" buildaction="Compile" />
     <File name="api/Objects/Unit.cs" subtype="Code" buildaction="Compile" />
     <File name="api/Objects/UnitEquipmentItem.cs" subtype="Code" buildaction="Compile" />
     <File name="api/Objects/UnitEquipmentItemObj.cs" subtype="Code" buildaction="Compile" />
@@ -89,7 +88,6 @@
     <File name="dtds/army.dtd" subtype="Code" buildaction="Nothing" />
     <File name="dtds/race.dtd" subtype="Code" buildaction="Nothing" />
     <File name="dtds/system.dtd" subtype="Code" buildaction="Nothing" />
-    <File name="dtds/translation.dtd" subtype="Code" buildaction="Nothing" />
   </Contents>
   <References>
     <ProjectReference type="Gac" localcopy="True" refto="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
--- a/api/Factories/Xml/WarFoundryXmlFactory.cs	Fri Dec 26 12:45:32 2008 +0000
+++ b/api/Factories/Xml/WarFoundryXmlFactory.cs	Sun Jan 04 12:13:59 2009 +0000
@@ -242,12 +242,11 @@
 					LogNotifier.DebugFormat(GetType(), "Found {0} categories", cats.Length);
 					
 					XmlElement statsElem = (XmlElement)catsElem.NextSibling;					
-					Dictionary<string, SystemStats> sysStats = CreateSystemStatsSetFromElement(statsElem);
+					LoadSystemStatsFromElement(statsElem, system);
 					string defaultStatsID = statsElem.GetAttribute("defaultStats");
 										
 					LogNotifier.DebugFormat(GetType(), "Complete loading of {0}", system.Name);
 					system.Categories = cats;
-					system.SystemStats = new SystemStatsSet(sysStats); 
 					system.StandardSystemStatsID = defaultStatsID;
 				}
 				else if (obj is Race)
@@ -583,7 +582,7 @@
 			}
 			else
 			{
-				statsSet = system.SystemStats[statsID];
+				statsSet = system.GetSystemStatsForID(statsID);
 			}
 			
 			Stats stats = new Stats(statsSet);
@@ -608,17 +607,13 @@
 			return stats;
 		}
 		
-		private Dictionary<string, SystemStats> CreateSystemStatsSetFromElement(XmlElement elem)
+		private void LoadSystemStatsFromElement(XmlElement elem, GameSystem system)
 		{
-			Dictionary<string, SystemStats> dict = new Dictionary<string,SystemStats>();
-			
 			foreach (XmlElement stats in elem.ChildNodes)
 			{
 				SystemStats sysStats = CreateSystemStatsFromElement(stats);
-				dict.Add(sysStats.ID, sysStats);
+				system.AddSystemStats(sysStats);
 			}
-			
-			return dict;
 		}
 		
 		private SystemStats CreateSystemStatsFromElement(XmlElement elem)
--- a/api/Objects/GameSystem.cs	Fri Dec 26 12:45:32 2008 +0000
+++ b/api/Objects/GameSystem.cs	Sun Jan 04 12:13:59 2009 +0000
@@ -15,12 +15,13 @@
 	{
 		private bool warnOnError;
 		private Category[] categories;
-		private SystemStatsSet stats;
+		private Dictionary<string, SystemStats> stats;
 		private string defaultStats;
 		private FileInfo sourceFile;
 
 		public GameSystem(string systemID, string systemName) : base(systemID, systemName)
 		{
+			stats = new Dictionary<string,SystemStats>();
 		}
 				
 		/*public void CompleteLoading(Category[] cats, Dictionary<string, SystemStats> sysStats, string defaultStatsID)
@@ -48,16 +49,19 @@
 		}
 
 		public Category GetCategory(string id)
-		{
-			for (int i = 0; i<categories.Length; i++)
-			{
-				if (categories[i].ID == id)
+		{
+			Category categoryForID = null;
+			
+			for (int i = 0; i<Categories.Length; i++)
+			{
+				Category cat = Categories[i]; 
+				if (cat.ID == id)
 				{
-					return categories[i];
+					categoryForID = cat;
 				}
 			}
 
-			return null;
+			return categoryForID;
 		}
 
 		public Category[] Categories
@@ -108,11 +112,16 @@
 			set { warnOnError = value; }
 		}
 		
+		public void AddSystemStats(SystemStats sysStats)
+		{
+			stats[sysStats.ID] = sysStats;
+		}
+		
 		public SystemStats StandardSystemStats
 		{
 			get
 			{
-				return SystemStats[defaultStats];
+				return stats[defaultStats];
 			}
 		}
 		
@@ -132,18 +141,23 @@
 			}
 		}
 
-		public SystemStatsSet SystemStats
+		public SystemStats[] SystemStats
 		{
 			get 
-			{ 
-				return stats;
-			}
-			set
-			{
-				stats = value;
+			{ 
+				SystemStats[] statsArray = new SystemStats[stats.Count];
+				stats.Values.CopyTo(statsArray, 0);
+				return statsArray;
 			}
 		}
 		
+		public SystemStats GetSystemStatsForID(string id)
+		{
+			SystemStats statsForID;
+			stats.TryGetValue(id, out statsForID);
+			return statsForID;
+		}
+		
 		public Race SystemDefaultRace
 		{
 			get { return WarFoundryLoader.GetDefault().GetRace(this, Race.SYSTEM_DEFAULT_RACE_ID); }
--- a/api/Objects/SystemStatsSet.cs	Fri Dec 26 12:45:32 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,38 +0,0 @@
-using System;
-using System.Collections.Generic;
-
-namespace IBBoard.WarFoundry.API.Objects
-{
-	public class SystemStatsSet
-	{
-		private Dictionary<string, SystemStats> statsMap;
-		public SystemStatsSet(Dictionary<string, SystemStats> stats)
-		{
-			statsMap = stats;
-		}
-		
-		public SystemStats this[string key]
-		{
-			get { return statsMap[key]; }
-		
-			set
-			{
-				if (statsMap.ContainsKey(key))
-				{
-					statsMap[key] = value;
-				}
-				else
-				{
-					throw new ArgumentException("Key must already exist for values to be set");
-				}
-			}
-		}
-		
-		public string[] GetSystemStatsIDs()
-		{
-			string[] ids = new string[statsMap.Count];
-			statsMap.Keys.CopyTo(ids, 0);
-			return ids;
-		}
-	}
-}
--- a/dtds/translation.dtd	Fri Dec 26 12:45:32 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,4 +0,0 @@
-<!ELEMENT translations (translation*)>
-<!ELEMENT translation (#PCDATA)> <!-- it's a damned ugly hack, but C# won't take "#CDATA" so use #PCDATA and always treat it as CDATA -->
-<!ATTLIST translation id ID #REQUIRED>
-<!ATTLIST translations lang CDATA #REQUIRED>
\ No newline at end of file