comparison foosdk/sdk/foobar2000/SDK/cfg_var.cpp @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #include "foobar2000-sdk-pch.h"
2 #include "cfg_var.h"
3 #include "configStore.h"
4
5 namespace fb2k {
6 pfc::string8 formatCfgVarName(const GUID& guid) {
7 return pfc::format("cfg_var.", pfc::print_guid(guid));
8 }
9 pfc::string8 advconfig_autoName(const GUID& id) {
10 return pfc::format("advconfig.unnamed.", pfc::print_guid(id));
11 }
12 pfc::string8 advconfig_autoName(const GUID& id, const char* specified) {
13 if (specified) return specified;
14 return advconfig_autoName(id);
15 }
16 }
17 namespace cfg_var_modern {
18
19 #ifdef FOOBAR2000_HAVE_CFG_VAR_LEGACY
20 void cfg_string::set_data_raw(stream_reader* p_stream, t_size p_sizehint, abort_callback& p_abort) {
21 (void)p_sizehint;
22 pfc::string8_fastalloc temp;
23 p_stream->read_string_raw(temp, p_abort);
24 this->set(temp);
25 }
26
27 void cfg_int::set_data_raw(stream_reader* p_stream, t_size p_sizehint, abort_callback& p_abort) {
28 switch (p_sizehint) {
29 case 4:
30 { int32_t v; p_stream->read_lendian_t(v, p_abort); set(v); }
31 break;
32 case 8:
33 { int64_t v; p_stream->read_lendian_t(v, p_abort); set(v); }
34 break;
35 }
36 }
37
38 void cfg_bool::set_data_raw(stream_reader* p_stream, t_size p_sizehint, abort_callback& p_abort) {
39 (void)p_sizehint;
40 uint8_t b;
41 if (p_stream->read(&b, 1, p_abort) == 1) {
42 this->set(b != 0);
43 }
44 }
45 #endif
46
47 int64_t cfg_int::get() {
48 std::call_once(m_init, [this] {
49 m_val = fb2k::configStore::get()->getConfigInt(formatName(), m_initVal);
50 });
51 return m_val;
52 }
53
54 void cfg_int::set(int64_t v) {
55 if (v != get()) {
56 m_val = v;
57 fb2k::configStore::get()->setConfigInt(formatName(), v);
58 }
59 }
60
61 bool cfg_bool::get() {
62 std::call_once(m_init, [this] {
63 m_val = fb2k::configStore::get()->getConfigBool(formatName(), m_initVal);
64 });
65 return m_val;
66 }
67
68 void cfg_bool::set(bool v) {
69 if (v != get()) {
70 m_val = v;
71 fb2k::configStore::get()->setConfigBool(formatName(), v);
72 }
73 }
74
75 void cfg_string::set(const char* v) {
76 if (strcmp(v, get()) != 0) {
77 pfc::string8 obj = v;
78
79 {
80 PFC_INSYNC_WRITE(m_valueGuard);
81 m_value = std::move(obj);
82 }
83
84 fb2k::configStore::get()->setConfigString(formatName(), v);
85 }
86 }
87
88 void cfg_string::get(pfc::string_base& out) {
89 std::call_once(m_init, [this] {
90 pfc::string8 v = fb2k::configStore::get()->getConfigString(formatName(), m_initVal)->c_str();
91 PFC_INSYNC_WRITE(m_valueGuard);
92 m_value = std::move(v);
93 });
94
95 PFC_INSYNC_READ(m_valueGuard);
96 out = m_value;
97 }
98
99 pfc::string8 cfg_string::get() {
100 pfc::string8 ret; get(ret); return ret;
101 }
102
103
104 pfc::string8 cfg_var_common::formatVarName(const GUID& guid) {
105 return fb2k::formatCfgVarName( guid );
106 }
107
108 pfc::string8 cfg_var_common::formatName() const {
109 return formatVarName(this->m_guid);
110 }
111
112 fb2k::memBlockRef cfg_blob::get() {
113 std::call_once(m_init, [this] {
114 auto v = fb2k::configStore::get()->getConfigBlob(formatName(), m_initVal);
115 PFC_INSYNC_WRITE(m_dataGuard);
116 m_data = std::move(v);
117 });
118 PFC_INSYNC_READ(m_dataGuard);
119 return m_data;
120 }
121
122 void cfg_blob::set_(fb2k::memBlockRef arg) {
123 {
124 PFC_INSYNC_WRITE(m_dataGuard);
125 m_data = arg;
126 }
127
128 auto api = fb2k::configStore::get();
129 auto name = formatName();
130 if (arg.is_valid()) api->setConfigBlob(name, arg);
131 else api->deleteConfigBlob(name);
132 }
133
134 void cfg_blob::set(fb2k::memBlockRef arg) {
135 auto old = get();
136 if (!fb2k::memBlock::equals(old, arg)) {
137 set_(arg);
138 }
139 }
140
141 void cfg_blob::set(const void* ptr, size_t size) {
142 set(fb2k::makeMemBlock(ptr, size));
143 }
144
145 #ifdef FOOBAR2000_HAVE_CFG_VAR_LEGACY
146 void cfg_blob::set_data_raw(stream_reader* p_stream, t_size p_sizehint, abort_callback& p_abort) {
147 pfc::mem_block block;
148 block.resize(p_sizehint);
149 p_stream->read_object(block.ptr(), p_sizehint, p_abort);
150 set(fb2k::memBlock::blockWithData(std::move(block)));
151 }
152 #endif
153
154 double cfg_float::get() {
155 std::call_once(m_init, [this] {
156 m_val = fb2k::configStore::get()->getConfigFloat(formatName(), m_initVal);
157 });
158 return m_val;
159 }
160
161 void cfg_float::set(double v) {
162 if (v != get()) {
163 m_val = v;
164 fb2k::configStore::get()->setConfigFloat(formatName(), v);
165 }
166 }
167
168 #ifdef FOOBAR2000_HAVE_CFG_VAR_LEGACY
169 void cfg_float::set_data_raw(stream_reader* p_stream, t_size p_sizehint, abort_callback& p_abort) {
170 switch (p_sizehint) {
171 case 4:
172 { float v; if (p_stream->read(&v, 4, p_abort) == 4) set(v); }
173 break;
174 case 8:
175 { double v; if (p_stream->read(&v, 8, p_abort) == 8) set(v); }
176 break;
177 }
178 }
179 #endif
180 }