64 lines
2.1 KiB
C#
64 lines
2.1 KiB
C#
|
using System;
|
||
|
using System.IO;
|
||
|
using System.Numerics;
|
||
|
using Newtonsoft.Json;
|
||
|
|
||
|
namespace ThemeParkClicker {
|
||
|
public static class SaveHandler {
|
||
|
|
||
|
private static readonly JsonSerializer Serializer = JsonSerializer.Create(new JsonSerializerSettings {
|
||
|
DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate,
|
||
|
TypeNameHandling = TypeNameHandling.Auto,
|
||
|
Formatting = Formatting.Indented
|
||
|
});
|
||
|
private const int SaveVersion = 1;
|
||
|
|
||
|
public static void Save(GameImpl game) {
|
||
|
var file = GetSaveFile(true);
|
||
|
using (var stream = new JsonTextWriter(file.CreateText())) {
|
||
|
var data = new SaveData {
|
||
|
SaveVersion = SaveVersion,
|
||
|
Tickets = game.Tickets,
|
||
|
LastUpdate = game.LastUpdate,
|
||
|
Map = game.Map
|
||
|
};
|
||
|
Serializer.Serialize(stream, data);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static bool Load(GameImpl game) {
|
||
|
var file = GetSaveFile(false);
|
||
|
if (!file.Exists)
|
||
|
return false;
|
||
|
using (var stream = new JsonTextReader(file.OpenText())) {
|
||
|
var data = Serializer.Deserialize<SaveData>(stream);
|
||
|
game.Tickets = data.Tickets;
|
||
|
game.LastUpdate = data.LastUpdate;
|
||
|
game.Map = data.Map;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
public static DirectoryInfo GetGameDirectory(bool create) {
|
||
|
var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
|
||
|
var dir = new DirectoryInfo(Path.Combine(path, "ThemeParkClicker"));
|
||
|
if (!dir.Exists && create)
|
||
|
dir.Create();
|
||
|
return dir;
|
||
|
}
|
||
|
|
||
|
private static FileInfo GetSaveFile(bool create) {
|
||
|
return new FileInfo(Path.Combine(GetGameDirectory(create).FullName, "Save"));
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
public class SaveData {
|
||
|
|
||
|
public int SaveVersion;
|
||
|
public BigInteger Tickets;
|
||
|
public DateTime LastUpdate;
|
||
|
public ParkMap Map;
|
||
|
|
||
|
}
|
||
|
}
|