view RollcallUnitTypeParser.cs @ 9:5f0259e69a7f

Remove call to add category to unit type because of changes in #32 no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 15 Mar 2009 16:08:43 +0000
parents 35bc86f8c283
children 9d0d40324494
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.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)
		{
		}
	}
}