diff RollcallFactory.cs @ 6:0509ed2e686a

Re #16 - File loading * Refactor race detail parsing and unit type parsing in to their own classes Re #17 - Unit detail loading * Add a couple of extra attributes to those loaded
author IBBoard <dev@ibboard.co.uk>
date Mon, 26 Jan 2009 20:31:05 +0000
parents 8c34e01a11da
children 35bc86f8c283
line wrap: on
line diff
--- a/RollcallFactory.cs	Sun Jan 25 14:51:51 2009 +0000
+++ b/RollcallFactory.cs	Mon Jan 26 20:31:05 2009 +0000
@@ -15,12 +15,18 @@
 {
 	public class RollcallFactory : AbstractNonNativeFileExtensionWarFoundryFactory
 	{
+		private static RollcallFactory factory;
 		private GameSystem rollcallSystem;
 		private bool addedSystem = false;
 		
-		public static RollcallFactory CreateFactory()
+		public static RollcallFactory GetFactory()
 		{
-			return new RollcallFactory();
+			if (factory == null)
+			{
+				factory = new RollcallFactory();
+			}
+			
+			return factory;
 		}
 
 		private RollcallFactory()
@@ -45,6 +51,14 @@
 		protected override string RaceFileExtension {
 			get { return ".adf"; }
 		}
+
+		public GameSystem RollcallSystem
+		{
+			get
+			{
+				return rollcallSystem;
+			}
+		}
 		
 		protected override ICollection<IWarFoundryObject> DoCreateObjectsFromFile(FileInfo file)
 		{			
@@ -61,7 +75,7 @@
 			
 			if (!addedSystem && objects.Count > 0)
 			{
-				objects.Add(rollcallSystem);
+				objects.Add(RollcallSystem);
 				addedSystem = true;
 			}
 			
@@ -76,117 +90,12 @@
 		protected override Race CreateRaceFromFile(FileInfo file)
 		{
 			IniFile rollcallFile = IniFileReader.ReadFile(file);			
-			Race race = ReadRaceDetails(rollcallFile);
-			ReadCategories(rollcallFile, race);
-			ReadUnitTypeAndEquipmentSections(rollcallFile, race);
-			return race;
-		}
-
-		private Race ReadRaceDetails(IniFile file)
-		{
-			string id = null, name = null;
-			id = "Rollcall" + file["Army"]["BitCodeID"].Value;
-			name = file["Army"]["Name"].Value;
-			LogNotifier.Debug(GetType(), "Loading Rollcall race ID "+id);
-			Race race = new Race(id, name, "Rollcall", this);
-			race.GameSystem = rollcallSystem;
+			Race race = RollcallRaceParser.ReadRaceDetails(rollcallFile);
+			RollcallRaceParser.ReadCategories(rollcallFile, race);
+			RollcallRaceParser.ReadUnitTypeAndEquipmentSections(rollcallFile, race);
 			return race;
 		}
 		
-		private void ReadCategories(IniFile file, Race race)
-		{
-			IniSection section = file["Category"];
-
-			foreach (string key in section.Keys)
-			{
-				string valueString = section[key].Value;
-				string[] values = valueString.Split(',');
-
-				if (values.Length == 3)
-				{
-					LogNotifier.Debug(GetType(), "Loading category " + values[0]);
-					int minPercent = 0;
-					int.TryParse(values[1], out minPercent);
-					int maxPercent = 100;
-					int.TryParse(values[2], out maxPercent);
-					maxPercent = Math.Max(0, Math.Min(100, Math.Max(minPercent, maxPercent)));
-					minPercent = Math.Max(0, Math.Min(100, minPercent));
-					Category category = new Category(key, values[0]);
-					category.MaximumPercentage = maxPercent;
-					category.MinimumPercentage = minPercent;
-					race.AddCategory(category);
-					
-				}
-				//Special cases (allies and aliases) need to be handled later					                              
-			}
-		}
-
-		private void ReadUnitTypeAndEquipmentSections(IniFile file, Race race)
-		{
-			foreach (IniSection section in file)
-			{
-				string sectionName = section.Name;
-				
-				if (sectionName == "Army" || sectionName == "Category")
-				{
-					continue;
-				}
-				else if (sectionName.StartsWith("Unit"))
-				{
-					ReadUnitTypeSection(file, section, race);
-				}
-				else
-				{
-					ReadEquipmentSection(file, section, race);
-				}	
-			}
-		}
-
-		private UnitType ReadUnitTypeSectionFromID(IniFile file, String sectionID, Race race)
-		{
-			return ReadUnitTypeSection(file, "Unit"+sectionID, race);
-		}
-
-		private UnitType ReadUnitTypeSection(IniFile file, String sectionName, Race race)
-		{
-			return ReadUnitTypeSection(file, file[sectionName], race);
-		}
-		
-		private UnitType ReadUnitTypeSection(IniFile file, IniSection section, Race race)
-		{
-			UnitType unitType = race.GetUnitType(section.Name);
-			
-			if (unitType == null)
-			{
-				string unitID = GetUnitID(section);
-				string name = section["Name"].Value;
-				unitType = new UnitType(unitID, name, race);
-				Category mainCat = race.GetCategory(section["Category"].Value);
-				unitType.AddCategory(mainCat);
-				unitType.MainCategory = mainCat;
-				//Load other values
-				race.AddUnitType(unitType);
-			}
-
-			return unitType;
-		}
-
-		private string GetUnitID(IniSection section)
-		{
-			string sectionName = section.Name;
-
-			if (sectionName != "Unit" + section["UnitID"].Value)
-			{
-				throw new InvalidFileException("Attribute 'UnitID' for "+sectionName+" did not match section name");
-			}
-
-			return sectionName;
-		}
-		
-		private void ReadEquipmentSection(IniFile file, IniSection section, Race race)
-		{
-		}
-		
 		protected override GameSystem CreateGameSystemFromFile (FileInfo file)
 		{
 			throw new InvalidDataException("No such file format (Rollcall GameSystem)");