Mercurial > repos > IBDev-IBBoard.WarFoundry.API
view api/Factories/Xml/WarFoundryXmlSaver.cs @ 51:b271a2252758
Re #50 - Fully load XML files
* Load remaining GameSystem attributes
* Add "AllowAllies" property to GameSystem class
* Add "is loading" methods to "staged loading" interface
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 28 Mar 2009 21:00:35 +0000 |
parents | 306558904c2a |
children | 3ea0ab04352b |
line wrap: on
line source
// This file (WarFoundryXmlSaver.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard. // // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. using System; using System.Collections.Generic; using System.IO; using IBBoard.Lang; using IBBoard.WarFoundry.API.Objects; namespace IBBoard.WarFoundry.API.Factories.Xml { public class WarFoundryXmlSaver { //FIXME: Rework to saving files in Zips private static WarFoundryXmlSaver saver; private static List<Type> saveAsTypes; private Dictionary<WarFoundryObject, string> savePaths = new Dictionary<WarFoundryObject,string>(); public static WarFoundryXmlSaver GetSaver() { if (saver == null) { saveAsTypes = new List<Type>(); saveAsTypes.Add(typeof(Army)); saver = new WarFoundryXmlSaver(); } return saver; } public bool SaveAs(WarFoundryObject toSave, string saveAsPath) { if (CanSaveType(toSave)) { FileStream fs = null; bool success = false; try { fs = new FileStream(saveAsPath, FileMode.Create, FileAccess.Write); byte[] bytes = StringManipulation.StringToBytes(CreateXmlString(toSave)); fs.Write(bytes, 0, bytes.Length); fs.Flush(); savePaths.Add(toSave, saveAsPath); success = true; } finally { if (fs!=null && fs.CanWrite) { fs.Close(); } } return success; } else { throw new ArgumentException("Cannot directly save objects of type "+toSave.GetType()); } } public bool Save(WarFoundryObject toSave) { if (CanSave(toSave)) { return SaveAs(toSave, savePaths[toSave]); } else { throw new InvalidOperationException("Cannot save an object that has not previously been saved using SaveAs"); } } private string CreateXmlString(WarFoundryObject toSave) { return ""; //TODO: Create XML string as appropriate } public bool CanSaveAs(WarFoundryObject obj) { return CanSaveType(obj); } public bool CanSave(WarFoundryObject obj) { return savePaths.ContainsKey(obj); } public bool CanSaveType(WarFoundryObject obj) { return saveAsTypes.Contains(obj.GetType()); } } }