aboutsummaryrefslogtreecommitdiff
path: root/TimeHACK.Engine/SaveSystem.cs
blob: 89c9cb0ea2f8ddec2bea2f0fb9bb35a7bbe109c1 (plain) (blame)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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 Boolean DevMode = false;

        public static string GameDirectory
        {
            get
            {
                return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "TimeHACK");
            }
        }

        public static string AllProfilesDirectory
        {
            get
            {
                return Path.Combine(GameDirectory, "Profiles");
            }
        }

        public static string ProfileName = "";
        public static string ProfileFile = "main.save";

        public static string ProfileDirectory
        {
            get
            {
                return Path.Combine(GameDirectory, Path.Combine("Profiles", ProfileName));
            }
        }

        public static bool LoadSave()
        {
            // ON A FINAL RELEASE USE THE "FINAL RELEASE THINGS"
            #region Final Release Things
            //Read base64 string from file
            //string b64 = File.ReadAllText(Path.Combine(ProfileDirectory, ProfileFile));
            //Get Unicode byte array
            //byte[] bytes = Convert.FromBase64String(b64);
            //Decode the Unicode
            //string json = Encoding.UTF8.GetString(bytes);
            //Deserialize save object.
            #endregion
            // USE THE THINGS IN THE "DEVELOPER THINGS" FOR A DEVELOPMENT RELEASE
            #region Developer Things
            string json = File.ReadAllText(Path.Combine(ProfileDirectory, ProfileFile));
            #endregion
            CurrentSave = JsonConvert.DeserializeObject<Save>(json);
            return true;
        }

        public static void NewGame()
        {

            //TODO: User must set a username....somehow
            if (!Directory.Exists(GameDirectory))
                Directory.CreateDirectory(GameDirectory);

            if (!Directory.Exists(AllProfilesDirectory))
                Directory.CreateDirectory(AllProfilesDirectory);

            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, Formatting.Indented);

            // ADD THE TWO LINES OF CODE BELOW ON A FINAL RELEASE
            //Get JSON bytes (Unicode format).
            //var bytes = Encoding.UTF8.GetBytes(json);
            //Encode the array into Base64.
            //string b64 = Convert.ToBase64String(bytes);
            //Write to disk.

            // CHANGE THE "JSON" TO "B64" ON A FINAL RELEASE!
            File.WriteAllText(Path.Combine(ProfileDirectory, ProfileFile), json);
        }
    }

    public class Save
    {
        public string Username { get; set; }
        public Dictionary<string, bool> InstalledPrograms { get; set; }
        public List<string> ExperiencedStories { get; set; }
    }
}