# HG changeset patch # User IBBoard # Date 1232295843 0 # Node ID 5a1df00b035966603177386cac64e5a031500d1c # Parent 607c3232d6896b795e8de0a53381b768ec45f39e Re #9 - Make WarFoundry API load files in small methods * Add "add unit type" and "add equipment" methods to Race * Deprecate old "set unit types" and "set equipment" methods on Race * Update WarFoundryXmlFactory to use new methods * Create DuplicateItemException for later use diff -r 607c3232d689 -r 5a1df00b0359 api/Factories/Xml/WarFoundryXmlFactory.cs --- a/api/Factories/Xml/WarFoundryXmlFactory.cs Sun Jan 04 20:43:36 2009 +0000 +++ b/api/Factories/Xml/WarFoundryXmlFactory.cs Sun Jan 18 16:24:03 2009 +0000 @@ -238,13 +238,11 @@ XmlNode elem = GetExtraData(race); XmlNode colNode = elem.FirstChild; - - Dictionary unitTypes = new Dictionary(); - + foreach (XmlElement node in colNode.ChildNodes) { UnitType type = CreateUnitTypeFromElement(node, race, race.GameSystem); - unitTypes.Add(type.ID, type); + race.AddUnitType(type); } colNode = colNode.NextSibling; @@ -258,23 +256,18 @@ colNode = colNode.NextSibling; } - - Dictionary raceEquipment = new Dictionary(); - + if (colNode!=null && colNode.Name == WarFoundryXmlElementName.RACE_EQUIPMENT_ITEMS_ELEMENT.Value) { foreach (XmlElement node in colNode.ChildNodes) { EquipmentItem item = CreateEquipmentItemFromElement(node, race); - raceEquipment.Add(item.ID, item); + race.AddEquipmentItem(item); } } Dictionary raceAbilities = new Dictionary(); //TODO: Load abilities - - race.SetUnitTypes(unitTypes); - race.SetEquipmentItems(raceEquipment); race.SetAbilities(raceAbilities); race.SetAsFullyLoaded(); LogNotifier.DebugFormat(GetType(), "Completed loading of Race with ID {0}", race.Name); diff -r 607c3232d689 -r 5a1df00b0359 api/Objects/DuplicateItemException.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/api/Objects/DuplicateItemException.cs Sun Jan 18 16:24:03 2009 +0000 @@ -0,0 +1,15 @@ +// This file (DuplicateItemException.cs) is a part of [PROJECT NAME] and is copyright 2009 IBBoard. +// +// The file and the library/program it is in are licensed under the GNU LGPL license. Please see COPYING.LGPL for more information and the full license. + +using System; + +namespace IBBoard.WarFoundry +{ + public class DuplicateItemException + { + public DuplicateItemException() + { + } + } +} diff -r 607c3232d689 -r 5a1df00b0359 api/Objects/Race.cs --- a/api/Objects/Race.cs Sun Jan 04 20:43:36 2009 +0000 +++ b/api/Objects/Race.cs Sun Jan 18 16:24:03 2009 +0000 @@ -130,10 +130,20 @@ { return categories.Count > 0; } - + + public void AddEquipmentItem(EquipmentItem item) + { + //TODO: Throw DuplicateItemException + equipment.Add(item.ID, item); + } + + [Obsolete("Use AddEquipmentItem method instead")] public void SetEquipmentItems(Dictionary items) { - equipment = items; + foreach (EquipmentItem item in items.Values) + { + AddEquipmentItem(item); + } } public EquipmentItem GetEquipmentItem(string id) @@ -153,20 +163,25 @@ return items; } - + + public void AddUnitType(UnitType type) + { + unitTypes.Add(type.ID, type); + CacheUnitType(type); + } + + [Obsolete("Use AddUnitType method instead")] public void SetUnitTypes(Dictionary types) { - unitTypes = types; - unitTypesByCat = null; + foreach (UnitType type in types.Values) + { + AddUnitType(type); + } } public UnitType[] GetUnitTypes(Category cat) - { - if (unitTypesByCat==null) - { - BuildUnitTypesByCategoryCache(); - } - + { + BuildUnitTypesByCategoryCache(); Dictionary unitTypesDictionary; unitTypesByCat.TryGetValue(cat, out unitTypesDictionary); UnitType[] unitTypesArray; @@ -182,34 +197,49 @@ return unitTypesArray; } + + private void CacheUnitType(UnitType unit) + { + BuildUnitTypesByCategoryCache(); + Dictionary catUnitTypes; + unitTypesByCat.TryGetValue(unit.MainCategory, out catUnitTypes); + + if (catUnitTypes == null) + { + throw new InvalidFileException(String.Format("Unit type {0} with name {1} is a unit of an undefined category", unit.ID, unit.Name)); + } + + catUnitTypes.Add(unit.ID, unit); + } private void BuildUnitTypesByCategoryCache() { + if (unitTypesByCat == null) + { + DoBuildUnitTypesByCategoryCache(); + } + } + + private void DoBuildUnitTypesByCategoryCache() + { unitTypesByCat = new Dictionary>(); - foreach (Category category in Categories) - { - unitTypesByCat.Add(category, new Dictionary()); - } - - Dictionary catUnitTypes; - - foreach (UnitType unit in unitTypes.Values) - { - unitTypesByCat.TryGetValue(unit.MainCategory, out catUnitTypes); - - if (catUnitTypes == null) - { - throw new InvalidFileException(String.Format("Unit type {0} with name {1} is a unit of an undefined category", unit.ID, unit.Name)); - } - - catUnitTypes.Add(unit.ID, unit); - } + foreach (Category category in Categories) + { + unitTypesByCat.Add(category, new Dictionary()); + } + + foreach (UnitType unit in unitTypes.Values) + { + CacheUnitType(unit); + } } public UnitType GetUnitType(string id) - { - return (UnitType)unitTypes[id]; + { + UnitType type = null; + unitTypes.TryGetValue(id, out type); + return type; } public List GetAbilityList()