yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
wasm_config.cc
Go to the documentation of this file.
1#ifdef __EMSCRIPTEN__
2
4
5#include <cstdlib>
6#include <emscripten.h>
7
8namespace yaze {
9namespace app {
10namespace platform {
11
12// clang-format off
13
14// Helper to read string from JS config
15EM_JS(char*, WasmConfig_GetString, (const char* path, const char* defaultVal), {
16 try {
17 var config = window.YAZE_CONFIG || {};
18 var parts = UTF8ToString(path).split('.');
19 var value = config;
20 for (var i = 0; i < parts.length; i++) {
21 if (value && typeof value === 'object' && parts[i] in value) {
22 value = value[parts[i]];
23 } else {
24 value = UTF8ToString(defaultVal);
25 break;
26 }
27 }
28 if (typeof value !== 'string') {
29 value = UTF8ToString(defaultVal);
30 }
31 var lengthBytes = lengthBytesUTF8(value) + 1;
32 var stringOnWasmHeap = _malloc(lengthBytes);
33 stringToUTF8(value, stringOnWasmHeap, lengthBytes);
34 return stringOnWasmHeap;
35 } catch (e) {
36 console.error('[WasmConfig] Error reading string:', e);
37 var def = UTF8ToString(defaultVal);
38 var len = lengthBytesUTF8(def) + 1;
39 var ptr = _malloc(len);
40 stringToUTF8(def, ptr, len);
41 return ptr;
42 }
43});
44
45// Helper to read number from JS config
46EM_JS(double, WasmConfig_GetNumber, (const char* path, double defaultVal), {
47 try {
48 var config = window.YAZE_CONFIG || {};
49 var parts = UTF8ToString(path).split('.');
50 var value = config;
51 for (var i = 0; i < parts.length; i++) {
52 if (value && typeof value === 'object' && parts[i] in value) {
53 value = value[parts[i]];
54 } else {
55 return defaultVal;
56 }
57 }
58 return typeof value === 'number' ? value : defaultVal;
59 } catch (e) {
60 console.error('[WasmConfig] Error reading number:', e);
61 return defaultVal;
62 }
63});
64
65// Helper to read int from JS config
66EM_JS(int, WasmConfig_GetInt, (const char* path, int defaultVal), {
67 try {
68 var config = window.YAZE_CONFIG || {};
69 var parts = UTF8ToString(path).split('.');
70 var value = config;
71 for (var i = 0; i < parts.length; i++) {
72 if (value && typeof value === 'object' && parts[i] in value) {
73 value = value[parts[i]];
74 } else {
75 return defaultVal;
76 }
77 }
78 return typeof value === 'number' ? Math.floor(value) : defaultVal;
79 } catch (e) {
80 console.error('[WasmConfig] Error reading int:', e);
81 return defaultVal;
82 }
83});
84
85// clang-format on
86
88 // Prevent concurrent loading
89 bool expected = false;
90 if (!loading_.compare_exchange_strong(expected, true,
91 std::memory_order_acq_rel)) {
92 // Already loading, wait for completion
93 return;
94 }
95
96 // Lock for writing config values
97 std::lock_guard<std::mutex> lock(config_mutex_);
98
99 // Collaboration settings
100 char* server_url = WasmConfig_GetString("collaboration.serverUrl", "");
101 collaboration.server_url = std::string(server_url);
102 free(server_url);
103
105 WasmConfig_GetNumber("collaboration.userTimeoutSeconds", 30.0);
107 WasmConfig_GetNumber("collaboration.cursorSendIntervalMs", 100.0) / 1000.0;
108 collaboration.max_change_size_bytes = static_cast<size_t>(
109 WasmConfig_GetInt("collaboration.maxChangeSizeBytes", 1024));
110
111 // Autosave settings
113 WasmConfig_GetInt("autosave.intervalSeconds", 60);
115 WasmConfig_GetInt("autosave.maxRecoverySlots", 5);
116
117 // Terminal settings
119 WasmConfig_GetInt("terminal.maxHistoryItems", 50);
121 WasmConfig_GetInt("terminal.maxOutputLines", 1000);
122
123 // UI settings
124 ui.min_zoom = static_cast<float>(WasmConfig_GetNumber("ui.minZoom", 0.25));
125 ui.max_zoom = static_cast<float>(WasmConfig_GetNumber("ui.maxZoom", 4.0));
127 WasmConfig_GetInt("ui.touchGestureThreshold", 10);
128
129 // Cache settings
130 char* cache_version = WasmConfig_GetString("cache.version", "v1");
131 cache.version = std::string(cache_version);
132 free(cache_version);
133
135 WasmConfig_GetInt("cache.maxRomCacheSizeMb", 100);
136
137 // AI settings
138 ai.enabled = WasmConfig_GetInt("ai.enabled", 1) != 0;
139 char* ai_model = WasmConfig_GetString("ai.model", "gemini-2.5-flash");
140 ai.model = std::string(ai_model);
141 free(ai_model);
142
143 char* ai_endpoint = WasmConfig_GetString("ai.endpoint", "");
144 ai.endpoint = std::string(ai_endpoint);
145 free(ai_endpoint);
146
147 ai.max_response_length = WasmConfig_GetInt("ai.maxResponseLength", 4096);
148
149 // Deployment info (read-only defaults, but can be overridden)
150 char* server_repo = WasmConfig_GetString("deployment.serverRepo",
151 "https://github.com/scawful/yaze-server");
152 deployment.server_repo = std::string(server_repo);
153 free(server_repo);
154
155 deployment.default_port = WasmConfig_GetInt("deployment.defaultPort", 8765);
156
157 char* protocol_version = WasmConfig_GetString("deployment.protocolVersion", "2.0");
158 deployment.protocol_version = std::string(protocol_version);
159 free(protocol_version);
160
161 loaded_.store(true, std::memory_order_release);
162 loading_.store(false, std::memory_order_release);
163}
164
165WasmConfig& WasmConfig::Get() {
166 static WasmConfig instance;
167 return instance;
168}
169
171 // Server status fetching is handled via JavaScript in the web shell.
172 // The web shell calls fetch() on /health and populates window.YAZE_SERVER_STATUS.
173 // This C++ function is a stub - actual status is read from JS config on next LoadFromJavaScript().
174 // TODO: Implement async status fetching via emscripten_async_call if needed.
175}
176
177} // namespace platform
178} // namespace app
179} // namespace yaze
180
181#endif // __EMSCRIPTEN__
EM_JS(void, CallJsAiDriver,(const char *history_json), { if(window.yaze &&window.yaze.ai &&window.yaze.ai.processAgentRequest) { window.yaze.ai.processAgentRequest(UTF8ToString(history_json));} else { console.error("AI Driver not found in window.yaze.ai.processAgentRequest");} })
struct yaze::app::platform::WasmConfig::Terminal terminal
static WasmConfig & Get()
struct yaze::app::platform::WasmConfig::UI ui
struct yaze::app::platform::WasmConfig::AI ai
struct yaze::app::platform::WasmConfig::Autosave autosave
struct yaze::app::platform::WasmConfig::Collaboration collaboration
struct yaze::app::platform::WasmConfig::Deployment deployment
struct yaze::app::platform::WasmConfig::Cache cache