-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigHandler.cs
86 lines (83 loc) · 2.38 KB
/
ConfigHandler.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
namespace esthar_practice
{
class ConfigHandler
{
readonly string configName = "save.json";
readonly string configPath;
public ConfigHandler()
{
string appPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
configPath = Path.Combine(appPath, configName);
}
public List<SavedValue> LoadJson()
{
List<SavedValue> savedValues = new List<SavedValue>();
if (File.Exists(configPath))
{
using (StreamReader r = new StreamReader(configPath))
{
string json = r.ReadToEnd();
try
{
savedValues = JsonConvert.DeserializeObject<List<SavedValue>>(json);
}
catch (Newtonsoft.Json.JsonReaderException)
{
SavedValue val = new SavedValue
{
name = "Error- Check save.json",
};
savedValues.Add(val);
}
}
}
return savedValues;
}
public void SaveJson(List<SavedValue> values)
{
string json = JsonConvert.SerializeObject(values, Formatting.Indented);
try
{
File.WriteAllText(configPath, json);
}
catch (Exception)
{
// Future error handling here
}
}
}
public class SavedValue
{
public string name;
public int StepId;
public int StepFrac;
public int TotalEncs;
public int DangerValue;
public int Offset;
public int LastEncId;
public string Dump()
{
return JsonConvert.SerializeObject(this);
}
public bool IsValid()
{
if (StepId >= 0
&& StepFrac >= 0
&& TotalEncs >= 0
&& DangerValue >= 0
&& Offset >= 0
&& LastEncId >= 0)
{
return true;
}
return false;
}
}
}