Mercurial > repos > IBBoard.WarFoundry.API
changeset 50:bb6b993b98bf
Re #10 - Refactor for readability
* Re-order methods so that they are in the correct order to read the code from top to bottom
* Reduce visibility of methods where possible
* Make use of XMLTools class
* Refactor "can complete loading" checks in to single method
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 28 Mar 2009 20:51:06 +0000 |
parents | 9d31d063b194 |
children | b271a2252758 |
files | api/Factories/Xml/WarFoundryXmlFactory.cs |
diffstat | 1 files changed, 160 insertions(+), 220 deletions(-) [+] |
line wrap: on
line diff
--- a/api/Factories/Xml/WarFoundryXmlFactory.cs Sat Mar 28 16:45:24 2009 +0000 +++ b/api/Factories/Xml/WarFoundryXmlFactory.cs Sat Mar 28 20:51:06 2009 +0000 @@ -40,7 +40,7 @@ return factory; } - protected WarFoundryXmlFactory() : base() + private WarFoundryXmlFactory() : base() { //Hide constructor } @@ -60,7 +60,18 @@ return file.FindEntry("data.racex", true) > -1; } - protected XmlElement GetRootElementFromStream(Stream stream, WarFoundryXmlElementName elementName) + protected override Stream GetArmyDataStream(ZipFile file) + { + return file.GetInputStream(file.FindEntry("data.armyx", true)); + } + + protected override Army CreateArmyFromStream (ZipFile file, Stream dataStream) + { + XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.ARMY_ELEMENT); + return CreateArmyFromElement(file, elem); + } + + private XmlElement GetRootElementFromStream(Stream stream, WarFoundryXmlElementName elementName) { XmlDocument doc = CreateXmlDocumentFromStream(stream); XmlElement elem = (XmlElement)doc.LastChild; @@ -73,102 +84,22 @@ return elem; } - protected override Stream GetArmyDataStream(ZipFile file) - { - return file.GetInputStream(file.FindEntry("data.armyx", true)); - } - - protected override Army CreateArmyFromStream (ZipFile file, Stream dataStream) - { - XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.ARMY_ELEMENT); - return CreateArmyFromElement(file, elem); - } - - private Army CreateArmyFromElement(ZipFile file, XmlElement elem) - { - string name = elem.GetAttribute("name"); - string systemID = elem.GetAttribute("gameSystem"); - GameSystem system = WarFoundryLoader.GetDefault().GetGameSystem(systemID); - string raceID = elem.GetAttribute("race"); - Race race = WarFoundryLoader.GetDefault().GetRace(system, raceID); - int points = GetIntValueFromAttribute(elem, "maxPoints"); - Army army = new Army(race, name, points, file); - //TODO: Complete loading of army - return army; - } - - private void StoreExtraData(WarFoundryStagedLoadingObject wfObject, XmlElement elem) - { - extraData[wfObject] = elem.OwnerDocument; - } - - public XmlDocument GetExtraData(IWarFoundryObject obj) - { - XmlDocument extra = null; - extraData.TryGetValue(obj, out extra); - return extra; - } - - protected override Stream GetGameSystemDataStream (ZipFile file) + private XmlDocument CreateXmlDocumentFromStream(Stream stream) { - return file.GetInputStream(file.FindEntry("data.systemx", true)); - } - - protected override GameSystem CreateGameSystemFromStream (ZipFile file, Stream dataStream) - { - XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.SYSTEM_ELEMENT); - LogNotifier.Debug(GetType(), "Create GameSystem"); - return CreateSystemFromElement(file, elem); - } - - private GameSystem CreateSystemFromElement(ZipFile file, XmlElement elem) - { - string id = elem.GetAttribute("id"); - string name = elem.GetAttribute("name"); - GameSystem system = new GameSystem(id, name, this); - StoreExtraData(system, elem); - return system; - } - - protected override Stream GetRaceDataStream (ZipFile file) - { - return file.GetInputStream(file.FindEntry("data.racex", true)); - } - - protected override Race CreateRaceFromStream (ZipFile file, Stream dataStream) - { - XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.RACE_ELEMENT); - LogNotifier.Debug(GetType(), "Create Race"); - return CreateRaceFromElement(file, elem); - } - - private 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, this); - StoreExtraData(race, elem); - return race; - } - - protected XmlDocument CreateXmlDocumentFromStream(Stream stream) - { XmlDocument doc = new XmlDocument(); XmlReader reader = XmlReader.Create(stream, GetReaderSettings()); - - try - { - doc.Load(reader); + + try + { + doc.Load(reader); } - //Don't catch XMLSchemaExceptions - let them get thrown out - finally - { - reader.Close(); - } - - return doc; + //Don't catch XMLSchemaExceptions - let them get thrown out + finally + { + reader.Close(); + } + + return doc; } /// <summary> @@ -225,6 +156,75 @@ } } + private Army CreateArmyFromElement(ZipFile file, XmlElement elem) + { + string name = elem.GetAttribute("name"); + string systemID = elem.GetAttribute("gameSystem"); + GameSystem system = WarFoundryLoader.GetDefault().GetGameSystem(systemID); + string raceID = elem.GetAttribute("race"); + Race race = WarFoundryLoader.GetDefault().GetRace(system, raceID); + int points = XmlTools.GetIntValueFromAttribute(elem, "maxPoints"); + Army army = new Army(race, name, points, file); + //TODO: Complete loading of army + return army; + } + + protected override Stream GetGameSystemDataStream (ZipFile file) + { + return file.GetInputStream(file.FindEntry("data.systemx", true)); + } + + protected override GameSystem CreateGameSystemFromStream (ZipFile file, Stream dataStream) + { + XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.SYSTEM_ELEMENT); + LogNotifier.Debug(GetType(), "Create GameSystem"); + return CreateSystemFromElement(file, elem); + } + + private GameSystem CreateSystemFromElement(ZipFile file, XmlElement elem) + { + string id = elem.GetAttribute("id"); + string name = elem.GetAttribute("name"); + GameSystem system = new GameSystem(id, name, this); + StoreExtraData(system, elem); + return system; + } + + private void StoreExtraData(WarFoundryStagedLoadingObject wfObject, XmlElement elem) + { + extraData[wfObject] = elem.OwnerDocument; + } + + protected override Stream GetRaceDataStream (ZipFile file) + { + return file.GetInputStream(file.FindEntry("data.racex", true)); + } + + protected override Race CreateRaceFromStream (ZipFile file, Stream dataStream) + { + XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.RACE_ELEMENT); + LogNotifier.Debug(GetType(), "Create Race"); + return CreateRaceFromElement(file, elem); + } + + private 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, this); + StoreExtraData(race, elem); + return race; + } + + public XmlDocument GetExtraData(IWarFoundryObject obj) + { + XmlDocument extra = null; + extraData.TryGetValue(obj, out extra); + return extra; + } + private XmlNamespaceManager GetNamespaceManager() { if (nsManager == null) @@ -255,42 +255,6 @@ XmlNode node = SelectSingleNode(element, xpathQuery); return (node is XmlElement) ? (XmlElement) node : null; } - - private int GetIntValueFromAttribute(XmlElement elem, string attributeName) - { - try - { - return int.Parse(elem.GetAttribute(attributeName)); - } - catch(FormatException) - { - throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id"))); - } - } - - private double GetDoubleValueFromAttribute(XmlElement elem, string attributeName) - { - double doubleVal = double.NaN; - string attribValue = elem.GetAttribute(attributeName); - - if (attribValue == "INF") - { - doubleVal = double.PositiveInfinity; - } - else - { - try - { - return int.Parse(attribValue); - } - catch(FormatException) - { - throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id"))); - } - } - - return doubleVal; - } public override void CompleteLoading(IWarFoundryStagedLoadObject obj) { @@ -308,20 +272,12 @@ public void CompleteLoading(GameSystem system) { - if (system.IsFullyLoaded) + if (!CanCompleteLoading(system)) { - LogNotifier.DebugFormat(GetType(), "Object of type GameSystem with ID {0} is already fully loaded", system.ID); return; } - if (system.IsLoading) - { - LogNotifier.WarnFormat(GetType(), "Object of type GameSystem with ID {0} is already being loaded", system.ID); - return; - } - - system.SetAsLoading(); - + system.SetAsLoading(); XmlDocument extraData = GetExtraData(system); LoadCategoriesForSystem(system, extraData); XmlElement statsElem = SelectSingleElement(extraData, "/system:system/system:sysStatsList"); @@ -333,36 +289,73 @@ system.SetAsFullyLoaded(); } + private bool CanCompleteLoading(IWarFoundryStagedLoadObject obj) + { + bool canLoad = true; + + if (obj.IsFullyLoaded) + { + LogNotifier.DebugFormat(GetType(), "Object of type {0} with ID {1} is already fully loaded", obj.GetType().Name, obj.ID); + canLoad = false; + } + else if (obj.IsLoading) + { + LogNotifier.WarnFormat(GetType(), "Object of type {0} with ID {1} is already being loaded", obj.GetType().Name, obj.ID); + canLoad = false; + } + + return canLoad; + } + private void LoadCategoriesForSystem(GameSystem system, XmlNode elem) { WarFoundryObject tempObj; foreach (XmlElement cat in SelectNodes(elem, "/system:system/system:categories/cat:cat")) { - tempObj = CreateObjectFromElement(cat); - - if (tempObj is Category) - { - system.AddCategory((Category)tempObj); - } - else - { - LogNotifier.WarnFormat(GetType(), "Object for string {0} was not of type Category", cat.OuterXml); - } + system.AddCategory(CreateCategoryFromElement(cat)); } } + private Category CreateCategoryFromElement(XmlElement elem) + { + string id = elem.GetAttribute("id"); + string name = elem.GetAttribute("name"); + Category cat = new Category(id, name); + cat.MaximumPercentage = XmlTools.GetIntValueFromAttribute(elem, "maxPercentage"); + cat.MinimumPercentage = XmlTools.GetIntValueFromAttribute(elem, "minPercentage"); + cat.MaximumPoints = XmlTools.GetIntValueFromAttribute(elem, "maxPoints"); + cat.MinimumPoints = XmlTools.GetIntValueFromAttribute(elem, "minPoints"); + return cat; + } + + private void LoadSystemStatsForSystem(GameSystem system, XmlNode elem) + { + foreach (XmlElement stats in SelectNodes(elem, "/system:system/system:sysStatsList/system:sysStats")) + { + SystemStats sysStats = CreateSystemStatsFromElement(stats); + system.AddSystemStats(sysStats); + } + } + + private SystemStats CreateSystemStatsFromElement(XmlElement elem) + { + List<StatSlot> slots = new List<StatSlot>(); + string id = elem.GetAttribute("id"); + + foreach (XmlElement slot in elem.ChildNodes) + { + StatSlot statSlot = new StatSlot(slot.GetAttribute("name")); + slots.Add(statSlot); + } + + return new SystemStats(id, slots.ToArray()); + } + public void CompleteLoading(Race race) { - if (race.IsFullyLoaded) + if (!CanCompleteLoading(system)) { - LogNotifier.DebugFormat(GetType(), "Object of type Race with ID {0} is already fully loaded", race.ID); - return; - } - - if (race.IsLoading) - { - LogNotifier.WarnFormat(GetType(), "Object of type Race with ID {0} is already being loaded", race.ID); return; } @@ -396,49 +389,19 @@ race.SetAsFullyLoaded(); LogNotifier.DebugFormat(GetType(), "Completed loading of Race with ID {0}", race.ID); } - - private WarFoundryObject CreateObjectFromElement(XmlElement elem) - { - WarFoundryObject obj = null; - LogNotifier.DebugFormat(GetType(), "Create object for <{0}>", elem.Name); - - if (elem.LocalName.Equals(WarFoundryXmlElementName.CATEGORY_ELEMENT.Value)) - { - LogNotifier.Debug(GetType(), "Create Category"); - obj = CreateCategoryFromElement(elem); - } - else - { - LogNotifier.Debug(GetType(), "No match"); - } - - return obj; - } - - private Category CreateCategoryFromElement(XmlElement elem) - { - string id = elem.GetAttribute("id"); - string name = elem.GetAttribute("name"); - Category cat = new Category(id, name); - cat.MaximumPercentage = GetIntValueFromAttribute(elem, "maxPercentage"); - cat.MinimumPercentage = GetIntValueFromAttribute(elem, "minPercentage"); - cat.MaximumPoints = GetIntValueFromAttribute(elem, "maxPoints"); - cat.MinimumPoints = GetIntValueFromAttribute(elem, "minPoints"); - return cat; - } 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 = GetIntValueFromAttribute(elem, "maxNum"); - type.MinNumber = GetIntValueFromAttribute(elem, "minNum"); - type.MaxSize = GetIntValueFromAttribute(elem, "maxSize"); - type.MinSize = GetIntValueFromAttribute(elem, "minSize"); - type.BaseSize = GetIntValueFromAttribute(elem, "baseSize"); - type.CostPerTrooper = GetIntValueFromAttribute(elem, "points"); - type.BaseUnitCost = GetIntValueFromAttribute(elem, "unitPoints"); + 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); @@ -492,29 +455,6 @@ return stats; } - private void LoadSystemStatsForSystem(GameSystem system, XmlNode elem) - { - foreach (XmlElement stats in SelectNodes(elem, "/system:system/system:sysStatsList/system:sysStats")) - { - SystemStats sysStats = CreateSystemStatsFromElement(stats); - system.AddSystemStats(sysStats); - } - } - - private SystemStats CreateSystemStatsFromElement(XmlElement elem) - { - List<StatSlot> slots = new List<StatSlot>(); - string id = elem.GetAttribute("id"); - - foreach (XmlElement slot in elem.ChildNodes) - { - StatSlot statSlot = new StatSlot(slot.GetAttribute("name")); - slots.Add(statSlot); - } - - return new SystemStats(id, slots.ToArray()); - } - private EquipmentItem CreateEquipmentItemFromElement(XmlElement elem, Race race) { string id = elem.GetAttribute("id"); @@ -524,7 +464,7 @@ try { - cost = GetDoubleValueFromAttribute(elem, "cost"); + cost = XmlTools.GetDoubleValueFromAttribute(elem, "cost"); } catch(FormatException ex) { @@ -537,7 +477,7 @@ } catch(ArgumentException ex) { - throw new InvalidFileException("Attribute 'armourType' of equipment "+id+" was not a valid value from the enumeration"); + 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