Jypeli 10
The simple game programming library
Serialization.cs
Siirry tämän tiedoston dokumentaatioon.
1using System;
2using System.IO;
3
4namespace Jypeli
5{
6 public partial class FileManager
7 {
8 public LoadState BeginLoad( string fileName )
9 {
10 return new LoadState( this, fileName );
11 }
12
13 internal LoadState BeginLoad( StorageFile file, string fileName )
14 {
15 return new LoadState( file, fileName );
16 }
17
18 public SaveState BeginSave( string tag )
19 {
20 return new SaveState( this, tag );
21 }
22
23 public T Load<T>( T obj, string fileName )
24 {
25 if ( !Exists( fileName ) )
26 throw new FileNotFoundException();
27
28 LoadState state = BeginLoad( fileName );
29 T result = state.Load<T>( obj, "default" );
30 state.EndLoad();
31 return result;
32 }
33
34 public T TryLoad<T>( T obj, string fileName )
35 {
36 try
37 {
38 return Load<T>( obj, fileName );
39 }
40 catch ( Exception )
41 {
42 return obj;
43 }
44 }
45
46 public void Save<T>( T obj, string fileName )
47 {
48 SaveState state = BeginSave( fileName );
49 state.Save( obj, typeof( T ), "default" );
50 state.EndSave();
51 }
52
53 public void TrySave<T>( T obj, string fileName )
54 {
55 try
56 {
57 Save<T>( obj, fileName );
58 }
59 catch ( Exception )
60 {
61 }
62 }
63
64 public void Save( object obj, string fileName )
65 {
66 SaveState state = BeginSave( fileName );
67 state.Save( obj, obj.GetType(), "default" );
68 state.EndSave();
69 }
70
71 public void TrySave( object obj, string fileName )
72 {
73 try
74 {
75 Save( obj, fileName );
76 }
77 catch ( Exception )
78 {
79 }
80 }
81
87 public void Export( Stream objStream, string fileName )
88 {
89 using ( var f = Create( fileName ) )
90 {
91 objStream.CopyStreamTo( f.Stream );
92 }
93 }
94 }
95}
SaveState BeginSave(string tag)
T Load< T >(T obj, string fileName)
void TrySave< T >(T obj, string fileName)
T TryLoad< T >(T obj, string fileName)
StorageFile Create(string fileName)
Luo uuden tiedoston
Definition: Files.cs:13
void Export(Stream objStream, string fileName)
Vie virran sisällön tiedostoon.
void Save< T >(T obj, string fileName)
void TrySave(object obj, string fileName)
void Save(object obj, string fileName)
virtual bool Exists(string fileName)
Kertoo onko tiedosto tai hakemisto olemassa.
Definition: Files.cs:23
LoadState BeginLoad(StorageFile file, string fileName)
LoadState BeginLoad(string fileName)
Definition: Serialization.cs:8
object Load(object obj, Type type, string name)
Definition: LoadState.cs:90
void EndLoad()
Definition: LoadState.cs:31
void Save(object obj, Type objType, string name)
Definition: SaveState.cs:47
void EndSave()
Definition: SaveState.cs:64