-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIsolatedStorageSaveManager.cs
71 lines (63 loc) · 2.64 KB
/
IsolatedStorageSaveManager.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
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace MonoGameSaveManager
{
/// <summary>
/// Uses the .NET IsolatedStorage to load/save game data.
/// Saves end up on "C:\Users\...\AppData\Local\IsolatedStorage\..." on Windows.
/// </summary>
public class IsolatedStorageSaveManager : SaveManager
{
/// <summary>
/// /// <summary>
/// Creates a new save game manager based on .NET IsolatedStorage.
/// </summary>
/// <param name="folderName">Name of the folder containing the save.</param>
/// <param name="fileName">Name of the save file.</param>
public IsolatedStorageSaveManager(string folderName, string fileName)
: base(folderName, fileName)
{ }
public override void Load()
{
using (IsolatedStorageFile isf = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))
{
// Ignore if directory doesn't exist.
if (!isf.DirectoryExists(folderName))
return;
string filePath = Path.Combine(folderName, fileName);
// Ignore if save doesn't exist.
if (!isf.FileExists(filePath))
return;
// Open the save file.
using (IsolatedStorageFileStream stream = isf.OpenFile(filePath, FileMode.Open))
{
// Get the XML data from the stream and convert it to object.
XmlSerializer serializer = new XmlSerializer(typeof(SaveData));
Data = (SaveData)serializer.Deserialize(stream);
}
}
}
public override void Save()
{
using (IsolatedStorageFile isf = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))
{
// Create directory if it doesn't exist.
if (!isf.DirectoryExists(folderName))
isf.CreateDirectory(folderName);
string filePath = Path.Combine(folderName, fileName);
// Create new save file.
using (IsolatedStorageFileStream stream = isf.CreateFile(filePath))
{
// Convert the object to XML data and put it in the stream.
XmlSerializer serializer = new XmlSerializer(typeof(SaveData));
serializer.Serialize(stream, Data);
}
}
}
}
}