view RollcallUnitTypeParser.cs @ 6:0509ed2e686a

Re #16 - File loading * Refactor race detail parsing and unit type parsing in to their own classes Re #17 - Unit detail loading * Add a couple of extra attributes to those loaded
author IBBoard <dev@ibboard.co.uk>
date Mon, 26 Jan 2009 20:31:05 +0000
parents
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)
		{
		}
	}
}