Mercurial > repos > IBDev-IBBoard.WarFoundry.API
diff api/Factories/Xml/WarFoundryXmlFactory.cs @ 8:613bc5eaac59
Re #9 - Make WarFoundry loading granular
* Remove specific staged loading classes
* Rework category loading for GameSystem and Race to make it use AddCategory(Category) method
* Promote staged loading from Native Factory to all Factories level
* Refactor XML Factory to use smaller methods
Also removed some commented code that isn't used any more
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 04 Jan 2009 19:24:13 +0000 |
parents | 150a5669cd7b |
children | 6ad505b6c36e |
line wrap: on
line diff
--- a/api/Factories/Xml/WarFoundryXmlFactory.cs Sun Jan 04 13:12:55 2009 +0000 +++ b/api/Factories/Xml/WarFoundryXmlFactory.cs Sun Jan 04 19:24:13 2009 +0000 @@ -132,7 +132,7 @@ { string id = elem.GetAttribute("id"); string name = elem.GetAttribute("name"); - GameSystem system = new StagedLoadingGameSystem(id, name, this); + GameSystem system = new GameSystem(id, name, this); //system.SourceZipFile = file.; extraData[system] = elem.OwnerDocument; return system; @@ -156,151 +156,121 @@ string subid = elem.GetAttribute("subid"); string systemID = elem.GetAttribute("system"); string name = elem.GetAttribute("name"); - Race race = new StagedLoadingRace(id, subid, name, systemID, this); + Race race = new Race(id, subid, name, systemID, this); //race.SourceZipFile = file; //TODO reference source file extraData[race] = elem.OwnerDocument; return race; - } + } - /*public WarFoundryObject CreateObjectFromStream(ZipFile file, Stream stream) - { - try + public override void CompleteLoading(IWarFoundryStagedLoadObject obj) + { + LogNotifier.DebugFormat(GetType(), "Complete loading of {0} with ID {1}", obj.GetType().Name, obj.ID); + + if (obj is GameSystem) + { + CompleteLoading((GameSystem)obj); + } + else if (obj is Race) { - WarFoundryObject obj = LoadFileObjectFromElement(file, elem); + CompleteLoading((Race)obj); + } + } + + public XmlDocument GetExtraData(IWarFoundryObject obj) + { + XmlDocument extra = null; + extraData.TryGetValue(obj, out extra); + return extra; + } + + public void CompleteLoading(GameSystem system) + { + if (system.IsFullyLoaded) + { + LogNotifier.DebugFormat(GetType(), "Object of type GameSystem with ID {0} is already fully loaded", system.ID); + return; + } + + XmlDocument extra = GetExtraData(system); + XmlNode elem = extra.LastChild; + + XmlNode catsElem = elem.FirstChild; + WarFoundryObject tempObj; + + foreach (XmlElement cat in catsElem.ChildNodes) + { + tempObj = CreateObjectFromElement(cat); - if (obj != null) - { - extraData[obj] = doc; - return obj; + if (tempObj is Category) + { + system.AddCategory((Category)tempObj); } else { - throw new InvalidFileException(String.Format(Translation.GetTranslation("ErrorInvalidXmlFile", "XML file '{0}' was not a loadable XML file. Please ensure it is a valid and supported WarFoundry XML file."), file.Name)); + LogNotifier.WarnFormat(GetType(), "Object for string {0} was not of type Category", cat.OuterXml); } - } - catch (XmlSchemaException ex) - { - throw new InvalidFileException(String.Format(Translation.GetTranslation("ErrorInvalidXmlFile", "Failed to parse invalid XML data file in {0}. Error at line {1} position {2}: {3}"), file.Name, ex.LineNumber, ex.LinePosition, ex.Message.Substring(0, ex.Message.IndexOf(".")+1)), ex); } - catch (InvalidFileException ex) - { - throw new InvalidFileException(String.Format(Translation.GetTranslation("ErrorInvalidNamedXmlFile", "XML data file in '{0}' was not a valid XML file. It should contain three child nodes - XML definition, DocType and root node."), file.Name), ex); - } + + XmlElement statsElem = (XmlElement)catsElem.NextSibling; + LoadSystemStatsFromElement(statsElem, system); + string defaultStatsID = statsElem.GetAttribute("defaultStats"); + + LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.Name); + system.StandardSystemStatsID = defaultStatsID; + system.SetAsFullyLoaded(); } - private WarFoundryObject LoadFileObjectFromElement(ZipFile file, XmlElement elem) + public void CompleteLoading(Race race) { - WarFoundryObject obj = null; - - if (elem.Name.Equals(WarFoundryXmlElementName.SYSTEM_ELEMENT.Value)) + if (race.IsFullyLoaded) { - logger.Debug("Create GameSystem"); - obj = CreateSystemFromElement(file, elem); - } - else if (elem.Name.Equals(WarFoundryXmlElementName.RACE_ELEMENT.Value)) - { - logger.Debug("Create Race"); - obj = CreateRaceFromElement(file, elem); + LogNotifier.DebugFormat(GetType(), "Object of type Race with ID {0} is already fully loaded", race.ID); + return; } - return obj; - }*/ - - public override void CompleteLoading(IWarFoundryStagedLoadObject obj) - { - LogNotifier.DebugFormat(GetType(), "Complete loading of {0} with ID {1}", obj.GetType().Name, obj.ID); - - if (!obj.IsFullyLoaded) - { - XmlDocument extra = extraData[obj]; - - if (obj is GameSystem) + XmlDocument extra = GetExtraData(race); + XmlNode elem = extra.LastChild; + XmlNode colNode = elem.FirstChild; + + Dictionary<string, UnitType> unitTypes = new Dictionary<string, UnitType>(); + + foreach (XmlElement node in colNode.ChildNodes) + { + UnitType type = CreateUnitTypeFromElement(node, race, race.GameSystem); + unitTypes.Add(type.ID, type); + } + + colNode = colNode.NextSibling; + + if (colNode!=null && colNode.Name == WarFoundryXmlElementName.CATEGORIES_ELEMENT.Value) + { + foreach (XmlElement node in colNode.ChildNodes) { - GameSystem system = (GameSystem)obj; - XmlNode elem = extra.LastChild; - - XmlNode catsElem = elem.FirstChild; - Category[] cats; - List<Category> catList = new List<Category>(); - WarFoundryObject tempObj; - - foreach (XmlElement cat in catsElem.ChildNodes) - { - tempObj = CreateObjectFromElement(cat); - - if (tempObj is Category) - { - catList.Add((Category)tempObj); - } - else - { - LogNotifier.WarnFormat(GetType(), "Object for string {0} was not of type Category", cat.OuterXml); - } - } - - cats = catList.ToArray(); - LogNotifier.DebugFormat(GetType(), "Found {0} categories", cats.Length); - - XmlElement statsElem = (XmlElement)catsElem.NextSibling; - LoadSystemStatsFromElement(statsElem, system); - string defaultStatsID = statsElem.GetAttribute("defaultStats"); - - LogNotifier.DebugFormat(GetType(), "Complete loading of {0}", system.Name); - system.Categories = cats; - system.StandardSystemStatsID = defaultStatsID; + race.AddCategory(CreateCategoryFromElement(node)); } - else if (obj is Race) + + colNode = colNode.NextSibling; + } + + Dictionary<string, EquipmentItem> raceEquipment = new Dictionary<string, EquipmentItem>(); + + if (colNode!=null && colNode.Name == WarFoundryXmlElementName.RACE_EQUIPMENT_ITEMS_ELEMENT.Value) + { + foreach (XmlElement node in colNode.ChildNodes) { - Race race = (Race)obj; - XmlNode elem = extra.LastChild; - XmlNode colNode = elem.FirstChild; - - Dictionary<string, UnitType> unitTypes = new Dictionary<string, UnitType>(); - - foreach (XmlElement node in colNode.ChildNodes) - { - UnitType type = CreateUnitTypeFromElement(node, race, race.GameSystem); - unitTypes.Add(type.ID, type); - } - - colNode = colNode.NextSibling; - List<Category> catOverrides = new List<Category>(); - - if (colNode!=null && colNode.Name == WarFoundryXmlElementName.CATEGORIES_ELEMENT.Value) - { - foreach (XmlElement node in colNode.ChildNodes) - { - catOverrides.Add(CreateCategoryFromElement(node)); - } - - colNode = colNode.NextSibling; - } - - Dictionary<string, EquipmentItem> raceEquipment = new Dictionary<string, EquipmentItem>(); - - 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); - } - } - - Dictionary<string, Ability> raceAbilities = new Dictionary<string, Ability>(); - //TODO: Load abilities - - LogNotifier.DebugFormat(GetType(), "Complete loading of {0}", race.Name); - race.Categories = catOverrides.ToArray(); - race.SetUnitTypes(unitTypes); - race.SetEquipmentItems(raceEquipment); - race.SetAbilities(raceAbilities); + EquipmentItem item = CreateEquipmentItemFromElement(node, race); + raceEquipment.Add(item.ID, item); } } - else - { - LogNotifier.Debug(GetType(), "Object is already fully loaded"); - } + + Dictionary<string, Ability> raceAbilities = new Dictionary<string, Ability>(); + //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); } protected XmlDocument CreateXmlDocumentFromStream(Stream stream)