view RollcallRaceParser.cs @ 10:395f2bf0549e

Re #17 - Complete core Rollcall loading * Resolve warning about deprecated constructor
author IBBoard <dev@ibboard.co.uk>
date Thu, 09 Apr 2009 15:25:31 +0000
parents 35bc86f8c283
children 9d0d40324494
line wrap: on
line source

// This file (RollcallRaceParser.cs) is a part of 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.CustomMath;
using IBBoard.Ini;
using IBBoard.IO;
using IBBoard.Logging;
using IBBoard.WarFoundry.API.Objects;

namespace IBBoard.WarFoundry.Plugin.Rollcall
{
	/// <summary>
	/// A helper class to construct a base Race object from an INI file
	/// </summary>
	public class RollcallRaceParser
	{
		public static Race ReadRaceDetails(IniFile file)
		{
			string id = null, name = null;
			id = file.GetSectionLineValue("Army", "BitCodeID");
			
			if (id == null)
			{
				throw new InvalidFileException("BitCodeID field of Rollcall race did not exist");
			}
			
			id = "Rollcall" + id;
			name = file.GetSectionLineValue("Army", "Name");
			
			if (name == null)
			{
				throw new InvalidFileException("Name field of Rollcall race did not exist");
			}
			
			LogNotifier.Debug(typeof(RollcallRaceParser), "Loading Rollcall race ID "+id);
			Race race = new Race(id, name, RollcallFactory.GetFactory().RollcallSystem, RollcallFactory.GetFactory());
			race.GameSystem = RollcallFactory.GetFactory().RollcallSystem;
			return race;
		}
		
		public static void ReadCategories(IniFile file, Race race)
		{
			ReadCategories(file["Category"], race);
		}
		
		private static void ReadCategories(IniSection categorySection, Race race)
		{			
			foreach (string key in categorySection.Keys)
			{
				string valueString = categorySection.GetLineValue(key);
				string[] values = valueString.Split(',');

				if (values.Length == 3)
				{
					LogNotifier.Debug(typeof(RollcallRaceParser), "Loading category " + values[0]);
					Category category = new Category(key, values[0]);
					int minPercent = NumberParser.ParseAsInt(values[1], 0);
					int maxPercent = NumberParser.ParseAsInt(values[2], 100);
					maxPercent = Math.Max(0, Math.Min(100, Math.Max(minPercent, maxPercent)));
					minPercent = Math.Max(0, Math.Min(100, minPercent));
					category.MaximumPercentage = maxPercent;
					category.MinimumPercentage = minPercent;
					race.AddCategory(category);
					
				}
				else
				{
					LogNotifier.Warn(typeof(RollcallRaceParser), "Ignored non-standard category: "+key);
				}
				//Special cases (allies and aliases) need to be handled later					                              
			}
		}

		public static void ReadUnitTypeAndEquipmentSections(IniFile file, Race race)
		{
			foreach (IniSection section in file)
			{
				try
				{
					string sectionName = section.Name;
					
					if (sectionName == "Army" || sectionName == "Category")
					{
						continue;
					}
					else if (sectionName.StartsWith("Unit"))
					{
						RollcallUnitTypeParser.ReadUnitTypeSection(file, section, race);
					}
					else
					{
						RollcallUnitTypeParser.ReadEquipmentSection(file, section, race);
					}	
				}
				catch (InvalidFileException ex)
				{
					throw new InvalidFileException("Invalid file exception while loading data", ex);
				}
			}
		}
	}
}