aboutsummaryrefslogtreecommitdiff
path: root/TimeHACK.Main/SaveDialogs/SaveFileTroubleShooter.cs
blob: eb79543f324a069d9b4970419bbd444ad9d2613c (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
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TimeHACK.Engine;
using System.IO;

namespace TimeHACK.SaveDialogs
{
    public partial class SaveFileTroubleShooter : Form
    {
        public String log;
        Save savedata = new Save();
        String json;
        public SaveFileTroubleShooter()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SaveFileTroubleShooter_Load(object sender, EventArgs e)
        {
            BeginScan();
        }

        void WriteToLog(String toWrite)
        {
            log += toWrite + Environment.NewLine;
        }

        void BeginScan()
        {
            WriteToLog("Started scanning profile: " + SaveSystem.ProfileName);

            // Check if the main.save file exists

            if (!File.Exists(Path.Combine(SaveSystem.ProfileDirectory, "main.save")))
            {
                WriteToLog("ISSUE FOUND! File main.save doesn't exist");

                WriteToLog("Creating one...");

                SaveSystem.NewGame();

                WriteToLog("Created one!");

                EndScan(true);
                return;
            } else {
                WriteToLog("File main.save does exist - checking contents");

                // Read the main.save file
                json = File.ReadAllText(Path.Combine(SaveSystem.ProfileDirectory, "main.save"));

                savedata = Newtonsoft.Json.JsonConvert.DeserializeObject<Save>(json);

                // Check the values

                if (savedata.CurrentOS == null || savedata.CurrentOS == "")
                {
                    WriteToLog("ISSUE FOUND! Data for CurrentOS is null! Giving default value...");
                    savedata.CurrentOS = "95";
                    EndScan(true);
                }

                if (savedata.ExperiencedStories == null)
                {
                    WriteToLog("ISSUE FOUND! Data for ExperiencedStories is null! Giving default value...");
                    savedata.ExperiencedStories = new List<string>();
                }

                if (savedata.ExperiencedStories == null)
                {
                    WriteToLog("ISSUE FOUND! Data for ExperiencedStories is null! Giving default value...");
                    savedata.ExperiencedStories = new List<string>();
                }
            }

            if (!Directory.Exists(Path.Combine(SaveSystem.ProfileDirectory, "folders")))
            {
                WriteToLog("ISSUE FOUND! Directory 'folders' doesn't exist! Creating one...");
                Directory.CreateDirectory(Path.Combine(SaveSystem.ProfileDirectory, "folders"));
                SaveSystem.CheckFiles();
            }


        }

        void EndScan(Boolean successful)
        {
            pnlResolved.Visible = true;
            if (successful == true)
            {
                label2.Text = "The issue has been resolved.";
                // Set CurrentSave to the resolved one

                SaveSystem.CurrentSave = savedata;

                // Set the main.save file to the resolved one

                File.WriteAllText(Path.Combine(SaveSystem.ProfileDirectory, "main.save"), Newtonsoft.Json.JsonConvert.SerializeObject(savedata));

                textBox1.Text = log;
            } else {
                label2.Text = "The issue has not been resolved, sorry";
                textBox1.Text = log;
            }
        }
    }
}