Add project-specific configuration file. Only contains base_game_version right now.
This commit is contained in:
parent
525ef8c67c
commit
6b01d5a942
3 changed files with 132 additions and 11 deletions
|
@ -2,6 +2,7 @@
|
||||||
#define CONFIG_H
|
#define CONFIG_H
|
||||||
|
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
enum MapSortOrder {
|
enum MapSortOrder {
|
||||||
Group = 0,
|
Group = 0,
|
||||||
|
@ -9,23 +10,23 @@ enum MapSortOrder {
|
||||||
Layout = 2,
|
Layout = 2,
|
||||||
};
|
};
|
||||||
|
|
||||||
class KeyValueConfigBase
|
class KeyValueConfigBase : public QObject
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void save();
|
void save();
|
||||||
void load();
|
void load();
|
||||||
virtual ~KeyValueConfigBase();
|
virtual ~KeyValueConfigBase();
|
||||||
protected:
|
protected:
|
||||||
QString configFilename;
|
virtual QString getConfigFilepath() = 0;
|
||||||
virtual void parseConfigKeyValue(QString key, QString value) = 0;
|
virtual void parseConfigKeyValue(QString key, QString value) = 0;
|
||||||
virtual QMap<QString, QString> getKeyValueMap() = 0;
|
virtual QMap<QString, QString> getKeyValueMap() = 0;
|
||||||
|
virtual void onNewConfigFileCreated() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class PorymapConfig: public KeyValueConfigBase
|
class PorymapConfig: public KeyValueConfigBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
PorymapConfig() {
|
PorymapConfig() {
|
||||||
this->configFilename = "porymap.cfg";
|
|
||||||
this->recentProject = "";
|
this->recentProject = "";
|
||||||
this->recentMap = "";
|
this->recentMap = "";
|
||||||
this->mapSortOrder = MapSortOrder::Group;
|
this->mapSortOrder = MapSortOrder::Group;
|
||||||
|
@ -40,8 +41,10 @@ public:
|
||||||
MapSortOrder getMapSortOrder();
|
MapSortOrder getMapSortOrder();
|
||||||
bool getPrettyCursors();
|
bool getPrettyCursors();
|
||||||
protected:
|
protected:
|
||||||
|
QString getConfigFilepath();
|
||||||
void parseConfigKeyValue(QString key, QString value);
|
void parseConfigKeyValue(QString key, QString value);
|
||||||
QMap<QString, QString> getKeyValueMap();
|
QMap<QString, QString> getKeyValueMap();
|
||||||
|
void onNewConfigFileCreated() {}
|
||||||
private:
|
private:
|
||||||
QString recentProject;
|
QString recentProject;
|
||||||
QString recentMap;
|
QString recentMap;
|
||||||
|
@ -51,4 +54,30 @@ private:
|
||||||
|
|
||||||
extern PorymapConfig porymapConfig;
|
extern PorymapConfig porymapConfig;
|
||||||
|
|
||||||
|
enum BaseGameVersion {
|
||||||
|
pokeruby,
|
||||||
|
pokeemerald,
|
||||||
|
};
|
||||||
|
|
||||||
|
class ProjectConfig: public KeyValueConfigBase
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ProjectConfig() {
|
||||||
|
this->baseGameVersion = BaseGameVersion::pokeemerald;
|
||||||
|
}
|
||||||
|
void setBaseGameVersion(BaseGameVersion baseGameVersion);
|
||||||
|
BaseGameVersion getBaseGameVersion();
|
||||||
|
void setProjectDir(QString projectDir);
|
||||||
|
protected:
|
||||||
|
QString getConfigFilepath();
|
||||||
|
void parseConfigKeyValue(QString key, QString value);
|
||||||
|
QMap<QString, QString> getKeyValueMap();
|
||||||
|
void onNewConfigFileCreated();
|
||||||
|
private:
|
||||||
|
BaseGameVersion baseGameVersion;
|
||||||
|
QString projectDir;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern ProjectConfig projectConfig;
|
||||||
|
|
||||||
#endif // CONFIG_H
|
#endif // CONFIG_H
|
||||||
|
|
105
src/config.cpp
105
src/config.cpp
|
@ -2,7 +2,12 @@
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
|
#include <QFormLayout>
|
||||||
|
#include <QDialog>
|
||||||
|
#include <QDialogButtonBox>
|
||||||
#include <QList>
|
#include <QList>
|
||||||
|
#include <QComboBox>
|
||||||
|
#include <QLabel>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
|
|
||||||
|
@ -11,17 +16,19 @@ KeyValueConfigBase::~KeyValueConfigBase() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void KeyValueConfigBase::load() {
|
void KeyValueConfigBase::load() {
|
||||||
QFile file(this->configFilename);
|
QFile file(this->getConfigFilepath());
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
if (!file.open(QIODevice::WriteOnly)) {
|
if (!file.open(QIODevice::WriteOnly)) {
|
||||||
logError(QString("Could not create config file '%1'").arg(this->configFilename));
|
logError(QString("Could not create config file '%1'").arg(this->getConfigFilepath()));
|
||||||
} else {
|
} else {
|
||||||
file.close();
|
file.close();
|
||||||
|
this->onNewConfigFileCreated();
|
||||||
|
this->save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!file.open(QIODevice::ReadOnly)) {
|
if (!file.open(QIODevice::ReadOnly)) {
|
||||||
logError(QString("Could not open config file '%1': ").arg(this->configFilename) + file.errorString());
|
logError(QString("Could not open config file '%1': ").arg(this->getConfigFilepath()) + file.errorString());
|
||||||
}
|
}
|
||||||
|
|
||||||
QTextStream in(&file);
|
QTextStream in(&file);
|
||||||
|
@ -40,7 +47,7 @@ void KeyValueConfigBase::load() {
|
||||||
|
|
||||||
QRegularExpressionMatch match = re.match(line);
|
QRegularExpressionMatch match = re.match(line);
|
||||||
if (!match.hasMatch()) {
|
if (!match.hasMatch()) {
|
||||||
logWarn(QString("Invalid config line in %1: '%2'").arg(this->configFilename).arg(line));
|
logWarn(QString("Invalid config line in %1: '%2'").arg(this->getConfigFilepath()).arg(line));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,12 +64,12 @@ void KeyValueConfigBase::save() {
|
||||||
text += QString("%1=%2\n").arg(it.key()).arg(it.value());
|
text += QString("%1=%2\n").arg(it.key()).arg(it.value());
|
||||||
}
|
}
|
||||||
|
|
||||||
QFile file(this->configFilename);
|
QFile file(this->getConfigFilepath());
|
||||||
if (file.open(QIODevice::WriteOnly)) {
|
if (file.open(QIODevice::WriteOnly)) {
|
||||||
file.write(text.toUtf8());
|
file.write(text.toUtf8());
|
||||||
file.close();
|
file.close();
|
||||||
} else {
|
} else {
|
||||||
logError(QString("Could not open config file '%1' for writing: ").arg(this->configFilename) + file.errorString());
|
logError(QString("Could not open config file '%1' for writing: ").arg(this->getConfigFilepath()) + file.errorString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,6 +87,11 @@ const QMap<QString, MapSortOrder> mapSortOrderReverseMap = {
|
||||||
|
|
||||||
PorymapConfig porymapConfig;
|
PorymapConfig porymapConfig;
|
||||||
|
|
||||||
|
QString PorymapConfig::getConfigFilepath() {
|
||||||
|
// porymap config file is in the same directory as porymap itself.
|
||||||
|
return "porymap.cfg";
|
||||||
|
}
|
||||||
|
|
||||||
void PorymapConfig::parseConfigKeyValue(QString key, QString value) {
|
void PorymapConfig::parseConfigKeyValue(QString key, QString value) {
|
||||||
if (key == "recent_project") {
|
if (key == "recent_project") {
|
||||||
this->recentProject = value;
|
this->recentProject = value;
|
||||||
|
@ -97,10 +109,10 @@ void PorymapConfig::parseConfigKeyValue(QString key, QString value) {
|
||||||
this->mapSortOrder = mapSortOrderReverseMap.value(sortOrder);
|
this->mapSortOrder = mapSortOrderReverseMap.value(sortOrder);
|
||||||
} else {
|
} else {
|
||||||
this->mapSortOrder = MapSortOrder::Group;
|
this->mapSortOrder = MapSortOrder::Group;
|
||||||
logWarn(QString("Invalid config value for map_sort_order: '%1'. Must be 'group', 'area', or 'layout'."));
|
logWarn(QString("Invalid config value for map_sort_order: '%1'. Must be 'group', 'area', or 'layout'.").arg(value));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logWarn(QString("Invalid config key found in config file %1: '%2'").arg(this->configFilename).arg(key));
|
logWarn(QString("Invalid config key found in config file %1: '%2'").arg(this->getConfigFilepath()).arg(key));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,3 +160,80 @@ MapSortOrder PorymapConfig::getMapSortOrder() {
|
||||||
bool PorymapConfig::getPrettyCursors() {
|
bool PorymapConfig::getPrettyCursors() {
|
||||||
return this->prettyCursors;
|
return this->prettyCursors;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const QMap<BaseGameVersion, QString> baseGameVersionMap = {
|
||||||
|
{BaseGameVersion::pokeruby, "pokeruby"},
|
||||||
|
{BaseGameVersion::pokeemerald, "pokeemerald"},
|
||||||
|
};
|
||||||
|
|
||||||
|
const QMap<QString, BaseGameVersion> baseGameVersionReverseMap = {
|
||||||
|
{"pokeruby", BaseGameVersion::pokeruby},
|
||||||
|
{"pokeemerald", BaseGameVersion::pokeemerald},
|
||||||
|
};
|
||||||
|
|
||||||
|
ProjectConfig projectConfig;
|
||||||
|
|
||||||
|
QString ProjectConfig::getConfigFilepath() {
|
||||||
|
// porymap config file is in the same directory as porymap itself.
|
||||||
|
return QDir(this->projectDir).filePath("porymap.project.cfg");;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProjectConfig::parseConfigKeyValue(QString key, QString value) {
|
||||||
|
if (key == "base_game_version") {
|
||||||
|
QString baseGameVersion = value.toLower();
|
||||||
|
if (baseGameVersionReverseMap.contains(baseGameVersion)) {
|
||||||
|
this->baseGameVersion = baseGameVersionReverseMap.value(baseGameVersion);
|
||||||
|
} else {
|
||||||
|
this->baseGameVersion = BaseGameVersion::pokeemerald;
|
||||||
|
logWarn(QString("Invalid config value for base_game_version: '%1'. Must be 'pokeruby' or 'pokeemerald'.").arg(value));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
logWarn(QString("Invalid config key found in config file %1: '%2'").arg(this->getConfigFilepath()).arg(key));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QMap<QString, QString> ProjectConfig::getKeyValueMap() {
|
||||||
|
QMap<QString, QString> map;
|
||||||
|
map.insert("base_game_version", baseGameVersionMap.value(this->baseGameVersion));
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProjectConfig::onNewConfigFileCreated() {
|
||||||
|
QString dirName = QDir(this->projectDir).dirName().toLower();
|
||||||
|
if (baseGameVersionReverseMap.contains(dirName)) {
|
||||||
|
this->baseGameVersion = baseGameVersionReverseMap.value(dirName);
|
||||||
|
logInfo(QString("Auto-detected base_game_version as '%1'").arg(dirName));
|
||||||
|
} else {
|
||||||
|
QDialog dialog(nullptr, Qt::WindowTitleHint);
|
||||||
|
dialog.setWindowTitle("Project Configuration");
|
||||||
|
dialog.setWindowModality(Qt::NonModal);
|
||||||
|
|
||||||
|
QFormLayout form(&dialog);
|
||||||
|
|
||||||
|
QComboBox *baseGameVersionComboBox = new QComboBox();
|
||||||
|
baseGameVersionComboBox->addItem("pokeruby", BaseGameVersion::pokeruby);
|
||||||
|
baseGameVersionComboBox->addItem("pokeemerald", BaseGameVersion::pokeemerald);
|
||||||
|
form.addRow(new QLabel("Game Version"), baseGameVersionComboBox);
|
||||||
|
|
||||||
|
QDialogButtonBox buttonBox(QDialogButtonBox::Ok, Qt::Horizontal, &dialog);
|
||||||
|
connect(&buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
|
||||||
|
form.addRow(&buttonBox);
|
||||||
|
|
||||||
|
if (dialog.exec() == QDialog::Accepted) {
|
||||||
|
this->baseGameVersion = static_cast<BaseGameVersion>(baseGameVersionComboBox->currentData().toInt());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProjectConfig::setProjectDir(QString projectDir) {
|
||||||
|
this->projectDir = projectDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProjectConfig::setBaseGameVersion(BaseGameVersion baseGameVersion) {
|
||||||
|
this->baseGameVersion = baseGameVersion;
|
||||||
|
this->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
BaseGameVersion ProjectConfig::getBaseGameVersion() {
|
||||||
|
return this->baseGameVersion;
|
||||||
|
}
|
||||||
|
|
|
@ -196,6 +196,9 @@ bool MainWindow::openProject(QString dir) {
|
||||||
this->statusBar()->showMessage(QString("Opening project %1").arg(dir));
|
this->statusBar()->showMessage(QString("Opening project %1").arg(dir));
|
||||||
|
|
||||||
bool success = true;
|
bool success = true;
|
||||||
|
projectConfig.setProjectDir(dir);
|
||||||
|
projectConfig.load();
|
||||||
|
|
||||||
bool already_open = isProjectOpen() && (editor->project->root == dir);
|
bool already_open = isProjectOpen() && (editor->project->root == dir);
|
||||||
if (!already_open) {
|
if (!already_open) {
|
||||||
editor->project = new Project;
|
editor->project = new Project;
|
||||||
|
|
Loading…
Reference in a new issue