-
Notifications
You must be signed in to change notification settings - Fork 1
/
Configuration.cpp
208 lines (157 loc) · 6.02 KB
/
Configuration.cpp
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
/** $VER: configuration_t.cpp (2024.08.04) P. Stuer **/
#include "pch.h"
#include "Configuration.h"
#include "Encoding.h"
#include "Resources.h"
#include <SDK/file.h>
#include <SDK/advconfig_impl.h>
#include <pfc/string_conv.h>
#include <pfc/string-conv-lite.h>
using namespace pfc;
using namespace stringcvt;
#include <pathcch.h>
#pragma comment(lib, "pathcch")
#pragma hdrstop
/// <summary>
/// Initializes a new instance.
/// </summary>
configuration_t::configuration_t()
{
{
GUID Guid;
(void) ::CoCreateGuid(&Guid);
wchar_t ProfileName[64];
::swprintf_s(ProfileName, _countof(ProfileName), TEXT(STR_COMPONENT_BASENAME) L"-%08X-%04X-%04X-%02X%02X%02X%02X%02X%02X%02X%02X", (int) Guid.Data1, (int) Guid.Data2, (int) Guid.Data3, Guid.Data4[0], Guid.Data4[1], Guid.Data4[2], Guid.Data4[3], Guid.Data4[4], Guid.Data4[5], Guid.Data4[6], Guid.Data4[7]);
_ProfileName = ProfileName;
}
Reset();
}
/// <summary>
/// Resets this instance.
/// </summary>
void configuration_t::Reset() noexcept
{
_Name = TEXT(STR_COMPONENT_NAME);
pfc::string8 Path = pfc::io::path::combine(core_api::get_profile_path(), STR_COMPONENT_BASENAME);
if (::_strnicmp(Path, "file://", 7) == 0)
Path = Path.subString(7);
{
wchar_t FilePath[MAX_PATH];
::wcscpy_s(FilePath, _countof(FilePath), ::UTF8ToWide(Path.c_str()).c_str());
HRESULT hr = ::PathCchAppend(FilePath, _countof(FilePath), L"Template.html");
if (SUCCEEDED(hr))
_TemplateFilePath = FilePath;
else
::wcscpy_s(FilePath, _countof(FilePath), L"Template.html");
}
{
_UserDataFolderPath = ::UTF8ToWide(Path.c_str());
}
_WindowSize = 100; // ms
_WindowSizeUnit = WindowSizeUnit::Milliseconds;
_ReactionAlignment = 0.25;
_ClearOnStartup = ClearOnStartup::None;
_InPrivateMode = false;
_ScrollbarStyle = ScrollbarStyle::Fluent;
}
/// <summary>
/// Implements the = operator.
/// </summary>
configuration_t & configuration_t::operator=(const configuration_t & other)
{
_Name = other._Name;
_TemplateFilePath = other._TemplateFilePath;
_UserDataFolderPath = other._UserDataFolderPath;
_WindowSize = other._WindowSize;
_WindowSizeUnit = other._WindowSizeUnit;
_ReactionAlignment = other._ReactionAlignment;
_ProfileName = other._ProfileName;
_ClearOnStartup = other._ClearOnStartup;
_InPrivateMode = other._InPrivateMode;
_ScrollbarStyle = other._ScrollbarStyle;
return *this;
}
/// <summary>
/// Reads this instance with the specified reader.
/// </summary>
void configuration_t::Read(stream_reader * reader, size_t size, abort_callback & abortHandler, bool isPreset) noexcept
{
Reset();
try
{
int32_t Version;
reader->read(&Version, sizeof(Version), abortHandler);
pfc::string UTF8String;
// Version 1, v0.1.4.0
reader->read_string(UTF8String, abortHandler); _TemplateFilePath = pfc::wideFromUTF8(UTF8String);
// Version 2, v0.1.5.0-alpha1
if (Version >= 2)
{
reader->read_string(UTF8String, abortHandler); _Name = pfc::wideFromUTF8(UTF8String);
reader->read_string(UTF8String, abortHandler); _UserDataFolderPath = pfc::wideFromUTF8(UTF8String);
}
// Version 3, v0.1.5.0-alpha2
if (Version >= 3)
{
reader->read_object_t(_WindowSize, abortHandler);
uint32_t Value; reader->read_object_t(Value, abortHandler); _WindowSizeUnit = (WindowSizeUnit) Value;
reader->read_object_t(_ReactionAlignment, abortHandler);
}
// Version 4, v0.1.5.6
if (Version >= 4)
{
reader->read_string(UTF8String, abortHandler); _ProfileName = pfc::wideFromUTF8(UTF8String);
}
// Version 5, v0.1.6.0
if (Version >= 5)
{
uint32_t Value; reader->read_object_t(Value, abortHandler); _ClearOnStartup = (ClearOnStartup) Value;
}
// Version 6, v0.1.6.3-alpha3
if (Version >= 6)
{
reader->read_object_t(_InPrivateMode, abortHandler);
}
// Version 7, v0.1.8.0
if (Version >= 7)
{
uint32_t Value; reader->read_object_t(Value, abortHandler); _ScrollbarStyle = (ScrollbarStyle) Value;
}
}
catch (exception & ex)
{
console::printf(STR_COMPONENT_BASENAME " failed to read configuration: %s", ex.what());
Reset();
}
}
/// <summary>
/// Writes this instance to the specified writer.
/// </summary>
void configuration_t::Write(stream_writer * writer, abort_callback & abortHandler, bool isPreset) const noexcept
{
try
{
writer->write_object_t(_CurrentVersion, abortHandler);
// Version 1, v0.1.4.0
pfc::string UTF8String = pfc::utf8FromWide(_TemplateFilePath.c_str()); writer->write_string(UTF8String, abortHandler);
// Version 2, v0.1.5.0-alpha1
UTF8String = pfc::utf8FromWide(_Name.c_str()); writer->write_string(UTF8String, abortHandler);
UTF8String = pfc::utf8FromWide(_UserDataFolderPath.c_str()); writer->write_string(UTF8String, abortHandler);
// Version 3, v0.1.5.0-alpha2
writer->write_object_t(_WindowSize, abortHandler);
uint32_t Value = (uint32_t) _WindowSizeUnit; writer->write_object_t(Value, abortHandler);
writer->write_object_t(_ReactionAlignment, abortHandler);
// Version 4, v0.1.5.6
UTF8String = pfc::utf8FromWide(_ProfileName.c_str()); writer->write_string(UTF8String, abortHandler);
// Version 5, v0.1.6.0
Value = (uint32_t) _ClearOnStartup; writer->write_object_t(Value, abortHandler);
// Version 6, v0.1.6.3-alpha3
writer->write_object_t(_InPrivateMode, abortHandler);
// Version 7, v0.1.8.0
Value = (uint32_t) _ScrollbarStyle; writer->write_object_t(Value, abortHandler);
}
catch (exception & ex)
{
console::printf(STR_COMPONENT_BASENAME " failed to write configuration: %s", ex.what());
}
}