-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.cpp
108 lines (90 loc) · 2.64 KB
/
config.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "config.h"
#include <QtCore>
#include <QFile>
#include <QFileInfo>
#include <QJsonDocument>
namespace koala { namespace chillin { namespace client {
Config *Config::ins = new Config();
Config::Config()
{
}
Config::~Config()
{
}
void Config::initialize(const QString &cfgPath)
{
ins->parseFile(cfgPath);
ins->parseArgs();
}
void Config::parseFile(const QString &cfgPath)
{
QString cfgAbsPath = cfgPath;
if (cfgPath.isNull() || cfgPath == "")
cfgAbsPath = "gamecfg.json";
if (!QFileInfo(cfgPath).isAbsolute())
cfgAbsPath = QCoreApplication::applicationDirPath() + "/" + cfgAbsPath;
if (!QFileInfo(cfgAbsPath).exists())
{
QFile::copy(":/config/gamecfg.json", cfgAbsPath);
QFile file(cfgAbsPath);
file.setPermissions(
QFile::ReadOwner | QFile::WriteOwner |
QFile::ReadGroup | QFile::WriteGroup |
QFile::ReadOther
);
}
QFile file(cfgAbsPath);
file.open(QIODevice::ReadOnly | QIODevice::Text);
QString jsonData = file.readAll();
file.close();
ins->config = QJsonDocument::fromJson(jsonData.toUtf8()).object();
}
void Config::parseArgs()
{
QRegExp regex("config\\.(.+)(\\.(.+))*\\=(.+)?");
for (auto arg : QCoreApplication::arguments())
if (regex.indexIn(arg) > -1)
{
auto key = regex.cap(1);
auto value = regex.cap(4);
if (key == "net.host")
{
QJsonObject tmp = ins->config["net"].toObject();
tmp.insert("host", value);
ins->config.insert("net", tmp);
}
else if (key == "net.port")
{
QJsonObject tmp = ins->config["net"].toObject();
tmp.insert("port", value.toInt());
ins->config.insert("net", tmp);
}
else if (key == "ai.agent_name")
{
QJsonObject tmp = ins->config["ai"].toObject();
tmp.insert("agent_name", value);
ins->config.insert("ai", tmp);
}
else if (key == "ai.team_nickname")
{
QJsonObject tmp = ins->config["ai"].toObject();
tmp.insert("team_nickname", value);
ins->config.insert("ai", tmp);
}
else if (key == "ai.token")
{
QJsonObject tmp = ins->config["ai"].toObject();
tmp.insert("token", value);
ins->config.insert("ai", tmp);
}
}
}
Config *Config::instance()
{
return ins;
}
const QJsonObject &Config::getConfig() const
{
return ins->config;
}
}}}