From c081b8cfa9624b12fe830f1455b5c3a4e81dc1aa Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Thu, 30 Jun 2022 20:57:51 +0200 Subject: [PATCH 1/2] Don't load content entries from global and local configs --- apps/launcher/maindialog.cpp | 54 ++++++++++-------------------- components/config/gamesettings.cpp | 12 ++++--- components/config/gamesettings.hpp | 6 ++-- 3 files changed, 28 insertions(+), 44 deletions(-) diff --git a/apps/launcher/maindialog.cpp b/apps/launcher/maindialog.cpp index 5492538306..b7b4c0b4b9 100644 --- a/apps/launcher/maindialog.cpp +++ b/apps/launcher/maindialog.cpp @@ -334,54 +334,36 @@ bool Launcher::MainDialog::setupGameSettings() QString userPath = QString::fromUtf8(mCfgMgr.getUserConfigPath().string().c_str()); QString globalPath = QString::fromUtf8(mCfgMgr.getGlobalPath().string().c_str()); - // Load the user config file first, separately - // So we can write it properly, uncontaminated - QString path = userPath + QLatin1String("openmw.cfg"); - QFile file(path); + QFile file; - qDebug() << "Loading config file:" << path.toUtf8().constData(); - - if (file.exists()) { - if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { - cfgError(tr("Error opening OpenMW configuration file"), - tr("
Could not open %0 for reading

\ - Please make sure you have the right permissions \ - and try again.
").arg(file.fileName())); - return false; - } - QTextStream stream(&file); - stream.setCodec(QTextCodec::codecForName("UTF-8")); - - mGameSettings.readUserFile(stream); - file.close(); - } - - // Now the rest - priority: user > local > global - QStringList paths; - paths.append(globalPath + QString("openmw.cfg")); - paths.append(localPath + QString("openmw.cfg")); - paths.append(userPath + QString("openmw.cfg")); - - for (const QString &path2 : paths) + auto loadFile = [&] (const QString& path, bool(Config::GameSettings::*reader)(QTextStream&, bool), bool ignoreContent = false) { - qDebug() << "Loading config file:" << path2.toUtf8().constData(); - - file.setFileName(path2); + qDebug() << "Loading config file:" << path.toUtf8().constData(); + file.setFileName(path); if (file.exists()) { if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { cfgError(tr("Error opening OpenMW configuration file"), - tr("
Could not open %0 for reading

\ - Please make sure you have the right permissions \ - and try again.
").arg(file.fileName())); + tr("
Could not open %0 for reading

\ + Please make sure you have the right permissions \ + and try again.
").arg(file.fileName())); return false; } QTextStream stream(&file); stream.setCodec(QTextCodec::codecForName("UTF-8")); - mGameSettings.readFile(stream); + (mGameSettings.*reader)(stream, ignoreContent); file.close(); } - } + }; + + // Load the user config file first, separately + // So we can write it properly, uncontaminated + loadFile(userPath + QLatin1String("openmw.cfg"), &Config::GameSettings::readUserFile); + + // Now the rest - priority: user > local > global + loadFile(globalPath + QString("openmw.cfg"), &Config::GameSettings::readFile, true); + loadFile(localPath + QString("openmw.cfg"), &Config::GameSettings::readFile, true); + loadFile(userPath + QString("openmw.cfg"), &Config::GameSettings::readFile); return true; } diff --git a/components/config/gamesettings.cpp b/components/config/gamesettings.cpp index 0ec13abe36..3598e739b5 100644 --- a/components/config/gamesettings.cpp +++ b/components/config/gamesettings.cpp @@ -80,17 +80,17 @@ QStringList Config::GameSettings::values(const QString &key, const QStringList & return defaultValues; } -bool Config::GameSettings::readFile(QTextStream &stream) +bool Config::GameSettings::readFile(QTextStream &stream, bool ignoreContent) { - return readFile(stream, mSettings); + return readFile(stream, mSettings, ignoreContent); } -bool Config::GameSettings::readUserFile(QTextStream &stream) +bool Config::GameSettings::readUserFile(QTextStream &stream, bool ignoreContent) { - return readFile(stream, mUserSettings); + return readFile(stream, mUserSettings, ignoreContent); } -bool Config::GameSettings::readFile(QTextStream &stream, QMultiMap &settings) +bool Config::GameSettings::readFile(QTextStream &stream, QMultiMap &settings, bool ignoreContent) { QMultiMap cache; QRegExp keyRe("^([^=]+)\\s*=\\s*(.+)$"); @@ -139,6 +139,8 @@ bool Config::GameSettings::readFile(QTextStream &stream, QMultiMap &settings); - bool readUserFile(QTextStream &stream); + bool readFile(QTextStream &stream, bool ignoreContent = false); + bool readFile(QTextStream &stream, QMultiMap &settings, bool ignoreContent = false); + bool readUserFile(QTextStream &stream, bool ignoreContent = false); bool writeFile(QTextStream &stream); bool writeFileWithComments(QFile &file); From 478ad07607334db528bb216d5714c5f1b3450438 Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Sat, 2 Jul 2022 11:19:36 +0200 Subject: [PATCH 2/2] Only load global cfg if local wasn't found --- apps/launcher/maindialog.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/apps/launcher/maindialog.cpp b/apps/launcher/maindialog.cpp index b7b4c0b4b9..ab6a3e52a3 100644 --- a/apps/launcher/maindialog.cpp +++ b/apps/launcher/maindialog.cpp @@ -336,7 +336,7 @@ bool Launcher::MainDialog::setupGameSettings() QFile file; - auto loadFile = [&] (const QString& path, bool(Config::GameSettings::*reader)(QTextStream&, bool), bool ignoreContent = false) + auto loadFile = [&] (const QString& path, bool(Config::GameSettings::*reader)(QTextStream&, bool), bool ignoreContent = false) -> std::optional { qDebug() << "Loading config file:" << path.toUtf8().constData(); file.setFileName(path); @@ -346,24 +346,34 @@ bool Launcher::MainDialog::setupGameSettings() tr("
Could not open %0 for reading

\ Please make sure you have the right permissions \ and try again.
").arg(file.fileName())); - return false; + return {}; } QTextStream stream(&file); stream.setCodec(QTextCodec::codecForName("UTF-8")); (mGameSettings.*reader)(stream, ignoreContent); file.close(); + return true; } + return false; }; // Load the user config file first, separately // So we can write it properly, uncontaminated - loadFile(userPath + QLatin1String("openmw.cfg"), &Config::GameSettings::readUserFile); + if(!loadFile(userPath + QLatin1String("openmw.cfg"), &Config::GameSettings::readUserFile)) + return false; // Now the rest - priority: user > local > global - loadFile(globalPath + QString("openmw.cfg"), &Config::GameSettings::readFile, true); - loadFile(localPath + QString("openmw.cfg"), &Config::GameSettings::readFile, true); - loadFile(userPath + QString("openmw.cfg"), &Config::GameSettings::readFile); + if(auto result = loadFile(localPath + QString("openmw.cfg"), &Config::GameSettings::readFile, true)) + { + // Load global if local wasn't found + if(!*result && !loadFile(globalPath + QString("openmw.cfg"), &Config::GameSettings::readFile, true)) + return false; + } + else + return false; + if(!loadFile(userPath + QString("openmw.cfg"), &Config::GameSettings::readFile)) + return false; return true; }