# HG changeset patch # User Tsudico # Date 1294371350 0 # Node ID 349e521785c11cabb82686db112a04a40a956410 # Parent ed5e6d92ecd97c9e7aa5ad9dfd7a3731ba13bf50 Re #338: WarFoundry.API - Save System Data Created IWarFoundryGameSystemSaver Created WarFoundryXmlGameSystemSaver Updated WarFoundrySaver Created GetGameSystemSaver Created SetGameSystemSaver diff -r ed5e6d92ecd9 -r 349e521785c1 IBBoard.WarFoundry.API.csproj --- a/IBBoard.WarFoundry.API.csproj Sat Dec 18 22:34:32 2010 +0000 +++ b/IBBoard.WarFoundry.API.csproj Fri Jan 07 03:35:50 2011 +0000 @@ -1,9 +1,9 @@ - - + + Debug AnyCPU - 9.0.30729 + 9.0.21022 2.0 {951E6C7A-7FBA-4F68-9D9E-F48618BB9626} Library @@ -12,11 +12,9 @@ IBBoard.WarFoundry.API - 2.0 + 3.5 - - publish\ true Disk @@ -61,6 +59,7 @@ --> + @@ -116,6 +115,7 @@ + diff -r ed5e6d92ecd9 -r 349e521785c1 api/Factories/Xml/WarFoundryXmlGameSystemSaver.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/api/Factories/Xml/WarFoundryXmlGameSystemSaver.cs Fri Jan 07 03:35:50 2011 +0000 @@ -0,0 +1,149 @@ +// This file (WarFoundryXmlGameSystemSaver.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 WarFoundryXmlGameSystemSaver : IWarFoundryGameSystemSaver + { + public const string GAMESYSTEM_FILE_EXTENSION = ".system"; + + public bool Save(GameSystem toSave, string savePath) + { + bool success = false; + ZipFile file = null; + + if (!savePath.EndsWith(GAMESYSTEM_FILE_EXTENSION)) + { + savePath = savePath + GAMESYSTEM_FILE_EXTENSION; + } + + try + { + file = ZipFile.Create(savePath); + file.BeginUpdate(); + file.Add(new StringZipEntrySource(CreateXmlString(toSave)), "data.systemx"); + file.CommitUpdate(); + success = true; + } + finally + { + if (file != null) + { + file.Close(); + } + } + + return success; + } + + private string CreateXmlString(WarFoundryObject toSave) + { + string xmlString = ""; + + if (toSave is GameSystem) + { + xmlString = CreateGameSystemXmlString((GameSystem)toSave); + } + + return xmlString; + } + + private string CreateGameSystemXmlString(GameSystem 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/system"); + schema.Namespaces.Add("cats", "http://ibboard.co.uk/warfoundry/cats"); + doc.Schemas.Add(schema); + XmlElement root = doc.CreateElement("system"); + root.SetAttribute("xmlns", "http://ibboard.co.uk/warfoundry/system"); + root.SetAttribute("xmlns:cats", "http://ibboard.co.uk/warfoundry/cats"); + doc.AppendChild(root); + root.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(toSave.ID)); + root.SetAttribute("name", toSave.Name); + root.SetAttribute("defaultArmySize", toSave.SystemArmyDefaultSize.ToString()); + root.SetAttribute("warn", toSave.WarnOnError.ToString().ToLowerInvariant()); + root.SetAttribute("allowAllies", toSave.AllowAllies.ToString().ToLowerInvariant()); + XmlElement cats = doc.CreateElement("categories"); + root.AppendChild(cats); + + foreach (Category cat in toSave.Categories) + { + cats.AppendChild(CreateCategoryElement(cat, doc)); + } + + XmlElement sysStatsList = doc.CreateElement("sysStatsList"); + sysStatsList.SetAttribute("defaultStats", XmlTools.GetAsciiXmlIdForString(toSave.StandardSystemStatsID)); + root.AppendChild(sysStatsList); + + foreach(SystemStats stats in toSave.SystemStats) + { + sysStatsList.AppendChild(CreateSystemStatsElement(stats, doc)); + } + + return doc.OuterXml; + } + + private XmlElement CreateCategoryElement(Category cat, XmlDocument doc) + { + XmlElement catElem = doc.CreateElement("cats:cat"); + catElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(cat.ID)); + catElem.SetAttribute("name", (cat.HasDefaultName() ? "" : cat.Name)); + if (cat.MinimumPoints > 0) + { + catElem.SetAttribute("minPoints", cat.MaximumPercentage.ToString()); + } + if (cat.MaximumPoints < 100) + { + catElem.SetAttribute("maxPoints", cat.MaximumPercentage.ToString()); + } + if(cat.MinimumPercentage > 0) + { + catElem.SetAttribute("minPercentage", cat.MaximumPercentage.ToString()); + } + if(cat.MaximumPercentage < 100) + { + catElem.SetAttribute("maxPercentage", cat.MaximumPercentage.ToString()); + } + + return catElem; + } + + private XmlElement CreateSystemStatsElement(SystemStats stats, XmlDocument doc) + { + XmlElement statsElem = doc.CreateElement("sysStats"); + statsElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(stats.ID)); + + foreach(StatSlot stat in stats.StatSlots) + { + statsElem.AppendChild(CreateSystemStatElement(stat, doc)); + } + + return statsElem; + } + + private XmlElement CreateSystemStatElement(StatSlot stat, XmlDocument doc) + { + XmlElement statElem = doc.CreateElement("sysStat"); + statElem.SetAttribute("name", stat.Name); + + return statElem; + } + } +} diff -r ed5e6d92ecd9 -r 349e521785c1 api/Savers/IWarFoundryGameSystemSaver.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/api/Savers/IWarFoundryGameSystemSaver.cs Fri Jan 07 03:35:50 2011 +0000 @@ -0,0 +1,26 @@ +// This file (IWarFoundrySystemSaver.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 IBBoard.WarFoundry.API.Objects; + +namespace IBBoard.WarFoundry.API.Savers +{ + public interface IWarFoundryGameSystemSaver + { + /// + /// Saves a to a file on disk. + /// + /// + /// The to save + /// + /// + /// The path to save the system to + /// + /// + /// TRUE if saving succedes, else FALSE + /// + bool Save(GameSystem system, string path); + } +} \ No newline at end of file diff -r ed5e6d92ecd9 -r 349e521785c1 api/Savers/WarFoundrySaver.cs --- a/api/Savers/WarFoundrySaver.cs Sat Dec 18 22:34:32 2010 +0000 +++ b/api/Savers/WarFoundrySaver.cs Fri Jan 07 03:35:50 2011 +0000 @@ -9,6 +9,7 @@ public class WarFoundrySaver { private static IWarFoundryFileSaver fileSaver; + private static IWarFoundryGameSystemSaver systemSaver; public static IWarFoundryFileSaver GetSaver() { @@ -19,5 +20,15 @@ { fileSaver = newFileSaver; } + + public static IWarFoundryGameSystemSaver GetGameSystemSaver() + { + return systemSaver; + } + + public static void SetGameSystemSaver(IWarFoundryGameSystemSaver newGameSystemSaver) + { + systemSaver = newGameSystemSaver; + } } }