view RollcallFactory.cs @ 5:8c34e01a11da

Re #1 - LGPL license all code * Add LGPL header to all files * Add COPYING.LGPL and COPYING.GPL with content of license
author IBBoard <dev@ibboard.co.uk>
date Sun, 25 Jan 2009 14:51:51 +0000
parents c82b194801dc
children 0509ed2e686a
line wrap: on
line source

// This file (RollcallFactory.cs) is a part of the IBBoard.WarFoundry.Plugin.Rollcall project and is copyright 2009 IBBoard.
//
// The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.

using System;
using System.Collections.Generic;
using System.IO;
using IBBoard.Ini;
using IBBoard.IO;
using IBBoard.Logging;
using IBBoard.WarFoundry.API.Factories;
using IBBoard.WarFoundry.API.Objects;

namespace IBBoard.WarFoundry.Plugin.Rollcall
{
	public class RollcallFactory : AbstractNonNativeFileExtensionWarFoundryFactory
	{
		private GameSystem rollcallSystem;
		private bool addedSystem = false;
		
		public static RollcallFactory CreateFactory()
		{
			return new RollcallFactory();
		}

		private RollcallFactory()
		{
			rollcallSystem = new GameSystem("Rollcall", "Rollcall armies", this);
		}
		
		public override string NonNativeDataType
		{
			get { return "Rollcall ADF files"; }
		}

		
		protected override string ArmyFileExtension {
			get { return null; }
		}
		
		protected override string GameSystemFileExtension {
			get { return null; }
		}

		protected override string RaceFileExtension {
			get { return ".adf"; }
		}
		
		protected override ICollection<IWarFoundryObject> DoCreateObjectsFromFile(FileInfo file)
		{			
			ICollection<IWarFoundryObject> objects = new List<IWarFoundryObject>();
			
			if (CheckCanHandleFileAsRace(file))
			{
				objects.Add(CreateRaceFromFile(file));
			}
			else if (CheckCanHandleFileAsArmy(file))
			{
				objects.Add(CreateArmyFromFile(file));
			}
			
			if (!addedSystem && objects.Count > 0)
			{
				objects.Add(rollcallSystem);
				addedSystem = true;
			}
			
			return objects;
		}

		protected override Army CreateArmyFromFile (FileInfo file)
		{			
			throw new NotImplementedException();
		}
		
		protected override Race CreateRaceFromFile(FileInfo file)
		{
			IniFile rollcallFile = IniFileReader.ReadFile(file);			
			Race race = ReadRaceDetails(rollcallFile);
			ReadCategories(rollcallFile, race);
			ReadUnitTypeAndEquipmentSections(rollcallFile, race);
			return race;
		}

		private Race ReadRaceDetails(IniFile file)
		{
			string id = null, name = null;
			id = "Rollcall" + file["Army"]["BitCodeID"].Value;
			name = file["Army"]["Name"].Value;
			LogNotifier.Debug(GetType(), "Loading Rollcall race ID "+id);
			Race race = new Race(id, name, "Rollcall", this);
			race.GameSystem = rollcallSystem;
			return race;
		}
		
		private void ReadCategories(IniFile file, Race race)
		{
			IniSection section = file["Category"];

			foreach (string key in section.Keys)
			{
				string valueString = section[key].Value;
				string[] values = valueString.Split(',');

				if (values.Length == 3)
				{
					LogNotifier.Debug(GetType(), "Loading category " + values[0]);
					int minPercent = 0;
					int.TryParse(values[1], out minPercent);
					int maxPercent = 100;
					int.TryParse(values[2], out maxPercent);
					maxPercent = Math.Max(0, Math.Min(100, Math.Max(minPercent, maxPercent)));
					minPercent = Math.Max(0, Math.Min(100, minPercent));
					Category category = new Category(key, values[0]);
					category.MaximumPercentage = maxPercent;
					category.MinimumPercentage = minPercent;
					race.AddCategory(category);
					
				}
				//Special cases (allies and aliases) need to be handled later					                              
			}
		}

		private void ReadUnitTypeAndEquipmentSections(IniFile file, Race race)
		{
			foreach (IniSection section in file)
			{
				string sectionName = section.Name;
				
				if (sectionName == "Army" || sectionName == "Category")
				{
					continue;
				}
				else if (sectionName.StartsWith("Unit"))
				{
					ReadUnitTypeSection(file, section, race);
				}
				else
				{
					ReadEquipmentSection(file, section, race);
				}	
			}
		}

		private UnitType ReadUnitTypeSectionFromID(IniFile file, String sectionID, Race race)
		{
			return ReadUnitTypeSection(file, "Unit"+sectionID, race);
		}

		private UnitType ReadUnitTypeSection(IniFile file, String sectionName, Race race)
		{
			return ReadUnitTypeSection(file, file[sectionName], race);
		}
		
		private UnitType ReadUnitTypeSection(IniFile file, IniSection section, Race race)
		{
			UnitType unitType = race.GetUnitType(section.Name);
			
			if (unitType == null)
			{
				string unitID = 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;
				//Load other values
				race.AddUnitType(unitType);
			}

			return unitType;
		}

		private 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;
		}
		
		private void ReadEquipmentSection(IniFile file, IniSection section, Race race)
		{
		}
		
		protected override GameSystem CreateGameSystemFromFile (FileInfo file)
		{
			throw new InvalidDataException("No such file format (Rollcall GameSystem)");
		}
	}
}