-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Preferences.cs
178 lines (159 loc) · 7.36 KB
/
Preferences.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Jellyfish.Exceptions;
using Newtonsoft.Json;
namespace Jellyfish
{
/// <summary>
/// An abstract class definition of an application preferences manager which loads and saves JSON Preferences
/// </summary>
public abstract class Preferences
{
/// <summary>
/// The size of the buffer for the filestream in bytes
/// </summary>
public static int FileBufferSize { get; set; } = 4096;
/// <summary>
/// Path to the Application Data directory (%AppData%)
/// </summary>
public static string AppData => Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
/// <summary>
/// Name of the calling executable
/// </summary>
public static string ExecutableName => Process.GetCurrentProcess().ProcessName;
/// <summary>
/// The recommended path of the Preferences file (<c>%AppData%\[app name]\config.json</c>)
/// </summary>
public static string RecommendedPath => System.IO.Path.Combine(AppData, ExecutableName, "config.json");
/// <summary>
/// Load a preferences instance from the given file
/// </summary>
/// <typeparam name="TPreferences">The type of the Preferences (must inherit from <see cref="Preferences" />)</typeparam>
/// <param name="path">The path to the preferences file (See <see cref="RecommendedPath" />)</param>
/// <returns>A deserialized instance of the preferences</returns>
public static TPreferences Load<TPreferences>(string path) where TPreferences : Preferences
{
if (!File.Exists(path))
{
throw new PreferencesFileNotFoundException(path);
}
string json = File.ReadAllText(path);
return JsonConvert.DeserializeObject<TPreferences>(json);
}
/// <summary>
/// Load a preferences instance from the given file asynchronously
/// </summary>
/// <typeparam name="TPreferences">The type of the Preferences (must inherit from <see cref="Preferences" />)</typeparam>
/// <param name="path">The path to the preferences file (See <see cref="RecommendedPath" />)</param>
/// <returns>A deserialized instance of the preferences</returns>
public static async Task<TPreferences> LoadAsync<TPreferences>(string path) where TPreferences : Preferences
{
if (!File.Exists(path))
{
throw new PreferencesFileNotFoundException(path);
}
// Read file async
var bytes = new List<byte>();
using (var sourceStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None,
FileBufferSize, true))
{
int read;
do
{
var buffer = new byte[FileBufferSize];
read = await sourceStream.ReadAsync(buffer, 0, FileBufferSize).ConfigureAwait(false);
bytes.AddRange(buffer);
} while (read > 0);
}
string json = Encoding.Unicode.GetString(bytes.ToArray());
return JsonConvert.DeserializeObject<TPreferences>(json);
}
/// <summary>
/// Load a preferences instance from the given file or return null if not found
/// </summary>
/// <typeparam name="TPreferences">The type of the Preferences (must inherit from <see cref="Preferences" />)</typeparam>
/// <param name="path">The path to the preferences file (See <see cref="RecommendedPath" />)</param>
/// <returns>A deserialized instance of the preferences</returns>
public static TPreferences LoadOrDefault<TPreferences>(string path) where TPreferences : Preferences
{
if (!File.Exists(path))
{
return default(TPreferences);
}
string json = File.ReadAllText(path);
return JsonConvert.DeserializeObject<TPreferences>(json);
}
/// <summary>
/// Load a preferences instance from the given file asynchronously or return null if not found
/// </summary>
/// <typeparam name="TPreferences">The type of the Preferences (must inherit from <see cref="Preferences" />)</typeparam>
/// <param name="path">The path to the preferences file (See <see cref="RecommendedPath" />)</param>
/// <returns>A deserialized instance of the preferences</returns>
public static async Task<TPreferences> LoadOrDefaultAsync<TPreferences>(string path)
where TPreferences : Preferences
{
if (!File.Exists(path))
{
return default(TPreferences);
}
// Read file async
var bytes = new List<byte>();
using (var sourceStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None,
FileBufferSize, true))
{
int read;
do
{
var buffer = new byte[FileBufferSize];
read = await sourceStream.ReadAsync(buffer, 0, FileBufferSize).ConfigureAwait(false);
bytes.AddRange(buffer);
} while (read > 0);
}
string json = Encoding.Unicode.GetString(bytes.ToArray());
return JsonConvert.DeserializeObject<TPreferences>(json);
}
/// <summary>
/// Serialize and save the preferences instance to the preferences file (<see cref="Path" />)
/// </summary>
/// <param name="preferences">The preferences object to save</param>
/// <param name="path">The path to the preferences file</param>
public static void Save<TPreferences>(TPreferences preferences, string path) where TPreferences : Preferences
{
CreateDirectory(path);
string json = JsonConvert.SerializeObject(preferences);
File.WriteAllText(path, json);
}
/// <summary>
/// Serialize and save the preferences instance to the preferences file (<see cref="Path" />) asynchronous
/// </summary>
/// <param name="preferences">The preferences object to save</param>
/// <param name="path">The path to the preferences file</param>
public static async Task SaveAsync<TPreferences>(TPreferences preferences, string path) where TPreferences : Preferences
{
CreateDirectory(path);
string json = JsonConvert.SerializeObject(preferences);
var content = Encoding.Unicode.GetBytes(json);
using (var sourceStream = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.None,
FileBufferSize, true))
{
await sourceStream.WriteAsync(content, 0, content.Length).ConfigureAwait(false);
}
}
private static void CreateDirectory(string path)
{
var file = new FileInfo(path);
var directory = file.Directory;
if (directory != null)
{
if (!directory.Exists)
{
directory.Create();
}
}
}
}
}