changeset 8:35bc86f8c283

Re #17 - Load unit data * Parse more attributes including troop cost * Improve handling of required values * Refactor code in to generic parent
author IBBoard <dev@ibboard.co.uk>
date Sat, 21 Feb 2009 21:00:52 +0000
parents f7decc0332c5
children 5f0259e69a7f
files RollcallFactory.cs RollcallRaceParser.cs RollcallUnitTypeParser.cs
diffstat 3 files changed, 96 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/RollcallFactory.cs	Tue Feb 17 16:36:15 2009 +0000
+++ b/RollcallFactory.cs	Sat Feb 21 21:00:52 2009 +0000
@@ -61,17 +61,8 @@
 		}
 		
 		protected override ICollection<IWarFoundryObject> DoCreateObjectsFromFile(FileInfo file)
-		{			
-			ICollection<IWarFoundryObject> objects = new List<IWarFoundryObject>();
-			
-			if (CheckCanHandleFileAsRace(file))
-			{
-				objects.Add(CreateRaceFromFile(file));
-			}
-			else if (CheckCanHandleFileAsArmy(file))
-			{
-				objects.Add(CreateArmyFromFile(file));
-			}
+		{
+			ICollection<IWarFoundryObject> objects = base.DoCreateObjectsFromFile(file);
 			
 			if (!addedSystem && objects.Count > 0)
 			{
@@ -89,7 +80,7 @@
 		
 		protected override Race CreateRaceFromFile(FileInfo file)
 		{
-			IniFile rollcallFile = IniFileReader.ReadFile(file);			
+			IniFile rollcallFile = IniFileReader.ReadFile(file);
 			Race race = RollcallRaceParser.ReadRaceDetails(rollcallFile);
 			RollcallRaceParser.ReadCategories(rollcallFile, race);
 			RollcallRaceParser.ReadUnitTypeAndEquipmentSections(rollcallFile, race);
--- a/RollcallRaceParser.cs	Tue Feb 17 16:36:15 2009 +0000
+++ b/RollcallRaceParser.cs	Sat Feb 21 21:00:52 2009 +0000
@@ -5,6 +5,7 @@
 using System;
 using IBBoard.CustomMath;
 using IBBoard.Ini;
+using IBBoard.IO;
 using IBBoard.Logging;
 using IBBoard.WarFoundry.API.Objects;
 
@@ -18,21 +19,37 @@
 		public static Race ReadRaceDetails(IniFile file)
 		{
 			string id = null, name = null;
-			id = "Rollcall" + file["Army"]["BitCodeID"].Value;
-			name = file["Army"]["Name"].Value;
+			id = file.GetSectionLineValue("Army", "BitCodeID");
+			
+			if (id == null)
+			{
+				throw new InvalidFileException("BitCodeID field of Rollcall race did not exist");
+			}
+			
+			id = "Rollcall" + id;
+			name = file.GetSectionLineValue("Army", "Name");
+			
+			if (name == null)
+			{
+				throw new InvalidFileException("Name field of Rollcall race did not exist");
+			}
+			
 			LogNotifier.Debug(typeof(RollcallRaceParser), "Loading Rollcall race ID "+id);
-			Race race = new Race(id, name, "Rollcall", RollcallFactory.GetFactory());
+			Race race = new Race(id, name, RollcallFactory.GetFactory().RollcallSystem.ID, RollcallFactory.GetFactory());
 			race.GameSystem = RollcallFactory.GetFactory().RollcallSystem;
 			return race;
 		}
 		
 		public static void ReadCategories(IniFile file, Race race)
 		{
-			IniSection section = file["Category"];
-
-			foreach (string key in section.Keys)
+			ReadCategories(file["Category"], race);
+		}
+		
+		private static void ReadCategories(IniSection categorySection, Race race)
+		{			
+			foreach (string key in categorySection.Keys)
 			{
-				string valueString = section[key].Value;
+				string valueString = categorySection.GetLineValue(key);
 				string[] values = valueString.Split(',');
 
 				if (values.Length == 3)
@@ -48,6 +65,10 @@
 					race.AddCategory(category);
 					
 				}
+				else
+				{
+					LogNotifier.Warn(typeof(RollcallRaceParser), "Ignored non-standard category: "+key);
+				}
 				//Special cases (allies and aliases) need to be handled later					                              
 			}
 		}
@@ -56,20 +77,27 @@
 		{
 			foreach (IniSection section in file)
 			{
-				string sectionName = section.Name;
-				
-				if (sectionName == "Army" || sectionName == "Category")
+				try
 				{
-					continue;
+					string sectionName = section.Name;
+					
+					if (sectionName == "Army" || sectionName == "Category")
+					{
+						continue;
+					}
+					else if (sectionName.StartsWith("Unit"))
+					{
+						RollcallUnitTypeParser.ReadUnitTypeSection(file, section, race);
+					}
+					else
+					{
+						RollcallUnitTypeParser.ReadEquipmentSection(file, section, race);
+					}	
 				}
-				else if (sectionName.StartsWith("Unit"))
+				catch (InvalidFileException ex)
 				{
-					RollcallUnitTypeParser.ReadUnitTypeSection(file, section, race);
+					throw new InvalidFileException("Invalid file exception while loading data", ex);
 				}
-				else
-				{
-					RollcallUnitTypeParser.ReadEquipmentSection(file, section, race);
-				}	
 			}
 		}
 	}
--- a/RollcallUnitTypeParser.cs	Tue Feb 17 16:36:15 2009 +0000
+++ b/RollcallUnitTypeParser.cs	Sat Feb 21 21:00:52 2009 +0000
@@ -32,32 +32,29 @@
 			if (unitType == null)
 			{
 				string unitID = RollcallUnitTypeParser.GetUnitID(section);
-				string name = section["Name"].Value;
+				string name = section.GetLineValue("Name", "");
+				
+				if (name == null || name == "")
+				{
+					throw new InvalidFileException("Attribute 'Name' for "+unitID+" was missing");
+				}
+				
 				unitType = new UnitType(unitID, name, race);
-				Category mainCat = race.GetCategory(section["Category"].Value);
+				
+				Category mainCat = race.GetCategory(section.GetLineValue("Category"));
+				
+				if (mainCat == null)
+				{
+					throw new InvalidFileException("Attribute 'Category' for "+unitID+" did not match a category");
+				}
+				
 				unitType.AddCategory(mainCat);
 				unitType.MainCategory = mainCat;
-				IniKeyValuePairLine line = section["MaximumSize"];
-
-				if (line!=null)
-				{
-					unitType.MaxSize = NumberParser.ParseAsInt(line.Value, -1);
-				}
-
-				line = section["MinimumSize"];
-
-				if (line!=null)
-				{
-					unitType.MinSize = NumberParser.ParseAsInt(line.Value, 1);
-				}
-
-				line = section["TroopCost"];
-
-				if (line!=null)
-				{
-					unitType.CostPerTrooper = NumberParser.ParseAsInt(line.Value, 0);
-				}
-				
+				unitType.MaxSize = GetRequiredNumericLine(section, "MaximumSize");
+				unitType.MinSize = GetNumericLine(section, "MinimumSize", 1);
+				unitType.MaxNumber = GetNumericLine(section, "MinNumber", 0);
+				unitType.MinNumber = GetNumericLine(section, "MaxNumber", -1);
+				unitType.CostPerTrooper = GetRequiredNumericLine(section, "TroopCost");
 				race.AddUnitType(unitType);
 			}
 
@@ -68,7 +65,11 @@
 		{
 			string sectionName = section.Name;
 
-			if (sectionName != "Unit" + section["UnitID"].Value)
+			if (!sectionName.StartsWith("Unit") || sectionName.Length <= 4)
+			{
+				throw new InvalidFileException("Unit section named "+sectionName+" did not have a valid section name");
+			}
+			if (sectionName != "Unit" + section.GetLineValue("UnitID", ""))
 			{
 				throw new InvalidFileException("Attribute 'UnitID' for "+sectionName+" did not match section name");
 			}
@@ -76,6 +77,29 @@
 			return sectionName;
 		}
 		
+		private static int GetRequiredNumericLine(IniSection section, string key)
+		{
+			int lineValue = GetNumericLine(section, key);
+			
+			if (lineValue == int.MinValue)
+			{
+				throw new InvalidFileException("Attribute '"+key+"' was required but did not exist in section "+section.Name);
+			}
+			
+			return lineValue;
+		}
+		
+		private static int GetNumericLine(IniSection section, string key)
+		{
+			return GetNumericLine(section, key, int.MinValue);
+		}
+		
+		private static int GetNumericLine(IniSection section, string key, int defaultValue)
+		{
+			string line = section.GetLineValue(key, defaultValue.ToString());
+			return NumberParser.ParseAsInt(line, defaultValue);
+		}
+		
 		public static void ReadEquipmentSection(IniFile file, IniSection section, Race race)
 		{
 		}