view RollcallUnitTypeParser.cs @ 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 0509ed2e686a
children 5f0259e69a7f
line wrap: on
line source

// 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.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.GetLineValue("Category"));
				
				if (mainCat == null)
				{
					throw new InvalidFileException("Attribute 'Category' for "+unitID+" did not match a category");
				}
				
				unitType.AddCategory(mainCat);
				unitType.MainCategory = mainCat;
				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);
			}

			return unitType;
		}

		private static string GetUnitID(IniSection section)
		{
			string sectionName = section.Name;

			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");
			}

			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)
		{
		}
	}
}