Mercurial > repos > IBDev-IBBoard.WarFoundry.API
view api/Factories/Xml/WarFoundryXmlGameSystemSaver.cs @ 312:3854c26073c4
Re #253: Allow multiple data files in a single zip
* Rebuild file loading to start to allow multiple files in a native file
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 26 Feb 2011 20:15:12 +0000 |
parents | 349e521785c1 |
children |
line wrap: on
line source
// 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; } } }