-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSettingsLoader.cs
193 lines (173 loc) · 6.1 KB
/
SettingsLoader.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using Newtonsoft.Json;
using RSG.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEngine;
namespace RSG
{
/// <summary>
/// Represents a particular settings file.
/// </summary>
public interface ISettings<SettingsT>
where SettingsT : new()
{
/// <summary>
/// The loaded settings data.
/// </summary>
SettingsT Data
{
get;
}
/// <summary>
/// Event raised when the underlying settings file has changed and the settings have been reloaded.
/// </summary>
event EventHandler<EventArgs> SettingsChanged;
}
/// <summary>
/// Represents a particular settings file.
/// </summary>
public class Settings<SettingsT> : ISettings<SettingsT>
where SettingsT : new()
{
/// <summary>
/// The settings file that is loaded and watched for reload.
/// </summary>
private string settingsFilePath;
private Utils.ILogger logger;
/// <summary>
/// Used for delegating actions to the main thread as it is very difficult to play with the Unity API on a worker thread.
/// </summary>
private IDispatcher dispatcher;
/// <summary>
/// The loaded settings data.
/// </summary>
public SettingsT Data
{
get;
private set;
}
/// <summary>
/// Event raised when the underlying settings file has changed and the settings have been reloaded.
/// </summary>
public event EventHandler<EventArgs> SettingsChanged;
public Settings(string settingsFilePath, Utils.ILogger logger, IDispatcher dispatcher)
{
Argument.StringNotNullOrEmpty(() => settingsFilePath);
Argument.NotNull(() => logger);
Argument.NotNull(() => dispatcher);
this.settingsFilePath = settingsFilePath;
this.logger = logger;
this.dispatcher = dispatcher;
try
{
LoadSettings();
}
catch (Exception)
{
this.Data = new SettingsT(); // Error has already been reported.
}
WatchConfigFile();
}
/// <summary>
/// Load (or reload) the settings files.
/// </summary>
private void LoadSettings()
{
if (File.Exists(settingsFilePath))
{
try
{
this.Data = JsonConvert.DeserializeObject<SettingsT>(File.ReadAllText(settingsFilePath));
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to load settings file {SettingsFilePath}", settingsFilePath);
throw ex;
}
}
else
{
this.Data = new SettingsT();
}
}
/// <summary>
/// Watch the config file for changes, and reload as necessary.
/// </summary>
private void WatchConfigFile()
{
if (!File.Exists(settingsFilePath))
{
logger.LogError("Not going to watch settings directory {SettingsFilePath}, this directory doesn't exist.", settingsFilePath);
return;
}
try
{
var watcher = new FileSystemWatcher();
watcher.Path = Path.GetDirectoryName(settingsFilePath);
watcher.Filter = Path.GetFileName(settingsFilePath);
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Changed +=
(object sender, FileSystemEventArgs e) =>
{
try
{
LoadSettings();
}
catch
{
// Error has already been reported, swallow errors but don't replace the data.
}
if (SettingsChanged != null)
{
dispatcher.InvokeAsync(() => SettingsChanged(this, EventArgs.Empty));
}
};
watcher.EnableRaisingEvents = true;
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to set watch on file {SettingsFilePath}", settingsFilePath);
}
}
}
/// <summary>
/// For loading, saving and managing settings.
/// </summary>
public interface ISettingsLoader
{
/// <summary>
/// Load typed settings.
/// If requested file doesn't exist, a default settings object will be created.
/// </summary>
ISettings<SettingsT> Load<SettingsT>(string settingsName)
where SettingsT : new();
}
/// <summary>
/// For loading, saving and managing settings.
/// </summary>
[LazySingleton(typeof(ISettingsLoader))]
public class SettingsLoader : ISettingsLoader
{
[Dependency]
public Utils.ILogger Logger { get; set; }
[Dependency]
public IDispatcher Dispatcher { get; set; }
/// <summary>
/// The path that contains settings.
/// </summary>
public string settingsFolderPath = Path.Combine(Application.dataPath, "Settings");
/// <summary>
/// Load typed settings.
/// If requested file doesn't exist, a default settings object will be created.
/// </summary>
public ISettings<SettingsT> Load<SettingsT>(string settingsName)
where SettingsT : new()
{
var settingsFilePath = Path.Combine(settingsFolderPath, settingsName + ".json");
return new Settings<SettingsT>(settingsFilePath, Logger, Dispatcher);
}
}
}