forked from MrBesen/lolautoaccept
239 lines
5.8 KiB
C++
239 lines
5.8 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<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()) {
|
|
champs.push_back(jchampstr);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
enabled = getValue(j, "enabled", false);
|
|
}
|
|
|
|
Config::StageConfig::operator QJsonObject() const {
|
|
QJsonObject out;
|
|
|
|
QJsonArray jchamps;
|
|
for(const QString& champ : champs) {
|
|
if(!champ.isEmpty()) {
|
|
jchamps.push_back(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<QString>(j, "position"));
|
|
if((int) position < 0 || position > Position::UTILITY) {
|
|
qWarning() << "invalid config value \"position\"";
|
|
position = Position::MIDDLE;
|
|
}
|
|
}
|
|
Config::PositionConfig::operator QJsonObject() const {
|
|
QJsonObject out;
|
|
|
|
out["ban"] = (QJsonObject) ban;
|
|
out["pick"] = (QJsonObject) pick;
|
|
out["position"] = toString(position);
|
|
|
|
return out;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
Config::RootConfig::RootConfig() {}
|
|
|
|
Config::RunePageConfig::RunePageConfig(QString name, const RunePage& rp) : name(name), runepage(rp) {}
|
|
|
|
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;
|
|
}
|
|
|
|
Config::RootConfig::RootConfig(const QJsonObject& j) {
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
}
|
|
|
|
runepagesConfig = getValue<GeneralRunePageConfig>(j, "runepages");
|
|
|
|
enabledAutoAccept = getValue(j, "enabledAutoAccept", true);
|
|
enabledSmiteWarn = getValue(j, "enabledSmiteWarn", true);
|
|
enabledAutoWrite = getValue(j, "enabledAutoWrite", false);
|
|
autoWriteText = getValue<QString>(j, "autoWriteText");
|
|
}
|
|
|
|
Config::RootConfig::operator QJsonObject() const {
|
|
QJsonObject out;
|
|
|
|
QJsonArray positionarr;
|
|
for(auto pos : positionConfigs) {
|
|
positionarr.push_back((QJsonObject) *pos.get());
|
|
}
|
|
|
|
out["positions"] = positionarr;
|
|
out["runepages"] = (QJsonObject) runepagesConfig;
|
|
out.insert("enabledAutoAccept", enabledAutoAccept);
|
|
out.insert("enabledSmiteWarn", enabledSmiteWarn);
|
|
out.insert("enabledAutoWrite", enabledAutoWrite);
|
|
out.insert("autoWriteText", autoWriteText);
|
|
out.insert("version", 1);
|
|
|
|
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(configFilePath);
|
|
if(!conffile.open(QIODevice::ReadOnly)) {
|
|
qCritical() << "could not open configfile: " << configFilePath;
|
|
return false;
|
|
}
|
|
|
|
QJsonParseError err;
|
|
QJsonDocument doc = QJsonDocument::fromJson(conffile.readAll(), &err);
|
|
if(err.error != QJsonParseError::NoError) {
|
|
qCritical() << "config parse error: " << err.errorString() << " position: " << err.offset;
|
|
return false;
|
|
}
|
|
if(doc.isObject()) {
|
|
// implicit cast
|
|
root = doc.object();
|
|
|
|
qInfo() << "config loaded";
|
|
return true;
|
|
}
|
|
|
|
qCritical() << "config is not a json object!";
|
|
return false;
|
|
}
|
|
|
|
void Config::save() {
|
|
mkdirs(configFolderPath);
|
|
|
|
QFile conffile(configFilePath);
|
|
if(!conffile.open(QIODevice::WriteOnly)) {
|
|
qCritical() << "could not open configfile: " << configFilePath;
|
|
return;
|
|
}
|
|
|
|
QJsonDocument doc;
|
|
doc.setObject(root);
|
|
conffile.write(doc.toJson());
|
|
qInfo() << "config saved";
|
|
}
|
|
|
|
Config::RootConfig& Config::getConfig() {
|
|
return root;
|
|
}
|