Server config now uses json

This commit is contained in:
Anonymous-275 2021-04-22 02:28:02 +03:00
parent f1e1b6cc28
commit 1bee72a175
6 changed files with 221 additions and 32 deletions

View File

@ -17,18 +17,26 @@ class Application final {
public: public:
// types // types
struct TSettings { struct TSettings {
TSettings() noexcept TSettings() noexcept :
: DebugModeEnabled(true) { } ServerName("BeamMP Server"),
ServerDesc("BeamMP Default Description"),
Resource("Resources"),
MapName("/levels/gridmap/info.json"),
MaxPlayers(10),
Private(true),
MaxCars(1),
DebugModeEnabled(false),
Port(30814){}
std::string ServerName; std::string ServerName;
std::string ServerDesc; std::string ServerDesc;
std::string Resource; std::string Resource;
std::string MapName; std::string MapName;
std::string Key; std::string Key;
int MaxPlayers {}; int MaxPlayers;
bool Private {}; bool Private;
int MaxCars {}; int MaxCars;
bool DebugModeEnabled; bool DebugModeEnabled;
int Port {}; int Port;
std::string CustomIP; std::string CustomIP;
[[nodiscard]] bool HasCustomIP() const { return !CustomIP.empty(); } [[nodiscard]] bool HasCustomIP() const { return !CustomIP.empty(); }
}; };

9
include/Json.h Normal file
View File

@ -0,0 +1,9 @@
//
// Created by anon on 4/21/21.
//
#pragma once
#include "rapidjson/stringbuffer.h"
#include "rapidjson/prettywriter.h"
#include "rapidjson/document.h"
#include "rapidjson/writer.h"

View File

@ -7,6 +7,9 @@ public:
explicit TConfig(const std::string& ConfigFile); explicit TConfig(const std::string& ConfigFile);
private: private:
static void ReadJson();
static void PrintDebug();
static void ManageJson();
static std::string RemoveComments(const std::string& Line); static std::string RemoveComments(const std::string& Line);
static void SetValues(const std::string& Line, int Index); static void SetValues(const std::string& Line, int Index);
}; };

View File

