From 0ab0fcd06019ef947140c07026e369ea8e73f7d6 Mon Sep 17 00:00:00 2001 From: scatterflower Date: Sun, 23 Aug 2020 21:30:38 -0500 Subject: [PATCH] cleanup some things --- include/advertiser.h | 2 +- include/akashimain.h | 1 - include/config_manager.h | 16 ++++++++++++---- src/advertiser.cpp | 6 ++++-- src/akashimain.cpp | 20 ++++++++------------ src/aopacket.cpp | 4 ++-- src/config_manager.cpp | 30 ++++++++++++++++-------------- src/server.cpp | 5 +---- 8 files changed, 44 insertions(+), 40 deletions(-) diff --git a/include/advertiser.h b/include/advertiser.h index 8b18294..f479348 100644 --- a/include/advertiser.h +++ b/include/advertiser.h @@ -13,7 +13,7 @@ class Advertiser : public QObject { public: Advertiser(QString p_ip, int p_port, int p_ws_port, int p_local_port, - QString p_name, QString p_description); + QString p_name, QString p_description, QObject *parent = nullptr); void contactMasterServer(); signals: diff --git a/include/akashimain.h b/include/akashimain.h index 7b7aee3..749f690 100644 --- a/include/akashimain.h +++ b/include/akashimain.h @@ -22,7 +22,6 @@ public: AkashiMain(QWidget *parent = nullptr); ~AkashiMain(); - QSettings config; ConfigManager config_manager; void generateDefaultConfig(bool backup_old); diff --git a/include/config_manager.h b/include/config_manager.h index e391fea..3d9a9a2 100644 --- a/include/config_manager.h +++ b/include/config_manager.h @@ -10,14 +10,22 @@ class ConfigManager { public: - ConfigManager(QSettings *); + ConfigManager(); bool initConfig(); void generateDefaultConfig(bool backup_old); void updateConfig(int current_version); - bool loadServerSettings(QString *ms_ip, int *port, int *ws_port, - int *local_port, QString *name, QString *description, - bool *advertise_server); + struct server_settings { + QString ms_ip; + int port; + int ws_port; + int local_port; + QString name; + QString description; + bool advertise_server; + }; + + bool loadServerSettings(server_settings *settings); private: QSettings *config; diff --git a/src/advertiser.cpp b/src/advertiser.cpp index cf84cf6..05e1b8c 100644 --- a/src/advertiser.cpp +++ b/src/advertiser.cpp @@ -1,7 +1,9 @@ #include "include/advertiser.h" Advertiser::Advertiser(QString p_ip, int p_port, int p_ws_port, - int p_local_port, QString p_name, QString p_description) + int p_local_port, QString p_name, QString p_description, + QObject *parent) + : QObject(parent) { ip = p_ip; port = p_port; @@ -41,7 +43,7 @@ void Advertiser::socketConnected() concat_ports = QString::number(local_port) + "&" + QString::number(ws_port); AOPacket ao_packet("SCC", {concat_ports, name, description, - "akashi v" + QApplication::applicationVersion()}); + "akashi v" + QApplication::applicationVersion()}); QByteArray data = ao_packet.toUtf8(); socket->write(data); diff --git a/src/akashimain.cpp b/src/akashimain.cpp index ea784ae..10ef1e0 100644 --- a/src/akashimain.cpp +++ b/src/akashimain.cpp @@ -2,8 +2,7 @@ #include "ui_akashimain.h" AkashiMain::AkashiMain(QWidget *parent) - : QMainWindow(parent), config("config.ini", QSettings::IniFormat), - config_manager(&config), ui(new Ui::AkashiMain) + : QMainWindow(parent), config_manager(), ui(new Ui::AkashiMain) { ui->setupUi(this); qDebug("Main application started"); @@ -11,27 +10,24 @@ AkashiMain::AkashiMain(QWidget *parent) if (config_manager.initConfig()) { // Config is sound, so proceed with starting the server // Validate some of the config before passing it on - QString ms_ip, name, description; - int port, ws_port, local_port; - bool advertise_server; - bool config_valid = config_manager.loadServerSettings( - &ms_ip, &port, &ws_port, &local_port, &name, &description, - &advertise_server); + ConfigManager::server_settings settings; + bool config_valid = config_manager.loadServerSettings(&settings); if (!config_valid) { // TODO: send signal config invalid config_manager.generateDefaultConfig(true); } else { - if (advertise_server) { - advertiser = - new Advertiser(ms_ip, port, ws_port, local_port, name, description); + if (settings.advertise_server) { + advertiser = new Advertiser(settings.ms_ip, settings.port, + settings.ws_port, settings.local_port, + settings.name, settings.description, this); advertiser->contactMasterServer(); } // TODO: start the server here // TODO: send signal server starting. - server = new Server(port, ws_port); + server = new Server(settings.port, settings.ws_port); server->start(); } } diff --git a/src/aopacket.cpp b/src/aopacket.cpp index 30ade9e..357efa2 100644 --- a/src/aopacket.cpp +++ b/src/aopacket.cpp @@ -19,6 +19,6 @@ QString AOPacket::toString() QByteArray AOPacket::toUtf8() { - QString packet_string = toString(); - return packet_string.toUtf8(); + QString packet_string = toString(); + return packet_string.toUtf8(); } diff --git a/src/config_manager.cpp b/src/config_manager.cpp index 3b8484d..2709cf8 100644 --- a/src/config_manager.cpp +++ b/src/config_manager.cpp @@ -1,6 +1,9 @@ #include -ConfigManager::ConfigManager(QSettings *p_config) { config = p_config; } +ConfigManager::ConfigManager() +{ + config = new QSettings("config.ini", QSettings::IniFormat); +} // Validate and set up the config bool ConfigManager::initConfig() @@ -106,23 +109,22 @@ void ConfigManager::updateConfig(int current_version) } // Validate and retriever settings related to advertising and the server -bool ConfigManager::loadServerSettings(QString *ms_ip, int *port, int *ws_port, - int *local_port, QString *name, - QString *description, - bool *advertise_server) +bool ConfigManager::loadServerSettings(server_settings *settings) { bool port_conversion_success; bool ws_port_conversion_success; bool local_port_conversion_success; config->beginGroup("Options"); - *ms_ip = config->value("ms_ip", "master.aceattorneyonline.com").toString(); - *port = config->value("ms_port", "27016").toInt(&port_conversion_success); - *ws_port = + settings->ms_ip = + config->value("ms_ip", "master.aceattorneyonline.com").toString(); + settings->port = + config->value("ms_port", "27016").toInt(&port_conversion_success); + settings->ws_port = config->value("webao_port", "27017").toInt(&ws_port_conversion_success); - *local_port = + settings->local_port = config->value("port", "27016").toInt(&local_port_conversion_success); - *name = config->value("server_name", "My First Server").toString(); - *description = + settings->name = config->value("server_name", "My First Server").toString(); + settings->description = config->value("server_description", "This is my flashy new server") .toString(); config->endGroup(); @@ -132,12 +134,12 @@ bool ConfigManager::loadServerSettings(QString *ms_ip, int *port, int *ws_port, } else { if (config->value("advertise", "true").toString() != "true") - *advertise_server = false; + settings->advertise_server = false; else - *advertise_server = true; + settings->advertise_server = true; if (config->value("webao_enable", "true").toString() != "true") - *ws_port = -1; + settings->ws_port = -1; return true; } diff --git a/src/server.cpp b/src/server.cpp index 5f658b7..b5a3a6e 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -6,7 +6,4 @@ Server::Server(int p_port, int p_ws_port) ws_port = p_ws_port; } -void Server::start() -{ - -} +void Server::start() {}