diff --git a/include/Common.h b/include/Common.h index 59802fd..ee32b0e 100644 --- a/include/Common.h +++ b/include/Common.h @@ -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(); } }; diff --git a/include/Json.h b/include/Json.h new file mode 100644 index 0000000..d319099 --- /dev/null +++ b/include/Json.h @@ -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" diff --git a/include/TConfig.h b/include/TConfig.h index 725ac61..bd1287f 100644 --- a/include/TConfig.h +++ b/include/TConfig.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); }; diff --git a/src/TConfig.cpp b/src/TConfig.cpp index ece98c1..7adf0c1 100644 --- a/src/TConfig.cpp +++ b/src/TConfig.cpp @@ -1,7 +1,24 @@ -#include "../include/TConfig.h" +#include "TConfig.h" #include - +#include "Json.h" +#include 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 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()) + ""); +} + diff --git a/src/TNetwork.cpp b/src/TNetwork.cpp index c2269f9..bd7d00e 100644 --- a/src/TNetwork.cpp +++ b/src/TNetwork.cpp @@ -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) { diff --git a/src/TServer.cpp b/src/TServer.cpp index 857317b..c389ece 100644 --- a/src/TServer.cpp +++ b/src/TServer.cpp @@ -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;