clang format

This commit is contained in:
scatterflower 2020-08-23 14:45:11 -05:00
parent a3d2c1aa66
commit 28ed456386
12 changed files with 283 additions and 258 deletions

2
.clang-format Normal file
View File

@ -0,0 +1,2 @@
BasedOnStyle: LLVM
BreakBeforeBraces: Stroustrup

View File

@ -3,16 +3,17 @@
#include <include/packet_manager.h> #include <include/packet_manager.h>
#include <QString>
#include <QTcpSocket>
#include <QApplication> #include <QApplication>
#include <QHostAddress> #include <QHostAddress>
#include <QString>
#include <QTcpSocket>
class Advertiser : public QObject { class Advertiser : public QObject {
Q_OBJECT Q_OBJECT
public: public:
Advertiser(QString p_ip, int p_port, int p_ws_port, int p_local_port, QString p_name, QString p_description); Advertiser(QString p_ip, int p_port, int p_ws_port, int p_local_port,
QString p_name, QString p_description);
void contactMasterServer(); void contactMasterServer();
signals: signals:

View File

@ -5,16 +5,17 @@
#include <include/config_manager.h> #include <include/config_manager.h>
#include <include/server.h> #include <include/server.h>
#include <QDebug>
#include <QMainWindow> #include <QMainWindow>
#include <QSettings> #include <QSettings>
#include <QDebug>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui { class AkashiMain; } namespace Ui {
class AkashiMain;
}
QT_END_NAMESPACE QT_END_NAMESPACE
class AkashiMain : public QMainWindow class AkashiMain : public QMainWindow {
{
Q_OBJECT Q_OBJECT
public: public:
@ -26,6 +27,7 @@ public:
void generateDefaultConfig(bool backup_old); void generateDefaultConfig(bool backup_old);
void updateConfig(int current_version); void updateConfig(int current_version);
private: private:
Ui::AkashiMain *ui; Ui::AkashiMain *ui;
Advertiser *advertiser; Advertiser *advertiser;

View File

@ -3,22 +3,24 @@
#define CONFIG_VERSION 1 #define CONFIG_VERSION 1
#include <QSettings>
#include <QFileInfo>
#include <QDir>
#include <QDebug> #include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QSettings>
class ConfigManager{ class ConfigManager {
public: public:
ConfigManager(QSettings*); ConfigManager(QSettings *);
bool initConfig(); bool initConfig();
void generateDefaultConfig(bool backup_old); void generateDefaultConfig(bool backup_old);
void updateConfig(int current_version); 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); bool loadServerSettings(QString *ms_ip, int *port, int *ws_port,
int *local_port, QString *name, QString *description,
bool *advertise_server);
private: private:
QSettings* config; QSettings *config;
}; };
#endif // CONFIG_MANAGER_H #endif // CONFIG_MANAGER_H

View File

@ -4,7 +4,7 @@
#include <QString> #include <QString>
#include <QStringList> #include <QStringList>
class PacketManager{ class PacketManager {
public: public:
static QString buildPacket(QString header, QStringList contents); static QString buildPacket(QString header, QStringList contents);
}; };

View File

@ -1,11 +1,10 @@
#ifndef SERVER_H #ifndef SERVER_H
#define SERVER_H #define SERVER_H
#include <QTcpServer>
#include <QString> #include <QString>
#include <QTcpServer>
class Server : public QObject class Server : public QObject {
{
Q_OBJECT Q_OBJECT
public: public:

View File

@ -1,6 +1,7 @@
#include "include/advertiser.h" #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) Advertiser::Advertiser(QString p_ip, int p_port, int p_ws_port,
int p_local_port, QString p_name, QString p_description)
{ {
ip = p_ip; ip = p_ip;
port = p_port; port = p_port;
@ -10,7 +11,8 @@ Advertiser::Advertiser(QString p_ip, int p_port, int p_ws_port, int p_local_port
description = p_description; description = p_description;
} }
void Advertiser::contactMasterServer() { void Advertiser::contactMasterServer()
{
socket = new QTcpSocket(this); socket = new QTcpSocket(this);
connect(socket, SIGNAL(readyRead()), this, SLOT(readData())); connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
connect(socket, SIGNAL(connected()), this, SLOT(socketConnected())); connect(socket, SIGNAL(connected()), this, SLOT(socketConnected()));
@ -20,30 +22,35 @@ void Advertiser::contactMasterServer() {
socket->connectToHost(ip, port); socket->connectToHost(ip, port);
} }
void Advertiser::readData() { void Advertiser::readData()
{
// The information coming back from the MS isn't very useful // The information coming back from the MS isn't very useful
// However, it can be useful to see it when debugging // However, it can be useful to see it when debugging
// TODO: master network debug switch // TODO: master network debug switch
// qDebug() << socket->readAll(); // qDebug() << socket->readAll();
} }
void Advertiser::socketConnected() { void Advertiser::socketConnected()
{
// TODO: fire a signal here, i18n // TODO: fire a signal here, i18n
qDebug("Connected to the master server"); qDebug("Connected to the master server");
QString concat_ports; QString concat_ports;
if(ws_port == -1) if (ws_port == -1)
concat_ports = QString::number(local_port); concat_ports = QString::number(local_port);
else else
concat_ports = QString::number(local_port) + "&" + QString::number(ws_port); concat_ports = QString::number(local_port) + "&" + QString::number(ws_port);
QString ao_packet = PacketManager::buildPacket("SCC", {concat_ports, name, description, "akashi v" + QApplication::applicationVersion()}); QString ao_packet = PacketManager::buildPacket(
"SCC", {concat_ports, name, description,
"akashi v" + QApplication::applicationVersion()});
QByteArray data = ao_packet.toUtf8(); QByteArray data = ao_packet.toUtf8();
socket->write(data); socket->write(data);
socket->flush(); socket->flush();
} }
void Advertiser::socketDisconnected() { void Advertiser::socketDisconnected()
{
// TODO: fire a signal here, i18n // TODO: fire a signal here, i18n
qDebug("Connection to master server lost"); qDebug("Connection to master server lost");
} }

View File

@ -2,28 +2,30 @@
#include "ui_akashimain.h" #include "ui_akashimain.h"
AkashiMain::AkashiMain(QWidget *parent) AkashiMain::AkashiMain(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent), config("config.ini", QSettings::IniFormat),
, config("config.ini", QSettings::IniFormat) config_manager(&config), ui(new Ui::AkashiMain)
, config_manager(&config)
, ui(new Ui::AkashiMain)
{ {
ui->setupUi(this); ui->setupUi(this);
qDebug("Main application started"); qDebug("Main application started");
if(config_manager.initConfig()) { if (config_manager.initConfig()) {
// Config is sound, so proceed with starting the server // Config is sound, so proceed with starting the server
// Validate some of the config before passing it on // Validate some of the config before passing it on
QString ms_ip, name, description; QString ms_ip, name, description;
int port, ws_port, local_port; int port, ws_port, local_port;
bool advertise_server; bool advertise_server;
bool config_valid = config_manager.loadServerSettings(&ms_ip, &port, &ws_port, &local_port, &name, &description, &advertise_server); bool config_valid = config_manager.loadServerSettings(
&ms_ip, &port, &ws_port, &local_port, &name, &description,
&advertise_server);
if(!config_valid) { if (!config_valid) {
// TODO: send signal config invalid // TODO: send signal config invalid
config_manager.generateDefaultConfig(true); config_manager.generateDefaultConfig(true);
} else { }
if(advertise_server) { else {
advertiser = new Advertiser(ms_ip, port, ws_port, local_port, name, description); if (advertise_server) {
advertiser =
new Advertiser(ms_ip, port, ws_port, local_port, name, description);
advertiser->contactMasterServer(); advertiser->contactMasterServer();
} }
@ -35,8 +37,4 @@ AkashiMain::AkashiMain(QWidget *parent)
} }
} }
AkashiMain::~AkashiMain() AkashiMain::~AkashiMain() { delete ui; }
{
delete ui;
}

View File

@ -1,9 +1,6 @@
#include <include/config_manager.h> #include <include/config_manager.h>
ConfigManager::ConfigManager(QSettings* p_config) ConfigManager::ConfigManager(QSettings *p_config) { config = p_config; }
{
config = p_config;
}
// Validate and set up the config // Validate and set up the config
bool ConfigManager::initConfig() bool ConfigManager::initConfig()
@ -11,40 +8,43 @@ bool ConfigManager::initConfig()
config->beginGroup("Info"); config->beginGroup("Info");
QString config_version = config->value("version", "none").toString(); QString config_version = config->value("version", "none").toString();
config->endGroup(); config->endGroup();
if(config_version == "none") { if (config_version == "none") {
QFileInfo check_file("config.ini"); QFileInfo check_file("config.ini");
// TODO: put proper translatable warnings here // TODO: put proper translatable warnings here
if (!(check_file.exists() && check_file.isFile())){ if (!(check_file.exists() && check_file.isFile())) {
// TODO: send signal config doesn't exist // TODO: send signal config doesn't exist
generateDefaultConfig(false); generateDefaultConfig(false);
} else { }
else {
// TODO: send signal config is invalid // TODO: send signal config is invalid
generateDefaultConfig(true); generateDefaultConfig(true);
} }
return false; return false;
} }
else if(config_version != QString::number(CONFIG_VERSION)) { else if (config_version != QString::number(CONFIG_VERSION)) {
bool version_number_is_valid; bool version_number_is_valid;
int current_version = config_version.toInt(&version_number_is_valid); int current_version = config_version.toInt(&version_number_is_valid);
if(version_number_is_valid) if (version_number_is_valid)
updateConfig(current_version); updateConfig(current_version);
else else
generateDefaultConfig(true); // Version number isn't a number at all generateDefaultConfig(true); // Version number isn't a number at all
// This means the config is invalid // This means the config is invalid
// TODO: send invalid config signal // TODO: send invalid config signal
return false; return false;
} else { }
else {
// Config is valid and up to date, so let's go ahead // Config is valid and up to date, so let's go ahead
return true; return true;
} }
} }
// Setting backup_old to true will move the existing config.ini to config_old.ini // Setting backup_old to true will move the existing config.ini to
// config_old.ini
void ConfigManager::generateDefaultConfig(bool backup_old) void ConfigManager::generateDefaultConfig(bool backup_old)
{ {
qDebug() << "Config is invalid or missing, making a new one..."; qDebug() << "Config is invalid or missing, making a new one...";
QDir dir = QDir::current(); QDir dir = QDir::current();
if(backup_old) { if (backup_old) {
// TODO: failsafe if config_old.ini already exists // TODO: failsafe if config_old.ini already exists
dir.rename("config.ini", "config_old.ini"); dir.rename("config.ini", "config_old.ini");
} }
@ -78,21 +78,24 @@ void ConfigManager::generateDefaultConfig(bool backup_old)
// Ensure version continuity with config versions // Ensure version continuity with config versions
void ConfigManager::updateConfig(int current_version) void ConfigManager::updateConfig(int current_version)
{ {
if(current_version > CONFIG_VERSION) { if (current_version > CONFIG_VERSION) {
// Config version is newer than the latest version, and the config is invalid // Config version is newer than the latest version, and the config is
// This could also mean the server is out of date, and the user should be shown a relevant message // invalid This could also mean the server is out of date, and the user
// Regardless, regen the config anyways // should be shown a relevant message Regardless, regen the config anyways
// TODO: send signal config is invalid // TODO: send signal config is invalid
generateDefaultConfig(true); generateDefaultConfig(true);
} }
else if (current_version < 0){ else if (current_version < 0) {
// Negative version number? Invalid! // Negative version number? Invalid!
generateDefaultConfig(true); generateDefaultConfig(true);
} else { }
else {
// TODO: send signal config is out of date, and is being updated // TODO: send signal config is out of date, and is being updated
// Update the config as needed using a switch. This is nice because we can fall through as we go up the version ladder. // Update the config as needed using a switch. This is nice because we can
switch(current_version){ // fall through as we go up the version ladder.
case 0: // Version 0 doesn't actually exist, but we should check for it just in case switch (current_version) {
case 0: // Version 0 doesn't actually exist, but we should check for it just
// in case
case 1: case 1:
config->beginGroup("Info"); config->beginGroup("Info");
config->setValue("version", CONFIG_VERSION); config->setValue("version", CONFIG_VERSION);
@ -103,7 +106,10 @@ void ConfigManager::updateConfig(int current_version)
} }
// Validate and retriever settings related to advertising and the server // 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(QString *ms_ip, int *port, int *ws_port,
int *local_port, QString *name,
QString *description,
bool *advertise_server)
{ {
bool port_conversion_success; bool port_conversion_success;
bool ws_port_conversion_success; bool ws_port_conversion_success;
@ -111,20 +117,26 @@ bool ConfigManager::loadServerSettings(QString* ms_ip, int* port, int* ws_port,
config->beginGroup("Options"); config->beginGroup("Options");
*ms_ip = config->value("ms_ip", "master.aceattorneyonline.com").toString(); *ms_ip = config->value("ms_ip", "master.aceattorneyonline.com").toString();
*port = config->value("ms_port", "27016").toInt(&port_conversion_success); *port = config->value("ms_port", "27016").toInt(&port_conversion_success);
*ws_port = config->value("webao_port", "27017").toInt(&ws_port_conversion_success); *ws_port =
*local_port = config->value("port", "27016").toInt(&local_port_conversion_success); config->value("webao_port", "27017").toInt(&ws_port_conversion_success);
*local_port =
config->value("port", "27016").toInt(&local_port_conversion_success);
*name = config->value("server_name", "My First Server").toString(); *name = config->value("server_name", "My First Server").toString();
*description = config->value("server_description", "This is my flashy new server").toString(); *description =
config->value("server_description", "This is my flashy new server")
.toString();
config->endGroup(); config->endGroup();
if(!port_conversion_success || !ws_port_conversion_success || !local_port_conversion_success) { if (!port_conversion_success || !ws_port_conversion_success ||
!local_port_conversion_success) {
return false; return false;
} else { }
if(config->value("advertise", "true").toString() != "true") else {
if (config->value("advertise", "true").toString() != "true")
*advertise_server = false; *advertise_server = false;
else else
*advertise_server = true; *advertise_server = true;
if(config->value("webao_enable", "true").toString() != "true") if (config->value("webao_enable", "true").toString() != "true")
*ws_port = -1; *ws_port = -1;
return true; return true;

View File

@ -1,12 +1,12 @@
#include "include/akashimain.h" #include "include/akashimain.h"
#include <QApplication> #include <QApplication>
#include <QCommandLineParser>
#include <QCommandLineOption> #include <QCommandLineOption>
#include <QCommandLineParser>
#include <QDebug> #include <QDebug>
#include <QLibraryInfo> #include <QLibraryInfo>
#include <QTranslator>
#include <QSettings> #include <QSettings>
#include <QTranslator>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -19,7 +19,8 @@ int main(int argc, char *argv[])
QString language = config.value("language", QLocale().bcp47Name()).toString(); QString language = config.value("language", QLocale().bcp47Name()).toString();
QTranslator qt_translator; QTranslator qt_translator;
qt_translator.load("qt_" + language, QLibraryInfo::location(QLibraryInfo::TranslationsPath)); qt_translator.load("qt_" + language,
QLibraryInfo::location(QLibraryInfo::TranslationsPath));
app.installTranslator(&qt_translator); app.installTranslator(&qt_translator);
QTranslator translator; QTranslator translator;
@ -27,18 +28,22 @@ int main(int argc, char *argv[])
app.installTranslator(&translator); app.installTranslator(&translator);
QCommandLineParser parser; QCommandLineParser parser;
parser.setApplicationDescription(app.translate("main", "A server for Attorney Online 2")); parser.setApplicationDescription(
app.translate("main", "A server for Attorney Online 2"));
parser.addHelpOption(); parser.addHelpOption();
parser.addVersionOption(); parser.addVersionOption();
QCommandLineOption headlessOption(QStringList() << "l" << "headless", app.translate("main", "Run the server without a GUI")); QCommandLineOption headlessOption(
QStringList() << "l"
<< "headless",
app.translate("main", "Run the server without a GUI"));
parser.addOption(headlessOption); parser.addOption(headlessOption);
parser.process(app); parser.process(app);
bool headless = parser.isSet(headlessOption); bool headless = parser.isSet(headlessOption);
AkashiMain w; AkashiMain w;
if(!headless) if (!headless)
w.show(); w.show();
return app.exec(); return app.exec();

View File

@ -3,7 +3,7 @@
QString PacketManager::buildPacket(QString header, QStringList contents) QString PacketManager::buildPacket(QString header, QStringList contents)
{ {
QString ao_packet = header; QString ao_packet = header;
for(int i = 0; i < contents.length(); i++){ for (int i = 0; i < contents.length(); i++) {
ao_packet += "#" + contents[i]; ao_packet += "#" + contents[i];
} }
ao_packet += "#%"; ao_packet += "#%";

View File

@ -6,7 +6,4 @@ Server::Server(int p_port, int p_ws_port)
ws_port = p_ws_port; ws_port = p_ws_port;
} }
void Server::start() void Server::start() {}
{
}