mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-07-01 15:26:59 +00:00
Server config now uses json
This commit is contained in:
parent
f1e1b6cc28
commit
1bee72a175
@ -17,18 +17,26 @@ class Application final {
|
||||
public:
|
||||
// types
|
||||
struct TSettings {
|
||||
TSettings() noexcept
|
||||
: DebugModeEnabled(true) { }
|
||||
TSettings() noexcept :
|
||||
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 ServerDesc;
|
||||
std::string Resource;
|
||||
std::string MapName;
|
||||
std::string Key;
|
||||
int MaxPlayers {};
|
||||
bool Private {};
|
||||
int MaxCars {};
|
||||
int MaxPlayers;
|
||||
bool Private;
|
||||
int MaxCars;
|
||||
bool DebugModeEnabled;
|
||||
int Port {};
|
||||
int Port;
|
||||
std::string CustomIP;
|
||||
[[nodiscard]] bool HasCustomIP() const { return !CustomIP.empty(); }
|
||||
};
|
||||
|
9
include/Json.h
Normal file
9
include/Json.h
Normal 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"
|
@ -7,6 +7,9 @@ public:
|
||||
explicit TConfig(const std::string& ConfigFile);
|
||||
|
||||
private:
|
||||
static void ReadJson();
|
||||
static void PrintDebug();
|
||||
static void ManageJson();
|
||||
static std::string RemoveComments(const std::string& Line);
|
||||
static void SetValues(const std::string& Line, int Index);
|
||||
};
|
||||
|
213
src/TConfig.cpp
213
src/TConfig.cpp
@ -1,7 +1,24 @@
|
||||
#include "../include/TConfig.h"
|
||||
#include "TConfig.h"
|
||||
#include <fstream>
|
||||
|
||||
#include "Json.h"
|
||||
#include <iostream>
|
||||
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);
|
||||
if (File.good()) {
|
||||
std::string line;
|
||||
@ -10,13 +27,13 @@ TConfig::TConfig(const std::string& ConfigFile) {
|
||||
index++;
|
||||
}
|
||||
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));
|
||||
_Exit(0);
|
||||
}
|
||||
File.close();
|
||||
File.open(("Server.cfg"));
|
||||
info(("Config found updating values"));
|
||||
File.open("Server.cfg");
|
||||
info("Old Config found updating values");
|
||||
index = 1;
|
||||
while (std::getline(File, line)) {
|
||||
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 {
|
||||
info(("Config not found generating default"));
|
||||
info("Config not found generating default");
|
||||
std::ofstream FileStream;
|
||||
FileStream.open(ConfigFile);
|
||||
// TODO REPLACE THIS SHIT OMG
|
||||
FileStream << ("# This is the BeamMP Server Configuration File v0.60\n"
|
||||
// TODO REPLACE THIS SHIT OMG -- replacing
|
||||
FileStream << "# This is the BeamMP Server Configuration File v0.60\n"
|
||||
"Debug = false # true or false to enable debug console output\n"
|
||||
"Private = true # Private?\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"
|
||||
"Desc = \"BeamMP Default Description\" # Server Description\n"
|
||||
"use = \"Resources\" # Resource file name\n"
|
||||
"AuthKey = \"\" # Auth Key");
|
||||
"AuthKey = \"\" # Auth Key";
|
||||
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));
|
||||
_Exit(0);
|
||||
}
|
||||
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()) + "");
|
||||
|
||||
ManageJson();
|
||||
PrintDebug();
|
||||
}
|
||||
|
||||
void TConfig::ReadJson() {
|
||||
auto Size = fs::file_size("Config.json");
|
||||
if(Size < 3)return;
|
||||
|
||||
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) {
|
||||
@ -124,3 +283,17 @@ void TConfig::SetValues(const std::string& Line, int Index) {
|
||||
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()) + "");
|
||||
}
|
||||
|
||||
|
@ -204,9 +204,7 @@ void TNetwork::TCPServerMain() {
|
||||
|
||||
#undef GetObject //Fixes Windows
|
||||
|
||||
#include "rapidjson/document.h"
|
||||
#include "rapidjson/stringbuffer.h"
|
||||
#include "rapidjson/writer.h"
|
||||
#include "Json.h"
|
||||
namespace json = rapidjson;
|
||||
|
||||
void TNetwork::Identify(SOCKET TCPSock) {
|
||||
|
@ -9,9 +9,7 @@
|
||||
|
||||
#undef GetObject //Fixes Windows
|
||||
|
||||
#include "rapidjson/document.h"
|
||||
#include "rapidjson/stringbuffer.h"
|
||||
#include "rapidjson/writer.h"
|
||||
#include "Json.h"
|
||||
|
||||
namespace json = rapidjson;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user