-
Notifications
You must be signed in to change notification settings - Fork 0
/
Registry.h
271 lines (242 loc) · 6.99 KB
/
Registry.h
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#pragma once
#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN
#include <Windows.h>
#undef VC_EXTRALEAN
#else
#include <Windows.h>
#endif // VC_EXTRALEAN
#include <bond/core/bond.h>
#include <bond/stream/input_buffer.h>
#ifdef _DEBUG
#include <iomanip>
#endif
namespace NightLightLibrary
{
namespace Registry
{
class Watcher
{
public:
Watcher() {};
~Watcher();
Watcher(const Watcher&) = delete;
Watcher& operator=(const Watcher&) = delete;
const bool isWatching() const noexcept;
void start(const std::vector<LPCSTR>& subKeys, const std::function<void(LPCSTR)>& callback);
void stop() noexcept;
const bool isPaused() const noexcept;
void pause() noexcept;
void resume() noexcept;
private:
std::atomic<HANDLE> _cancelEvent{ NULL };
std::atomic<HANDLE> _lastBreathEvent{ NULL };
std::atomic<bool> _watching{ false };
std::atomic<bool> _paused{ false };
void setWatching(const bool watching) noexcept;
void setPaused(const bool paused) noexcept;
void watchLoop(const std::vector<LPCSTR>& subKeys, const std::function<void(LPCSTR)>& callback);
const int watch(std::vector<HANDLE>& events, std::vector<HKEY>& keys);
}; // class Watcher
struct Header
{
uint32_t h1{ 0 };
FILETIME filetime{ 0, 0 };
uint32_t h2{ 0 };
}; // struct Header
// bond data
struct Metadata
{
int16_t protocol{ ::bond::ProtocolType::COMPACT_PROTOCOL };
int16_t version{ ::bond::v1 };
}; // struct Metadata
template<typename T> struct Bond
{
// needs to be implemented for re-using objects with bond
// should reset bond schema properties to default values
virtual T& _reset() = 0;
protected:
~Bond() {};
}; // struct Bond
namespace Name
{
constexpr LPCSTR SubkeyAttribute = "RegistrySubkey";
constexpr LPCSTR Value = "Data";
} // namespace Name
template<typename T> struct Record
{
constexpr static const LPCSTR getRegistryValueName() noexcept { return Registry::Name::Value; };
constexpr static const LPCSTR getRegistryKey() { assert(false); return ""; };
Header _header;
Metadata _metadata;
bool _dirty{ false };
static const bool load(T& obj) {
if (!Registry::load(obj))
return false;
obj._dirty = false;
return true;
}
static const bool save(T& obj) {
if (obj._dirty)
obj._dirty = Registry::save(obj);
return !obj._dirty;
}
virtual T& save() = 0;
void startWatching(const std::function<void()>& callback = []() noexcept {})
{
if (_watcher)
_watcher->stop();
else
_watcher = std::make_unique<Watcher>();
const std::vector<LPCSTR> subKeys{ T::getRegistryKey() };
const std::function<void(LPCSTR)> wrapper = [&, callback](LPCSTR) {
callback();
};
_watcher->start(subKeys, wrapper);
}
void stopWatching() noexcept { _watcher.reset(); }
void pauseWatching() noexcept { if (_watcher) _watcher->pause(); }
void resumeWatching() noexcept { if (_watcher) _watcher->resume(); }
protected:
~Record() {};
private:
std::unique_ptr<Watcher> _watcher;
}; // struct Record
#ifdef _DEBUG
inline void printData(const uint8_t* data, uint32_t size)
{
const uint8_t* const end = data + size;
std::cout << "REG DATA:" << std::hex;
while (data < end)
std::cout << " " << std::setfill('0') << std::setw(2) << (int)*(data++);
std::cout << std::dec << " END" << std::endl;
}
#endif
inline const DWORD getValueSize(const LPCSTR& regSubkey, const LPCSTR& regValueName) noexcept
{
DWORD dataSize = 0;
DWORD type = REG_BINARY;
const LSTATUS s = ::RegGetValueA(
HKEY_CURRENT_USER,
regSubkey,
regValueName,
RRF_RT_REG_BINARY,
&type,
nullptr,
&dataSize
);
if (s == ERROR_SUCCESS)
return dataSize;
return 0;
} // getValueSize()
template<typename T> inline void unmarshal(const ::bond::InputBuffer& buffer, T& obj)
{
// Unmarshal reads protocol version information from input stream and uses
// appropriate protocol reader to deserialize data.
bond::Unmarshal(buffer, obj);
} // unmarshal()
template<typename T> const bool load(T& obj)
{
static_assert(std::is_base_of<Record<T>, T>::value, "must be a Registry::Record");
DWORD dataSize = getValueSize(T::getRegistryKey(), T::getRegistryValueName());
if (dataSize == 0 || dataSize < sizeof(obj._header) + sizeof(obj._metadata))
return false;
std::vector<uint8_t> data(dataSize);
DWORD types = REG_BINARY;
const LSTATUS s = ::RegGetValueA(
HKEY_CURRENT_USER,
T::getRegistryKey(),
T::getRegistryValueName(),
RRF_RT_REG_BINARY,
&types,
data.data(),
&dataSize
);
if (s != ERROR_SUCCESS)
return false;
#ifdef _DEBUG
printData((uint8_t*)(data.data()), dataSize);
#endif
try
{
::bond::InputBuffer input = ::bond::InputBuffer(&data[0], dataSize);
// copy header for saving back to registry
input.Read(&(obj._header), sizeof(obj._header));
// copy metadata "manually" because InputBuffer cannot rewind in c++
memcpy(&(obj._metadata), &data[sizeof(obj._header)], sizeof(obj._metadata));
unmarshal(input, obj._reset());
}
catch (const std::exception& e)
{
#ifdef _DEBUG
std::cout << "bond read fail: " << e.what() << std::endl;
#else // _DEBUG
UNREFERENCED_PARAMETER(e);
#endif // _DEBUG
return false;
}
return true;
} // load()
template <typename T> const bool save(T& obj)
{
static_assert(std::is_base_of<Record<T>, T>::value, "must be a Registry::Record");
::bond::OutputBuffer output;
// restore the original header with updated time
GetSystemTimeAsFileTime(&(obj._header.filetime));
try
{
output.Write(obj._header);
switch (obj._metadata.protocol)
{
#ifdef BOND_COMPACT_BINARY_PROTOCOL
case ::bond::ProtocolType::COMPACT_PROTOCOL:
{
::bond::CompactBinaryWriter<::bond::OutputBuffer> writer(output, obj._metadata.version);
::bond::Marshal(obj, writer);
}
break;
#endif
#ifdef BOND_FAST_BINARY_PROTOCOL
case ::bond::ProtocolType::FAST_PROTOCOL:
{
::bond::FastBinaryWriter<::bond::OutputBuffer> writer(output);
::bond::Marshal(obj, writer);
}
break;
#endif
#ifdef BOND_SIMPLE_BINARY_PROTOCOL
case ::bond::ProtocolType::SIMPLE_PROTOCOL:
{
::bond::SimpleBinaryWriter<::bond::OutputBuffer> writer(output, obj._metadata.version);
::bond::Marshal(obj, writer);
}
break;
#endif
default:
return false;
}
}
catch (const std::exception& e)
{
#ifdef _DEBUG
std::cout << "bond write fail: " << e.what() << std::endl;
#else // _DEBUG
UNREFERENCED_PARAMETER(e);
#endif // _DEBUG
return false;
}
const LSTATUS s = ::RegSetKeyValueA(
HKEY_CURRENT_USER,
T::getRegistryKey(),
T::getRegistryValueName(),
REG_BINARY,
output.GetBuffer().data(),
output.GetBuffer().size()
);
#ifdef _DEBUG
printData((uint8_t*)(output.GetBuffer().data()), output.GetBuffer().size());
#endif // _DEBUG
return (s == ERROR_SUCCESS);
} // save()
} // namespace Registry
}; // namespace NightLightLibrary