view RollcallRaceParser.cs @ 13:9aecf6628eb0

Re #121: Migrate to AGPL license * Update all Rollcall plugin files to AGPL license * Include AGPL license and remove GPL/LGPL documents * Fix copyright dates where they're known
author IBBoard <dev@ibboard.co.uk>
date Sat, 15 Aug 2009 10:44:40 +0000
parents 9d0d40324494
children
line wrap: on
line source

// This file (RollcallRaceParser.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.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);
				}
			}
		}
	}
}