forked from danarcher/XCom2ModTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettings.cs
85 lines (70 loc) · 2.86 KB
/
Settings.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
using System;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace XCom2ModTool
{
internal class Settings
{
private static readonly string Path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Program.ProductName, "settings.json");
public static Settings Default { get; private set; }
[JsonIgnore]
public bool Debug { get; set; }
[JsonIgnore]
public bool Highlander { get; set; }
[JsonConverter(typeof(EditionJsonConverter))]
public XCom2Edition Edition { get; set; } = XCom2.Base;
public bool UseColoredOutput { get; set; } = true;
[JsonConverter(typeof(StringEnumConverter))]
public ConsoleColor ErrorColor { get; set; } = ConsoleColor.Red;
[JsonConverter(typeof(StringEnumConverter))]
public ConsoleColor WarningColor { get; set; } = ConsoleColor.Yellow;
[JsonConverter(typeof(StringEnumConverter))]
public ConsoleColor InfoColor { get; set; } = ConsoleColor.Gray;
[JsonConverter(typeof(StringEnumConverter))]
public ConsoleColor VerboseColor { get; set; } = ConsoleColor.Gray;
public static void Load()
{
try
{
Default = JsonConvert.DeserializeObject<Settings>(File.ReadAllText(Path));
}
catch (Exception ex) when (ex is DirectoryNotFoundException || ex is FileNotFoundException)
{
Default = new Settings();
}
catch (Exception ex)
{
Report.Exception(ex, "Settings could not be loaded; reverting to defaults");
Default = new Settings();
}
}
public static void Save()
{
try
{
Directory.CreateDirectory(System.IO.Path.GetDirectoryName(Path));
File.WriteAllText(Path, JsonConvert.SerializeObject(Default, Formatting.Indented));
}
catch (Exception ex)
{
Report.Exception(ex, "Settings could not be saved");
}
}
public class EditionJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType) => typeof(XCom2Edition) == objectType;
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var internalName = (string)reader.Value;
return XCom2.Editions.First(x => x.InternalName == internalName);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var edition = (XCom2Edition)value;
writer.WriteValue(edition.InternalName);
}
}
}
}