Mercurial > repos > snowblizz-super-API-ideas
changeset 109:45f1db6356e9
Fixes #53: Make XML file saver
* force file extension
* Make sure IDs are in valid XML format
* Remove commented example of file
* Output contained units
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 22 Aug 2009 10:51:50 +0000 |
parents | 2060f23abee9 |
children | f0fb96d0cfe9 |
files | api/Factories/Xml/WarFoundryXmlSaver.cs |
diffstat | 1 files changed, 45 insertions(+), 25 deletions(-) [+] |
line wrap: on
line diff
--- a/api/Factories/Xml/WarFoundryXmlSaver.cs Fri Aug 21 20:12:27 2009 +0000 +++ b/api/Factories/Xml/WarFoundryXmlSaver.cs Sat Aug 22 10:51:50 2009 +0000 @@ -8,6 +8,7 @@ using System.Xml; using System.Xml.Schema; using IBBoard.Lang; +using IBBoard.Xml; using IBBoard.WarFoundry.API.Factories.Xml.Zip; using IBBoard.WarFoundry.API.Objects; using IBBoard.WarFoundry.API.Savers; @@ -17,10 +18,17 @@ { public class WarFoundryXmlSaver : IWarFoundryFileSaver { + public const string ARMY_FILE_EXTENSION = ".army"; + public bool Save(Army toSave, string savePath) { bool success = false; ZipFile file = null; + + if (!savePath.EndsWith(ARMY_FILE_EXTENSION)) + { + savePath = savePath + ARMY_FILE_EXTENSION; + } try { @@ -55,28 +63,6 @@ private string CreateArmyXmlString(Army toSave) { - /* -<army id="12345" name="Sample Army" system="sampleSystem" race="Empire" maxPoints="500"> - <units> - <unit id="unit1" unitType="Empire1" unitName="General Eustace" size="1"> - <equipment> - <equipItem id="equip1" amount="1"/> - </equipment> - </unit> - <unit id="unit2" unitType="Empire2" unitName="First Swordsmen" size="20"> - <equipment> - <equipItem id="equip1" amount="1"/> - <equipItem id="equip2" amount="1"/> - </equipment> - </unit> - <unit id="unit3" unitType="Empire2" unitName="First Greatswords" size="15"> - <equipment> - <equipItem id="equip3" amount="1"/> - </equipment> - </unit> - </units> -</army> -*/ XmlDocument doc = new XmlDocument(); XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null); doc.AppendChild(declaration); @@ -86,8 +72,10 @@ doc.Schemas.Add(schema); XmlElement root = doc.CreateElement("army"); doc.AppendChild(root); - root.SetAttribute("id", toSave.ID); + root.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(toSave.ID)); root.SetAttribute("name", toSave.Name); + //Don't convert system and race to ID format as they could be stored in non-XML file formats + //If they are in XML files then they'll already be valid root.SetAttribute("system", toSave.GameSystem.ID); root.SetAttribute("race", toSave.Race.ID); root.SetAttribute("maxPoints", toSave.MaxPoints.ToString()); @@ -97,7 +85,6 @@ foreach (Unit unit in toSave.GetUnits()) { units.AppendChild(CreateUnitElement(unit, doc)); - } return doc.OuterXml; @@ -106,7 +93,7 @@ private XmlElement CreateUnitElement(Unit unit, XmlDocument doc) { XmlElement unitElem = doc.CreateElement("unit"); - unitElem.SetAttribute("id", unit.ID); + unitElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(unit.ID)); unitElem.SetAttribute("unitName", unit.Name); unitElem.SetAttribute("unitType", unit.UnitType.ID); unitElem.SetAttribute("size", unit.Size.ToString()); @@ -123,6 +110,13 @@ unitElem.AppendChild(equipmentElem); } + XmlElement containedElem = CreateContainedUnitsElement(unit, doc); + + if (containedElem != null) + { + unitElem.AppendChild(containedElem); + } + return unitElem; } @@ -153,5 +147,31 @@ equipmentItemElem.SetAttribute("amountType", unit.GetEquipmentAmountIsRatio(item) ? "ratio" : "fixed"); return equipmentItemElem; } + + private XmlElement CreateContainedUnitsElement(Unit unit, XmlDocument doc) + { + Unit[] containedUnits = unit.ContainedUnits; + int containedCount = containedUnits.Length; + XmlElement containedElem = null; + + if (containedCount > 0) + { + containedElem = doc.CreateElement("contained"); + + for (int i = 0; i < containedCount; i++) + { + containedElem.AppendChild(CreateContainedUnitElement(containedUnits[i], doc)); + } + } + + return containedElem; + } + + private XmlElement CreateContainedUnitElement(Unit unit, XmlDocument doc) + { + XmlElement containedUnitElem = doc.CreateElement("containedUnit"); + containedUnitElem.SetAttribute("containedID", XmlTools.GetAsciiXmlIdForString(unit.ID)); + return containedUnitElem; + } } }