changeset 2:9b17b3e35b9d

Fixes #15 - Make Rollcall plugin use IBBoard.Ini * Reference IBBoard.Ini * Update Rollcall WarFoundry factory to use the IBBoard.Ini library for file parsing
author IBBoard <dev@ibboard.co.uk>
date Sun, 18 Jan 2009 15:39:18 +0000
parents d3e0e2914400
children 2a21138f50ed
files IBBoard.WarFoundry.Plugin.Rollcall.mdp RollcallFactory.cs
diffstat 2 files changed, 89 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/IBBoard.WarFoundry.Plugin.Rollcall.mdp	Fri Dec 19 16:46:04 2008 +0000
+++ b/IBBoard.WarFoundry.Plugin.Rollcall.mdp	Sun Jan 18 15:39:18 2009 +0000
@@ -21,5 +21,7 @@
     <ProjectReference type="Gac" localcopy="True" refto="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
     <ProjectReference type="Project" localcopy="True" refto="IBBoard.WarFoundry.API" />
     <ProjectReference type="Project" localcopy="True" refto="IBBoard" />
+    <ProjectReference type="Project" localcopy="True" refto="IBBoard.Ini" />
   </References>
+  <GtkDesignInfo />
 </Project>
\ No newline at end of file
--- a/RollcallFactory.cs	Fri Dec 19 16:46:04 2008 +0000
+++ b/RollcallFactory.cs	Sun Jan 18 15:39:18 2009 +0000
@@ -21,6 +21,8 @@
 using System;
 using System.Collections.Generic;
 using System.IO;
+using IBBoard.Ini;
+using IBBoard.IO;
 using IBBoard.Logging;
 using IBBoard.WarFoundry.API.Factories;
 using IBBoard.WarFoundry.API.Objects;
@@ -28,7 +30,7 @@
 namespace IBBoard.WarFoundry.Plugin.Rollcall
 {
 	public class RollcallFactory : AbstractNonNativeFileExtensionWarFoundryFactory
-	{
+	{		
 		public static RollcallFactory CreateFactory()
 		{
 			return new RollcallFactory();
@@ -70,7 +72,7 @@
 			{
 				objects = new List<IWarFoundryObject>();
 				objects.Add(obj);
-				objects.Add(new GameSystem("Rollcall", "Rollcall armies"));
+				objects.Add(new GameSystem("Rollcall", "Rollcall armies", this));
 			}
 			
 			return objects;
@@ -83,78 +85,115 @@
 		
 		protected override Race CreateRaceFromFile(FileInfo file)
 		{
-			//TODO parse army file
-			StreamReader stream = file.OpenText();
+			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;
-			
-			while (!stream.EndOfStream)
+			id = "Rollcall" + file["Army"]["BitCodeID"].Value;
+			name = file["Army"]["Name"].Value;
+			LogNotifier.Debug(GetType(), "Loading Rollcall race ID "+id);
+			return new Race(id, name, "Rollcall", this);
+		}
+		
+		private void ReadCategories(IniFile file, Race race)
+		{
+			IniSection section = file["Category"];
+
+			foreach (string key in section.Keys)
 			{
-				string line = stream.ReadLine();
-				
-				if (line.StartsWith("["))
-				{					
-					string sectionTitle = line.Trim().Substring(1, line.Length - 2);
-					string sectionTitleLower = sectionTitle.ToLower();
-					
-					if (sectionTitleLower.Equals("army"))
-					{
-						ReadRaceSection(stream, out id, out name);
-					}
-					else if (sectionTitleLower.Equals("category"))
-					{
-						ReadCategorySection(stream);
-					}
-					else if (sectionTitleLower.StartsWith("unit"))
-					{
-						ReadUnitSection(stream, sectionTitle);
-					}
-					else
-					{
-						ReadEquipmentSection(stream, sectionTitle);
-					}
-				}				
-				//else ignore the line
+				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));
+					race.AddCategory(new Category(key, values[0], 0, 0, minPercent, maxPercent, 0, -1, 0, 0, 0));
+				}
+				//Special cases (allies and aliases) need to be handled later					                              
 			}
-			
-			LogNotifier.Debug(GetType(), "Loaded race ID "+id);
-			return new Race(id, name, "Rollcall");
 		}
 
-		private void ReadRaceSection(StreamReader stream, out string id, out string name)
+		private void ReadUnitTypeAndEquipmentSections(IniFile file, Race race)
 		{
-			id = name = null;
-			
-			while (!stream.EndOfStream && stream.Peek() != '[')
+			foreach (IniSection section in file)
 			{
-				string line = stream.ReadLine();
+				string sectionName = section.Name;
 				
-				if (line.StartsWith("BitCodeID="))
+				if (sectionName == "Army" || sectionName == "Category")
 				{
-					id = line.Substring(10);
+					continue;
 				}
-				else if (line.StartsWith("Name="))
+				else if (sectionName.StartsWith("Unit"))
 				{
-					name = line.Substring(5);
+					ReadUnitTypeSection(file, section, race);
 				}
+				else
+				{
+					ReadEquipmentSection(file, section, race);
+				}	
 			}
 		}
-		
-		private void ReadCategorySection(StreamReader stream)
+
+		private UnitType ReadUnitTypeSectionFromID(IniFile file, String sectionID, Race race)
 		{
+			return ReadUnitTypeSection(file, file["Unit"+sectionID], race);
+		}
+
+		private UnitType ReadUnitTypeSection(IniFile file, String sectionName, Race race)
+		{
+			return ReadUnitTypeSection(file, file[sectionName], race);
 		}
 		
-		private void ReadUnitSection(StreamReader stream, string unitSectionTitle)
+		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;
+				string catID = section["Category"].Value;
+				string[] cats = new string[]{ catID };
+				//Load other values
+				unitType = new UnitType(unitID, name, catID, cats, 0, 1, 1, 1, 10, 10, null, null, race);
+				//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(StreamReader stream, string equipmentSectionTitle)
+		private void ReadEquipmentSection(IniFile file, IniSection section, Race race)
 		{
 		}
 		
 		protected override GameSystem CreateGameSystemFromFile (FileInfo file)
 		{
 			//Return null because there is no such thing
-			//TODO Check whether we should exception
+			//TODO Determine whether we should exception
 			return null;
 		}
 	}