view RollcallUnitTypeParser.cs @ 7:f7decc0332c5

Re #30 - Migrate WarFoundry to .csproj * Migrate Rollcall plugin to .csproj and remove .mdp
author IBBoard <dev@ibboard.co.uk>
date Tue, 17 Feb 2009 16:36:15 +0000
parents 0509ed2e686a
children 35bc86f8c283
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["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)
		{
		}
	}
}