-
Notifications
You must be signed in to change notification settings - Fork 356
/
Copy pathSaveGame.cs
80 lines (60 loc) · 2.18 KB
/
SaveGame.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using Microsoft.Xna.Framework.Storage;
using System.Xml.Serialization;
using System.IO;
namespace Microsoft.Xna.Samples.Storage
{
[Serializable]
public struct SaveGame
{
public string Name;
public int HiScore;
public DateTime Date;
[NonSerialized]
public int DontKeep;
}
public class SaveGameStorage
{
public void Save(SaveGame sg)
{
StorageDevice device = StorageDevice.ShowStorageDeviceGuide();
// Open a storage container
StorageContainer container = device.OpenContainer("TestStorage");
// Get the path of the save game
string filename = Path.Combine(container.Path, "savegame.xml");
// Open the file, creating it if necessary
FileStream stream = File.Open(filename, FileMode.OpenOrCreate);
// Convert the object to XML data and put it in the stream
XmlSerializer serializer = new XmlSerializer(typeof(SaveGame));
serializer.Serialize(stream, sg);
// Close the file
stream.Close();
// Dispose the container, to commit changes
container.Dispose();
}
public SaveGame Load()
{
SaveGame ret = new SaveGame();
StorageDevice device = StorageDevice.ShowStorageDeviceGuide();
// Open a storage container
StorageContainer container = device.OpenContainer("TestStorage");
// Get the path of the save game
string filename = Path.Combine(container.Path, "savegame.xml");
// Check to see if the save exists
if (!File.Exists(filename))
// Notify the user there is no save
return ret;
// Open the file
FileStream stream = File.Open(filename, FileMode.OpenOrCreate,
FileAccess.Read);
// Read the data from the file
XmlSerializer serializer = new XmlSerializer(typeof(SaveGame));
ret = (SaveGame)serializer.Deserialize(stream);
// Close the file
stream.Close();
// Dispose the container
container.Dispose();
return ret;
}
}
}