-
Notifications
You must be signed in to change notification settings - Fork 6
/
kConfig.cs
145 lines (126 loc) · 4.24 KB
/
kConfig.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.IO;
using Newtonsoft.Json;
using Terraria;
using Terraria.ModLoader;
namespace kRPG
{
public class kConfig
{
//Make this fancier?
private static Config _configLocal = new Config();
private static Config _configServer = new Config();
private static ConfigStats _stats = new ConfigStats();
public static ClientConfig ClientSide => ConfigLocal.ClientSide;
public static Config ConfigLocal
{
get
{
if (_configLocal != null)
return _configLocal;
_configLocal = new Config();
LoadConfig(ConfigPath, ref _configLocal);
return _configLocal;
}
private set => _configLocal = value;
}
public static string ConfigPath => Main.SavePath + Path.DirectorySeparatorChar + "kRPG_Settings.json";
public static Config ConfigServer
{
get => _configServer ?? (_configServer = new Config());
private set => _configServer = value;
}
public static ConfigStats Stats
{
get
{
if (_stats != null)
return _stats;
_stats = new ConfigStats();
LoadConfig(StatsPath, ref _stats);
return _stats;
}
private set => _stats = value;
}
public static string StatsPath => Main.SavePath + Path.DirectorySeparatorChar + "kRPG_Stats.json";
public static void Initialize()
{
try
{
ConfigLocal = new Config();
Stats = new ConfigStats();
Load();
}
catch (SystemException e)
{
ModLoader.GetMod(Constants.ModName).Logger.InfoFormat(e.ToString());
}
}
public static void Load()
{
try
{
Directory.CreateDirectory(Main.SavePath);
_configLocal = new Config();
LoadConfig(ConfigPath, ref _configLocal);
if (_configLocal == null) _configLocal = new Config();
Save();
_stats = new ConfigStats();
LoadConfig(StatsPath, ref _stats);
if (_stats == null) _stats = new ConfigStats();
SaveStats();
}
catch (SystemException e)
{
ModLoader.GetMod(Constants.ModName).Logger.InfoFormat(e.ToString());
}
}
private static void LoadConfig<T>(string path, ref T config) where T : class
{
try
{
if (!File.Exists(path))
return;
using (StreamReader reader = new StreamReader(path))
{
config = JsonConvert.DeserializeObject<T>(reader.ReadToEnd());
}
}
catch (SystemException e)
{
ModLoader.GetMod(Constants.ModName).Logger.InfoFormat(e.ToString());
}
}
public static void Save()
{
try
{
Directory.CreateDirectory(Main.SavePath);
File.WriteAllText(ConfigPath, JsonConvert.SerializeObject(ConfigLocal, Formatting.Indented).Replace(" ", "\t"));
}
catch (SystemException e)
{
ModLoader.GetMod(Constants.ModName).Logger.InfoFormat(e.ToString());
}
}
public static void SaveStats()
{
Directory.CreateDirectory(Main.SavePath);
File.WriteAllText(StatsPath, JsonConvert.SerializeObject(Stats, Formatting.Indented).Replace(" ", "\t"));
}
public class ClientConfig
{
public bool ArpgMiniMap { get; set; } = false;
public bool ManualInventory { get; set; } = false;
public bool SmartInventory { get; set; } = false;
}
public class Config
{
public ClientConfig ClientSide { get; set; } = new ClientConfig();
}
public class ConfigStats
{
public string LastStartVersion { get; set; } = "1.1.1";
}
}
}