Mercurial > repos > IBDev-IBBoard.WarFoundry.API
view api/Factories/Xml/WarFoundryXmlArmyParser.cs @ 128:45a9452579a2
Fixes #50: Complete core loading of WarFoundry XML files
* Add exceptions on invalid data - completes WarFoundry 0.1 loading needs
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Wed, 26 Aug 2009 18:37:55 +0000 |
parents | 52e8c3cdde10 |
children | df61d26c23fb |
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.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, "gameSystem", 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>(); 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, "unitType", unitTypeId); } string name = unitElem.GetAttribute("unitName"); int size = XmlTools.GetIntValueFromAttribute(unitElem, "size"); Unit unit = new Unit(id, name, size, unitType, army.GetCategory(unitType.MainCategory)); army.AddUnit(unit); units.Add(id, unit); LoadUnitEquipment(unitElem, unit); } } } private void LoadUnitEquipment(XmlElement unitElem, Unit unit) { foreach (XmlElement elem in WarFoundryXmlFactoryUtils.SelectNodes(unitElem, "army:equipment/army:equipItem")) { string equipID = elem.GetAttribute("id"); bool isCustom = XmlTools.GetBoolValueFromAttribute(elem, "isCustomEquipment"); if (!isCustom) { UnitEquipmentItem item = unit.UnitType.GetEquipmentItem(equipID); if (item == null) { throw new RequiredDataMissingException(file.Name, "equipItem ID", 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); } } else { //TODO: Load custom equipment } } } } }