Mercurial > repos > IBDev-IBBoard.WarFoundry.API
comparison api/Factories/Xml/WarFoundryXmlSaver.cs @ 0:520818033bb6
Initial commit of WarFoundry code
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Fri, 19 Dec 2008 15:57:51 +0000 |
parents | |
children | 306558904c2a |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:520818033bb6 |
---|---|
1 using System; | |
2 using System.Collections.Generic; | |
3 using System.IO; | |
4 using IBBoard.Lang; | |
5 using IBBoard.WarFoundry.API.Objects; | |
6 | |
7 namespace IBBoard.WarFoundry.API.Factories.Xml | |
8 { | |
9 public class WarFoundryXmlSaver | |
10 { | |
11 //FIXME: Rework to saving files in Zips | |
12 private static WarFoundryXmlSaver saver; | |
13 private static List<Type> saveAsTypes; | |
14 private Dictionary<WarFoundryObject, string> savePaths = new Dictionary<WarFoundryObject,string>(); | |
15 | |
16 public static WarFoundryXmlSaver GetSaver() | |
17 { | |
18 if (saver == null) | |
19 { | |
20 saveAsTypes = new List<Type>(); | |
21 saveAsTypes.Add(typeof(Army)); | |
22 saver = new WarFoundryXmlSaver(); | |
23 } | |
24 | |
25 return saver; | |
26 } | |
27 | |
28 public bool SaveAs(WarFoundryObject toSave, string saveAsPath) | |
29 { | |
30 if (CanSaveType(toSave)) | |
31 { | |
32 FileStream fs = null; | |
33 bool success = false; | |
34 | |
35 try | |
36 { | |
37 fs = new FileStream(saveAsPath, FileMode.Create, FileAccess.Write); | |
38 byte[] bytes = StringManipulation.StringToBytes(CreateXmlString(toSave)); | |
39 fs.Write(bytes, 0, bytes.Length); | |
40 fs.Flush(); | |
41 savePaths.Add(toSave, saveAsPath); | |
42 success = true; | |
43 } | |
44 finally | |
45 { | |
46 if (fs!=null && fs.CanWrite) | |
47 { | |
48 fs.Close(); | |
49 } | |
50 } | |
51 | |
52 return success; | |
53 } | |
54 else | |
55 { | |
56 throw new ArgumentException("Cannot directly save objects of type "+toSave.GetType()); | |
57 } | |
58 } | |
59 | |
60 public bool Save(WarFoundryObject toSave) | |
61 { | |
62 if (CanSave(toSave)) | |
63 { | |
64 return SaveAs(toSave, savePaths[toSave]); | |
65 } | |
66 else | |
67 { | |
68 throw new InvalidOperationException("Cannot save an object that has not previously been saved using SaveAs"); | |
69 } | |
70 } | |
71 | |
72 private string CreateXmlString(WarFoundryObject toSave) | |
73 { | |
74 return ""; //TODO: Create XML string as appropriate | |
75 } | |
76 | |
77 public bool CanSaveAs(WarFoundryObject obj) | |
78 { | |
79 return CanSaveType(obj); | |
80 } | |
81 | |
82 public bool CanSave(WarFoundryObject obj) | |
83 { | |
84 return savePaths.ContainsKey(obj); | |
85 } | |
86 | |
87 public bool CanSaveType(WarFoundryObject obj) | |
88 { | |
89 return saveAsTypes.Contains(obj.GetType()); | |
90 } | |
91 } | |
92 } |