-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutl_settings.cpp
More file actions
283 lines (244 loc) · 6.5 KB
/
Copy pathutl_settings.cpp
File metadata and controls
283 lines (244 loc) · 6.5 KB
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
#include "utl_settings.h"
#include <cassert>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <functional>
#include <sstream>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <cstdlib>
#include "utl_io.h"
#endif
namespace settings
{
static std::function<void(const std::string&)> s_log_callback;
void set_log_callback(std::function<void(const std::string&)> cb) { s_log_callback = std::move(cb); }
// Portable getenv wrapper: uses _dupenv_s on MSVC to avoid C4996 deprecation;
// falls back to std::getenv elsewhere. Returns empty string on failure/missing.
static std::string get_env_var(const char* name)
{
#ifdef _MSC_VER
char* buf = nullptr;
size_t sz = 0;
if (_dupenv_s(&buf, &sz, name) == 0 && buf != nullptr)
{
std::string val(buf);
free(buf);
return val;
}
return {};
#else
const char* v = std::getenv(name);
return v ? std::string(v) : std::string{};
#endif
}
std::filesystem::path user_settings_json_path()
{
#ifdef __EMSCRIPTEN__
return {};
#else
const std::filesystem::path dir = user_config_directory();
if (dir.empty())
return {};
return dir / "ezycad_settings.json";
#endif
}
std::string load_with_defaults()
{
std::string content;
#ifdef __EMSCRIPTEN__
void* ptr = (void*)(intptr_t)EM_ASM_INT({
var s = localStorage.getItem("ezycad_settings") || "";
var len = lengthBytesUTF8(s) + 1;
var buf = _malloc(len);
if (buf)
stringToUTF8(s, buf, len);
return buf;
});
if (ptr)
{
content = (const char*)ptr;
free(ptr);
}
#else
auto read_file = [](const std::filesystem::path& p) -> std::string
{
std::ifstream f(p, std::ios::binary);
if (!f)
return {};
std::ostringstream os;
os << f.rdbuf();
return os.str();
};
const std::filesystem::path user_p = user_settings_json_path();
if (!user_p.empty())
content = read_file(user_p);
if (content.empty())
{
// Legacy: cwd ezycad_settings.json (same directory as exe when launched that way).
content = read_file(std::filesystem::path("ezycad_settings.json"));
}
#endif
if (content.empty())
{
content = load_defaults();
if (!content.empty())
save(content);
}
return content;
}
std::string load_defaults()
{
if (s_log_callback)
s_log_callback("Settings: loading defaults from res/ezycad_settings.json");
#ifdef __EMSCRIPTEN__
// Read from preloaded file in virtual FS (--preload-file ...@/res/ezycad_settings.json).
std::ifstream f("/res/ezycad_settings.json");
if (!f)
{
if (s_log_callback)
s_log_callback("Settings: failed to load defaults (preloaded file not found)");
return {};
}
std::ostringstream os;
os << f.rdbuf();
std::string result = os.str();
if (s_log_callback)
s_log_callback("Settings: loaded defaults from res/ezycad_settings.json");
return result;
#else
std::ifstream f("res/ezycad_settings.json");
assert(f && "res/ezycad_settings.json not found (required on native)");
if (!f)
return {};
std::ostringstream os;
os << f.rdbuf();
std::string result = os.str();
if (s_log_callback)
s_log_callback("Settings: loaded defaults from res/ezycad_settings.json");
return result;
#endif
}
void save(const std::string& content)
{
#ifdef __EMSCRIPTEN__
EM_ASM_({ localStorage.setItem('ezycad_settings', UTF8ToString($0)); }, content.c_str());
#else
const std::filesystem::path user_p = user_settings_json_path();
if (!user_p.empty())
{
std::error_code ec;
std::filesystem::create_directories(user_p.parent_path(), ec);
std::ofstream f(user_p, std::ios::binary);
if (f)
{
f.write(content.data(), static_cast<std::streamsize>(content.size()));
return;
}
}
std::ofstream f("ezycad_settings.json", std::ios::binary);
if (f)
f.write(content.data(), static_cast<std::streamsize>(content.size()));
#endif
}
std::filesystem::path user_config_directory()
{
#ifdef __EMSCRIPTEN__
(void)0;
return {};
#elif defined(_WIN32)
std::string appdata = get_env_var("APPDATA");
if (appdata.empty())
return {};
return std::filesystem::path(appdata) / "EzyCad";
#elif defined(__APPLE__)
std::string home = get_env_var("HOME");
if (home.empty())
return {};
return std::filesystem::path(home) / "Library" / "Application Support" / "EzyCad";
#else
std::string xdg = get_env_var("XDG_CONFIG_HOME");
if (!xdg.empty())
return std::filesystem::path(xdg) / "EzyCad";
std::string home = get_env_var("HOME");
if (home.empty())
return {};
return std::filesystem::path(home) / ".config" / "EzyCad";
#endif
}
std::filesystem::path user_startup_project_path()
{
const std::filesystem::path dir = user_config_directory();
if (dir.empty())
return {};
return dir / "startup.ezy";
}
std::string load_user_startup_project()
{
#ifdef __EMSCRIPTEN__
void* ptr = (void*)(intptr_t)EM_ASM_INT({
var s = localStorage.getItem("ezycad_startup_ezy") || "";
var len = lengthBytesUTF8(s) + 1;
var buf = _malloc(len);
if (buf)
stringToUTF8(s, buf, len);
return buf;
});
if (!ptr)
return {};
std::string stored((const char*)ptr);
free(ptr);
if (stored.rfind("B64:", 0) == 0)
{
const std::vector<uint8_t> decoded = ezy_base64_decode(stored.substr(4));
return std::string(reinterpret_cast<const char*>(decoded.data()), decoded.size());
}
return stored;
#else
const std::filesystem::path p = user_startup_project_path();
if (p.empty())
return {};
std::ifstream f(p, std::ios::binary);
if (!f)
return {};
std::ostringstream os;
os << f.rdbuf();
return os.str();
#endif
}
bool save_user_startup_project(const std::vector<uint8_t>& ezy_bytes)
{
#ifdef __EMSCRIPTEN__
const std::string b64 = std::string("B64:") + ezy_base64_encode(ezy_bytes);
EM_ASM_({ localStorage.setItem('ezycad_startup_ezy', UTF8ToString($0)); }, b64.c_str());
return true;
#else
const std::filesystem::path p = user_startup_project_path();
if (p.empty())
return false;
std::error_code ec;
std::filesystem::create_directories(p.parent_path(), ec);
if (ec)
return false;
std::ofstream f(p, std::ios::binary);
if (!f)
return false;
f.write(reinterpret_cast<const char*>(ezy_bytes.data()), static_cast<std::streamsize>(ezy_bytes.size()));
return true;
#endif
}
void clear_user_startup_project()
{
#ifdef __EMSCRIPTEN__
EM_ASM({ localStorage.removeItem('ezycad_startup_ezy'); });
#else
const std::filesystem::path p = user_startup_project_path();
if (!p.empty())
{
std::error_code ec;
std::filesystem::remove(p, ec);
}
#endif
}
} // namespace settings