-
Notifications
You must be signed in to change notification settings - Fork 0
/
Configuration.cs
95 lines (87 loc) · 3.01 KB
/
Configuration.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
using ModAPI.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json.Linq;
namespace ModAPI
{
public partial class Configuration
{
private static NLog.ILogger Logger = NLog.LogManager.GetLogger("Configuration");
private static bool Loading = false;
private static JObject ConfigObject;
public static Dictionary<string, Configuration.Game> Games = new Dictionary<string, Configuration.Game>();
static Configuration()
{
var configFile = Path.Combine(DataDirectory, "config.json");
Loading = true;
if (File.Exists(configFile))
{
try
{
ConfigObject = JObject.Parse(File.ReadAllText(configFile));
}
catch (Exception e)
{
Logger.Error(e, "Error while loading configuration");
}
}
/*Games = new List<Game>()
{
new Game("hokkolife", "Hokko Life", "Hokko Life.exe"),
new Game("sunhaven", "Sun Haven", "Sun Haven.exe")
};*/
Loading = false;
if (File.Exists("Games.json"))
{
var games = JObject.Parse(File.ReadAllText("Games.json"));
foreach (var game in games)
{
if (game.Value is JObject j)
Games.Add(game.Key, new Configuration.Game(game.Key, j));
}
}
else Logger.Warn("Games.json does not exist.");
}
public static JObject GetGameConfiguration(string id)
{
if (ConfigObject.ContainsKey(id) && ConfigObject[id] is JObject gameConfig)
{
return gameConfig;
}
return null;
}
public static void Save()
{
if (Loading) return;
JObject config = new JObject();
foreach (var game in Games)
{
config[game.Key] = App.Instance.Games[game.Key].GetConfiguration();
}
var configFile = Path.Combine(DataDirectory, "config.json");
File.WriteAllText(configFile, config.ToString());
}
private static string _DataDirectory;
public static string DataDirectory
{
get
{
if (_DataDirectory == null)
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ModAPI");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
_DataDirectory = path;
}
}
return _DataDirectory;
}
}
}
}