view API/Factories/Xml/WarFoundryXmlArmyParser.cs @ 496:00d6cf940c3c

Re #420: Saved army does not save "contained" structure * Add loading of nesting from .army files
author IBBoard <dev@ibboard.co.uk>
date Sat, 01 Sep 2012 15:28:26 +0100
parents 3c4a6403a88c
children
line wrap: on
line source

//  This file (WarFoundryXmlArmyParser.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 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 System.Collections.Generic;
using System.Xml;
using IBBoard.IO;
using IBBoard.Xml;
using ICSharpCode.SharpZipLib.Zip;
using IBBoard.WarFoundry.API.Objects;

namespace IBBoard.WarFoundry.API.Factories.Xml
{
	public class WarFoundryXmlArmyParser
	{
		private ZipFile file;
		private XmlElement elem;
		private Army army;
		private Dictionary<String, Unit> units;
		
		public WarFoundryXmlArmyParser(ZipFile file, XmlElement elem)
		{
			this.file = file;
			this.elem = elem;
		}

		public Army GetArmy()
		{
			if (army == null)
			{
				ParseArmy();
			}

			return army;
		}

		private void ParseArmy()
		{
			string name = elem.GetAttribute("name");
			string systemID = elem.GetAttribute("system");
			GameSystem system = WarFoundryLoader.GetDefault().GetGameSystem(systemID);
			
			if (system == null)
			{
				throw new RequiredDataMissingException(file.Name, "Game System", systemID);
			}
			
			string raceID = elem.GetAttribute("race");
			Race race = WarFoundryLoader.GetDefault().GetRace(system, raceID);
			
			if (race == null)
			{
				throw new RequiredDataMissingException(file.Name, "Race", raceID);
			}
			
			int points = XmlTools.GetIntValueFromAttribute(elem, "maxPoints");			
			army = new Army(race, name, points, file);
			LoadUnits();
		}

		private void LoadUnits()
		{
			units = new Dictionary<string, Unit>();
			Dictionary<string, ICollection<string>> containedUnits = new Dictionary<string, ICollection<string>>();

			foreach (XmlElement unitElem in WarFoundryXmlFactoryUtils.SelectNodes(elem, "/army:army/army:units/army:unit"))
			{
				string id = unitElem.GetAttribute("id");

				if (!units.ContainsKey(id))
				{
					string unitTypeId = unitElem.GetAttribute("unitType");
					UnitType unitType = army.Race.GetUnitType(unitTypeId);

					if (unitType == null)
					{
						throw new RequiredDataMissingException(file.Name, "Unit Type", unitTypeId);
					}
					
					string name = unitElem.GetAttribute("unitName");
					int size = XmlTools.GetIntValueFromAttribute(unitElem, "size");
					
					string catID = unitElem.GetAttribute("category");
					Category cat = army.Race.GetCategory(catID);
					
					if (cat == null)
					{
						cat = unitType.MainCategory;
					}

					XmlNodeList containedElems = WarFoundryXmlFactoryUtils.SelectNodes(unitElem, "army:contained/army:containedUnit");

					if (containedElems.Count > 0)
					{
						containedUnits[id] = GetContainedIDs(containedElems);
					}


					Unit unit = new Unit(id, name, size, unitType, army.GetCategory(cat));
					army.AddUnit(unit, cat);
					units.Add(id, unit);

					LoadUnitEquipment(unitElem, unit);
				}
				else
				{
					throw new InvalidFileException("Duplicate unit ID found in army file: " + id);
				}
			}

			AddParenting(containedUnits);
		}

		private ICollection<string> GetContainedIDs(XmlNodeList containedElems)
		{
			ICollection<string> ids = new List<string>();

			foreach (XmlElement elem in containedElems)
			{
				ids.Add(elem.GetAttribute("containedID"));
			}

			return ids;
		}

		private void LoadUnitEquipment(XmlElement unitElem, Unit unit)
		{
			foreach (XmlElement elem in WarFoundryXmlFactoryUtils.SelectNodes(unitElem, "army:equipment/army:equipItem"))
			{
				string equipID = elem.GetAttribute("id");
				UnitEquipmentItem item = unit.UnitType.GetEquipmentItem(equipID);
	
				if (item == null)
				{
					throw new RequiredDataMissingException(file.Name, "Equipment Item", equipID);
				}

				double amount = XmlTools.GetDoubleValueFromAttribute(elem, "amount");
				string equipTypeString = elem.GetAttribute("amountType");

				if (equipTypeString == "ratio")
				{
					unit.SetEquipmentRatio(item, amount);
				}
				else
				{
					//amount should be a whole number, so do type-cast rounding
					unit.SetEquipmentAmount(item, (int)amount);
				}
			}
		}

		private void AddParenting(Dictionary<string, ICollection<string>> containedUnits)
		{
			foreach (KeyValuePair<string, ICollection<string>> parenting in containedUnits)
			{
				Unit parent = units[parenting.Key];

				foreach (string childID in parenting.Value)
				{
					parent.AddContainedUnit(units[childID]);
				}
			}
		}
	}
}