forked from MrBesen/lolautoaccept
173 lines
4.1 KiB
C++
173 lines
4.1 KiB
C++
#include "config.h"
|
|
|
|
#include "files.h"
|
|
#include <QFile>
|
|
#include <QJsonDocument>
|
|
#include <QJsonArray>
|
|
|
|
#include <Log.h>
|
|
|
|
#include "json.h"
|
|
|
|
#ifdef WIN32
|
|
#define CONFPATH "lolautoacceptor/"
|
|
#else
|
|
#define CONFPATH ".config/lolautoaccept/"
|
|
#endif
|
|
|
|
Config::StageConfig::StageConfig() : enabled(false) {}
|
|
|
|
Config::StageConfig::StageConfig(const QJsonObject& j) {
|
|
if(j["champ"].isString()) {
|
|
champs.push_back(getValue<std::string>(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()) {
|
|
champs.push_back(jchampstr.toStdString());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
enabled = getValue(j, "enabled", false);
|
|
}
|
|
|
|
Config::StageConfig::operator QJsonObject() const {
|
|
QJsonObject out;
|
|
|
|
QJsonArray jchamps;
|
|
for(const std::string& champ : champs) {
|
|
if(!champ.empty()) {
|
|
jchamps.push_back(QString::fromStdString(champ));
|
|
}
|
|
}
|
|
|
|
out.insert("champs", jchamps);
|
|
out.insert("enabled", enabled);
|
|
|
|
return out;
|
|
}
|
|
|
|
Config::PositionConfig::PositionConfig() {}
|
|
Config::PositionConfig::PositionConfig(const QJsonObject& j) {
|
|
ban = getValue<Config::StageConfig>(j, "ban");
|
|
pick = getValue<Config::StageConfig>(j, "pick");
|
|
position = toPosition(getValue<std::string>(j, "position"));
|
|
if((int) position < 0 || position > Position::UTILITY) {
|
|
Log::warn << "invalid config value \"position\"";
|
|
position = Position::MIDDLE;
|
|
}
|
|
}
|
|
Config::PositionConfig::operator QJsonObject() const {
|
|
QJsonObject out;
|
|
|
|
out["ban"] = (QJsonObject) ban;
|
|
out["pick"] = (QJsonObject) pick;
|
|
out["position"] = QString::fromStdString(toString(position));
|
|
|
|
return out;
|
|
}
|
|
|
|
Config::RootConfig::RootConfig() {}
|
|
|
|
Config::RootConfig::RootConfig(const QJsonObject& j) {
|
|
auto jposref = j["positions"];
|
|
if(jposref.isArray()) {
|
|
QJsonArray jpos = jposref.toArray();
|
|
|
|
positionConfigs.reserve(jpos.size());
|
|
for(QJsonValue val : jpos) {
|
|
if(val.isObject()) {
|
|
positionConfigs.push_back(std::make_shared<Config::PositionConfig>(val.toObject())); // implicit cast to PositionConfig
|
|
}
|
|
}
|
|
}
|
|
|
|
enabledAutoAccept = getValue(j, "enabledAutoAccept", true);
|
|
enabledSmiteWarn = getValue(j, "enabledSmiteWarn", true);
|
|
}
|
|
|
|
Config::RootConfig::operator QJsonObject() const {
|
|
QJsonObject out;
|
|
|
|
QJsonArray positionarr;
|
|
for(auto pos : positionConfigs) {
|
|
positionarr.push_back((QJsonObject) *pos.get());
|
|
}
|
|
|
|
out["positions"] = positionarr;
|
|
out.insert("enabledAutoAccept", enabledAutoAccept);
|
|
out.insert("enabledSmiteWarn", enabledSmiteWarn);
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
// add a new config
|
|
auto posconfig = std::make_shared<Config::PositionConfig>();
|
|
positionConfigs.push_back(posconfig);
|
|
posconfig->position = position;
|
|
return posconfig;
|
|
}
|
|
|
|
Config::Config() {
|
|
configFolderPath = getHome() + CONFPATH;
|
|
configFilePath = configFolderPath + "config.json";
|
|
mkdirs(configFolderPath);
|
|
}
|
|
|
|
Config::~Config() {}
|
|
|
|
bool Config::load() {
|
|
QFile conffile(QString::fromStdString(configFilePath));
|
|
if(!conffile.open(QIODevice::ReadOnly)) {
|
|
Log::error << "could not open configfile: " << configFilePath;
|
|
return false;
|
|
}
|
|
|
|
QJsonParseError err;
|
|
QJsonDocument doc = QJsonDocument::fromJson(conffile.readAll(), &err);
|
|
if(err.error != QJsonParseError::NoError) {
|
|
Log::error << "config parse error: " << err.errorString().toStdString() << " position: " << err.offset;
|
|
return false;
|
|
}
|
|
if(doc.isObject()) {
|
|
root = doc.object();
|
|
Log::info << "config loaded";
|
|
return true;
|
|
}
|
|
|
|
Log::error << "config is not a json object!";
|
|
return false;
|
|
}
|
|
|
|
void Config::save() {
|
|
Log::note << "Config::save()";
|
|
mkdirs(configFolderPath);
|
|
|
|
QFile conffile(QString::fromStdString(configFilePath));
|
|
if(!conffile.open(QIODevice::WriteOnly)) {
|
|
Log::error << "could not open configfile: " << configFilePath;
|
|
return;
|
|
}
|
|
|
|
QJsonDocument doc;
|
|
doc.setObject(root);
|
|
conffile.write(doc.toJson());
|
|
Log::info << "config saved";
|
|
}
|
|
|
|
Config::RootConfig& Config::getConfig() {
|
|
return root;
|
|
}
|