mirror of
https://github.com/mkxp-z/mkxp-z.git
synced 2025-08-04 05:55:31 +02:00
make config code slightly more readable
This commit is contained in:
parent
745dc3854f
commit
e91c470ea0
2 changed files with 17 additions and 17 deletions
|
@ -662,29 +662,27 @@ RB_METHOD(mkxpLaunch) {
|
||||||
return RUBY_Qnil;
|
return RUBY_Qnil;
|
||||||
}
|
}
|
||||||
|
|
||||||
json5pp::value userSettings;
|
json5pp::value loadUserSettings() {
|
||||||
|
json5pp::value ret;
|
||||||
void loadUserSettings() {
|
|
||||||
if (!userSettings.is_null())
|
|
||||||
return;
|
|
||||||
|
|
||||||
VALUE cpath = rb_utf8_str_new_cstr(shState->config().userConfPath.c_str());
|
VALUE cpath = rb_utf8_str_new_cstr(shState->config().userConfPath.c_str());
|
||||||
|
|
||||||
if (rb_funcall(rb_cFile, rb_intern("exists?"), 1, cpath) == Qtrue) {
|
if (rb_funcall(rb_cFile, rb_intern("exists?"), 1, cpath) == Qtrue) {
|
||||||
VALUE f = rb_funcall(rb_cFile, rb_intern("open"), 2, cpath, rb_str_new("r", 1));
|
VALUE f = rb_funcall(rb_cFile, rb_intern("open"), 2, cpath, rb_str_new("r", 1));
|
||||||
VALUE data = rb_funcall(f, rb_intern("read"), 0);
|
VALUE data = rb_funcall(f, rb_intern("read"), 0);
|
||||||
rb_funcall(f, rb_intern("close"), 0);
|
rb_funcall(f, rb_intern("close"), 0);
|
||||||
userSettings = rb2json(data);
|
ret = json5pp::parse5(RSTRING_PTR(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!userSettings.is_object())
|
if (!ret.is_object())
|
||||||
userSettings = json5pp::object({});
|
ret = json5pp::object({});
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void saveUserSettings() {
|
void saveUserSettings(json5pp::value &settings) {
|
||||||
VALUE cpath = rb_utf8_str_new_cstr(shState->config().userConfPath.c_str());
|
VALUE cpath = rb_utf8_str_new_cstr(shState->config().userConfPath.c_str());
|
||||||
VALUE f = rb_funcall(rb_cFile, rb_intern("open"), 2, cpath, rb_str_new("w", 1));
|
VALUE f = rb_funcall(rb_cFile, rb_intern("open"), 2, cpath, rb_str_new("w", 1));
|
||||||
rb_funcall(f, rb_intern("write"), 1, rb_utf8_str_new_cstr(userSettings.stringify5(json5pp::rule::space_indent<>()).c_str()));
|
rb_funcall(f, rb_intern("write"), 1, rb_utf8_str_new_cstr(settings.stringify5(json5pp::rule::space_indent<>()).c_str()));
|
||||||
rb_funcall(f, rb_intern("close"), 0);
|
rb_funcall(f, rb_intern("close"), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -695,8 +693,8 @@ RB_METHOD(mkxpGetJSONSetting) {
|
||||||
rb_scan_args(argc, argv, "1", &sname);
|
rb_scan_args(argc, argv, "1", &sname);
|
||||||
SafeStringValue(sname);
|
SafeStringValue(sname);
|
||||||
|
|
||||||
loadUserSettings();
|
auto settings = loadUserSettings();
|
||||||
auto &s = userSettings.as_object();
|
auto &s = settings.as_object();
|
||||||
|
|
||||||
if (s[RSTRING_PTR(sname)].is_null()) {
|
if (s[RSTRING_PTR(sname)].is_null()) {
|
||||||
return json2rb(shState->config().raw.as_object()[RSTRING_PTR(sname)]);
|
return json2rb(shState->config().raw.as_object()[RSTRING_PTR(sname)]);
|
||||||
|
@ -713,8 +711,10 @@ RB_METHOD(mkxpSetJSONSetting) {
|
||||||
rb_scan_args(argc, argv, "2", &sname, &svalue);
|
rb_scan_args(argc, argv, "2", &sname, &svalue);
|
||||||
SafeStringValue(sname);
|
SafeStringValue(sname);
|
||||||
|
|
||||||
userSettings.as_object()[RSTRING_PTR(sname)] = rb2json(svalue);
|
auto settings = loadUserSettings();
|
||||||
saveUserSettings();
|
auto &s = settings.as_object();
|
||||||
|
s.emplace(RSTRING_PTR(sname), rb2json(svalue));
|
||||||
|
saveUserSettings(settings);
|
||||||
|
|
||||||
return Qnil;
|
return Qnil;
|
||||||
}
|
}
|
||||||
|
|
|
@ -245,7 +245,7 @@ try { exp } catch (...) {}
|
||||||
readGameINI();
|
readGameINI();
|
||||||
|
|
||||||
// Now check for an extra mkxp.conf in the user's save directory and merge anything else from that
|
// Now check for an extra mkxp.conf in the user's save directory and merge anything else from that
|
||||||
userConfPath = customDataPath + "/" CONF_FILE;
|
userConfPath = mkxp_fs::normalizePath(std::string(customDataPath + "/" CONF_FILE).c_str(), 0, 1);
|
||||||
json::value userConf = readConfFile(userConfPath.c_str());
|
json::value userConf = readConfFile(userConfPath.c_str());
|
||||||
copyObject(optsJ, userConf);
|
copyObject(optsJ, userConf);
|
||||||
|
|
||||||
|
@ -388,7 +388,7 @@ void Config::readGameINI() {
|
||||||
if (dataPathApp.empty())
|
if (dataPathApp.empty())
|
||||||
dataPathApp = game.title;
|
dataPathApp = game.title;
|
||||||
|
|
||||||
customDataPath = prefPath(dataPathOrg.c_str(), dataPathApp.c_str());
|
customDataPath = mkxp_fs::normalizePath(prefPath(dataPathOrg.c_str(), dataPathApp.c_str()).c_str(), 0, 1);
|
||||||
|
|
||||||
if (rgssVersion == 0) {
|
if (rgssVersion == 0) {
|
||||||
/* Try to guess RGSS version based on Data/Scripts extension */
|
/* Try to guess RGSS version based on Data/Scripts extension */
|
||||||
|
|
Loading…
Add table
Reference in a new issue