mirror of
https://github.com/OpenMW/openmw.git
synced 2025-01-16 15:29:55 +00:00
Added settings file reader/writers for openmw.cfg and settings.cfg
This commit is contained in:
parent
cac68c9e87
commit
6faf6f57e1
6 changed files with 206 additions and 0 deletions
|
@ -9,6 +9,9 @@ set(LAUNCHER
|
|||
model/modelitem.cpp
|
||||
model/esm/esmfile.cpp
|
||||
|
||||
settings/gamesettings.cpp
|
||||
settings/graphicssettings.cpp
|
||||
|
||||
utils/filedialog.cpp
|
||||
utils/naturalsort.cpp
|
||||
utils/lineedit.cpp
|
||||
|
@ -28,6 +31,10 @@ set(LAUNCHER_HEADER
|
|||
model/modelitem.hpp
|
||||
model/esm/esmfile.hpp
|
||||
|
||||
settings/gamesettings.hpp
|
||||
settings/graphicssettings.hpp
|
||||
settings/settingsbase.hpp
|
||||
|
||||
utils/lineedit.hpp
|
||||
utils/filedialog.hpp
|
||||
utils/naturalsort.hpp
|
||||
|
|
34
apps/launcher/settings/gamesettings.cpp
Normal file
34
apps/launcher/settings/gamesettings.cpp
Normal file
|
@ -0,0 +1,34 @@
|
|||
#include <QTextStream>
|
||||
#include <QString>
|
||||
#include <QRegExp>
|
||||
#include <QMap>
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
#include "gamesettings.hpp"
|
||||
|
||||
GameSettings::GameSettings()
|
||||
{
|
||||
}
|
||||
|
||||
GameSettings::~GameSettings()
|
||||
{
|
||||
}
|
||||
|
||||
bool GameSettings::writeFile(QTextStream &stream)
|
||||
{
|
||||
QMap<QString, QString> settings = SettingsBase::getSettings();
|
||||
|
||||
QMapIterator<QString, QString> i(settings);
|
||||
while (i.hasNext()) {
|
||||
i.next();
|
||||
|
||||
// Quote values with spaces
|
||||
if (i.value().contains(" ")) {
|
||||
stream << i.key() << "=\"" << i.value() << "\"\n";
|
||||
} else {
|
||||
stream << i.key() << "=" << i.value() << "\n";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
15
apps/launcher/settings/gamesettings.hpp
Normal file
15
apps/launcher/settings/gamesettings.hpp
Normal file
|
@ -0,0 +1,15 @@
|
|||
#ifndef GAMESETTINGS_HPP
|
||||
#define GAMESETTINGS_HPP
|
||||
|
||||
#include "settingsbase.hpp"
|
||||
|
||||
class GameSettings : public SettingsBase<QMultiMap<QString, QString>>
|
||||
{
|
||||
public:
|
||||
GameSettings();
|
||||
~GameSettings();
|
||||
|
||||
bool writeFile(QTextStream &stream);
|
||||
};
|
||||
|
||||
#endif // GAMESETTINGS_HPP
|
46
apps/launcher/settings/graphicssettings.cpp
Normal file
46
apps/launcher/settings/graphicssettings.cpp
Normal file
|
@ -0,0 +1,46 @@
|
|||
#include <QTextStream>
|
||||
#include <QString>
|
||||
#include <QRegExp>
|
||||
#include <QMap>
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
#include "graphicssettings.hpp"
|
||||
|
||||
GraphicsSettings::GraphicsSettings()
|
||||
{
|
||||
}
|
||||
|
||||
GraphicsSettings::~GraphicsSettings()
|
||||
{
|
||||
}
|
||||
|
||||
bool GraphicsSettings::writeFile(QTextStream &stream)
|
||||
{
|
||||
QString sectionPrefix;
|
||||
QRegExp sectionRe("([^/]+)/(.+)$");
|
||||
QMap<QString, QString> settings = SettingsBase::getSettings();
|
||||
|
||||
QMapIterator<QString, QString> i(settings);
|
||||
while (i.hasNext()) {
|
||||
i.next();
|
||||
|
||||
QString prefix;
|
||||
QString key;
|
||||
|
||||
if (sectionRe.exactMatch(i.key())) {
|
||||
prefix = sectionRe.cap(1);
|
||||
key = sectionRe.cap(2);
|
||||
}
|
||||
|
||||
if (sectionPrefix != prefix) {
|
||||
sectionPrefix = prefix;
|
||||
stream << "\n[" << prefix << "]\n";
|
||||
}
|
||||
|
||||
stream << key << " = " << i.value() << "\n";
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
16
apps/launcher/settings/graphicssettings.hpp
Normal file
16
apps/launcher/settings/graphicssettings.hpp
Normal file
|
@ -0,0 +1,16 @@
|
|||
#ifndef GRAPHICSSETTINGS_HPP
|
||||
#define GRAPHICSSETTINGS_HPP
|
||||
|
||||
#include "settingsbase.hpp"
|
||||
|
||||
class GraphicsSettings : public SettingsBase<QMap<QString, QString>>
|
||||
{
|
||||
public:
|
||||
GraphicsSettings();
|
||||
~GraphicsSettings();
|
||||
|
||||
bool writeFile(QTextStream &stream);
|
||||
|
||||
};
|
||||
|
||||
#endif // GRAPHICSSETTINGS_HPP
|
88
apps/launcher/settings/settingsbase.hpp
Normal file
88
apps/launcher/settings/settingsbase.hpp
Normal file
|
@ -0,0 +1,88 @@
|
|||
#ifndef SETTINGSBASE_HPP
|
||||
#define SETTINGSBASE_HPP
|
||||
|
||||
#include <QTextStream>
|
||||
#include <QString>
|
||||
#include <QRegExp>
|
||||
#include <QMap>
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
template <class Map>
|
||||
class SettingsBase
|
||||
{
|
||||
|
||||
public:
|
||||
SettingsBase() {}
|
||||
~SettingsBase() {}
|
||||
|
||||
inline QString value(const QString &key, const QString &defaultValue = QString())
|
||||
{
|
||||
return mSettings.value(key).isEmpty() ? defaultValue : mSettings.value(key);
|
||||
}
|
||||
|
||||
inline void setValue(const QString &key, const QString &value)
|
||||
{
|
||||
mSettings.insert(key, value);
|
||||
}
|
||||
|
||||
Map getSettings() {return mSettings;}
|
||||
|
||||
bool readFile(QTextStream &stream)
|
||||
{
|
||||
mCache.clear();
|
||||
|
||||
QString sectionPrefix;
|
||||
QRegExp sectionRe("^\\[([^]]+)\\]");
|
||||
QRegExp keyRe("^([^=]+)\\s*=\\s*(.+)$");
|
||||
|
||||
while (!stream.atEnd()) {
|
||||
QString line = stream.readLine().simplified();
|
||||
|
||||
if (line.isEmpty() || line.startsWith("#"))
|
||||
continue;
|
||||
|
||||
if (sectionRe.exactMatch(line)) {
|
||||
sectionPrefix = sectionRe.cap(1);
|
||||
sectionPrefix.append("/");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (keyRe.indexIn(line) != -1) {
|
||||
|
||||
QString key = keyRe.cap(1).simplified();
|
||||
QString value = keyRe.cap(2).simplified();
|
||||
|
||||
if (!sectionPrefix.isEmpty())
|
||||
key.prepend(sectionPrefix);
|
||||
|
||||
// QMap will replace the value if key exists, QMultiMap creates a new one
|
||||
mCache.insert(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
if (mSettings.isEmpty()) {
|
||||
mSettings = mCache; // This is the first time we read a file
|
||||
return true;
|
||||
}
|
||||
|
||||
// Replace values from previous settings
|
||||
QMapIterator<QString, QString> i(mCache);
|
||||
while (i.hasNext()) {
|
||||
i.next();
|
||||
if (mSettings.contains(i.key()))
|
||||
mSettings.remove(i.key());
|
||||
}
|
||||
|
||||
// Merge the changed keys with those which didn't
|
||||
mSettings.unite(mCache);
|
||||
qDebug() << mSettings;
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
Map mSettings;
|
||||
Map mCache;
|
||||
};
|
||||
|
||||
#endif // SETTINGSBASE_HPP
|
Loading…
Reference in a new issue