-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathJsonDatabase.cs
More file actions
157 lines (135 loc) · 4.45 KB
/
Copy pathJsonDatabase.cs
File metadata and controls
157 lines (135 loc) · 4.45 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Intersect.Configuration;
using Intersect.Core;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Intersect.Client.Framework.Database;
public class JsonDatabase : GameDatabase
{
private readonly string _instancePath;
private JObject? _instance;
public JsonDatabase()
{
_instancePath = GetInstancePath(ClientConfiguration.Instance);
_ = TryOpenOrCreate(_instancePath, out _instance);
}
public JsonDatabase(string path)
{
_instancePath = path;
_ = TryOpenOrCreate(_instancePath, out _instance);
}
private static string GetInstancePath(ClientConfiguration instance)
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), ".intersect",
Assembly.GetEntryAssembly().GetName().Name, $"{instance.Host}.{instance.Port}.json");
}
private bool TryOpenOrCreate(string instancePath, [NotNullWhen(true)] out JObject? instance)
{
try
{
var fileInfo = new FileInfo(instancePath);
if (!fileInfo.Exists)
{
if (!fileInfo.Directory!.Exists)
{
fileInfo.Directory.Create();
}
//Allow game devs to provide default settings
var defaultSettingsJson = Path.Combine("resources", "default_settings.json");
if (File.Exists(defaultSettingsJson))
{
File.Copy(defaultSettingsJson, instancePath);
}
else
{
using var streamWriter = fileInfo.CreateText();
streamWriter.WriteLine("{}");
instance = new JObject();
return true;
}
}
using var streamReader = fileInfo.OpenText();
var json = streamReader.ReadToEnd();
instance = JObject.Parse(json);
return true;
}
catch (Exception exception)
{
ApplicationContext.Context.Value?.Logger.LogError(
exception,
$"Failed to open or create part or all of the path: {instancePath}"
);
instance = default;
return false;
}
}
private bool TryWrite(string instancePath, JObject instance)
{
try
{
var fileInfo = new FileInfo(instancePath);
if (!fileInfo.Exists)
{
if (!fileInfo.Directory!.Exists)
{
fileInfo.Directory.Create();
}
}
using var streamWriter = fileInfo.CreateText();
var json = instance.ToString(
#if DEBUG
Formatting.Indented
#else
Formatting.None
#endif
);
streamWriter.WriteLine(json);
return true;
}
catch (Exception exception)
{
ApplicationContext.Context.Value?.Logger.LogError(
exception,
$"Failed to open or create part or all of the path, or failed while writing to: {instancePath}"
);
instance = default;
return false;
}
}
private void Persist() => _ = TryWrite(_instancePath, _instance);
public override void DeletePreference(string key)
{
_instance?.Remove(key);
Persist();
}
public override bool HasPreference(string key) => _instance?.ContainsKey(key) ?? false;
public override void SavePreference<TValue>(string key, TValue value)
{
if (_instance == default)
{
return;
}
var stringifiedValue = Convert.ToString(value);
_instance[key] = JValue.CreateString(stringifiedValue);
Persist();
}
public override string LoadPreference(string key)
{
var token = _instance?[key];
if (token == default)
{
return string.Empty;
}
if (token.Type == JTokenType.String)
{
return token.Value<string>();
}
ApplicationContext.Context.Value?.Logger.LogWarning(
$"Found invalid type {token.Type} stored in {key} instead of {JTokenType.String}."
);
return string.Empty;
}
public override bool LoadConfig() => ClientConfiguration.LoadAndSave() != default;
}