Mercurial > repos > IBDev-IBBoard.WarFoundry.API
changeset 315:6cb0fb78b9a6
Re #324: Add saving of Race and System data to files
* Convert IWarFoundryFileSaver from a marker interface into an interface with a purpose of its own so that we can support multiple data files in one zip
* Repurpose WarFoundryXmlSaver to be just an Army saver - we still need to make it usable as part of a larger whole
* Add a WarFoundryLoadedObject class for objects loaded from files and make Race/Army/GameSystem inherit from it
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 28 Feb 2011 21:09:20 +0000 |
parents | bd5d8bfe18a6 |
children | 40a2df1f629a |
files | IBBoard.WarFoundry.API.csproj api/Factories/Xml/WarFoundryXmlArmySaver.cs api/Factories/Xml/WarFoundryXmlSaver.cs api/Objects/Army.cs api/Objects/WarFoundryLoadedObject.cs api/Objects/WarFoundryStagedLoadingObject.cs api/Savers/IWarFoundryFileSaver.cs api/Savers/WarFoundrySaver.cs |
diffstat | 8 files changed, 231 insertions(+), 213 deletions(-) [+] |
line wrap: on
line diff
--- a/IBBoard.WarFoundry.API.csproj Sun Feb 27 20:01:04 2011 +0000 +++ b/IBBoard.WarFoundry.API.csproj Mon Feb 28 21:09:20 2011 +0000 @@ -8,7 +8,7 @@ <ProjectGuid>{951E6C7A-7FBA-4F68-9D9E-F48618BB9626}</ProjectGuid> <OutputType>Library</OutputType> <AppDesignerFolder>Properties</AppDesignerFolder> - <RootNamespace>IBBoard.WarFoundry.API</RootNamespace> + <RootNamespace>IBBoard.WarFoundry</RootNamespace> <AssemblyName>IBBoard.WarFoundry.API</AssemblyName> <FileUpgradeFlags> </FileUpgradeFlags> @@ -80,7 +80,6 @@ <Compile Include="api\Factories\IWarFoundryFactory.cs" /> <Compile Include="api\Factories\Xml\WarFoundryXmlElementName.cs" /> <Compile Include="api\Factories\Xml\WarFoundryXmlFactory.cs" /> - <Compile Include="api\Factories\Xml\WarFoundryXmlSaver.cs" /> <Compile Include="api\FileLoadFailure.cs" /> <Compile Include="api\Objects\Ability.cs" /> <Compile Include="api\Objects\Army.cs" /> @@ -186,6 +185,8 @@ <Compile Include="api\Factories\DummyWarFoundryFactory.cs" /> <Compile Include="api\Savers\IWarFoundryArmySaver.cs" /> <Compile Include="api\Savers\IWarFoundryRaceSaver.cs" /> + <Compile Include="api\Objects\WarFoundryLoadedObject.cs" /> + <Compile Include="api\Factories\Xml\WarFoundryXmlArmySaver.cs" /> </ItemGroup> <ItemGroup> <Reference Include="System.Xml" /> @@ -233,4 +234,13 @@ <Install>true</Install> </BootstrapperPackage> </ItemGroup> + <ProjectExtensions> + <MonoDevelop> + <Properties> + <Policies> + <DotNetNamingPolicy DirectoryNamespaceAssociation="PrefixedFlat" ResourceNamePolicy="FileFormatDefault" /> + </Policies> + </Properties> + </MonoDevelop> + </ProjectExtensions> </Project> \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/api/Factories/Xml/WarFoundryXmlArmySaver.cs Mon Feb 28 21:09:20 2011 +0000 @@ -0,0 +1,186 @@ +// This file (WarFoundryXmlSaver.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2008, 2009 IBBoard. +// +// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. + +using System; +using System.Collections.Generic; +using System.IO; +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; +using IBBoard.WarFoundry.API.Util; +using ICSharpCode.SharpZipLib.Zip; + +namespace IBBoard.WarFoundry.API.Factories.Xml +{ + public class WarFoundryXmlArmySaver : IWarFoundryArmySaver + { + 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 + { + file = ZipFile.Create(savePath); + file.BeginUpdate(); + file.Add(new StringZipEntrySource(CreateXmlString(toSave)), "data.armyx"); + file.CommitUpdate(); + success = true; + } + finally + { + if (file != null) + { + file.Close(); + } + } + + return success; + } + + private string CreateXmlString(WarFoundryObject toSave) + { + string xmlString = ""; + + if (toSave is Army) + { + xmlString = CreateArmyXmlString((Army)toSave); + } + + return xmlString; + } + + private string CreateArmyXmlString(Army toSave) + { + XmlDocument doc = new XmlDocument(); + XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null); + doc.AppendChild(declaration); + XmlSchema schema = new XmlSchema(); + schema.Namespaces.Add("", "http://ibboard.co.uk/warfoundry/army"); + schema.Namespaces.Add("core", "http://ibboard.co.uk/warfoundry/core"); + doc.Schemas.Add(schema); + XmlElement root = doc.CreateElement("army"); + root.SetAttribute("xmlns", "http://ibboard.co.uk/warfoundry/army"); + root.SetAttribute("xmlns:core", "http://ibboard.co.uk/warfoundry/core"); + doc.AppendChild(root); + 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()); + XmlElement units = doc.CreateElement("units"); + root.AppendChild(units); + + foreach (Unit unit in toSave.GetUnits()) + { + units.AppendChild(CreateUnitElement(unit, doc)); + } + + return doc.OuterXml; + } + + private XmlElement CreateUnitElement(Unit unit, XmlDocument doc) + { + XmlElement unitElem = doc.CreateElement("unit"); + unitElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(unit.ID)); + unitElem.SetAttribute("unitName", (unit.HasDefaultName() ? "" : unit.Name)); + unitElem.SetAttribute("unitType", unit.UnitType.ID); + unitElem.SetAttribute("size", unit.Size.ToString()); + + if (!unit.Race.Equals(unit.Army.Race)) + { + unitElem.SetAttribute("race", unit.Race.ID); + } + + Category unitCategory = unit.Category.Category; + if (!unit.UnitType.MainCategory.Equals(unitCategory)) + { + unitElem.SetAttribute("category", unitCategory.ID); + } + + XmlElement equipmentElem = CreateEquipmentItemsElement(unit, doc); + + if (equipmentElem != null) + { + unitElem.AppendChild(equipmentElem); + } + + XmlElement containedElem = CreateContainedUnitsElement(unit, doc); + + if (containedElem != null) + { + unitElem.AppendChild(containedElem); + } + + return unitElem; + } + + private XmlElement CreateEquipmentItemsElement(Unit unit, XmlDocument doc) + { + UnitEquipmentItem[] equipItems = unit.GetEquipment(); + int equipItemCount = equipItems.Length; + XmlElement equipmentElem = null; + + if (equipItemCount > 0) + { + equipmentElem = doc.CreateElement("equipment"); + + for (int i = 0; i < equipItemCount; i++) + { + equipmentElem.AppendChild(CreateEquipmentElement(equipItems[i], unit, doc)); + } + } + + return equipmentElem; + } + + private XmlElement CreateEquipmentElement(UnitEquipmentItem item, Unit unit, XmlDocument doc) + { + XmlElement equipmentItemElem = doc.CreateElement("equipItem"); + equipmentItemElem.SetAttribute("id", item.ID); + equipmentItemElem.SetAttribute("amount", UnitEquipmentUtil.GetEquipmentAmount(unit, item).ToString()); + equipmentItemElem.SetAttribute("amountType", UnitEquipmentUtil.GetEquipmentAmountIsRatio(unit, 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; + } + } +}
--- a/api/Factories/Xml/WarFoundryXmlSaver.cs Sun Feb 27 20:01:04 2011 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,196 +0,0 @@ -// This file (WarFoundryXmlSaver.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2008, 2009 IBBoard. -// -// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. - -using System; -using System.Collections.Generic; -using System.IO; -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; -using IBBoard.WarFoundry.API.Util; -using ICSharpCode.SharpZipLib.Zip; - -namespace IBBoard.WarFoundry.API.Factories.Xml -{ - 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 - { - file = ZipFile.Create(savePath); - file.BeginUpdate(); - file.Add(new StringZipEntrySource(CreateXmlString(toSave)), "data.armyx"); - file.CommitUpdate(); - success = true; - } - finally - { - if (file != null) - { - file.Close(); - } - } - - return success; - } - - private string CreateXmlString(WarFoundryObject toSave) - { - string xmlString = ""; - - if (toSave is Army) - { - xmlString = CreateArmyXmlString((Army)toSave); - } - - return xmlString; - } - - private string CreateArmyXmlString(Army toSave) - { - XmlDocument doc = new XmlDocument(); - XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null); - doc.AppendChild(declaration); - XmlSchema schema = new XmlSchema(); - schema.Namespaces.Add("", "http://ibboard.co.uk/warfoundry/army"); - schema.Namespaces.Add("core", "http://ibboard.co.uk/warfoundry/core"); - doc.Schemas.Add(schema); - XmlElement root = doc.CreateElement("army"); - root.SetAttribute("xmlns", "http://ibboard.co.uk/warfoundry/army"); - root.SetAttribute("xmlns:core", "http://ibboard.co.uk/warfoundry/core"); - doc.AppendChild(root); - 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()); - XmlElement units = doc.CreateElement("units"); - root.AppendChild(units); - - foreach (Unit unit in toSave.GetUnits()) - { - units.AppendChild(CreateUnitElement(unit, doc)); - } - - return doc.OuterXml; - } - - private XmlElement CreateUnitElement(Unit unit, XmlDocument doc) - { - XmlElement unitElem = doc.CreateElement("unit"); - unitElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(unit.ID)); - unitElem.SetAttribute("unitName", (unit.HasDefaultName() ? "" : unit.Name)); - unitElem.SetAttribute("unitType", unit.UnitType.ID); - unitElem.SetAttribute("size", unit.Size.ToString()); - - if (!unit.Race.Equals(unit.Army.Race)) - { - unitElem.SetAttribute("race", unit.Race.ID); - } - - Category unitCategory = unit.Category.Category; - if (!unit.UnitType.MainCategory.Equals(unitCategory)) - { - unitElem.SetAttribute("category", unitCategory.ID); - } - - XmlElement equipmentElem = CreateEquipmentItemsElement(unit, doc); - - if (equipmentElem != null) - { - unitElem.AppendChild(equipmentElem); - } - - XmlElement containedElem = CreateContainedUnitsElement(unit, doc); - - if (containedElem != null) - { - unitElem.AppendChild(containedElem); - } - - return unitElem; - } - - private XmlElement CreateEquipmentItemsElement(Unit unit, XmlDocument doc) - { - UnitEquipmentItem[] equipItems = unit.GetEquipment(); - int equipItemCount = equipItems.Length; - XmlElement equipmentElem = null; - - if (equipItemCount > 0) - { - equipmentElem = doc.CreateElement("equipment"); - - for (int i = 0; i < equipItemCount; i++) - { - equipmentElem.AppendChild(CreateEquipmentElement(equipItems[i], unit, doc)); - } - } - - return equipmentElem; - } - - private XmlElement CreateEquipmentElement(UnitEquipmentItem item, Unit unit, XmlDocument doc) - { - XmlElement equipmentItemElem = doc.CreateElement("equipItem"); - equipmentItemElem.SetAttribute("id", item.ID); - equipmentItemElem.SetAttribute("amount", UnitEquipmentUtil.GetEquipmentAmount(unit, item).ToString()); - equipmentItemElem.SetAttribute("amountType", UnitEquipmentUtil.GetEquipmentAmountIsRatio(unit, 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; - } - - public bool Save (Race race, string path) - { - throw new System.NotImplementedException (); - } - - public bool Save (GameSystem system, string path) - { - throw new System.NotImplementedException (); - } - } -}
--- a/api/Objects/Army.cs Sun Feb 27 20:01:04 2011 +0000 +++ b/api/Objects/Army.cs Mon Feb 28 21:09:20 2011 +0000 @@ -17,7 +17,7 @@ /// <summary> /// Summary description for Army. /// </summary> - public class Army : WarFoundryObject, ICostedWarFoundryObject + public class Army : WarFoundryLoadedObject, ICostedWarFoundryObject { //private GameSystem system; private Race armyRace;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/api/Objects/WarFoundryLoadedObject.cs Mon Feb 28 21:09:20 2011 +0000 @@ -0,0 +1,26 @@ +using System; +using IBBoard.WarFoundry.API.Objects; + +namespace IBBoard.WarFoundry.API.Objects +{ + /// <summary> + /// A marker class for objects that are loaded from a file + /// </summary> + public class WarFoundryLoadedObject : WarFoundryObject + { + protected WarFoundryLoadedObject() : base() + { + //Do nothing + } + + protected WarFoundryLoadedObject(string objName) : base(objName) + { + //Do nothing + } + + protected WarFoundryLoadedObject(string objId, string objName) : base(objId, objName) + { + //Do nothing + } + } +}
--- a/api/Objects/WarFoundryStagedLoadingObject.cs Sun Feb 27 20:01:04 2011 +0000 +++ b/api/Objects/WarFoundryStagedLoadingObject.cs Mon Feb 28 21:09:20 2011 +0000 @@ -8,7 +8,7 @@ namespace IBBoard.WarFoundry.API.Objects { - public class WarFoundryStagedLoadingObject : WarFoundryObject, IWarFoundryStagedLoadObject + public class WarFoundryStagedLoadingObject : WarFoundryLoadedObject, IWarFoundryStagedLoadObject { private bool isFullyLoaded; private bool isLoading;
--- a/api/Savers/IWarFoundryFileSaver.cs Sun Feb 27 20:01:04 2011 +0000 +++ b/api/Savers/IWarFoundryFileSaver.cs Mon Feb 28 21:09:20 2011 +0000 @@ -7,8 +7,11 @@ namespace IBBoard.WarFoundry.API.Savers { - public interface IWarFoundryFileSaver : IWarFoundryGameSystemSaver, IWarFoundryRaceSaver, IWarFoundryArmySaver + /// <summary> + /// Saves one or more objects into a native-format zip file. + /// </summary> + public interface IWarFoundryFileSaver { - //Marker interface + void Save(string path, params WarFoundryLoadedObject[] objects); } }
--- a/api/Savers/WarFoundrySaver.cs Sun Feb 27 20:01:04 2011 +0000 +++ b/api/Savers/WarFoundrySaver.cs Mon Feb 28 21:09:20 2011 +0000 @@ -9,7 +9,6 @@ public class WarFoundrySaver { private static IWarFoundryFileSaver fileSaver; - private static IWarFoundryGameSystemSaver systemSaver; public static IWarFoundryFileSaver GetSaver() { @@ -20,15 +19,5 @@ { fileSaver = newFileSaver; } - - public static IWarFoundryGameSystemSaver GetGameSystemSaver() - { - return systemSaver; - } - - public static void SetGameSystemSaver(IWarFoundryGameSystemSaver newGameSystemSaver) - { - systemSaver = newGameSystemSaver; - } } }