@ -1,7 +1,24 @@
#include "../include/TConfig.h" #include "TConfig.h"
#include <fstream> #include <fstream>
#include "Json.h"
#include <iostream>
TConfig::TConfig(const std::string& ConfigFile) { TConfig::TConfig(const std::string& ConfigFile) {
if(fs::exists("Config.json")){
info("New Config found updating values");
ReadJson();
return;
}
if(!fs::exists("Server.cfg")){
info("Config not found generating default");
ManageJson();
error("AuthKey cannot be empty check Config.json!");
std::this_thread::sleep_for(std::chrono::seconds(3));
Application::GracefullyShutdown();
return;
}
std::ifstream File(ConfigFile); std::ifstream File(ConfigFile);
if (File.good()) { if (File.good()) {
std::string line; std::string line;
@ -10,13 +27,13 @@ TConfig::TConfig(const std::string& ConfigFile) {
index++; index++;
} }
if (index - 1 < 11) { if (index - 1 < 11) {
error(("Outdated/Incorrect config please remove it server will close in 5 secs")); error("Outdated/Incorrect config please remove it server will close in 5 secs");
std::this_thread::sleep_for(std::chrono::seconds(3)); std::this_thread::sleep_for(std::chrono::seconds(3));
_Exit(0); _Exit(0);
} }
File.close(); File.close();
File.open(("Server.cfg")); File.open("Server.cfg");
info(("Config found updating values")); info("Old Config found updating values");
index = 1; index = 1;
while (std::getline(File, line)) { while (std::getline(File, line)) {
if (line.rfind('#', 0) != 0 && line.rfind(' ', 0) != 0) { //Checks if it starts as Comment if (line.rfind('#', 0) != 0 && line.rfind(' ', 0) != 0) { //Checks if it starts as Comment
@ -26,11 +43,11 @@ TConfig::TConfig(const std::string& ConfigFile) {
} }
} }
} else { } else {
info(("Config not found generating default")); info("Config not found generating default");
std::ofstream FileStream; std::ofstream FileStream;
FileStream.open(ConfigFile); FileStream.open(ConfigFile);
// TODO REPLACE THIS SHIT OMG // TODO REPLACE THIS SHIT OMG -- replacing
FileStream << ("# This is the BeamMP Server Configuration File v0.60\n" FileStream << "# This is the BeamMP Server Configuration File v0.60\n"
"Debug = false # true or false to enable debug console output\n" "Debug = false # true or false to enable debug console output\n"
"Private = true # Private?\n" "Private = true # Private?\n"
"Port = 30814 # Port to run the server on UDP and TCP\n" "Port = 30814 # Port to run the server on UDP and TCP\n"
@ -40,22 +57,164 @@ TConfig::TConfig(const std::string& ConfigFile) {
"Name = \"BeamMP New Server\" # Server Name\n" "Name = \"BeamMP New Server\" # Server Name\n"
"Desc = \"BeamMP Default Description\" # Server Description\n" "Desc = \"BeamMP Default Description\" # Server Description\n"
"use = \"Resources\" # Resource file name\n" "use = \"Resources\" # Resource file name\n"
"AuthKey = \"\" # Auth Key"); "AuthKey = \"\" # Auth Key";
FileStream.close(); FileStream.close();
error(("You are required to input the AuthKey")); error("You are required to input the AuthKey");
std::this_thread::sleep_for(std::chrono::seconds(3)); std::this_thread::sleep_for(std::chrono::seconds(3));
_Exit(0); _Exit(0);
} }
debug("Debug : " + std::string(Application::Settings.DebugModeEnabled ? "true" : "false"));
debug("Private : " + std::string(Application::Settings.Private ? "true" : "false")); ManageJson();
debug("Port : " + std::to_string(Application::Settings.Port)); PrintDebug();
debug("Max Cars : " + std::to_string(Application::Settings.MaxCars)); }
debug("MaxPlayers : " + std::to_string(Application::Settings.MaxPlayers));
debug("MapName : \"" + Application::Settings.MapName + "\""); void TConfig::ReadJson() {
debug("ServerName : \"" + Application::Settings.ServerName + "\""); auto Size = fs::file_size("Config.json");
debug("ServerDesc : \"" + Application::Settings.ServerDesc + "\""); if(Size < 3)return;
debug("File : \"" + Application::Settings.Resource + "\"");
debug("Key length : " + std::to_string(Application::Settings.Key.length()) + ""); std::ifstream ifs("Config.json");
if(ifs.is_open()) {
std::string cfg(Size, 0);
ifs.read(&cfg[0], long(Size));
ifs.close();
if(auto pos = cfg.find('{'); pos != std::string::npos) {
if(cfg.at(0) != '{') {
cfg = cfg.substr(pos);
}
}else{
error("Config file is not valid JSON!");
std::this_thread::sleep_for(std::chrono::seconds(3));
Application::GracefullyShutdown();
}
rapidjson::Document d;
d.Parse(cfg.c_str());
if(!d.HasParseError()){
auto& Val = d["Debug"];
if(!Val.IsNull() && Val.IsBool()) {
Application::Settings.DebugModeEnabled = Val.GetBool();
}else{
info("'Debug' Missing in config! Setting to 'false' by default");
}
Val = d["Private"];
if(!Val.IsNull() && Val.IsBool()) {
Application::Settings.Private = Val.GetBool();
}else{
info("'Private' Missing in config! Setting to 'true' by default");
}
Val = d["Port"];
if(!Val.IsNull() && Val.IsNumber()) {
Application::Settings.Port = Val.GetInt();
}else{
info("'Port' Missing in config! Setting to '30814' by default");
}
Val = d["MaxCars"];
if(!Val.IsNull() && Val.IsNumber()) {
Application::Settings.MaxCars = Val.GetInt();
}else{
info("'MaxCars' Missing in config! Setting to '1' by default");
}
Val = d["MaxPlayers"];
if(!Val.IsNull() && Val.IsNumber()) {
Application::Settings.MaxPlayers = Val.GetInt();
}else{
info("'MaxPlayers' Missing in config! Setting to '10' by default");
}
Val = d["Map"];
if(!Val.IsNull() && Val.IsString()) {
Application::Settings.MapName = Val.GetString();
}else{
info("'Map' Missing in config! Setting to '/levels/gridmap/info.json' by default");
}
Val = d["Name"];
if(!Val.IsNull() && Val.IsString()) {
Application::Settings.ServerName = Val.GetString();
}else{
info("'Name' Missing in config! Setting to 'BeamMP Server' by default");
}
Val = d["Desc"];
if(!Val.IsNull() && Val.IsString()) {
Application::Settings.ServerDesc = Val.GetString();
}else{
info("'Desc' Missing in config! Setting to 'BeamMP Default Description' by default");
}
Val = d["Resource"];
if(!Val.IsNull() && Val.IsString()) {
Application::Settings.Resource = Val.GetString();
}else{
info("'Resource' Missing in config! Setting to 'Resources' by default");
}
Val = d["AuthKey"];
if(!Val.IsNull() && Val.IsString()) {
Application::Settings.Key = Val.GetString();
if(Application::Settings.Key.empty()) {
error("AuthKey cannot be empty check Config.json!");
std::this_thread::sleep_for(std::chrono::seconds(3));
Application::GracefullyShutdown();
}
}else{
error("'AuthKey' Missing in config!");
std::this_thread::sleep_for(std::chrono::seconds(3));
Application::GracefullyShutdown();
}
}else{
error("Failed to parse JSON config! code " + std::to_string(d.GetParseError()));
}
}else{
error("Failed to read Config.json");
std::this_thread::sleep_for(std::chrono::seconds(3));
Application::GracefullyShutdown();
}
PrintDebug();
}
void TConfig::ManageJson() {
rapidjson::Document d;
d.Parse("{}");
d.AddMember("Debug",Application::Settings.DebugModeEnabled,d.GetAllocator());
d.AddMember("Private",Application::Settings.Private,d.GetAllocator());
d.AddMember("Port",Application::Settings.Port,d.GetAllocator());
d.AddMember("MaxCars",Application::Settings.MaxCars,d.GetAllocator());
d.AddMember("MaxPlayers",Application::Settings.MaxPlayers,d.GetAllocator());
d.AddMember("Map", rapidjson::StringRef(Application::Settings.MapName.c_str()),d.GetAllocator());
d.AddMember("Name", rapidjson::StringRef(Application::Settings.ServerName.c_str()),d.GetAllocator());
d.AddMember("Desc", rapidjson::StringRef(Application::Settings.ServerDesc.c_str()),d.GetAllocator());
d.AddMember("Resource", rapidjson::StringRef(Application::Settings.Resource.c_str()),d.GetAllocator());
d.AddMember("AuthKey", rapidjson::StringRef(Application::Settings.Key.c_str()),d.GetAllocator());
rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
d.Accept(writer);
std::ofstream cfg;
cfg.open("Config.json");
if(cfg.is_open()){
cfg << "BeamMP Server Configuration File\n"
<< buffer.GetString();
cfg.close();
}else{
error("Failed to create Config.json!");
}
} }
std::string TConfig::RemoveComments(const std::string& Line) { std::string TConfig::RemoveComments(const std::string& Line) {
@ -124,3 +283,17 @@ void TConfig::SetValues(const std::string& Line, int Index) {
break; break;
} }
} }
void TConfig::PrintDebug(){
debug("Debug : " + std::string(Application::Settings.DebugModeEnabled ? "true" : "false"));
debug("Private : " + std::string(Application::Settings.Private ? "true" : "false"));
debug("Port : " + std::to_string(Application::Settings.Port));
debug("Max Cars : " + std::to_string(Application::Settings.MaxCars));
debug("MaxPlayers : " + std::to_string(Application::Settings.MaxPlayers));
debug("MapName : \"" + Application::Settings.MapName + "\"");
debug("ServerName : \"" + Application::Settings.ServerName + "\"");
debug("ServerDesc : \"" + Application::Settings.ServerDesc + "\"");
debug("File : \"" + Application::Settings.Resource + "\"");
debug("Key length : " + std::to_string(Application::Settings.Key.length()) + "");
}

View File

@ -204,9 +204,7 @@ void TNetwork::TCPServerMain() {
#undef GetObject //Fixes Windows #undef GetObject //Fixes Windows
#include "rapidjson/document.h" #include "Json.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
namespace json = rapidjson; namespace json = rapidjson;
void TNetwork::Identify(SOCKET TCPSock) { void TNetwork::Identify(SOCKET TCPSock) {

View File

@ -9,9 +9,7 @@
#undef GetObject //Fixes Windows #undef GetObject //Fixes Windows
#include "rapidjson/document.h" #include "Json.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
namespace json = rapidjson; namespace json = rapidjson;