view RollcallUnitTypeParser.cs @ 14:f7582a3c8bdf default tip

* Fix tooling version - we're 2.0 compatible, not 3.5 no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Wed, 26 Jan 2011 20:04:35 +0000
parents 9aecf6628eb0
children
line wrap: on
line source

// This file (RollcallUnitTypeParser.cs) is a part of the IBBoard.WarFoundry.Plugin.Rollcall project and is copyright 2008, 2009 IBBoard.
//
// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING 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)
		{
		}
	}
}