add basic configuration logic

This commit is contained in:
scatterflower 2020-08-23 03:09:15 -05:00
parent 2fe4d5c234
commit 4fb4119700
7 changed files with 149 additions and 3 deletions

9
include/advertiser.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef MASTER_H
#define MASTER_H
class Advertiser{
public:
Advertiser();
};
#endif // MASTER_H

View File

@ -1,7 +1,13 @@
#ifndef AKASHIMAIN_H
#define AKASHIMAIN_H
#define CONFIG_VERSION 1
#include <QMainWindow>
#include <QSettings>
#include <QDebug>
#include <QFileInfo>
#include <QDir>
QT_BEGIN_NAMESPACE
namespace Ui { class AkashiMain; }
@ -15,6 +21,11 @@ public:
AkashiMain(QWidget *parent = nullptr);
~AkashiMain();
QSettings config;
bool initConfig();
void generateDefaultConfig(bool backup_old);
void updateConfig(int current_version);
private:
Ui::AkashiMain *ui;
};

11
include/server.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef SERVER_H
#define SERVER_H
class Server
{
public:
Server();
};
#endif // SERVER_H

5
src/advertiser.cpp Normal file
View File

@ -0,0 +1,5 @@
#include "include/advertiser.h"
Advertiser::Advertiser(){
}

View File

@ -1,14 +1,115 @@
#include "include/akashimain.h"
#include "ui_akashimain.h"
#include <QDebug>
AkashiMain::AkashiMain(QWidget *parent)
: QMainWindow(parent)
, config("config.ini", QSettings::IniFormat)
, ui(new Ui::AkashiMain)
{
ui->setupUi(this);
qDebug("Main application started");
if(initConfig()) {
// Config is sound, so proceed with starting the server
// TODO: start the server here
// TODO: send signal server starting
}
}
// Returns true if config is up to date and valid, false otherwise
bool AkashiMain::initConfig()
{
config.beginGroup("Info");
QString config_version = config.value("version", "none").toString();
config.endGroup();
if(config_version == "none") {
QFileInfo check_file("config.ini");
// TODO: put proper translatable warnings here
if (!(check_file.exists() && check_file.isFile())){
// TODO: send signal config doesn't exist
generateDefaultConfig(false);
} else {
// TODO: send signal config is invalid
generateDefaultConfig(true);
}
return false;
}
else if(config_version != QString::number(CONFIG_VERSION)) {
bool version_number_is_valid;
int current_version = config_version.toInt(&version_number_is_valid);
if(version_number_is_valid)
updateConfig(current_version);
else
generateDefaultConfig(true); // Version number isn't a number at all
// This means the config is invalid
// TODO: send invalid config signal
return false;
} else {
// Config is valid and up to date, so let's go ahead
return true;
}
}
// Setting backup_old to true will move the existing config.ini to config_old.ini
void AkashiMain::generateDefaultConfig(bool backup_old)
{
qDebug() << "Config is invalid or missing, making a new one...";
QDir dir = QDir::current();
if(backup_old) {
// TODO: failsafe if config_old.ini already exists
dir.rename("config.ini", "config_old.ini");
}
// Group: Info
// This contains basic metadata about the config
config.beginGroup("Info");
config.setValue("version", CONFIG_VERSION);
config.endGroup();
// Group: Options
// This contains general configuration
config.beginGroup("Options");
config.setValue("hostname", "$H");
config.setValue("max_players", "100");
config.setValue("port", "27016");
config.setValue("webao_enable", "true");
config.setValue("webao_port", "27017");
config.setValue("modpass", "password");
config.setValue("advertise", "true");
config.setValue("ms_ip", "master.aceattorneyonline.com");
config.setValue("ms_port", "27016");
config.setValue("server_name", "My First Server");
config.setValue("server_description", "This is my flashy new server");
config.setValue("multiclient_limit", "16");
config.setValue("max_message_size", "256");
config.endGroup();
}
void AkashiMain::updateConfig(int current_version)
{
if(current_version > CONFIG_VERSION) {
// Config version is newer than the latest version, and the config is invalid
// This could also mean the server is out of date, and the user should be shown a relevant message
// Regardless, regen the config anyways
// TODO: send signal config is invalid
generateDefaultConfig(true);
}
else if (current_version < 0){
// Negative version number? Invalid!
generateDefaultConfig(true);
} else {
// 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.
switch(current_version){
case 0: // Version 0 doesn't actually exist, but we should check for it just in case
case 1:
config.beginGroup("Info");
config.setValue("version", CONFIG_VERSION);
config.endGroup();
break; // This is the newest version, and nothing more needs to be done
}
}
}
AkashiMain::~AkashiMain()

View File

@ -6,6 +6,7 @@
#include <QDebug>
#include <QLibraryInfo>
#include <QTranslator>
#include <QSettings>
int main(int argc, char *argv[])
{
@ -13,7 +14,9 @@ int main(int argc, char *argv[])
QApplication::setApplicationName("akashi");
QApplication::setApplicationVersion("0.0.1");
QString language = QLocale().bcp47Name();
QSettings config("config.ini", QSettings::IniFormat);
config.beginGroup("Options");
QString language = config.value("language", QLocale().bcp47Name()).toString();
QTranslator qt_translator;
qt_translator.load("qt_" + language, QLibraryInfo::location(QLibraryInfo::TranslationsPath));

6
src/server.cpp Normal file
View File

@ -0,0 +1,6 @@
#include "include/server.h"
Server::Server()
{
}