diff options
| author | Michael <[email protected]> | 2017-05-07 20:04:31 -0400 |
|---|---|---|
| committer | Michael <[email protected]> | 2017-05-07 20:04:31 -0400 |
| commit | 9e2582cd3f5b2bf6f3e8c2d6434ab06ea97832a7 (patch) | |
| tree | 21a0103d5d44aeffe26fe0dd0f3253b2dd4e5957 /TimeHACK.Engine/SaveSystem.cs | |
| parent | 1220d4d995b1cf0c64e0a38e33a81f96c699858f (diff) | |
| download | histacom2-9e2582cd3f5b2bf6f3e8c2d6434ab06ea97832a7.tar.gz histacom2-9e2582cd3f5b2bf6f3e8c2d6434ab06ea97832a7.tar.bz2 histacom2-9e2582cd3f5b2bf6f3e8c2d6434ab06ea97832a7.zip | |
Implement a proper save system
Diffstat (limited to 'TimeHACK.Engine/SaveSystem.cs')
| -rw-r--r-- | TimeHACK.Engine/SaveSystem.cs | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/TimeHACK.Engine/SaveSystem.cs b/TimeHACK.Engine/SaveSystem.cs new file mode 100644 index 0000000..c6e19a2 --- /dev/null +++ b/TimeHACK.Engine/SaveSystem.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; + +namespace TimeHACK.Engine +{ + public static class SaveSystem + { + public static Save CurrentSave { get; private set; } + + public static string GameDirectory + { + get + { + return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "TimeHACK"); + } + } + + public static string ProfileDirectory + { + get + { + return Path.Combine(GameDirectory, "Profiles"); + } + } + + public static bool LoadSave() + { + if(File.Exists(Path.Combine(ProfileDirectory, "user.save"))) + { + //Read base64 string from file + string b64 = File.ReadAllText(Path.Combine(ProfileDirectory, "user.save")); + //Get Unicode byte array + byte[] bytes = Convert.FromBase64String(b64); + //Decode the Unicode + string json = Encoding.UTF8.GetString(bytes); + //Deserialize save object. + CurrentSave = JsonConvert.DeserializeObject<Save>(json); + return true; + } + else + { + NewGame(); + return false; + } + } + + public static void NewGame() + { + //TODO: User must set a username....somehow + if (!Directory.Exists(GameDirectory)) + Directory.CreateDirectory(GameDirectory); + + if (!Directory.Exists(ProfileDirectory)) + Directory.CreateDirectory(ProfileDirectory); + + var save = new Save(); + save.ExperiencedStories = new List<string>(); + save.InstalledPrograms = new Dictionary<string, bool>(); + CurrentSave = save; + SaveGame(); + } + + public static void SaveGame() + { + //Serialize the save to JSON. + string json = JsonConvert.SerializeObject(CurrentSave); + //Get JSON bytes (Unicode format). + var bytes = Encoding.UTF8.GetBytes(json); + //Encode the array into Base64. + string b64 = Convert.ToBase64String(bytes); + //Write to disk. + File.WriteAllText(Path.Combine(ProfileDirectory, "user.save"), b64); + } + } + + public class Save + { + public string Username { get; set; } + public Dictionary<string, bool> InstalledPrograms { get; set; } + public List<string> ExperiencedStories { get; set; } + } +} |
