Mercurial > repos > IBDev-IBBoard.WarFoundry.API
diff api/Factories/Xml/WarFoundryXmlGameSystemSaver.cs @ 297:349e521785c1
Re #338: WarFoundry.API - Save System Data
Created IWarFoundryGameSystemSaver
Created WarFoundryXmlGameSystemSaver
Updated WarFoundrySaver
Created GetGameSystemSaver
Created SetGameSystemSaver
author | Tsudico |
---|---|
date | Fri, 07 Jan 2011 03:35:50 +0000 |
parents | |
children |
line wrap: on
line diff
--- /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; + } + } +}