Merge branch 'launcher_typed_settings' into 'master'

Typed launcher settings

See merge request OpenMW/openmw!2650
7220-lua-add-a-general-purpose-lexical-parser
psi29a 2 years ago
commit aee7716c3a

@ -205,8 +205,7 @@ bool Launcher::DataFilesPage::loadSettings()
if (!currentProfile.isEmpty()) if (!currentProfile.isEmpty())
addProfile(currentProfile, true); addProfile(currentProfile, true);
QString language(mLauncherSettings.value(QLatin1String("Settings/language"))); const int index = mSelector->languageBox()->findText(mLauncherSettings.getLanguage());
int index = mSelector->languageBox()->findText(language);
if (index != -1) if (index != -1)
mSelector->languageBox()->setCurrentIndex(index); mSelector->languageBox()->setCurrentIndex(index);
@ -355,7 +354,7 @@ void Launcher::DataFilesPage::saveSettings(const QString& profile)
QString language(mSelector->languageBox()->currentText()); QString language(mSelector->languageBox()->currentText());
mLauncherSettings.setValue(QLatin1String("Settings/language"), language); mLauncherSettings.setLanguage(language);
if (language == QLatin1String("Polish")) if (language == QLatin1String("Polish"))
{ {

@ -1,10 +1,13 @@
#include "maindialog.hpp" #include "maindialog.hpp"
#include <components/debug/debuglog.hpp>
#include <components/files/conversion.hpp>
#include <components/files/qtconversion.hpp>
#include <components/misc/helpviewer.hpp> #include <components/misc/helpviewer.hpp>
#include <components/misc/utf8qtextstream.hpp>
#include <components/version/version.hpp> #include <components/version/version.hpp>
#include <QCloseEvent> #include <QCloseEvent>
#include <QDebug>
#include <QDir> #include <QDir>
#include <QMessageBox> #include <QMessageBox>
#include <QStringList> #include <QStringList>
@ -130,7 +133,7 @@ Launcher::FirstRunDialogResult Launcher::MainDialog::showFirstRunDialog()
} }
} }
if (mLauncherSettings.value(QString("General/firstrun"), QString("true")) == QLatin1String("true")) if (mLauncherSettings.isFirstRun())
{ {
QMessageBox msgBox; QMessageBox msgBox;
msgBox.setWindowTitle(tr("First run")); msgBox.setWindowTitle(tr("First run"));
@ -287,34 +290,30 @@ bool Launcher::MainDialog::setupLauncherSettings()
{ {
mLauncherSettings.clear(); mLauncherSettings.clear();
mLauncherSettings.setMultiValueEnabled(true); const QString path
= Files::pathToQString(mCfgMgr.getUserConfigPath() / Config::LauncherSettings::sLauncherConfigFileName);
QStringList paths; if (!QFile::exists(path))
paths.append(QString(Config::LauncherSettings::sLauncherConfigFileName)); return true;
paths.append(Files::pathToQString(mCfgMgr.getUserConfigPath() / Config::LauncherSettings::sLauncherConfigFileName));
Log(Debug::Verbose) << "Loading config file: " << path.toUtf8().constData();
for (const QString& path : paths)
{
qDebug() << "Loading config file:" << path.toUtf8().constData();
QFile file(path); QFile file(path);
if (file.exists())
{
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{ {
cfgError(tr("Error opening OpenMW configuration file"), cfgError(tr("Error opening OpenMW configuration file"),
tr("<br><b>Could not open %0 for reading</b><br><br> \ tr("<br><b>Could not open %0 for reading:</b><br><br>%1<br><br> \
Please make sure you have the right permissions \ Please make sure you have the right permissions \
and try again.<br>") and try again.<br>")
.arg(file.fileName())); .arg(file.fileName())
.arg(file.errorString()));
return false; return false;
} }
QTextStream stream(&file); QTextStream stream(&file);
Misc::ensureUtf8Encoding(stream); Misc::ensureUtf8Encoding(stream);
mLauncherSettings.readFile(stream); mLauncherSettings.readFile(stream);
}
file.close();
}
return true; return true;
} }
@ -327,7 +326,6 @@ bool Launcher::MainDialog::setupGameSettings()
auto loadFile = [&](const QString& path, bool (Config::GameSettings::*reader)(QTextStream&, bool), auto loadFile = [&](const QString& path, bool (Config::GameSettings::*reader)(QTextStream&, bool),
bool ignoreContent = false) -> std::optional<bool> { bool ignoreContent = false) -> std::optional<bool> {
qDebug() << "Loading config file:" << path.toUtf8().constData();
file.setFileName(path); file.setFileName(path);
if (file.exists()) if (file.exists())
{ {
@ -440,31 +438,20 @@ bool Launcher::MainDialog::setupGraphicsSettings()
void Launcher::MainDialog::loadSettings() void Launcher::MainDialog::loadSettings()
{ {
int width = mLauncherSettings.value(QString("General/MainWindow/width")).toInt(); const auto& mainWindow = mLauncherSettings.getMainWindow();
int height = mLauncherSettings.value(QString("General/MainWindow/height")).toInt(); resize(mainWindow.mWidth, mainWindow.mHeight);
move(mainWindow.mPosX, mainWindow.mPosY);
int posX = mLauncherSettings.value(QString("General/MainWindow/posx")).toInt();
int posY = mLauncherSettings.value(QString("General/MainWindow/posy")).toInt();
resize(width, height);
move(posX, posY);
} }
void Launcher::MainDialog::saveSettings() void Launcher::MainDialog::saveSettings()
{ {
QString width = QString::number(this->width()); mLauncherSettings.setMainWindow(Config::LauncherSettings::MainWindow{
QString height = QString::number(this->height()); .mWidth = width(),
.mHeight = height(),
mLauncherSettings.setValue(QString("General/MainWindow/width"), width); .mPosX = pos().x(),
mLauncherSettings.setValue(QString("General/MainWindow/height"), height); .mPosY = pos().y(),
});
QString posX = QString::number(this->pos().x()); mLauncherSettings.resetFirstRun();
QString posY = QString::number(this->pos().y());
mLauncherSettings.setValue(QString("General/MainWindow/posx"), posX);
mLauncherSettings.setValue(QString("General/MainWindow/posy"), posY);
mLauncherSettings.setValue(QString("General/firstrun"), QString("false"));
} }
bool Launcher::MainDialog::writeSettings() bool Launcher::MainDialog::writeSettings()

@ -393,7 +393,7 @@ void Wizard::MainWizard::writeSettings()
{ {
// Write the encoding and language settings // Write the encoding and language settings
QString language(field(QLatin1String("installation.language")).toString()); QString language(field(QLatin1String("installation.language")).toString());
mLauncherSettings.setValue(QLatin1String("Settings/language"), language); mLauncherSettings.setLanguage(language);
if (language == QLatin1String("Polish")) if (language == QLatin1String("Polish"))
{ {

@ -372,7 +372,6 @@ if (USE_QT)
add_component_qt_dir (config add_component_qt_dir (config
gamesettings gamesettings
launchersettings launchersettings
settingsbase
) )
add_component_qt_dir (process add_component_qt_dir (process

@ -12,6 +12,15 @@ const char Config::GameSettings::sArchiveKey[] = "fallback-archive";
const char Config::GameSettings::sContentKey[] = "content"; const char Config::GameSettings::sContentKey[] = "content";
const char Config::GameSettings::sDirectoryKey[] = "data"; const char Config::GameSettings::sDirectoryKey[] = "data";
namespace
{
QStringList reverse(QStringList values)
{
std::reverse(values.begin(), values.end());
return values;
}
}
Config::GameSettings::GameSettings(Files::ConfigurationManager& cfg) Config::GameSettings::GameSettings(Files::ConfigurationManager& cfg)
: mCfgMgr(cfg) : mCfgMgr(cfg)
{ {
@ -501,19 +510,19 @@ void Config::GameSettings::setContentList(
QStringList Config::GameSettings::getDataDirs() const QStringList Config::GameSettings::getDataDirs() const
{ {
return Config::LauncherSettings::reverse(mDataDirs); return reverse(mDataDirs);
} }
QStringList Config::GameSettings::getArchiveList() const QStringList Config::GameSettings::getArchiveList() const
{ {
// QMap returns multiple rows in LIFO order, so need to reverse // QMap returns multiple rows in LIFO order, so need to reverse
return Config::LauncherSettings::reverse(values(sArchiveKey)); return reverse(values(sArchiveKey));
} }
QStringList Config::GameSettings::getContentList() const QStringList Config::GameSettings::getContentList() const
{ {
// QMap returns multiple rows in LIFO order, so need to reverse // QMap returns multiple rows in LIFO order, so need to reverse
return Config::LauncherSettings::reverse(values(sContentKey)); return reverse(values(sContentKey));
} }
void Config::GameSettings::clear() void Config::GameSettings::clear()

@ -4,104 +4,220 @@
#include <QMultiMap> #include <QMultiMap>
#include <QRegularExpression> #include <QRegularExpression>
#include <QString> #include <QString>
#include <QStringList>
#include <QTextStream> #include <QTextStream>
#include <components/debug/debuglog.hpp>
#include <components/files/qtconversion.hpp> #include <components/files/qtconversion.hpp>
const char Config::LauncherSettings::sCurrentContentListKey[] = "Profiles/currentprofile"; #include "gamesettings.hpp"
const char Config::LauncherSettings::sLauncherConfigFileName[] = "launcher.cfg";
const char Config::LauncherSettings::sContentListsSectionPrefix[] = "Profiles/";
const char Config::LauncherSettings::sDirectoryListSuffix[] = "/data";
const char Config::LauncherSettings::sArchiveListSuffix[] = "/fallback-archive";
const char Config::LauncherSettings::sContentListSuffix[] = "/content";
QStringList Config::LauncherSettings::subKeys(const QString& key) namespace Config
{ {
QMultiMap<QString, QString> settings = SettingsBase::getSettings(); namespace
QStringList keys = settings.uniqueKeys(); {
constexpr char sSettingsSection[] = "Settings";
constexpr char sGeneralSection[] = "General";
constexpr char sProfilesSection[] = "Profiles";
constexpr char sLanguageKey[] = "language";
constexpr char sCurrentProfileKey[] = "currentprofile";
constexpr char sDataKey[] = "data";
constexpr char sArchiveKey[] = "fallback-archive";
constexpr char sContentKey[] = "content";
constexpr char sFirstRunKey[] = "firstrun";
constexpr char sMainWindowWidthKey[] = "MainWindow/width";
constexpr char sMainWindowHeightKey[] = "MainWindow/height";
constexpr char sMainWindowPosXKey[] = "MainWindow/posx";
constexpr char sMainWindowPosYKey[] = "MainWindow/posy";
QString makeNewContentListName()
{
// basically, use date and time as the name e.g. YYYY-MM-DDThh:mm:ss
const std::time_t rawtime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
tm timeinfo{};
#ifdef _WIN32
(void)localtime_s(&timeinfo, &rawtime);
#else
(void)localtime_r(&rawtime, &timeinfo);
#endif
constexpr int base = 10;
QChar zeroPad('0');
return QString("%1-%2-%3T%4:%5:%6")
.arg(timeinfo.tm_year + 1900, 4)
.arg(timeinfo.tm_mon + 1, 2, base, zeroPad)
.arg(timeinfo.tm_mday, 2, base, zeroPad)
.arg(timeinfo.tm_hour, 2, base, zeroPad)
.arg(timeinfo.tm_min, 2, base, zeroPad)
.arg(timeinfo.tm_sec, 2, base, zeroPad);
}
QRegularExpression keyRe("(.+)/"); bool parseBool(const QString& value, bool& out)
{
if (value == "false")
{
out = false;
return true;
}
if (value == "true")
{
out = true;
return true;
}
QStringList result; return false;
}
for (const QString& currentKey : keys) bool parseInt(const QString& value, int& out)
{ {
QRegularExpressionMatch match = keyRe.match(currentKey); bool ok = false;
if (match.hasMatch()) const int converted = value.toInt(&ok);
if (ok)
out = converted;
return ok;
}
bool parseProfilePart(
const QString& key, const QString& value, std::map<QString, LauncherSettings::Profile>& profiles)
{ {
QString prefixedKey = match.captured(1); const int separator = key.lastIndexOf('/');
if (separator == -1)
return false;
const QString profileName = key.mid(0, separator);
if (prefixedKey.startsWith(key)) if (key.endsWith(sArchiveKey))
{ {
QString subKey = prefixedKey.remove(key); profiles[profileName].mArchives.append(value);
if (!subKey.isEmpty()) return true;
result.append(subKey);
} }
if (key.endsWith(sDataKey))
{
profiles[profileName].mData.append(value);
return true;
} }
if (key.endsWith(sContentKey))
{
profiles[profileName].mContent.append(value);
return true;
} }
result.removeDuplicates(); return false;
return result;
} }
bool Config::LauncherSettings::writeFile(QTextStream& stream) bool parseSettingsSection(const QString& key, const QString& value, LauncherSettings::Settings& settings)
{
if (key == sLanguageKey)
{ {
QString sectionPrefix; settings.mLanguage = value;
QRegularExpression sectionRe("^([^/]+)/(.+)$"); return true;
QMultiMap<QString, QString> settings = SettingsBase::getSettings(); }
return false;
}
auto i = settings.end(); bool parseProfilesSection(const QString& key, const QString& value, LauncherSettings::Profiles& profiles)
while (i != settings.begin())
{ {
i--; if (key == sCurrentProfileKey)
{
profiles.mCurrentProfile = value;
return true;
}
QString prefix; return parseProfilePart(key, value, profiles.mValues);
QString key; }
QRegularExpressionMatch match = sectionRe.match(i.key()); bool parseGeneralSection(const QString& key, const QString& value, LauncherSettings::General& general)
if (match.hasMatch())
{ {
prefix = match.captured(1); if (key == sFirstRunKey)
key = match.captured(2); return parseBool(value, general.mFirstRun);
if (key == sMainWindowWidthKey)
return parseInt(value, general.mMainWindow.mWidth);
if (key == sMainWindowHeightKey)
return parseInt(value, general.mMainWindow.mHeight);
if (key == sMainWindowPosXKey)
return parseInt(value, general.mMainWindow.mPosX);
if (key == sMainWindowPosYKey)
return parseInt(value, general.mMainWindow.mPosY);
return false;
} }
// Get rid of legacy settings template <std::size_t size>
if (key.contains(QChar('\\'))) void writeSectionHeader(const char (&name)[size], QTextStream& stream)
continue; {
stream << "\n[" << name << "]\n";
}
if (key == QLatin1String("CurrentProfile")) template <std::size_t size>
continue; void writeKeyValue(const char (&key)[size], const QString& value, QTextStream& stream)
{
stream << key << '=' << value << '\n';
}
if (sectionPrefix != prefix) template <std::size_t size>
void writeKeyValue(const char (&key)[size], bool value, QTextStream& stream)
{ {
sectionPrefix = prefix; stream << key << '=' << (value ? "true" : "false") << '\n';
stream << "\n[" << prefix << "]\n";
} }
stream << key << "=" << i.value() << "\n"; template <std::size_t size>
void writeKeyValue(const char (&key)[size], int value, QTextStream& stream)
{
stream << key << '=' << value << '\n';
} }
return true; template <std::size_t size>
void writeKeyValues(
const QString& prefix, const char (&suffix)[size], const QStringList& values, QTextStream& stream)
{
for (const auto& v : values)
stream << prefix << '/' << suffix << '=' << v << '\n';
} }
QStringList Config::LauncherSettings::getContentLists() void writeSettings(const LauncherSettings::Settings& value, QTextStream& stream)
{ {
return subKeys(QString(sContentListsSectionPrefix)); writeSectionHeader(sSettingsSection, stream);
writeKeyValue(sLanguageKey, value.mLanguage, stream);
} }
QString Config::LauncherSettings::makeDirectoryListKey(const QString& contentListName) void writeProfiles(const LauncherSettings::Profiles& value, QTextStream& stream)
{
writeSectionHeader(sProfilesSection, stream);
writeKeyValue(sCurrentProfileKey, value.mCurrentProfile, stream);
for (auto it = value.mValues.rbegin(); it != value.mValues.rend(); ++it)
{ {
return QString(sContentListsSectionPrefix) + contentListName + QString(sDirectoryListSuffix); writeKeyValues(it->first, sArchiveKey, it->second.mArchives, stream);
writeKeyValues(it->first, sDataKey, it->second.mData, stream);
writeKeyValues(it->first, sContentKey, it->second.mContent, stream);
}
} }
QString Config::LauncherSettings::makeArchiveListKey(const QString& contentListName) void writeGeneral(const LauncherSettings::General& value, QTextStream& stream)
{ {
return QString(sContentListsSectionPrefix) + contentListName + QString(sArchiveListSuffix); writeSectionHeader(sGeneralSection, stream);
writeKeyValue(sFirstRunKey, value.mFirstRun, stream);
writeKeyValue(sMainWindowWidthKey, value.mMainWindow.mWidth, stream);
writeKeyValue(sMainWindowPosYKey, value.mMainWindow.mPosY, stream);
writeKeyValue(sMainWindowPosXKey, value.mMainWindow.mPosX, stream);
writeKeyValue(sMainWindowHeightKey, value.mMainWindow.mHeight, stream);
}
}
} }
QString Config::LauncherSettings::makeContentListKey(const QString& contentListName) void Config::LauncherSettings::writeFile(QTextStream& stream) const
{ {
return QString(sContentListsSectionPrefix) + contentListName + QString(sContentListSuffix); writeSettings(mSettings, stream);
writeProfiles(mProfiles, stream);
writeGeneral(mGeneral, stream);
}
QStringList Config::LauncherSettings::getContentLists()
{
QStringList result;
result.reserve(mProfiles.mValues.size());
for (const auto& [k, v] : mProfiles.mValues)
result.push_back(k);
return result;
} }
void Config::LauncherSettings::setContentList(const GameSettings& gameSettings) void Config::LauncherSettings::setContentList(const GameSettings& gameSettings)
@ -126,8 +242,8 @@ void Config::LauncherSettings::setContentList(const GameSettings& gameSettings)
// if any existing profile in launcher matches the content list, make that profile the default // if any existing profile in launcher matches the content list, make that profile the default
for (const QString& listName : getContentLists()) for (const QString& listName : getContentLists())
{ {
if (isEqual(files, getContentListFiles(listName)) && isEqual(archives, getArchiveList(listName)) if (files == getContentListFiles(listName) && archives == getArchiveList(listName)
&& isEqual(dirs, getDataDirectoryList(listName))) && dirs == getDataDirectoryList(listName))
{ {
setCurrentContentListName(listName); setCurrentContentListName(listName);
return; return;
@ -140,99 +256,98 @@ void Config::LauncherSettings::setContentList(const GameSettings& gameSettings)
setContentList(newContentListName, dirs, archives, files); setContentList(newContentListName, dirs, archives, files);
} }
void Config::LauncherSettings::removeContentList(const QString& contentListName)
{
remove(makeDirectoryListKey(contentListName));
remove(makeArchiveListKey(contentListName));
remove(makeContentListKey(contentListName));
}
void Config::LauncherSettings::setCurrentContentListName(const QString& contentListName)
{
remove(QString(sCurrentContentListKey));
setValue(QString(sCurrentContentListKey), contentListName);
}
void Config::LauncherSettings::setContentList(const QString& contentListName, const QStringList& dirNames, void Config::LauncherSettings::setContentList(const QString& contentListName, const QStringList& dirNames,
const QStringList& archiveNames, const QStringList& fileNames) const QStringList& archiveNames, const QStringList& fileNames)
{ {
auto const assign = [this](const QString key, const QStringList& list) { Profile& profile = mProfiles.mValues[contentListName];
for (auto const& item : list) profile.mData = dirNames;
setMultiValue(key, item); profile.mArchives = archiveNames;
}; profile.mContent = fileNames;
removeContentList(contentListName);
assign(makeDirectoryListKey(contentListName), dirNames);
assign(makeArchiveListKey(contentListName), archiveNames);
assign(makeContentListKey(contentListName), fileNames);
}
QString Config::LauncherSettings::getCurrentContentListName() const
{
return value(QString(sCurrentContentListKey));
} }
QStringList Config::LauncherSettings::getDataDirectoryList(const QString& contentListName) const QStringList Config::LauncherSettings::getDataDirectoryList(const QString& contentListName) const
{ {
// QMap returns multiple rows in LIFO order, so need to reverse const Profile* profile = findProfile(contentListName);
return reverse(getSettings().values(makeDirectoryListKey(contentListName))); if (profile == nullptr)
return {};
return profile->mData;
} }
QStringList Config::LauncherSettings::getArchiveList(const QString& contentListName) const QStringList Config::LauncherSettings::getArchiveList(const QString& contentListName) const
{ {
// QMap returns multiple rows in LIFO order, so need to reverse const Profile* profile = findProfile(contentListName);
return reverse(getSettings().values(makeArchiveListKey(contentListName))); if (profile == nullptr)
return {};
return profile->mArchives;
} }
QStringList Config::LauncherSettings::getContentListFiles(const QString& contentListName) const QStringList Config::LauncherSettings::getContentListFiles(const QString& contentListName) const
{ {
// QMap returns multiple rows in LIFO order, so need to reverse const Profile* profile = findProfile(contentListName);
return reverse(getSettings().values(makeContentListKey(contentListName))); if (profile == nullptr)
return {};
return profile->mContent;
} }
QStringList Config::LauncherSettings::reverse(const QStringList& toReverse) bool Config::LauncherSettings::setValue(const QString& sectionPrefix, const QString& key, const QString& value)
{ {
QStringList result; if (sectionPrefix == sSettingsSection)
result.reserve(toReverse.size()); return parseSettingsSection(key, value, mSettings);
std::reverse_copy(toReverse.begin(), toReverse.end(), std::back_inserter(result)); if (sectionPrefix == sProfilesSection)
return result; return parseProfilesSection(key, value, mProfiles);
} if (sectionPrefix == sGeneralSection)
return parseGeneralSection(key, value, mGeneral);
bool Config::LauncherSettings::isEqual(const QStringList& list1, const QStringList& list2)
{
if (list1.count() != list2.count())
{
return false; return false;
} }
for (int i = 0; i < list1.count(); ++i) void Config::LauncherSettings::readFile(QTextStream& stream)
{ {
if (list1.at(i) != list2.at(i)) const QRegExp sectionRe("^\\[([^]]+)\\]");
const QRegExp keyRe("^([^=]+)\\s*=\\s*(.+)$");
QString section;
while (!stream.atEnd())
{ {
return false; const QString line = stream.readLine();
if (line.isEmpty() || line.startsWith("#"))
continue;
if (sectionRe.exactMatch(line))
{
section = sectionRe.cap(1);
continue;
}
if (section.isEmpty())
continue;
if (keyRe.indexIn(line) == -1)
continue;
const QString key = keyRe.cap(1).trimmed();
const QString value = keyRe.cap(2).trimmed();
if (!setValue(section, key, value))
Log(Debug::Warning) << "Unsupported setting in the launcher config file: section: "
<< section.toUtf8().constData() << " key: " << key.toUtf8().constData()
<< " value: " << value.toUtf8().constData();
} }
} }
// if get here, lists are same const Config::LauncherSettings::Profile* Config::LauncherSettings::findProfile(const QString& name) const
return true; {
const auto it = mProfiles.mValues.find(name);
if (it == mProfiles.mValues.end())
return nullptr;
return &it->second;
} }
QString Config::LauncherSettings::makeNewContentListName() void Config::LauncherSettings::clear()
{ {
// basically, use date and time as the name e.g. YYYY-MM-DDThh:mm:ss mSettings = Settings{};
auto rawtime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); mGeneral = General{};
tm timeinfo{}; mProfiles = Profiles{};
#ifdef _WIN32
(void)localtime_s(&timeinfo, &rawtime);
#else
(void)localtime_r(&rawtime, &timeinfo);
#endif
int base = 10;
QChar zeroPad('0');
return QString("%1-%2-%3T%4:%5:%6")
.arg(timeinfo.tm_year + 1900, 4)
.arg(timeinfo.tm_mon + 1, 2, base, zeroPad)
.arg(timeinfo.tm_mday, 2, base, zeroPad)
.arg(timeinfo.tm_hour, 2, base, zeroPad)
.arg(timeinfo.tm_min, 2, base, zeroPad)
.arg(timeinfo.tm_sec, 2, base, zeroPad);
} }

@ -1,15 +1,59 @@
#ifndef LAUNCHERSETTINGS_HPP #ifndef LAUNCHERSETTINGS_HPP
#define LAUNCHERSETTINGS_HPP #define LAUNCHERSETTINGS_HPP
#include "gamesettings.hpp" #include <QString>
#include "settingsbase.hpp" #include <QStringList>
#include <map>
class QTextStream;
namespace Config namespace Config
{ {
class LauncherSettings : public SettingsBase<QMultiMap<QString, QString>> class GameSettings;
class LauncherSettings
{ {
public: public:
bool writeFile(QTextStream& stream); static constexpr char sLauncherConfigFileName[] = "launcher.cfg";
struct Settings
{
QString mLanguage;
};
struct MainWindow
{
int mWidth = 0;
int mHeight = 0;
int mPosX = 0;
int mPosY = 0;
};
struct General
{
bool mFirstRun = true;
MainWindow mMainWindow;
};
struct Profile
{
QStringList mArchives;
QStringList mData;
QStringList mContent;
};
struct Profiles
{
QString mCurrentProfile;
std::map<QString, Profile> mValues;
};
void readFile(QTextStream& stream);
void clear();
void writeFile(QTextStream& stream) const;
/// \return names of all Content Lists in the launcher's .cfg file. /// \return names of all Content Lists in the launcher's .cfg file.
QStringList getContentLists(); QStringList getContentLists();
@ -21,47 +65,36 @@ namespace Config
void setContentList(const QString& contentListName, const QStringList& dirNames, void setContentList(const QString& contentListName, const QStringList& dirNames,
const QStringList& archiveNames, const QStringList& fileNames); const QStringList& archiveNames, const QStringList& fileNames);
void removeContentList(const QString& contentListName); void removeContentList(const QString& value) { mProfiles.mValues.erase(value); }
void setCurrentContentListName(const QString& contentListName); void setCurrentContentListName(const QString& value) { mProfiles.mCurrentProfile = value; }
QString getCurrentContentListName() const; QString getCurrentContentListName() const { return mProfiles.mCurrentProfile; }
QStringList getDataDirectoryList(const QString& contentListName) const; QStringList getDataDirectoryList(const QString& contentListName) const;
QStringList getArchiveList(const QString& contentListName) const; QStringList getArchiveList(const QString& contentListName) const;
QStringList getContentListFiles(const QString& contentListName) const; QStringList getContentListFiles(const QString& contentListName) const;
/// \return new list that is reversed order of input bool isFirstRun() const { return mGeneral.mFirstRun; }
static QStringList reverse(const QStringList& toReverse);
static const char sLauncherConfigFileName[];
private: void resetFirstRun() { mGeneral.mFirstRun = false; }
/// \return key to use to get/set the files in the specified data Directory List
static QString makeDirectoryListKey(const QString& contentListName);
/// \return key to use to get/set the files in the specified Archive List
static QString makeArchiveListKey(const QString& contentListName);
/// \return key to use to get/set the files in the specified Content List QString getLanguage() const { return mSettings.mLanguage; }
static QString makeContentListKey(const QString& contentListName);
/// \return true if both lists are same void setLanguage(const QString& value) { mSettings.mLanguage = value; }
static bool isEqual(const QStringList& list1, const QStringList& list2);
static QString makeNewContentListName(); MainWindow getMainWindow() const { return mGeneral.mMainWindow; }
QStringList subKeys(const QString& key); void setMainWindow(const MainWindow& value) { mGeneral.mMainWindow = value; }
/// name of entry in launcher.cfg that holds name of currently selected Content List private:
static const char sCurrentContentListKey[]; Settings mSettings;
Profiles mProfiles;
General mGeneral;
/// section of launcher.cfg holding the Content Lists bool setValue(const QString& sectionPrefix, const QString& key, const QString& value);
static const char sContentListsSectionPrefix[];
static const char sDirectoryListSuffix[]; const Profile* findProfile(const QString& name) const;
static const char sArchiveListSuffix[];
static const char sContentListSuffix[];
}; };
} }
#endif // LAUNCHERSETTINGS_HPP #endif // LAUNCHERSETTINGS_HPP

@ -1,110 +0,0 @@
#ifndef SETTINGSBASE_HPP
#define SETTINGSBASE_HPP
#include <QRegularExpression>
#include <QString>
#include <QStringList>
#include <QTextStream>
namespace Config
{
template <class Map>
class SettingsBase
{
public:
SettingsBase() { mMultiValue = false; }
~SettingsBase() = default;
inline QString value(const QString& key, const QString& defaultValue = QString()) const
{
return mSettings.value(key).isEmpty() ? defaultValue : mSettings.value(key);
}
inline void setValue(const QString& key, const QString& value) { mSettings.replace(key, value); }
inline void setMultiValue(const QString& key, const QString& value)
{
QStringList values = mSettings.values(key);
if (!values.contains(value))
mSettings.insert(key, value);
}
inline void setMultiValueEnabled(bool enable) { mMultiValue = enable; }
inline void remove(const QString& key) { mSettings.remove(key); }
Map getSettings() const { return mSettings; }
bool readFile(QTextStream& stream)
{
Map cache;
QString sectionPrefix;
QRegularExpression sectionRe("^\\[([^]]+)\\]$");
QRegularExpression keyRe("^([^=]+)\\s*=\\s*(.+)$");
while (!stream.atEnd())
{
QString line = stream.readLine();
if (line.isEmpty() || line.startsWith("#"))
continue;
QRegularExpressionMatch sectionMatch = sectionRe.match(line);
if (sectionMatch.hasMatch())
{
sectionPrefix = sectionMatch.captured(1);
sectionPrefix.append("/");
continue;
}
QRegularExpressionMatch match = keyRe.match(line);
if (match.hasMatch())
{
QString key = match.captured(1).trimmed();
QString value = match.captured(2).trimmed();
if (!sectionPrefix.isEmpty())
key.prepend(sectionPrefix);
mSettings.remove(key);
QStringList values = cache.values(key);
if (!values.contains(value))
{
if (mMultiValue)
{
cache.insert(key, value);
}
else
{
cache.remove(key);
cache.insert(key, value);
}
}
}
}
if (mSettings.isEmpty())
{
mSettings = cache; // This is the first time we read a file
return true;
}
// Merge the changed keys with those which didn't
mSettings.unite(cache);
return true;
}
void clear() { mSettings.clear(); }
private:
Map mSettings;
bool mMultiValue;
};
}
#endif // SETTINGSBASE_HPP
Loading…
Cancel
Save