Jypeli 10
The simple game programming library
SaveState.cs
Siirry tämän tiedoston dokumentaatioon.
1using System;
2using System.Text;
3using System.Xml;
4using System.IO;
5
6namespace Jypeli
7{
8 public class SaveState : IDisposable
9 {
10 public StorageFile File { get; private set; }
11
12 private XmlWriter writer;
13 private XmlWriterSettings settings;
14 private bool closed = false;
15
16 internal SaveState( FileManager manager, string fileName )
17 {
18 File = manager.Open( fileName, true );
19 settings = new XmlWriterSettings();
20 settings.ConformanceLevel = ConformanceLevel.Document;
21 settings.Encoding = Encoding.UTF8;
22 settings.Indent = true;
24 writer.WriteStartDocument();
25 writer.WriteStartElement( "State" );
26 }
27
28 internal void BeginWriteXml()
29 {
30 if ( closed )
31 throw new IOException( "Tried to write to a closed state file." );
32
33 if ( writer == null )
34 writer = XmlWriter.Create( File.Stream, settings );
35 }
36
37 public void Dispose()
38 {
39 EndSave();
40 }
41
42 public void Save<T>( object obj, string name )
43 {
44 Save( obj, typeof( T ), name );
45 }
46
47 public void Save( object obj, Type objType, string name )
48 {
50 writer.WriteStartElement( "Object" );
51 writer.WriteAttributeString( "Name", name );
52
53#if WINDOWS_STOREAPP
54 writer.WriteAttributeString( "TypeAssembly", objType.AssemblyQualifiedName );
55#else
56 writer.WriteAttributeString( "TypeAssembly", objType.Assembly.FullName );
57#endif
58
59 writer.WriteAttributeString( "Type", objType.Name );
60 File.SaveData( writer, objType, obj, false );
61 writer.WriteEndElement();
62 }
63
64 public void EndSave()
65 {
66 if ( !closed )
67 {
69 writer.WriteEndElement();
70 writer.WriteEndDocument();
71 closed = true;
72 }
73
74 if ( writer != null )
75 {
76#if WINDOWS_STOREAPP
77 writer.Dispose();
78#else
79 writer.Close();
80#endif
81 writer = null;
82 }
83
84 if ( File != null )
85 {
86 File.Close();
87 File = null;
88 }
89 }
90 }
91}
virtual StorageFile Open(string fileName, bool write)
Avaa tiedoston.
Definition: Files.cs:36
void Save(object obj, Type objType, string name)
Definition: SaveState.cs:47
XmlWriterSettings settings
Definition: SaveState.cs:13
SaveState(FileManager manager, string fileName)
Definition: SaveState.cs:16
XmlWriter writer
Definition: SaveState.cs:12
void Save< T >(object obj, string name)
Definition: SaveState.cs:42
void EndSave()
Definition: SaveState.cs:64
void BeginWriteXml()
Definition: SaveState.cs:28
StorageFile File
Definition: SaveState.cs:10
void Dispose()
Definition: SaveState.cs:37
void SaveData(XmlWriter writer, Type type, object obj, bool saveAllFields)
Definition: StorageFile.cs:211