-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppInfo.cs
More file actions
185 lines (159 loc) · 6.54 KB
/
AppInfo.cs
File metadata and controls
185 lines (159 loc) · 6.54 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
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
// Copyright (c) 2019-2022 ReactiveUI Association Incorporated. All rights reserved.
// ReactiveUI Association Incorporated licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using ReactiveMarbles.CacheDatabase.Core;
using ReactiveMarbles.CacheDatabase.Settings.Core;
#if ENCRYPTED
using ReactiveMarbles.CacheDatabase.EncryptedSqlite3;
#else
using ReactiveMarbles.CacheDatabase.Sqlite3;
#endif
using System.Reflection;
#if ENCRYPTED
namespace ReactiveMarbles.CacheDatabase.EncryptedSettings
#else
namespace ReactiveMarbles.CacheDatabase.Settings
#endif
{
/// <summary>
/// App Info.
/// </summary>
public static class AppInfo
{
static AppInfo()
{
SettingsStores = new();
BlobCaches = new();
ExecutingAssemblyName = ExecutingAssembly.FullName!.Split(',')[0];
ApplicationRootPath = Path.Combine(Path.GetDirectoryName(ExecutingAssembly.Location)!, "..");
SettingsCachePath = Path.Combine(ApplicationRootPath, "SettingsCache");
Version = ExecutingAssembly.GetName().Version;
}
/// <summary>
/// Gets the application root path.
/// </summary>
/// <value>
/// The application root path.
/// </value>
public static string? ApplicationRootPath { get; }
/// <summary>
/// Gets the settings cache path.
/// </summary>
/// <value>
/// The settings cache path.
/// </value>
public static string? SettingsCachePath { get; private set; }
/// <summary>
/// Gets the executing assembly.
/// </summary>
/// <value>
/// The executing assembly.
/// </value>
public static Assembly ExecutingAssembly => Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();
/// <summary>
/// Gets the name of the executing assembly.
/// </summary>
/// <value>
/// The name of the executing assembly.
/// </value>
public static string? ExecutingAssemblyName { get; }
/// <summary>
/// Gets the version.
/// </summary>
/// <value>
/// The version.
/// </value>
public static Version? Version { get; }
internal static Dictionary<string, IBlobCache?> BlobCaches { get; }
internal static Dictionary<string, ISettingsStorage?> SettingsStores { get; }
/// <summary>
/// Overrides the settings cache path.
/// </summary>
/// <param name="path">The path.</param>
public static void OverrideSettingsCachePath(string path) => SettingsCachePath = path;
/// <summary>
/// Deletes the settings store.
/// </summary>
/// <typeparam name="T">The type of store to delete.</typeparam>
/// <param name="overrideDatabaseName">Name of the override database.</param>
/// <returns>
/// A Task.
/// </returns>
public static async Task DeleteSettingsStore<T>(string? overrideDatabaseName = null)
{
await DisposeSettingsStore<T>().ConfigureAwait(false);
File.Delete(Path.Combine(SettingsCachePath!, $"{overrideDatabaseName ?? typeof(T).Name}.db"));
}
/// <summary>
/// Gets the settings store.
/// </summary>
/// <typeparam name="T">The store to get.</typeparam>
/// <param name="overrideDatabaseName">Name of the override database.</param>
/// <returns>
/// A Settings Store.
/// </returns>
public static ISettingsStorage? GetSettingsStore<T>(string? overrideDatabaseName = null) =>
SettingsStores[overrideDatabaseName ?? typeof(T).Name];
/// <summary>
/// Disposes the settings store.
/// </summary>
/// <typeparam name="T">The type of store.</typeparam>
/// <param name="overrideDatabaseName">Name of the override database.</param>
/// <returns>
/// A Task.
/// </returns>
public static async Task DisposeSettingsStore<T>(string? overrideDatabaseName = null)
{
await GetSettingsStore<T>(overrideDatabaseName)!.DisposeAsync().ConfigureAwait(false);
await BlobCaches[overrideDatabaseName ?? typeof(T).Name]!.DisposeAsync().ConfigureAwait(false);
}
#if ENCRYPTED
/// <summary>
/// Setup the secure settings store.
/// </summary>
/// <typeparam name="T">The Type of settings store.</typeparam>
/// <param name="password">Secure password.</param>
/// <param name="initialise">Initialise the Settings values.</param>
/// <param name="overrideDatabaseName">Name of the override database.</param>
/// <returns>
/// The Settings store.
/// </returns>
public static async Task<T?> SetupSettingsStore<T>(string password, bool initialise = true, string? overrideDatabaseName = null)
where T : ISettingsStorage?, new()
{
Directory.CreateDirectory(SettingsCachePath!);
BlobCaches[typeof(T).Name] = new EncryptedSqliteBlobCache(Path.Combine(SettingsCachePath!, $"{overrideDatabaseName ?? typeof(T).Name}.db"), password);
var viewSettings = new T();
SettingsStores[typeof(T).Name] = viewSettings;
if (initialise)
{
await viewSettings.InitializeAsync().ConfigureAwait(false);
}
return viewSettings;
}
#else
/// <summary>
/// Setup the settings store.
/// </summary>
/// <typeparam name="T">The Type of settings store.</typeparam>
/// <param name="initialise">Initialise the Settings values.</param>
/// <param name="overrideDatabaseName">Name of the override database.</param>
/// <returns>
/// The Settings store.
/// </returns>
public static async Task<T?> SetupSettingsStore<T>(bool initialise = true, string? overrideDatabaseName = null)
where T : ISettingsStorage?, new()
{
Directory.CreateDirectory(SettingsCachePath!);
BlobCaches[typeof(T).Name] = new SqliteBlobCache(Path.Combine(SettingsCachePath!, $"{overrideDatabaseName ?? typeof(T).Name}.db"));
var viewSettings = new T();
SettingsStores[typeof(T).Name] = viewSettings;
if (initialise)
{
await viewSettings.InitializeAsync().ConfigureAwait(false);
}
return viewSettings;
}
#endif
}
}