changeset 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 f7decc0332c5
files IBBoard.WarFoundry.Plugin.Rollcall.mdp RollcallFactory.cs RollcallRaceParser.cs RollcallUnitTypeParser.cs
diffstat 4 files changed, 181 insertions(+), 111 deletions(-) [+]
line wrap: on
line diff
--- a/IBBoard.WarFoundry.Plugin.Rollcall.mdp	Sun Jan 25 14:51:51 2009 +0000
+++ b/IBBoard.WarFoundry.Plugin.Rollcall.mdp	Mon Jan 26 20:31:05 2009 +0000
@@ -18,6 +18,8 @@
     <File name="RollcallFactory.cs" subtype="Code" buildaction="Compile" />
     <File name="COPYING.GPL" subtype="Code" buildaction="Nothing" />
     <File name="COPYING.LGPL" subtype="Code" buildaction="Nothing" />
+    <File name="RollcallUnitTypeParser.cs" subtype="Code" buildaction="Compile" />
+    <File name="RollcallRaceParser.cs" subtype="Code" buildaction="Compile" />
   </Contents>
   <References>
     <ProjectReference type="Gac" localcopy="True" refto="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
--- 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)");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RollcallRaceParser.cs	Mon Jan 26 20:31:05 2009 +0000
@@ -0,0 +1,76 @@
+// This file (RollcallRaceParser.cs) is a part of IBBoard.WarFoundry.Plugin.Rollcall library and is copyright 2009 IBBoard.
+//
+// The file and the library/program it is in are licensed under the GNU LGPL license. Please see COPYING.LGPL for more information and the full license.
+
+using System;
+using IBBoard.CustomMath;
+using IBBoard.Ini;
+using IBBoard.Logging;
+using IBBoard.WarFoundry.API.Objects;
+
+namespace IBBoard.WarFoundry.Plugin.Rollcall
+{
+	/// <summary>
+	/// A helper class to construct a base Race object from an INI file
+	/// </summary>
+	public class RollcallRaceParser
+	{
+		public static Race ReadRaceDetails(IniFile file)
+		{
+			string id = null, name = null;
+			id = "Rollcall" + file["Army"]["BitCodeID"].Value;
+			name = file["Army"]["Name"].Value;
+			LogNotifier.Debug(typeof(RollcallRaceParser), "Loading Rollcall race ID "+id);
+			Race race = new Race(id, name, "Rollcall", 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)
+			{
+				string valueString = section[key].Value;
+				string[] values = valueString.Split(',');
+
+				if (values.Length == 3)
+				{
+					LogNotifier.Debug(typeof(RollcallRaceParser), "Loading category " + values[0]);
+					Category category = new Category(key, values[0]);
+					int minPercent = NumberParser.ParseAsInt(values[1], 0);
+					int maxPercent = NumberParser.ParseAsInt(values[2], 100);
+					maxPercent = Math.Max(0, Math.Min(100, Math.Max(minPercent, maxPercent)));
+					minPercent = Math.Max(0, Math.Min(100, minPercent));
+					category.MaximumPercentage = maxPercent;
+					category.MinimumPercentage = minPercent;
+					race.AddCategory(category);
+					
+				}
+				//Special cases (allies and aliases) need to be handled later					                              
+			}
+		}
+
+		public static 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"))
+				{
+					RollcallUnitTypeParser.ReadUnitTypeSection(file, section, race);
+				}
+				else
+				{
+					RollcallUnitTypeParser.ReadEquipmentSection(file, section, race);
+				}	
+			}
+		}
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RollcallUnitTypeParser.cs	Mon Jan 26 20:31:05 2009 +0000
@@ -0,0 +1,83 @@
+// This file (RollcallUnitTypeParser.cs) is a part of the IBBoard.WarFoundry.Plugin.Rollcall library and is copyright 2009 IBBoard.
+//
+// The file and the library/program it is in are licensed under the GNU LGPL license. Please see COPYING.LGPL for more information and the full license.
+
+using System;
+using IBBoard.IO;
+using IBBoard.Ini;
+using IBBoard.CustomMath;
+using IBBoard.WarFoundry.API.Objects;
+
+namespace IBBoard.WarFoundry.Plugin.Rollcall
+{
+	/// <summary>
+	/// A helper class to parse INI sections as Units
+	/// </summary>
+	public class RollcallUnitTypeParser
+	{
+		public static UnitType ReadUnitTypeSectionFromID(IniFile file, String sectionID, Race race)
+		{
+			return ReadUnitTypeSection(file, "Unit"+sectionID, race);
+		}
+
+		public static UnitType ReadUnitTypeSection(IniFile file, String sectionName, Race race)
+		{
+			return ReadUnitTypeSection(file, file[sectionName], race);
+		}
+		
+		public static UnitType ReadUnitTypeSection(IniFile file, IniSection section, Race race)
+		{
+			UnitType unitType = race.GetUnitType(section.Name);
+			
+			if (unitType == null)
+			{
+				string unitID = RollcallUnitTypeParser.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;
+				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);
+				}
+				
+				race.AddUnitType(unitType);
+			}
+
+			return unitType;
+		}
+
+		private static 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;
+		}
+		
+		public static void ReadEquipmentSection(IniFile file, IniSection section, Race race)
+		{
+		}
+	}
+}