view RollcallFactory.cs @ 3:2a21138f50ed

Re #9 - Make WarFoundry API use smaller methods * Use new UnitType constructor * Add units to race * Cascade up unit loading function hierarchy instead of jumping a step
author IBBoard <dev@ibboard.co.uk>
date Sun, 18 Jan 2009 20:54:26 +0000
parents 9b17b3e35b9d
children c82b194801dc
line wrap: on
line source

// RollcallFactory.cs
//
//  Copyright (C) 2008 IBBoard
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
//
//

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
	{		
		public static RollcallFactory CreateFactory()
		{
			return new RollcallFactory();
		}
		
		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 = null;
			IWarFoundryObject obj = null;
			
			if (IsRaceFile(file))
			{
				obj = CreateRaceFromFile(file);
			}
			else if (IsArmyFile(file))
			{
				obj = CreateArmyFromFile(file);
			}
			
			if (obj != null)
			{
				objects = new List<IWarFoundryObject>();
				objects.Add(obj);
				objects.Add(new GameSystem("Rollcall", "Rollcall armies", this));
			}
			
			return objects;
		}

		protected override Army CreateArmyFromFile (FileInfo file)
		{			
			return null;
		}
		
		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);
			return new Race(id, name, "Rollcall", this);
		}
		
		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));
					race.AddCategory(new Category(key, values[0], 0, 0, minPercent, maxPercent, 0, -1, 0, 0, 0));
				}
				//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)
		{
			//Return null because there is no such thing
			//TODO Determine whether we should exception
			return null;
		}
	}
}