Merge branch 'launcher-barely-knew-her' into 'master'
Backup and restore launcher settings / order when dropped by older versions Closes #9348 See merge request OpenMW/openmw!5735
This commit is contained in:
@@ -7,10 +7,18 @@
|
||||
|
||||
namespace
|
||||
{
|
||||
void read(Config::LauncherSettings& settings, QString text)
|
||||
void read(Config::LauncherSettings& settings, QString text, bool missingOnly = false)
|
||||
{
|
||||
QTextStream stream(&text);
|
||||
settings.readFile(stream);
|
||||
settings.readFile(stream, missingOnly);
|
||||
}
|
||||
|
||||
Config::LauncherSettings restore(const QString& main, const QString& backup)
|
||||
{
|
||||
Config::LauncherSettings settings;
|
||||
read(settings, main);
|
||||
read(settings, backup, true);
|
||||
return settings;
|
||||
}
|
||||
|
||||
Config::LauncherSettings parse(const QString& text)
|
||||
@@ -63,6 +71,7 @@ thing=5
|
||||
EXPECT_EQ(write(settings).toStdString(), R"(
|
||||
[Settings]
|
||||
language=en
|
||||
watchdog=true
|
||||
future=1
|
||||
|
||||
[Profiles]
|
||||
@@ -149,7 +158,7 @@ thing=5
|
||||
EXPECT_TRUE(write(settings).contains("Default/future=one\nDefault/future=two\nDefault/future=three\n"));
|
||||
}
|
||||
|
||||
TEST(LauncherSettingsTest, ContentOrderSurvivesAWriteAndRead)
|
||||
TEST(LauncherSettingsTest, contentOrderSurvivesAWriteAndRead)
|
||||
{
|
||||
Config::LauncherSettings settings;
|
||||
settings.setContentList("Default", { "/games/morrowind/Data Files" }, { "Morrowind.bsa" },
|
||||
@@ -162,7 +171,7 @@ thing=5
|
||||
EXPECT_EQ(reread.getContentListFiles("Default"), QStringList({ "Morrowind.esm", "Tribunal.esm" }));
|
||||
}
|
||||
|
||||
TEST(LauncherSettingsTest, ALauncherConfigWrittenBeforeTheOrderKeyStillLoads)
|
||||
TEST(LauncherSettingsTest, launcherConfigWrittenBeforeTheOrderKeyStillLoads)
|
||||
{
|
||||
const Config::LauncherSettings settings = parse(QString::fromUtf8(sConfig));
|
||||
|
||||
@@ -171,7 +180,7 @@ thing=5
|
||||
EXPECT_TRUE(settings.getUserFilesInOrder("Default").isEmpty());
|
||||
}
|
||||
|
||||
TEST(LauncherSettingsTest, TheOrderKeyDoesNotLeakIntoTheOtherProfileLists)
|
||||
TEST(LauncherSettingsTest, orderKeyDoesNotLeakIntoTheOtherProfileLists)
|
||||
{
|
||||
const Config::LauncherSettings settings = parse(QString::fromUtf8(sConfig)
|
||||
+ "Default/order=Morrowind.esm\n"
|
||||
@@ -184,4 +193,52 @@ thing=5
|
||||
EXPECT_EQ(settings.getDataDirectoryList("Default"), QStringList({ "/games/morrowind/Data Files" }));
|
||||
EXPECT_EQ(settings.getArchiveList("Default"), QStringList({ "Morrowind.bsa" }));
|
||||
}
|
||||
|
||||
TEST(LauncherSettingsTest, watchdogRestoreShouldAddMissingKeysAndMaintainFileOrder)
|
||||
{
|
||||
const QString current = QString::fromUtf8(sConfig);
|
||||
const QString backup = QString::fromUtf8(sConfig) +
|
||||
"Default/order=Morrowind.esm\n"
|
||||
"Default/order=SomethingDisabled.esp\n"
|
||||
"Default/order=Tribunal.esm\n";
|
||||
|
||||
const Config::LauncherSettings settings = restore(current, backup);
|
||||
|
||||
EXPECT_EQ(settings.getUserFilesInOrder("Default"),
|
||||
QStringList({ "Morrowind.esm", "SomethingDisabled.esp", "Tribunal.esm" }));
|
||||
EXPECT_EQ(settings.getContentListFiles("Default"), QStringList({ "Morrowind.esm", "Tribunal.esm" }));
|
||||
}
|
||||
|
||||
TEST(LauncherSettingsTest, watchdogRestoreShouldMaintainExistingKeyValues)
|
||||
{
|
||||
const QString current = QString::fromUtf8(sConfig) +
|
||||
"Default/order=Morrowind.esm\n"
|
||||
"Default/order=SomethingDisabled.esp\n"
|
||||
"Default/order=Tribunal.esm\n";
|
||||
const QString backup = QString::fromUtf8(sConfig) +
|
||||
"Default/order=SomethingDisabled.esp\n"
|
||||
"Default/order=Morrowind.esm\n"
|
||||
"Default/order=Tribunal.esm\n";
|
||||
|
||||
const Config::LauncherSettings settings = restore(current, backup);
|
||||
|
||||
EXPECT_EQ(settings.getCurrentContentListName(), "Default");
|
||||
EXPECT_EQ(settings.getUserFilesInOrder("Default"),
|
||||
QStringList({ "Morrowind.esm", "SomethingDisabled.esp", "Tribunal.esm" }));
|
||||
}
|
||||
|
||||
TEST(LauncherSettingsTest, watchdogShouldBeUnsetByDefault)
|
||||
{
|
||||
const Config::LauncherSettings settings = parse(QString::fromUtf8(sConfig));
|
||||
|
||||
EXPECT_FALSE(settings.getWatchdog());
|
||||
}
|
||||
|
||||
TEST(LauncherSettingsTest, watchdogShouldBeSetOnSuccessfulWrite)
|
||||
{
|
||||
const QString written = write(parse("[Settings]\nlanguage=English\n"));
|
||||
|
||||
EXPECT_EQ(written.count("watchdog="), 1);
|
||||
EXPECT_TRUE(written.contains("watchdog=true\n"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -314,18 +314,19 @@ bool Launcher::MainDialog::setupLauncherSettings()
|
||||
{
|
||||
mLauncherSettings.clear();
|
||||
|
||||
// Launcher settings
|
||||
const QString path
|
||||
= Files::pathToQString(mCfgMgr.getUserConfigPath() / Config::LauncherSettings::sLauncherConfigFileName);
|
||||
|
||||
if (!QFile::exists(path))
|
||||
return true;
|
||||
|
||||
Log(Debug::Info) << "Loading config file: " << path.toUtf8().constData();
|
||||
Log(Debug::Info) << "Loading Launcher config file: " << path.toUtf8().constData();
|
||||
|
||||
QFile file(path);
|
||||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
|
||||
{
|
||||
cfgError(tr("Error opening OpenMW configuration file"),
|
||||
cfgError(tr("Error opening Launcher configuration file"),
|
||||
tr("<br><b>Could not open %0 for reading:</b><br><br>%1<br><br>"
|
||||
"Please make sure you have the right permissions "
|
||||
"and try again.<br>")
|
||||
@@ -338,6 +339,35 @@ bool Launcher::MainDialog::setupLauncherSettings()
|
||||
Misc::ensureUtf8Encoding(stream);
|
||||
|
||||
mLauncherSettings.readFile(stream);
|
||||
file.close();
|
||||
|
||||
// Launcher settings backup
|
||||
file.setFileName(
|
||||
Files::pathToQString(mCfgMgr.getUserConfigPath() / Config::LauncherSettings::sLauncherConfigBackupFileName));
|
||||
|
||||
// Read from the backup only if the watchdog is unset and the backup exists
|
||||
if (mLauncherSettings.getWatchdog() || !file.exists())
|
||||
return true;
|
||||
|
||||
Log(Debug::Warning) << "Watchdog unset. Restoring missing keys from Launcher config backup file: "
|
||||
<< file.fileName().toUtf8().constData();
|
||||
|
||||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
|
||||
{
|
||||
cfgError(tr("Error opening Launcher configuration backup file"),
|
||||
tr("<br><b>Could not open %0 for reading:</b><br><br>%1<br><br>"
|
||||
"Please make sure you have the right permissions "
|
||||
"and try again.<br>")
|
||||
.arg(file.fileName().toHtmlEscaped())
|
||||
.arg(file.errorString().toHtmlEscaped()));
|
||||
return true;
|
||||
}
|
||||
|
||||
QTextStream backupStream(&file);
|
||||
Misc::ensureUtf8Encoding(backupStream);
|
||||
|
||||
mLauncherSettings.readFile(backupStream, true); // Restore missing settings
|
||||
file.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -543,12 +573,34 @@ bool Launcher::MainDialog::writeSettings()
|
||||
}
|
||||
|
||||
QTextStream stream(&file);
|
||||
stream.setDevice(&file);
|
||||
Misc::ensureUtf8Encoding(stream);
|
||||
|
||||
mLauncherSettings.writeFile(stream);
|
||||
file.close();
|
||||
|
||||
// Launcher settings backup
|
||||
file.setFileName(Files::pathToQString(userPath / Config::LauncherSettings::sLauncherConfigBackupFileName));
|
||||
|
||||
if (!file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate))
|
||||
{
|
||||
// File cannot be opened or created
|
||||
cfgError(tr("Error writing Launcher configuration backup file"),
|
||||
tr("<br><b>Could not open or create %0 for writing</b><br><br>"
|
||||
"Please make sure you have the right permissions "
|
||||
"and try again.<br>")
|
||||
.arg(file.fileName().toHtmlEscaped()));
|
||||
return false;
|
||||
}
|
||||
|
||||
QTextStream backupStream(&file);
|
||||
Misc::ensureUtf8Encoding(backupStream);
|
||||
|
||||
backupStream << "# Backup file written automatically by the OpenMW launcher. Do not edit or rename.\n"
|
||||
"# Your settings are located in launcher.cfg\n";
|
||||
|
||||
mLauncherSettings.writeFile(backupStream);
|
||||
file.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ namespace Config
|
||||
constexpr char sMainWindowHeightKey[] = "MainWindow/height";
|
||||
constexpr char sMainWindowPosXKey[] = "MainWindow/posx";
|
||||
constexpr char sMainWindowPosYKey[] = "MainWindow/posy";
|
||||
constexpr char sWatchdogKey[] = "watchdog";
|
||||
|
||||
QString makeNewContentListName()
|
||||
{
|
||||
@@ -122,6 +123,8 @@ namespace Config
|
||||
settings.mLanguage = value;
|
||||
return true;
|
||||
}
|
||||
if (key == sWatchdogKey)
|
||||
return parseBool(value, settings.mWatchdog);
|
||||
|
||||
settings.mUnknown.emplaceBack(key, value);
|
||||
return true;
|
||||
@@ -210,6 +213,7 @@ namespace Config
|
||||
{
|
||||
writeSectionHeader(sSettingsSection, stream);
|
||||
writeKeyValue(sLanguageKey, value.mLanguage, stream);
|
||||
writeKeyValue(sWatchdogKey, true, stream); // Set so future reads check for dropped launcher settings
|
||||
writeUnknownKeys(value.mUnknown, stream);
|
||||
}
|
||||
|
||||
@@ -411,11 +415,13 @@ bool Config::LauncherSettings::setValue(const QString& sectionPrefix, const QStr
|
||||
return true;
|
||||
}
|
||||
|
||||
void Config::LauncherSettings::readFile(QTextStream& stream)
|
||||
void Config::LauncherSettings::readFile(QTextStream& stream, bool missingOnly)
|
||||
{
|
||||
const QRegularExpression sectionRe("^\\[([^]]+)\\]$");
|
||||
const QRegularExpression keyRe("^([^=]+)\\s*=\\s*(.+)$");
|
||||
|
||||
QSet<QString> readKeys;
|
||||
|
||||
QString section;
|
||||
|
||||
while (!stream.atEnd())
|
||||
@@ -442,11 +448,18 @@ void Config::LauncherSettings::readFile(QTextStream& stream)
|
||||
const QString key = keyMatch.captured(1).trimmed();
|
||||
const QString value = keyMatch.captured(2).trimmed();
|
||||
|
||||
const QString id = "[" + section + "]." + key;
|
||||
if (missingOnly && mReadKeys.contains(id))
|
||||
continue;
|
||||
|
||||
if (!setValue(section, key, value))
|
||||
Log(Debug::Warning) << "Invalid value in the launcher config file: section: "
|
||||
<< section.toUtf8().constData() << " key: " << key.toUtf8().constData()
|
||||
<< " value: " << value.toUtf8().constData();
|
||||
else
|
||||
readKeys.insert(id);
|
||||
}
|
||||
mReadKeys.unite(readKeys);
|
||||
}
|
||||
|
||||
const Config::LauncherSettings::Profile* Config::LauncherSettings::findProfile(const QString& name) const
|
||||
@@ -463,5 +476,6 @@ void Config::LauncherSettings::clear()
|
||||
mGeneral = General{};
|
||||
mProfiles = Profiles{};
|
||||
mImporter = Importer{};
|
||||
mReadKeys.clear();
|
||||
mUnknownSections.clear();
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <QList>
|
||||
#include <QMap>
|
||||
#include <QPair>
|
||||
#include <QSet>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
|
||||
@@ -19,6 +20,7 @@ namespace Config
|
||||
{
|
||||
public:
|
||||
static constexpr char sLauncherConfigFileName[] = "launcher.cfg";
|
||||
static constexpr char sLauncherConfigBackupFileName[] = "launcher-wd.dat";
|
||||
|
||||
/// Keys of a section this version does not use, in file order, written back as read
|
||||
using UnknownKeys = QList<QPair<QString, QString>>;
|
||||
@@ -26,6 +28,7 @@ namespace Config
|
||||
struct Settings
|
||||
{
|
||||
QString mLanguage;
|
||||
bool mWatchdog = false;
|
||||
UnknownKeys mUnknown;
|
||||
};
|
||||
|
||||
@@ -66,7 +69,7 @@ namespace Config
|
||||
UnknownKeys mUnknown;
|
||||
};
|
||||
|
||||
void readFile(QTextStream& stream);
|
||||
void readFile(QTextStream& stream, bool missingOnly = false);
|
||||
|
||||
void clear();
|
||||
|
||||
@@ -101,6 +104,8 @@ namespace Config
|
||||
|
||||
void setLanguage(const QString& value) { mSettings.mLanguage = value; }
|
||||
|
||||
bool getWatchdog() const { return mSettings.mWatchdog; }
|
||||
|
||||
MainWindow getMainWindow() const { return mGeneral.mMainWindow; }
|
||||
|
||||
void setMainWindow(const MainWindow& value) { mGeneral.mMainWindow = value; }
|
||||
@@ -118,6 +123,7 @@ namespace Config
|
||||
Profiles mProfiles;
|
||||
General mGeneral;
|
||||
Importer mImporter;
|
||||
QSet<QString> mReadKeys;
|
||||
/// Sections this version does not know at all, in name order, written back as read
|
||||
QMap<QString, UnknownKeys> mUnknownSections;
|
||||
|
||||
|
||||
@@ -569,6 +569,18 @@ to default Morrowind fonts. Check this box if you still prefer original fonts ov
|
||||
<source><br><b>You do not have a game file selected.</b><br><br>OpenMW will not start without a game file selected.<br></source>
|
||||
<translation><br><b>Не сте избрали файл на играта.</b><br><br>OpenMW няма да стартира без избран файл на играта.<br></translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Error writing Launcher configuration backup file</source>
|
||||
<translation>Грешка при записване на резервното копие на конфигурационния файл на стартиращата програма</translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Error opening Launcher configuration file</source>
|
||||
<translation>Грешка при отваряне на конфигурационния файл на стартиращата програма</translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Error opening Launcher configuration backup file</source>
|
||||
<translation>Грешка при отваряне на резервното копие на конфигурационния файл на стартиращата програма</translation>
|
||||
</message>
|
||||
</context>
|
||||
<context>
|
||||
<name>Launcher::SettingsPage</name>
|
||||
|
||||
@@ -567,6 +567,18 @@ to default Morrowind fonts. Check this box if you still prefer original fonts ov
|
||||
<source><br><b>Could not create directory %0</b><br><br>%1<br></source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Error opening Launcher configuration file</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Error opening Launcher configuration backup file</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Error writing Launcher configuration backup file</source>
|
||||
<translation type="unfinished"></translation>
|
||||
</message>
|
||||
</context>
|
||||
<context>
|
||||
<name>Launcher::SettingsPage</name>
|
||||
|
||||
@@ -567,6 +567,18 @@ to default Morrowind fonts. Check this box if you still prefer original fonts ov
|
||||
<source><br><b>You do not have a game file selected.</b><br><br>OpenMW will not start without a game file selected.<br></source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Error writing Launcher configuration backup file</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Error opening Launcher configuration file</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Error opening Launcher configuration backup file</source>
|
||||
<translation></translation>
|
||||
</message>
|
||||
</context>
|
||||
<context>
|
||||
<name>Launcher::SettingsPage</name>
|
||||
|
||||
@@ -567,6 +567,18 @@ to default Morrowind fonts. Check this box if you still prefer original fonts ov
|
||||
<source><br><b>Could not create directory %0</b><br><br>%1<br></source>
|
||||
<translation><br><b>Impossible de créer le dossier %0</b><br><br>%1<br></translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Error writing Launcher configuration backup file</source>
|
||||
<translation>Erreur lors de l'écriture du fichier de sauvegarde des paramètres du Lanceur</translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Error opening Launcher configuration file</source>
|
||||
<translation>Erreur lors de l'ouverture du fichier de paramètres du Lanceur</translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Error opening Launcher configuration backup file</source>
|
||||
<translation>Erreur lors de l'ouverture du fichier de sauvegarde des paramètres du Lanceur</translation>
|
||||
</message>
|
||||
</context>
|
||||
<context>
|
||||
<name>Launcher::SettingsPage</name>
|
||||
|
||||
@@ -561,7 +561,7 @@ do domyślnych czcionek Morrowind. Zaznacz to pole, jeśli nadal wolisz oryginal
|
||||
</message>
|
||||
<message>
|
||||
<source>Error writing Launcher configuration file</source>
|
||||
<translation>Błąd zapisu pliku konfiguracyjnego programu uruchamiącego</translation>
|
||||
<translation>Błąd zapisu pliku konfiguracyjnego programu uruchamiającego</translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>No game file selected</source>
|
||||
@@ -571,6 +571,18 @@ do domyślnych czcionek Morrowind. Zaznacz to pole, jeśli nadal wolisz oryginal
|
||||
<source><br><b>You do not have a game file selected.</b><br><br>OpenMW will not start without a game file selected.<br></source>
|
||||
<translation><br><b>Nie wybrano pliku gry.</b><br><br>OpenMW nie uruchomi się bez wybranego pliku gry.<br></translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Error writing Launcher configuration backup file</source>
|
||||
<translation>Błąd zapisu kopii zapasowej pliku konfiguracyjnego programu uruchamiającego</translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Error opening Launcher configuration file</source>
|
||||
<translation>Wystąpił błąd podczas otwierania pliku konfiguracyjnego programu uruchamiającego</translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Error opening Launcher configuration backup file</source>
|
||||
<translation>Wystąpił błąd podczas otwierania kopii zapasowej pliku konfiguracyjnego programu uruchamiającego</translation>
|
||||
</message>
|
||||
</context>
|
||||
<context>
|
||||
<name>Launcher::SettingsPage</name>
|
||||
|
||||
@@ -569,6 +569,18 @@ to default Morrowind fonts. Check this box if you still prefer original fonts ov
|
||||
<source><br><b>Could not create directory %0</b><br><br>%1<br></source>
|
||||
<translation><br><b>Не удалось создать директорию %0</b><br><br>%1<br></translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Error writing Launcher configuration backup file</source>
|
||||
<translation>Не удалось сохранить данные в резервную копию файла с настройками лаунчера</translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Error opening Launcher configuration file</source>
|
||||
<translation>Не удалось открыть файл с настройками лаунчера</translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Error opening Launcher configuration backup file</source>
|
||||
<translation>Не удалось открыть резервную копию файла с настройками лаунчера</translation>
|
||||
</message>
|
||||
</context>
|
||||
<context>
|
||||
<name>Launcher::SettingsPage</name>
|
||||
|
||||
@@ -570,6 +570,18 @@ de ordinarie fonterna i Morrowind. Bocka denna ruta om du ändå föredrar ordin
|
||||
<source><br><b>Could not create directory %0</b><br><br>%1<br></source>
|
||||
<translation><br><b>Kunde inte skapa katalog %0</b><br><br>%1<br></translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Error writing Launcher configuration backup file</source>
|
||||
<translation>Fel när säkerhetskopia av Startarens-konfigurationsfil skulle skrivas</translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Error opening Launcher configuration file</source>
|
||||
<translation>Fel när Startarens-konfigurationsfil skulle öppnas</translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Error opening Launcher configuration backup file</source>
|
||||
<translation>Fel när säkerhetskopia av Startarens-konfigurationsfil skulle öppnas</translation>
|
||||
</message>
|
||||
</context>
|
||||
<context>
|
||||
<name>Launcher::SettingsPage</name>
|
||||
|
||||
Reference in New Issue
Block a user