GlosSIConfig: auto-migrate (default) settings

pull/192/head
Peter Repukat 2 years ago
parent 1a22423f56
commit e56a53a0b7

@ -328,61 +328,64 @@ QVariantMap UIModel::getDefaultConf() const
path /= "GlosSI"; path /= "GlosSI";
path /= "default.json"; path /= "default.json";
if (std::filesystem::exists(path)) { QJsonObject defaults = {
QFile file(QString::fromStdWString(path));
if (file.open(QIODevice::ReadOnly)) {
const auto data = file.readAll();
file.close();
return QJsonDocument::fromJson(data).object().toVariantMap();
}
}
QJsonObject obj = {
{"icon", QJsonValue::Null}, {"icon", QJsonValue::Null},
{"name", QJsonValue::Null}, {"name", QJsonValue::Null},
{"version", 1}, {"version", 1},
{"extendedLogging", false}, {"extendedLogging", false},
{"snapshotNotify", false}, {"snapshotNotify", false},
{"ignoreEGS", true}, {"ignoreEGS", true},
{ {"killEGS", false},
"controller", {"controller", QJsonObject{{"maxControllers", 1}, {"emulateDS4", false}, {"allowDesktopConfig", false}}},
QJsonObject{ {"devices",
{"maxControllers", 1}, QJsonObject{
{"emulateDS4", false}, {"hideDevices", true},
{"allowDesktopConfig", false} {"realDeviceIds", false},
} }},
}, {"launch",
{ QJsonObject{
"devices", {"closeOnExit", true},
QJsonObject{ {"launch", false},
{"hideDevices", true}, {"launchAppArgs", QJsonValue::Null},
{"realDeviceIds", false}, {"launchPath", QJsonValue::Null},
} {"waitForChildProcs", true},
}, }},
{ {"window",
"launch", QJsonObject{
QJsonObject{ {"disableOverlay", false},
{"closeOnExit", true}, {"maxFps", QJsonValue::Null},
{"launch", false}, {"scale", QJsonValue::Null},
{"launchAppArgs", QJsonValue::Null}, {"windowMode", false},
{"launchPath", QJsonValue::Null}, }},
{"waitForChildProcs", true},
}
},
{
"window",
QJsonObject{
{"disableOverlay", false},
{"maxFps", QJsonValue::Null},
{"scale", QJsonValue::Null},
{"windowMode", false},
}
},
}; };
saveDefaultConf(obj.toVariantMap()); if (std::filesystem::exists(path)) {
QFile file(QString::fromStdWString(path));
if (file.open(QIODevice::ReadOnly)) {
const auto data = file.readAll();
file.close();
auto json = QJsonDocument::fromJson(data).object();
const auto applyDefaults = [](QJsonObject obj, const QJsonObject& defaults,
auto applyDefaultsFn) -> QJsonObject {
for (const auto& key : defaults.keys()) {
qDebug() << key << ": " << obj[key];
if ((obj[key].isUndefined() || obj[key].isNull()) && !defaults[key].isNull()) {
obj[key] = defaults.value(key);
}
if (obj.value(key).isObject()) {
obj[key] = applyDefaultsFn(obj[key].toObject(), defaults.value(key).toObject(), applyDefaultsFn);
}
}
return obj;
};
json = applyDefaults(json, defaults, applyDefaults);
return json.toVariantMap();
}
}
saveDefaultConf(defaults.toVariantMap());
return getDefaultConf(); return getDefaultConf();
} }
void UIModel::saveDefaultConf(QVariantMap conf) const void UIModel::saveDefaultConf(QVariantMap conf) const
@ -497,9 +500,7 @@ void UIModel::onAvailFilesResponse(QNetworkReply* reply)
const auto defaultConf = getDefaultConf(); const auto defaultConf = getDefaultConf();
bool snapshotNotify = bool snapshotNotify =
defaultConf.contains("snapshotNotify") defaultConf.contains("snapshotNotify") ? defaultConf["snapshotNotify"].toJsonValue().toBool() : false;
? defaultConf["snapshotNotify"].toJsonValue().toBool()
: false;
struct VersionInfo { struct VersionInfo {
int major; int major;
@ -512,8 +513,9 @@ void UIModel::onAvailFilesResponse(QNetworkReply* reply)
std::vector<std::pair<QString, VersionInfo>> new_versions; std::vector<std::pair<QString, VersionInfo>> new_versions;
for (const auto& info : for (const auto& info :
json.keys() | std::ranges::views::filter([this, &json, snapshotNotify](const auto& key) { json.keys() | std::ranges::views::filter([this, &json, snapshotNotify](const auto& key) {
return notify_on_snapshots_ ? true return notify_on_snapshots_
: json[key].toObject().value("type") == (snapshotNotify ? "snapshot" : "release"); ? true
: json[key].toObject().value("type") == (snapshotNotify ? "snapshot" : "release");
}) | std::ranges::views::transform([&json](const auto& key) -> std::pair<QString, VersionInfo> { }) | std::ranges::views::transform([&json](const auto& key) -> std::pair<QString, VersionInfo> {
const auto versionString = json[key].toObject().value("version").toString(); const auto versionString = json[key].toObject().value("version").toString();
const auto cleanVersion = versionString.split("-")[0]; const auto cleanVersion = versionString.split("-")[0];

Loading…
Cancel
Save