lolautoaccept/src/config.cpp

239 lines
5.8 KiB
C++
Raw Permalink Normal View History

2022-04-24 01:27:46 +02:00
#include "config.h"
#include "files.h"
#include <QFile>
#include <QJsonDocument>
#include <QJsonArray>
2022-04-24 01:27:46 +02:00
#include <Log.h>
#include "json.h"
2022-08-24 16:12:03 +02:00
#ifdef WIN32
#define CONFPATH "lolautoacceptor/"
#else
#define CONFPATH ".config/lolautoaccept/"
#endif
2022-04-24 01:27:46 +02:00
Config::StageConfig::StageConfig() : enabled(false) {}
Config::StageConfig::StageConfig(const QJsonObject& j) {
if(j["champ"].isString()) {
2023-05-31 22:22:23 +02:00
champs.push_back(getValue<QString>(j, "champ"));
}
if(j["champs"].isArray()) {
QJsonArray jchamps = j["champs"].toArray();
for(auto jchamp : jchamps) {
if(jchamp.isString()) {
QString jchampstr = jchamp.toString();
if(!jchampstr.isEmpty()) {
2023-05-31 22:22:23 +02:00
champs.push_back(jchampstr);
}
}
}
}
2022-04-24 01:27:46 +02:00
enabled = getValue(j, "enabled", false);
}
Config::StageConfig::operator QJsonObject() const {
QJsonObject out;
QJsonArray jchamps;
2023-05-31 22:22:23 +02:00
for(const QString& champ : champs) {
if(!champ.isEmpty()) {
jchamps.push_back(champ);
}
}
out.insert("champs", jchamps);
2022-04-24 01:27:46 +02:00
out.insert("enabled", enabled);
return out;
}
2022-07-04 00:30:00 +02:00
Config::PositionConfig::PositionConfig() {}
Config::PositionConfig::PositionConfig(const QJsonObject& j) {
ban = getValue<Config::StageConfig>(j, "ban");
pick = getValue<Config::StageConfig>(j, "pick");
2023-05-31 22:22:23 +02:00
position = toPosition(getValue<QString>(j, "position"));
2022-07-04 00:30:00 +02:00
if((int) position < 0 || position > Position::UTILITY) {
2023-05-31 22:22:23 +02:00
qWarning() << "invalid config value \"position\"";
2022-07-04 00:30:00 +02:00
position = Position::MIDDLE;
}
}
Config::PositionConfig::operator QJsonObject() const {
QJsonObject out;
out["ban"] = (QJsonObject) ban;
out["pick"] = (QJsonObject) pick;
2023-05-31 22:22:23 +02:00
out["position"] = toString(position);
2022-07-04 00:30:00 +02:00
return out;
}
2023-04-23 19:13:49 +02:00
Config::RunePageConfig::RunePageConfig() {}
Config::RunePageConfig::RunePageConfig(const QJsonObject& j) {
name = getValue<QString>(j, "name");
runepage = getValue<::RunePage>(j, "runepage");
}
Config::RunePageConfig::operator QJsonObject() const {
QJsonObject out;
out["name"] = name;
out["runepage"] = static_cast<QJsonObject>(runepage);
return out;
}
2022-04-24 01:27:46 +02:00
Config::RootConfig::RootConfig() {}
2023-04-23 23:54:09 +02:00
Config::RunePageConfig::RunePageConfig(QString name, const RunePage& rp) : name(name), runepage(rp) {}
2023-04-30 22:10:21 +02:00
Config::GeneralRunePageConfig::GeneralRunePageConfig() {}
Config::GeneralRunePageConfig::GeneralRunePageConfig(const QJsonObject& j) {
auto runepagesRef = j["pages"];
if(runepagesRef.isArray()) {
QJsonArray jpages = runepagesRef.toArray();
runePages.reserve(jpages.size());
for(QJsonValue val : jpages) {
if(val.isObject()) {
runePages.push_back(std::make_shared<RunePageConfig>(val.toObject()));
}
}
}
autoSync = getValue(j, "autosync", true);
}
Config::GeneralRunePageConfig::operator QJsonObject() const {
QJsonObject out;
QJsonArray runepagesArr;
for(std::shared_ptr<RunePageConfig> rp : runePages) {
runepagesArr.push_back((QJsonObject) *rp);
}
out.insert("pages", runepagesArr);
out.insert("autosync", autoSync);
return out;
}
2022-04-24 01:27:46 +02:00
Config::RootConfig::RootConfig(const QJsonObject& j) {
2023-04-23 19:13:49 +02:00
if(j.contains("version")) {
int version = j["version"].toInt();
if(version > 1) {
// error
qCritical() << "config version is not known: " << version << " using the config might corrupt it.";
// TODO: make backup of config or something
}
// add migrations here if required
}
2022-07-04 00:30:00 +02:00
auto jposref = j["positions"];
if(jposref.isArray()) {
QJsonArray jpos = jposref.toArray();
positionConfigs.reserve(jpos.size());
for(QJsonValue val : jpos) {
if(val.isObject()) {
2022-08-20 21:46:44 +02:00
positionConfigs.push_back(std::make_shared<Config::PositionConfig>(val.toObject())); // implicit cast to PositionConfig
2022-07-04 00:30:00 +02:00
}
}
}
2023-04-30 22:10:21 +02:00
runepagesConfig = getValue<GeneralRunePageConfig>(j, "runepages");
2023-04-23 23:54:09 +02:00
2022-07-04 00:30:00 +02:00
enabledAutoAccept = getValue(j, "enabledAutoAccept", true);
2022-08-26 13:47:21 +02:00
enabledSmiteWarn = getValue(j, "enabledSmiteWarn", true);
2022-09-21 13:47:08 +02:00
enabledAutoWrite = getValue(j, "enabledAutoWrite", false);
2023-04-23 23:54:09 +02:00
autoWriteText = getValue<QString>(j, "autoWriteText");
2022-04-24 01:27:46 +02:00
}
Config::RootConfig::operator QJsonObject() const {
QJsonObject out;
2022-07-04 00:30:00 +02:00
QJsonArray positionarr;
for(auto pos : positionConfigs) {
positionarr.push_back((QJsonObject) *pos.get());
2022-07-04 00:30:00 +02:00
}
out["positions"] = positionarr;
2023-04-30 22:10:21 +02:00
out["runepages"] = (QJsonObject) runepagesConfig;
2022-04-24 01:27:46 +02:00
out.insert("enabledAutoAccept", enabledAutoAccept);
2022-08-26 13:47:21 +02:00
out.insert("enabledSmiteWarn", enabledSmiteWarn);
2022-09-21 13:47:08 +02:00
out.insert("enabledAutoWrite", enabledAutoWrite);
2023-04-23 23:54:09 +02:00
out.insert("autoWriteText", autoWriteText);
2023-04-23 19:13:49 +02:00
out.insert("version", 1);
2022-04-24 01:27:46 +02:00
return out;
}
std::shared_ptr<Config::PositionConfig> Config::RootConfig::getPositionConfig(Position position) {
// find existing configuration with given position
for(auto posc : positionConfigs) {
if(posc->position == position) {
return posc;
2022-07-04 00:30:00 +02:00
}
}
// add a new config
auto posconfig = std::make_shared<Config::PositionConfig>();
positionConfigs.push_back(posconfig);
posconfig->position = position;
return posconfig;
2022-07-04 00:30:00 +02:00
}
2022-04-24 01:27:46 +02:00
Config::Config() {
2022-08-24 16:12:03 +02:00
configFolderPath = getHome() + CONFPATH;
2022-04-24 01:27:46 +02:00
configFilePath = configFolderPath + "config.json";
2022-08-24 16:12:03 +02:00
mkdirs(configFolderPath);
2022-04-24 01:27:46 +02:00
}
Config::~Config() {}
bool Config::load() {
2023-05-31 22:22:23 +02:00
QFile conffile(configFilePath);
2022-04-24 01:27:46 +02:00
if(!conffile.open(QIODevice::ReadOnly)) {
2023-04-30 22:10:21 +02:00
qCritical() << "could not open configfile: " << configFilePath;
2022-04-24 01:27:46 +02:00
return false;
}
QJsonParseError err;
QJsonDocument doc = QJsonDocument::fromJson(conffile.readAll(), &err);
if(err.error != QJsonParseError::NoError) {
2023-04-30 22:10:21 +02:00
qCritical() << "config parse error: " << err.errorString() << " position: " << err.offset;
2022-04-24 01:27:46 +02:00
return false;
}
if(doc.isObject()) {
2023-04-23 19:13:49 +02:00
// implicit cast
2022-04-24 01:27:46 +02:00
root = doc.object();
2023-04-23 19:13:49 +02:00
2023-04-30 22:10:21 +02:00
qInfo() << "config loaded";
2022-04-24 01:27:46 +02:00
return true;
}
2023-04-30 22:10:21 +02:00
qCritical() << "config is not a json object!";
2022-04-24 01:27:46 +02:00
return false;
}
void Config::save() {
mkdirs(configFolderPath);
2023-05-31 22:22:23 +02:00
QFile conffile(configFilePath);
2022-04-24 01:27:46 +02:00
if(!conffile.open(QIODevice::WriteOnly)) {
2023-05-31 22:22:23 +02:00
qCritical() << "could not open configfile: " << configFilePath;
2022-04-24 01:27:46 +02:00
return;
}
QJsonDocument doc;
doc.setObject(root);
conffile.write(doc.toJson());
2023-04-30 22:10:21 +02:00
qInfo() << "config saved";
2022-04-24 01:27:46 +02:00
}
Config::RootConfig& Config::getConfig() {
return root;
}