From ddbba997e5a5bee85f9fcbc81107f33991533704 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sun, 13 Aug 2023 12:38:32 +0200 Subject: [PATCH] Working server --- src/ServerMain.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/src/ServerMain.cpp b/src/ServerMain.cpp index b9ae5b9..19b89a2 100644 --- a/src/ServerMain.cpp +++ b/src/ServerMain.cpp @@ -12,6 +12,35 @@ #endif #include "td/misc/Backward.hpp" +#include "td/misc/Log.h" +#include "td/misc/Format.h" +#include "server/Server.h" + +struct Argument { + std::string Name; + std::string Descritpion; +}; + +using namespace td::utils; + +static std::vector CMD_ARGUMENTS = { + { "" , "The map to load"}, + { "-P\t" , "The port to listen to. Default : 25565"}, + { "--help\t", "Displays help"} +}; + +static void DisplayHelp() { + LOG("Usage :"); + for (std::size_t i = 0; i < CMD_ARGUMENTS.size(); i++) { + Argument& arg = CMD_ARGUMENTS[i]; + LOG(format("\t%s\t%s", arg.Name.c_str(), arg.Descritpion.c_str())); + } +} + +static bool StartServer(const std::string& mapPath, std::uint16_t port) { + td::server::Server server {mapPath}; + return !server.Start(port); +} #ifdef __ANDROID__ extern "C" @@ -23,5 +52,42 @@ int main(int argc, const char* args[]) { backward::SignalHandling sh; #endif - return 0; + std::string mapFilePath; + std::uint16_t port = 25565; + + for (int i = 1; i < argc; i++) { + std::string arg = args[i]; + + if (arg == "--help") { + DisplayHelp(); + return EXIT_SUCCESS; + } + + if (arg == "-P") { + i++; + + if (i >= argc) { + LOG("You must specify a valid port !"); + return EXIT_FAILURE; + } + + try { + port = std::stoi(args[i]); + } catch (std::exception& e) { + LOG("You must specify a valid port !"); + return EXIT_FAILURE; + } + + continue; + } + + if (!mapFilePath.empty()) { + LOG(format("Uknown parameter : %s !", arg.c_str())); + return EXIT_FAILURE; + } + + mapFilePath = arg; + } + + return StartServer(mapFilePath, port); } \ No newline at end of file