forked from neutralinojs/neutralinojs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.cpp
294 lines (256 loc) · 10.1 KB
/
settings.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
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
#include <string>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <regex>
#include <vector>
#include <map>
#include <set>
#include "lib/json/json.hpp"
#include "settings.h"
#include "extensions_loader.h"
#include "resources.h"
#include "helpers.h"
#include "auth/authbasic.h"
#include "api/fs/fs.h"
#include "api/debug/debug.h"
#include "api/app/app.h"
#include "api/custom/custom.h"
#include "api/window/window.h"
#include "api/computer/computer.h"
using namespace std;
using json = nlohmann::json;
namespace settings {
json options;
json globalArgs;
string appPath;
string configFile = NEU_APP_CONFIG_FILE;
vector<settings::ConfigOverride> configOverrides;
string joinAppPath(const string &filename) {
return appPath + filename;
}
string getAppPath() {
return appPath;
}
string getConfigFile() {
return configFile;
}
json getConfig() {
if(!options.is_null())
return options;
json config;
try {
fs::FileReaderResult fileReaderResult = resources::getFile(configFile);
config = json::parse(fileReaderResult.data);
options = config;
// Apply config overrides
json patches;
for(const auto &cfgOverride: configOverrides) {
json patch;
patch["op"] = options[json::json_pointer(cfgOverride.key)].is_null()
? "add" : "replace";
patch["path"] = cfgOverride.key;
// String to actual types
if(cfgOverride.convertTo == "int") {
patch["value"] = stoi(cfgOverride.value);
}
else if(cfgOverride.convertTo == "bool") {
patch["value"] = cfgOverride.value == "true";
}
else {
patch["value"] = cfgOverride.value;
}
patches.push_back(patch);
}
if(!patches.is_null()) {
options = options.patch(patches);
}
}
catch(exception e) {
debug::log(debug::LogTypeError, errors::makeErrorMsg(errors::NE_CF_UNBLDCF, string(configFile)));
}
return options;
}
string getGlobalVars(){
string jsSnippet = "var NL_OS='" + string(NEU_OS_NAME) + "';";
jsSnippet += "var NL_ARCH='" + computer::getArch() + "';";
jsSnippet += "var NL_VERSION='" + string(NEU_VERSION) + "';";
jsSnippet += "var NL_COMMIT='" + string(NEU_COMMIT) + "';";
jsSnippet += "var NL_APPID='" + options["applicationId"].get<string>() + "';";
if(!options["version"].is_null()) {
jsSnippet += "var NL_APPVERSION='" + options["version"].get<string>() + "';";
}
jsSnippet += "var NL_PORT=" + to_string(settings::getOptionForCurrentMode("port").get<int>()) + ";";
jsSnippet += "var NL_MODE='" + options["defaultMode"].get<string>() + "';";
jsSnippet += "var NL_TOKEN='" + authbasic::getToken() + "';";
jsSnippet += "var NL_CWD='" + fs::getCurrentDirectory() + "';";
jsSnippet += "var NL_ARGS=" + globalArgs.dump() + ";";
jsSnippet += "var NL_PATH='" + appPath + "';";
jsSnippet += "var NL_PID=" + to_string(app::getProcessId()) + ";";
jsSnippet += "var NL_RESMODE='" + resources::getModeString() + "';";
jsSnippet += "var NL_EXTENABLED=" + json(extensions::isInitialized()).dump() + ";";
jsSnippet += "var NL_CMETHODS=" + json(custom::getMethods()).dump() + ";";
jsSnippet += "var NL_WSAVSTLOADED=" + json(window::isSavedStateLoaded()).dump() + ";";
jsSnippet += "var NL_CONFIGFILE='" + settings::getConfigFile() + "';";
json jGlobalVariables = settings::getOptionForCurrentMode("globalVariables");
if(!jGlobalVariables.is_null()) {
for(const auto &it: jGlobalVariables.items()) {
jsSnippet += "var NL_" + it.key() + "=JSON.parse('" + it.value().dump() + "');";
}
}
return jsSnippet;
}
settings::CliArg _parseArg(const string &argStr) {
settings::CliArg arg;
vector<string> argParts = helpers::split(argStr, '=');
if(argParts.size() == 2 && argParts[1].length() > 0) {
arg.key = argParts[0];
arg.value = argParts[1];
}
else {
arg.key = argStr;
}
return arg;
}
void setGlobalArgs(const json &args) {
int argIndex = 0;
globalArgs = args;
for(const string &arg: args) {
settings::CliArg cliArg = _parseArg(arg);
// Set default path
if(argIndex == 0) {
appPath = fs::getDirectoryName(arg);
if(appPath == "")
appPath = fs::getCurrentDirectory();
}
// Resources read mode (resources.neu or from directory)
if(cliArg.key == "--load-dir-res") {
resources::setMode(resources::ResourceModeDir);
continue;
}
// Set app path context
if(cliArg.key == "--path") {
appPath = cliArg.value;
continue;
}
// Set app config file name
if(cliArg.key == "--config-file") {
configFile = cliArg.value;
continue;
}
// Enable dev tools connection (as an extension)
// Not available for production (resources.neu-based) apps
if(cliArg.key == "--neu-dev-extension" && resources::getMode() == resources::ResourceModeDir) {
extensions::loadOne("js.neutralino.devtools");
continue;
}
// Override app configs
applyConfigOverride(cliArg);
argIndex++;
}
}
settings::AppMode getMode() {
string mode = options["defaultMode"].get<string>();
if(mode == "window") return settings::AppModeWindow;
if(mode == "browser") return settings::AppModeBrowser;
if(mode == "cloud") return settings::AppModeCloud;
if(mode == "chrome") return settings::AppModeChrome;
return settings::AppModeWindow;
}
void setPort(int port) {
options["port"] = port;
options["/modes/window/port"_json_pointer] = port;
options["/modes/browser/port"_json_pointer] = port;
options["/modes/cloud/port"_json_pointer] = port;
options["/modes/chrome/port"_json_pointer] = port;
}
void applyConfigOverride(const settings::CliArg &arg) {
map<string, vector<string>> cliMappings = {
// Top level
{"--mode", {"/defaultMode", "string"}},
{"--url", {"/url", "string"}},
{"--port", {"/port", "int"}},
{"--logging-enabled", {"/logging/enabled", "bool"}},
{"--logging-write-to-log-file", {"/logging/writeToLogFile", "bool"}},
{"--enable-server", {"/enableServer", "bool"}},
{"--enable-extensions", {"/enableExtensions", "bool"}},
{"--export-auth-info", {"/exportAuthInfo", "bool"}},
// Window mode
{"--window-title", {"/modes/window/title", "string"}},
{"--window-width", {"/modes/window/width", "int"}},
{"--window-height", {"/modes/window/height", "int"}},
{"--window-min-width", {"/modes/window/minWidth", "int"}},
{"--window-min-height", {"/modes/window/minHeight", "int"}},
{"--window-max-width", {"/modes/window/maxWidth", "int"}},
{"--window-max-height", {"/modes/window/maxHeight", "int"}},
{"--window-x", {"/modes/window/x", "int"}},
{"--window-y", {"/modes/window/y", "int"}},
{"--window-full-screen", {"/modes/window/fullScreen", "bool"}},
{"--window-always-on-top", {"/modes/window/alwaysOnTop", "bool"}},
{"--window-enable-inspector", {"/modes/window/enableInspector", "bool"}},
{"--window-borderless", {"/modes/window/borderless", "bool"}},
{"--window-maximize", {"/modes/window/maximize", "bool"}},
{"--window-hidden", {"/modes/window/hidden", "bool"}},
{"--window-resizable", {"/modes/window/resizable", "bool"}},
{"--window-maximizable", {"/modes/window/maximizable", "bool"}},
{"--window-center", {"/modes/window/center", "bool"}},
{"--window-transparent", {"/modes/window/transparent", "bool"}},
{"--window-exit-process-on-close", {"/modes/window/exitProcessOnClose", "bool"}},
{"--window-use-saved-state", {"/modes/window/useSavedState", "bool"}},
{"--window-icon", {"/modes/window/icon", "string"}},
{"--window-extend-user-agent-with", {"/modes/window/extendUserAgentWith", "string"}},
// Chrome mode
{"--chrome-width", {"/modes/chrome/width", "int"}},
{"--chrome-height", {"/modes/chrome/height", "int"}},
{"--chrome-args", {"/modes/chrome/args", "string"}}
};
// Allows overriding from modes
// So, update all modes' values from CLI args
set<string> cliMappingAliases = {
"/port",
"/url",
"/logging/enabled",
"/logging/writeToLogFile",
"/enableServer",
"/enableExtensions",
"/exportAuthInfo"
};
if(cliMappings.find(arg.key) != cliMappings.end()) {
if(arg.key == "--mode") {
vector<string> modes = helpers::getModes();
if(find(modes.begin(), modes.end(), arg.value) == modes.end()) {
debug::log(debug::LogTypeError, errors::makeErrorMsg(errors::NE_CF_UNSUPMD, arg.value));
return;
}
}
settings::ConfigOverride cfgOverride;
cfgOverride.key = cliMappings[arg.key][0];
cfgOverride.convertTo = cliMappings[arg.key][1];
cfgOverride.value = arg.value;
// Make cases like --window-full-screen -> window-full-screen=true
if(cfgOverride.convertTo == "bool" && cfgOverride.value.empty()) {
cfgOverride.value = "true";
}
// Add original
configOverrides.push_back(cfgOverride);
// Add aliases
if(cliMappingAliases.find(cfgOverride.key) != cliMappingAliases.end()) {
vector<string> modes = helpers::getModes();
for(const string &mode: modes) {
settings::ConfigOverride cfgOverrideAlias = cfgOverride;
cfgOverrideAlias.key = "/modes/" + mode + cfgOverride.key;
configOverrides.push_back(cfgOverrideAlias);
}
}
}
}
// Priority: mode -> root -> null
json getOptionForCurrentMode(const string &key) {
string mode = options["defaultMode"].get<string>();
json value = options["modes"][mode][key];
if(value.is_null()) {
value = options[key];
}
return value;
}
} // namespace settings