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