Mercurial > repos > snowblizz-super-API-ideas
view api/Factories/Xml/WarFoundryXmlRaceFactory.cs @ 52:64ef178c18aa
Re #10 - Refactor for readability
* Break WarFoundryXMLFactory out in to GameSystem, Race and Army factories
* Create factory utils classes with methods from WarFoundryXMLFactory for getting node lists etc
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 30 Mar 2009 19:44:03 +0000 |
parents | |
children | 1b35eed503ef |
line wrap: on
line source
// This file (WarFoundryXmlRaceFactory.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 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 System.Xml; using IBBoard.Xml; using IBBoard.IO; using IBBoard.Logging; using ICSharpCode.SharpZipLib.Zip; using IBBoard.WarFoundry.API.Objects; namespace IBBoard.WarFoundry.API.Factories.Xml { /// <summary> /// A sub-factory for loading WarFoundry Race XML files /// </summary> public class WarFoundryXmlRaceFactory : AbstractStagedLoadedSubFactory { private Dictionary<Race, XmlDocument> extraData = new Dictionary<Race, XmlDocument>(); public WarFoundryXmlRaceFactory(WarFoundryXmlFactory factory) : base (factory) { } private void StoreExtraData(Race wfObject, XmlElement elem) { extraData[wfObject] = elem.OwnerDocument; } private XmlDocument GetExtraData(Race obj) { XmlDocument extra = null; extraData.TryGetValue(obj, out extra); return extra; } public Race CreateRaceFromElement(ZipFile file, XmlElement elem) { string id = elem.GetAttribute("id"); string subid = elem.GetAttribute("subid"); string systemID = elem.GetAttribute("system"); string name = elem.GetAttribute("name"); Race race = new Race(id, subid, name, systemID, mainFactory); StoreExtraData(race, elem); return race; } public void CompleteLoading(Race race) { if (!WarFoundryXmlFactoryUtils.CanCompleteLoading(race)) { return; } race.SetAsLoading(); XmlDocument extraData = GetExtraData(race); foreach (XmlElement node in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:units/race:unit")) { UnitType type = CreateUnitTypeFromElement(node, race, race.GameSystem); race.AddUnitType(type); } foreach (XmlElement node in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:categories/cat:cat")) { race.AddCategory(CreateCategoryFromElement(node)); } foreach (XmlElement node in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:equipment/cat:equipmentItem")) { EquipmentItem item = CreateEquipmentItemFromElement(node, race); race.AddEquipmentItem(item); } foreach (XmlElement node in WarFoundryXmlFactoryUtils.SelectNodes(extraData, "/race:race/race:abilities/cat:ability")) { Ability ability = CreateAbilityFromElement(node, race); race.AddAbility(ability); } race.SetAsFullyLoaded(); LogNotifier.DebugFormat(GetType(), "Completed loading of Race with ID {0}", race.ID); } private UnitType CreateUnitTypeFromElement(XmlElement elem, Race parentRace, GameSystem system) { string id = elem.GetAttribute("id"); string name = elem.GetAttribute("typeName"); UnitType type = new UnitType(id, name, parentRace); type.MaxNumber = XmlTools.GetIntValueFromAttribute(elem, "maxNum"); type.MinNumber = XmlTools.GetIntValueFromAttribute(elem, "minNum"); type.MaxSize = XmlTools.GetIntValueFromAttribute(elem, "maxSize"); type.MinSize = XmlTools.GetIntValueFromAttribute(elem, "minSize"); type.BaseSize = XmlTools.GetIntValueFromAttribute(elem, "baseSize"); type.CostPerTrooper = XmlTools.GetIntValueFromAttribute(elem, "points"); type.BaseUnitCost = XmlTools.GetIntValueFromAttribute(elem, "unitPoints"); string mainCatID = elem.GetAttribute("cat"); Category cat = parentRace.GetCategory(mainCatID); if (cat == null) { throw new InvalidDataException(String.Format("Attribute 'cat' of UnitType {0} (value: {1}) did not reference a valid category", id, mainCatID)); } type.MainCategory = cat; XmlElement statsElement = WarFoundryXmlFactoryUtils.SelectSingleElement(elem, "/race:race/race:units/race:unit/race:stats"); type.UnitStats = ParseUnitStats(statsElement, system); //TODO: Add unit requirements LogNotifier.Debug(GetType(), "Loaded "+type.Name); return type; } private Stats ParseUnitStats(XmlElement elem, GameSystem system) { List<Stat> statsList = new List<Stat>(); String statsID = elem.GetAttribute("statSet"); SystemStats statsSet; if (statsID == "") { statsSet = system.StandardSystemStats; } else { statsSet = system.GetSystemStatsForID(statsID); } Stats stats = new Stats(statsSet); foreach (XmlElement stat in elem.ChildNodes) { String statID = stat.GetAttribute("name"); StatSlot slot = statsSet[statID]; if (slot!=null) { statsList.Add(new Stat(slot, stat.InnerText)); } else { throw new InvalidFileException("The stat "+statID+" was not found in stats set "+statsID); } } stats.SetStats(statsList); return stats; } private EquipmentItem CreateEquipmentItemFromElement(XmlElement elem, Race race) { string id = elem.GetAttribute("id"); string name = elem.GetAttribute("name"); double cost = 0, min = 0, max = 0; ArmourType armourType; try { cost = XmlTools.GetDoubleValueFromAttribute(elem, "cost"); } catch(FormatException ex) { throw new InvalidFileException("Attribute 'cost' of equipment item "+id+" was not a valid number", ex); } try { armourType = (ArmourType)Enum.Parse(typeof(ArmourType), elem.GetAttribute("armourType")); } catch(ArgumentException ex) { throw new InvalidFileException("Attribute 'armourType' of equipment "+id+" was not a valid value from the enumeration", ex); } //TODO: Parse equipment stats if there are any return new EquipmentItem(id, name, cost, min, max, armourType, race); } private Ability CreateAbilityFromElement(XmlElement elem, Race race) { string id = elem.GetAttribute("id"); string name = elem.GetAttribute("name"); Ability ability = new Ability(id, name); XmlNode node = elem.SelectSingleNode("description", WarFoundryXmlFactoryUtils.GetNamespaceManager()); ability.Description = (node == null) ? "" : node.InnerText; return ability; } } }