forked from everythingfunctional/fpm-for-VS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseOptionModel.cs
186 lines (162 loc) · 6.36 KB
/
BaseOptionModel.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading.Tasks;
using Microsoft;
using Microsoft.VisualStudio.Settings;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell.Settings;
using Microsoft.VisualStudio.Threading;
using Task = System.Threading.Tasks.Task;
namespace fpm_for_VS.Options
{
/// <summary>
/// A base class for specifying options
/// </summary>
internal abstract class BaseOptionModel<T> where T : BaseOptionModel<T>, new()
{
private static AsyncLazy<T> _liveModel = new AsyncLazy<T>(CreateAsync, ThreadHelper.JoinableTaskFactory);
private static AsyncLazy<ShellSettingsManager> _settingsManager = new AsyncLazy<ShellSettingsManager>(GetSettingsManagerAsync, ThreadHelper.JoinableTaskFactory);
protected BaseOptionModel()
{ }
/// <summary>
/// A singleton instance of the options. MUST be called from UI thread only.
/// </summary>
/// <remarks>
/// Call <see cref="GetLiveInstanceAsync()" /> instead if on a background thread or in an async context on the main thread.
/// </remarks>
public static T Instance
{
get
{
ThreadHelper.ThrowIfNotOnUIThread();
#pragma warning disable VSTHRD104 // Offer async methods
return ThreadHelper.JoinableTaskFactory.Run(GetLiveInstanceAsync);
#pragma warning restore VSTHRD104 // Offer async methods
}
}
/// <summary>
/// Get the singleton instance of the options. Thread safe.
/// </summary>
public static Task<T> GetLiveInstanceAsync() => _liveModel.GetValueAsync();
/// <summary>
/// Creates a new instance of the options class and loads the values from the store. For internal use only
/// </summary>
/// <returns></returns>
public static async Task<T> CreateAsync()
{
var instance = new T();
await instance.LoadAsync();
return instance;
}
/// <summary>
/// The name of the options collection as stored in the registry.
/// </summary>
protected virtual string CollectionName { get; } = typeof(T).FullName;
/// <summary>
/// Hydrates the properties from the registry.
/// </summary>
public virtual void Load()
{
ThreadHelper.JoinableTaskFactory.Run(LoadAsync);
}
/// <summary>
/// Hydrates the properties from the registry asyncronously.
/// </summary>
public virtual async Task LoadAsync()
{
ShellSettingsManager manager = await _settingsManager.GetValueAsync();
SettingsStore settingsStore = manager.GetReadOnlySettingsStore(SettingsScope.UserSettings);
if (!settingsStore.CollectionExists(CollectionName))
{
return;
}
foreach (PropertyInfo property in GetOptionProperties())
{
try
{
string serializedProp = settingsStore.GetString(CollectionName, property.Name);
object value = DeserializeValue(serializedProp, property.PropertyType);
property.SetValue(this, value);
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write(ex);
}
}
}
/// <summary>
/// Saves the properties to the registry.
/// </summary>
public virtual void Save()
{
ThreadHelper.JoinableTaskFactory.Run(SaveAsync);
}
/// <summary>
/// Saves the properties to the registry asyncronously.
/// </summary>
public virtual async Task SaveAsync()
{
ShellSettingsManager manager = await _settingsManager.GetValueAsync();
WritableSettingsStore settingsStore = manager.GetWritableSettingsStore(SettingsScope.UserSettings);
if (!settingsStore.CollectionExists(CollectionName))
{
settingsStore.CreateCollection(CollectionName);
}
foreach (PropertyInfo property in GetOptionProperties())
{
string output = SerializeValue(property.GetValue(this));
settingsStore.SetString(CollectionName, property.Name, output);
}
T liveModel = await GetLiveInstanceAsync();
if (this != liveModel)
{
await liveModel.LoadAsync();
}
}
/// <summary>
/// Serializes an object value to a string using the binary serializer.
/// </summary>
protected virtual string SerializeValue(object value)
{
using (var stream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, value);
stream.Flush();
return Convert.ToBase64String(stream.ToArray());
}
}
/// <summary>
/// Deserializes a string to an object using the binary serializer.
/// </summary>
protected virtual object DeserializeValue(string value, Type type)
{
byte[] b = Convert.FromBase64String(value);
using (var stream = new MemoryStream(b))
{
var formatter = new BinaryFormatter();
return formatter.Deserialize(stream);
}
}
private static async Task<ShellSettingsManager> GetSettingsManagerAsync()
{
#pragma warning disable VSTHRD010
// False-positive in Threading Analyzers. Bug tracked here https://github.com/Microsoft/vs-threading/issues/230
var svc = await AsyncServiceProvider.GlobalProvider.GetServiceAsync(typeof(SVsSettingsManager)) as IVsSettingsManager;
#pragma warning restore VSTHRD010
Assumes.Present(svc);
return new ShellSettingsManager(svc);
}
private IEnumerable<PropertyInfo> GetOptionProperties()
{
return GetType()
.GetProperties()
.Where(p => p.PropertyType.IsSerializable && p.PropertyType.IsPublic);
}
}
}