Jypeli 10
The simple game programming library
LoadState.cs
Siirry tämän tiedoston dokumentaatioon.
1using System;
2using System.Collections.Generic;
3using System.Xml;
4using System.IO;
5
6namespace Jypeli
7{
8 public class LoadState : IDisposable
9 {
10 public StorageFile File { get; private set; }
11
13 private XmlReader reader;
14 private List<String> objectsRead;
15
16 internal LoadState( FileManager manager, string fileName )
17 {
18 this.manager = manager;
19 File = manager.Open( fileName, false );
21 objectsRead = new List<string>();
22 }
23
24 internal LoadState( StorageFile file, string fileName )
25 {
26 File = file;
28 objectsRead = new List<string>();
29 }
30
31 public void EndLoad()
32 {
33#if WINDOWS_STOREAPP
34 reader.Dispose();
35#else
36 reader.Close();
37#endif
38 File.Close();
39 }
40
41 public void Dispose()
42 {
43#if WINDOWS_STOREAPP
44 reader.Dispose();
45#else
46 reader.Close();
47#endif
48 File.Close();
49 }
50
51 private void ResetFile()
52 {
53#if WINDOWS_STOREAPP
54 reader.Dispose();
55#else
56 reader.Close();
57#endif
58 reader = null;
59 objectsRead.Clear();
60
61 if ( File.Stream.CanSeek )
62 {
63 File.Stream.Seek( 0, System.IO.SeekOrigin.Begin );
64 }
65 else
66 {
67 string fileName = File.Name;
68 File.Close();
69 File = manager.Open( fileName, false );
70 }
71
73 }
74
75 private void BeginReadXml()
76 {
77 if ( reader != null )
78 return;
79
80 reader = XmlReader.Create( File.Stream );
81 reader.Read();
82 if ( !reader.IsStartElement( "State" ) ) throw new ArgumentException( "File is corrupted or not a save state file." );
83 }
84
85 public T Load<T>( T obj, string name )
86 {
87 return (T)Load( obj, typeof( T ), name );
88 }
89
90 public object Load( object obj, Type type, string name )
91 {
92 if ( objectsRead.Contains( name ) )
93 {
94 // Start from beginning
95 ResetFile();
96 }
97 else
99
100 int depth = 0;
101
102 while ( !reader.EOF )
103 {
104 reader.Read();
105
106 if ( reader.NodeType == XmlNodeType.None )
107 throw new IOException( "Error loading object: reader not initialized" );
108
109 if ( reader.NodeType == XmlNodeType.Element )
110 {
111 if ( depth == 0 )
112 {
113 if ( reader.Name != "Object" )
114 throw new XmlException( "Unexpected element in save state file: " + reader.Name );
115
116 string objName = reader.GetAttribute( "Name" );
117 if ( string.IsNullOrEmpty( objName ) )
118 throw new XmlException( "Unnamed object in save state file" );
119
120 if ( name == objName )
121 return File.LoadData( reader, type, obj );
122 else
123 objectsRead.Add( objName );
124 }
125
126 if ( !reader.IsEmptyElement )
127 depth++;
128 }
129 else if ( reader.NodeType == XmlNodeType.EndElement )
130 {
131 depth--;
132 if ( depth < 0 ) throw new InvalidOperationException( "Error loading object: parse depth < 0" );
133 }
134 }
135
136 throw new KeyNotFoundException( "Object with name " + name + " could not be found in file " + File.Name );
137 }
138 }
139}
virtual StorageFile Open(string fileName, bool write)
Avaa tiedoston.
Definition: Files.cs:36
object Load(object obj, Type type, string name)
Definition: LoadState.cs:90
LoadState(StorageFile file, string fileName)
Definition: LoadState.cs:24
StorageFile File
Definition: LoadState.cs:10
XmlReader reader
Definition: LoadState.cs:13
List< String > objectsRead
Definition: LoadState.cs:14
void EndLoad()
Definition: LoadState.cs:31
void BeginReadXml()
Definition: LoadState.cs:75
void Dispose()
Definition: LoadState.cs:41
void ResetFile()
Definition: LoadState.cs:51
T Load< T >(T obj, string name)
Definition: LoadState.cs:85
LoadState(FileManager manager, string fileName)
Definition: LoadState.cs:16
FileManager manager
Definition: LoadState.cs:12
object LoadData(XmlReader reader, Type type, object obj)
Definition: StorageFile.cs:81