forked from zao/foo_wave_seekbar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPersistentSettings.cc
455 lines (408 loc) · 13.4 KB
/
PersistentSettings.cc
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// Copyright Lars Viklund 2008 - 2011.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "PchSeekbar.h"
#include "PersistentSettings.h"
#include <set>
#include <sstream>
#include <fstream>
#include <boost/property_tree/info_parser.hpp>
#include <boost/property_tree/detail/rapidxml.hpp>
namespace rxml = boost::property_tree::detail::rapidxml;
namespace pt = boost::property_tree;
namespace rxml = pt::detail::rapidxml;
static GUID as_guid(std::string s)
{
GUID g = {};
unsigned char* guid_string = reinterpret_cast<unsigned char*>(&s[0]);
UuidFromStringA(guid_string, &g);
return g;
}
static std::string as_string(GUID const& g)
{
RPC_CSTR guid_string = NULL;
UuidToStringA(&g, &guid_string);
std::string ret = (char const*)guid_string;
RpcStringFreeA(&guid_string);
return ret;
}
template <typename T>
static std::string as_string(T const& t)
{
return std::to_string(t);
}
namespace wave
{
template <typename T>
typename std::enable_if<std::is_unsigned<T>::value>::type
scan_int(T& out, char const* from, size_t n)
{
std::istringstream iss(std::string(from, from + n));
uint64_t v = 0u;
iss >> v;
out = (T)v;
}
template <typename T>
typename std::enable_if<!std::is_unsigned<T>::value>::type
scan_int(T& out, char const* from, size_t n)
{
std::istringstream iss(std::string(from, from + n));
int64_t v = 0;
iss >> v;
out = (T)v;
}
template <typename T>
void extract_int(rxml::xml_node<>* node, T& out)
{
if (!node || strlen(node->value()) == 0)
throw std::runtime_error("Usable integer element not found.");
char* first = node->value();
char* last = first + node->value_size();
std::string s(first, last);
scan_int(out, node->value(), node->value_size());
}
inline void extract_bool(rxml::xml_node<>* node, bool& out)
{
int x = 0;
extract_int(node, x);
out = !!x;
}
template <typename T>
void extract_float(rxml::xml_node<>* node, T& out)
{
if (!node || strlen(node->value()) == 0)
throw std::runtime_error("Usable float element not found.");
char* first = node->value();
char* last = first + node->value_size();
out = (T)strtod(first, &last);
}
template <typename T, typename F>
void extract_array(rxml::xml_node<>* node, T& out, F f)
{
if (!node)
throw std::runtime_error("Usable array element not found.");
node = node->first_node("elems");
if (!node)
throw std::runtime_error("Tree of elems for array not found.");
auto item_node = node->first_node("item");
size_t i = 0;
for (;
item_node && i < out.size();
item_node = item_node->next_sibling("item"), ++i)
{
f(item_node, out[i]);
}
if (i != out.size())
throw std::runtime_error("Item count mismatch for array.");
}
template <typename T, typename F>
void extract_carray(rxml::xml_node<>* node, T* out, size_t N, F f)
{
size_t const num_elems = N;
if (!node)
throw std::runtime_error("Usable C-array element not found.");
auto item_node = node->first_node("item");
size_t i = 0;
for (;
item_node && i < num_elems;
item_node = item_node->next_sibling("item"), ++i)
{
f(item_node, out[i]);
}
if (i != num_elems)
throw std::runtime_error("Item count mismatch for array.");
}
template <typename T, typename F>
void extract_vector(rxml::xml_node<>* node, std::vector<T>& out, F f)
{
if (!node)
throw std::runtime_error("Usable vector element not found.");
auto item_node = node->first_node("item");
for (;
item_node;
item_node = item_node->next_sibling("item"))
{
T t;
f(item_node, t);
out.push_back(t);
}
}
template <typename T>
struct extract_fun
{
typedef typename std::function<void (rxml::xml_node<>*, typename std::remove_const<T>::type&)> type;
};
template <typename M>
void extract_map(rxml::xml_node<>* node, M& out, typename extract_fun<typename M::key_type>::type key_fun, typename extract_fun<typename M::mapped_type>::type value_fun)
{
if (!node)
throw std::runtime_error("Usable map element not found.");
auto item_node = node->first_node("item");
for (;
item_node;
item_node = item_node->next_sibling("item"))
{
std::pair<typename std::remove_const<typename M::key_type>::type, typename M::mapped_type> p;
extract_pair(item_node, p, key_fun, value_fun);
out[p.first] = p.second;
}
}
template <typename T1, typename T2>
void extract_pair(rxml::xml_node<>* node, std::pair<T1, T2>& out, typename extract_fun<T1>::type f1, typename extract_fun<T2>::type f2)
{
if (!node)
throw std::runtime_error("Usable pair element not found.");
auto first_node = node->first_node("first");
auto second_node = node->first_node("second");
f1(first_node, out.first);
f2(second_node, out.second);
}
inline void extract_color(rxml::xml_node<>* node, wave::color& out)
{
if (!node)
throw std::runtime_error("Usable color element not found.");
auto r_node = node->first_node("r");
auto g_node = r_node->next_sibling("g");
if (!g_node)
g_node = r_node->next_sibling("r");
auto b_node = g_node->next_sibling("b");
if (!b_node)
b_node = g_node->next_sibling("r");
auto a_node = node->first_node("a");
extract_float(r_node, out.r);
extract_float(g_node, out.g);
extract_float(b_node, out.b);
extract_float(a_node, out.a);
}
inline void extract_guid(rxml::xml_node<>* node, GUID& out)
{
if (!node)
throw std::runtime_error("Usable GUID element not found.");
extract_int(node->first_node("Data1"), out.Data1);
extract_int(node->first_node("Data2"), out.Data2);
extract_int(node->first_node("Data3"), out.Data3);
extract_carray(node->first_node("Data4"), out.Data4, 8, &extract_int<uint8_t>);
}
inline void extract_string(rxml::xml_node<>* node, std::string& out)
{
if (!node)
throw std::runtime_error("Usable string element not found.");
out.assign(node->value(), node->value() + node->value_size());
}
}
namespace wave
{
persistent_settings::persistent_settings()
: active_frontend_kind(config::frontend_direct3d9), has_border(true), shade_played(true)
, display_mode(config::display_normal), flip_display(false), downmix_display(config::downmix_none)
, generic_strings(&less_guid)
{
std::fill(colors.begin(), colors.end(), color());
std::fill(override_colors.begin(), override_colors.end(), false);
channel_order = {
{ audio_chunk::channel_back_left, true },
{ audio_chunk::channel_front_left, true },
{ audio_chunk::channel_front_center, true },
{ audio_chunk::channel_front_right, true },
{ audio_chunk::channel_back_right, true },
{ audio_chunk::channel_lfe, true }
};
insert_remaining_channels();
}
void persistent_settings::insert_remaining_channels()
{
std::set<int> all_channels = {
(audio_chunk::channel_back_left),
(audio_chunk::channel_front_left),
(audio_chunk::channel_front_center),
(audio_chunk::channel_front_right),
(audio_chunk::channel_back_right),
(audio_chunk::channel_lfe),
(audio_chunk::channel_front_center_left),
(audio_chunk::channel_front_center_right),
(audio_chunk::channel_back_center),
(audio_chunk::channel_side_left),
(audio_chunk::channel_side_right),
(audio_chunk::channel_top_center),
(audio_chunk::channel_top_front_left),
(audio_chunk::channel_top_front_center),
(audio_chunk::channel_top_front_right),
(audio_chunk::channel_top_back_left),
(audio_chunk::channel_top_back_center),
(audio_chunk::channel_top_back_right)
};
for (int ch : all_channels)
{
if (std::find_if(channel_order.begin(), channel_order.end(), [ch](decltype(channel_order[0]) const& a) { return a.first == ch; }) == channel_order.end())
channel_order.push_back(std::make_pair(ch, false));
}
}
void read_s11n_node(rxml::xml_node<>* doc, persistent_settings& settings)
{
auto node = doc->first_node("settings");
if (!node)
throw std::runtime_error("Couldn't find <settings> element in saved settings.");
auto version_attr = node->first_attribute("version");
std::string version_string(version_attr->value(), version_attr->value() + version_attr->value_size());
int version = atoi(version_string.c_str());
if (version >= 0)
extract_int(node->first_node("active_frontend_kind"), settings.active_frontend_kind);
if (version >= 2)
extract_bool(node->first_node("has_border"), settings.has_border);
if (version >= 3)
extract_array(node->first_node("colors"), settings.colors, &extract_color);
if (version >= 4)
extract_array(node->first_node("override_colors"), settings.override_colors, &extract_bool);
if (version >= 6)
extract_bool(node->first_node("shade_played"), settings.shade_played);
if (version >= 7)
{
extract_int(node->first_node("display_mode"), settings.display_mode);
bool to_mono;
extract_bool(node->first_node("downmix_display"), to_mono);
settings.downmix_display = (to_mono ? config::downmix_mono : config::downmix_none);
}
if (version >= 9)
{
settings.channel_order.clear();
extract_vector(node->first_node("channel_order"), settings.channel_order, [](rxml::xml_node<>* node, std::pair<int, bool>& pair)
{
extract_pair<int, bool>(node, pair, &extract_int<int>, &extract_bool);
});
settings.insert_remaining_channels();
}
if (version >= 10)
{
extract_map(node->first_node("generic_strings"), settings.generic_strings, &extract_guid, &extract_string);
}
if (version >= 11)
{
extract_bool(node->first_node("flip_display"), settings.flip_display);
}
}
void persistent_settings::from_ptree(boost::property_tree::ptree const& src)
{
active_frontend_kind = (config::frontend)src.get<int>("active_frontend_kind");
has_border = src.get<bool>("has_border");
{
auto colors_tree = src.get_child("colors");
auto I = colors_tree.begin();
for (size_t i = 0; i < colors.size(); ++i, ++I)
{
color c;
c.r = I->second.get<float>("r");
c.g = I->second.get<float>("g");
c.b = I->second.get<float>("b");
c.a = I->second.get<float>("a");
colors[i] = c;
override_colors[i] = I->second.get<bool>("override");
}
}
shade_played = src.get<bool>("shade_played");
display_mode = (config::display_mode)src.get<int>("display_mode");
try
{
downmix_display = (config::downmix)src.get<int>("downmix_display");
}
catch (boost::property_tree::ptree_bad_data e)
{
// fallback to old to-mono boolean flag
bool to_mono = src.get<bool>("downmix_display");
downmix_display = (to_mono ? config::downmix_mono : config::downmix_stereo);
}
{
auto channel_order_tree = src.get_child("channel_order");
channel_order.clear();
for (auto I = channel_order_tree.begin(); I != channel_order_tree.end(); ++I)
{
std::pair<int, bool> p;
p.first = I->second.get<int>("channel");
p.second = I->second.get<bool>("enabled");
channel_order.push_back(p);
}
}
{
auto generic_strings_tree = src.get_child("generic_strings");
for (auto I = generic_strings_tree.begin(); I != generic_strings_tree.end(); ++I)
{
GUID key = as_guid(I->first);
std::string value = I->second.get_value<std::string>();
generic_strings[key] = value;
}
}
flip_display = src.get<bool>("flip_display");
}
void persistent_settings::to_ptree(boost::property_tree::ptree& out) const
{
using boost::property_tree::ptree;
out.put("active_frontend_kind", (int)active_frontend_kind);
out.put("has_border", has_border);
ptree& colors_pt = out.add("colors", "");
for (size_t i = 0; i < colors.size(); ++i)
{
color const& c = colors[i];
bool overridden = override_colors[i];
ptree& _ = colors_pt.add("color", "");
_.add("r", c.r);
_.add("g", c.g);
_.add("b", c.b);
_.add("a", c.a);
_.add("override", overridden);
}
out.put("shade_played", shade_played);
out.put("display_mode", display_mode);
out.put("flip_display", flip_display);
out.put("downmix_display", downmix_display);
ptree& channel_order_pt = out.add("channel_order", "");
for (auto& p : channel_order)
{
ptree& _ = channel_order_pt.add("mapping", "");
_.add("channel", p.first);
_.add("enabled", p.second);
}
ptree& generic_strings_pt = out.add("generic_strings", "");
for (auto& p : generic_strings)
{
generic_strings_pt.add(as_string(p.first), p.second);
}
}
static std::string slurp_file(std::string filename)
{
std::ifstream is(filename.c_str(), std::ios::binary);
is.seekg(0, std::ios::end);
auto cb = is.tellg();
is.seekg(0, std::ios::beg);
std::string ret((int)cb, '\0');
is.read(&ret[0], cb);
return ret;
}
template <typename Iterator>
static std::pair<Iterator, Iterator> find_first_regex(Iterator begin, Iterator end, std::regex const& re)
{
std::match_results<Iterator> m;
if (std::regex_search(begin, end, m, re)) {
return { m[0].first, m[0].second };
}
return { end, end };
}
static std::string extract_xml_part(std::string const& contents)
{
auto first_tag = find_first_regex(std::cbegin(contents), std::cend(contents), std::regex(R"(<\?xml version=)"));
auto last_tag = find_first_regex(first_tag.second, std::cend(contents), std::regex("</boost_serialization>"));
return std::string(first_tag.first, last_tag.second);
}
void read_s11n_xml(std::string xml, persistent_settings& settings)
{
if (xml.empty())
return;
rxml::xml_document<> doc;
enum { ParseFlags = rxml::parse_declaration_node | rxml::parse_doctype_node };
doc.parse<ParseFlags>(&xml[0]);
rxml::xml_node<>* node = doc.first_node("boost_serialization");
if (node)
{
read_s11n_node(node, settings);
}
}
}