From ba3fd0e1446ee5ba0efc7fa2c6bdf53435e1a518 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Thu, 15 Jul 2021 00:04:44 +0200 Subject: [PATCH] add GetServerVersion --- include/Common.h | 19 +++++++++++++++---- src/Common.cpp | 13 +++++++++++-- src/THeartbeatThread.cpp | 4 ++-- src/TLuaFile.cpp | 9 +++++++++ src/TNetwork.cpp | 2 +- src/TServer.cpp | 2 +- 6 files changed, 39 insertions(+), 10 deletions(-) diff --git a/include/Common.h b/include/Common.h index c95bd2d..f832b1b 100644 --- a/include/Common.h +++ b/include/Common.h @@ -12,6 +12,14 @@ extern TSentry Sentry; #include "TConsole.h" +struct Version { + uint8_t major; + uint8_t minor; + uint8_t patch; + Version(uint8_t major, uint8_t minor, uint8_t patch); + std::string AsString(); +}; + // static class handling application start, shutdown, etc. // yes, static classes, singletons, globals are all pretty // bad idioms. In this case we need a central way to access @@ -44,6 +52,7 @@ public: std::string CustomIP; [[nodiscard]] bool HasCustomIP() const { return !CustomIP.empty(); } }; + using TShutdownHandler = std::function; // methods @@ -54,9 +63,9 @@ public: // Causes all threads to finish up and exit gracefull gracefully static void GracefullyShutdown(); static TConsole& Console() { return *mConsole; } - static std::string ServerVersion() { return "2.3.1"; } -#warning "change version from 2.2.0 to real version" - static std::string ClientVersion() { return "2.0"; } + static std::string ServerVersionString(); + static const Version& ServerVersion() { return mVersion; } + static std::string ClientVersionString() { return "2.0"; } static std::string PPS() { return mPPS; } static void SetPPS(const std::string& NewPPS) { mPPS = NewPPS; } @@ -76,6 +85,8 @@ private: static std::unique_ptr mConsole; static inline std::mutex mShutdownHandlersMutex {}; static inline std::deque mShutdownHandlers {}; + + static inline Version mVersion { 2, 2, 0 }; }; std::string ThreadName(bool DebugModeOverride = false); @@ -146,4 +157,4 @@ void LogChatMessage(const std::string& name, int id, const std::string& msg); #define Biggest 30000 std::string Comp(std::string Data); -std::string DeComp(std::string Compressed); +std::string DeComp(std::string Compressed); \ No newline at end of file diff --git a/src/Common.cpp b/src/Common.cpp index 3fe52bf..0e52b2c 100644 --- a/src/Common.cpp +++ b/src/Common.cpp @@ -144,6 +144,15 @@ void RegisterThread(const std::string& str) { threadNameMap[std::this_thread::get_id()] = str; } +Version::Version(uint8_t major, uint8_t minor, uint8_t patch) + : major(major) + , minor(minor) + , patch(patch) { } + +std::string Version::AsString() { + std::stringstream ss {}; + ss << major << "." << minor << "." << patch; + return ss.str(); void LogChatMessage(const std::string& name, int id, const std::string& msg) { std::stringstream ss; ss << "[CHAT] "; @@ -153,5 +162,5 @@ void LogChatMessage(const std::string& name, int id, const std::string& msg) { ss << name << ""; } ss << msg; - Application::Console().Write(ss.str()); -} + +} \ No newline at end of file diff --git a/src/THeartbeatThread.cpp b/src/THeartbeatThread.cpp index 6a72004..826e0b1 100644 --- a/src/THeartbeatThread.cpp +++ b/src/THeartbeatThread.cpp @@ -95,8 +95,8 @@ std::string THeartbeatThread::GenerateCall() { << "&port=" << Application::Settings.Port << "&map=" << Application::Settings.MapName << "&private=" << (Application::Settings.Private ? "true" : "false") - << "&version=" << Application::ServerVersion() - << "&clientversion=" << Application::ClientVersion() + << "&version=" << Application::ServerVersionString() + << "&clientversion=" << Application::ClientVersionString() << "&name=" << Application::Settings.ServerName << "&modlist=" << mResourceManager.TrimmedList() << "&modstotalsize=" << mResourceManager.MaxModSize() diff --git a/src/TLuaFile.cpp b/src/TLuaFile.cpp index b16b9de..4de5d47 100644 --- a/src/TLuaFile.cpp +++ b/src/TLuaFile.cpp @@ -809,6 +809,14 @@ int lua_GetOSName(lua_State* L) { return 1; } +int lua_GetServerVersion(lua_State* L) { + const auto& ver = Application::ServerVersion(); + lua_pushinteger(L, ver.major); + lua_pushinteger(L, ver.minor); + lua_pushinteger(L, ver.patch); + return 3; +} + // status, body = HttpsGET(host, port, target) // example usage: // send a GET https://example.com:443/index.html: @@ -938,6 +946,7 @@ void TLuaFile::Load() { LuaTable::InsertFunction(mLuaState, "GetOSName", lua_GetOSName); LuaTable::InsertFunction(mLuaState, "HttpsGET", lua_HttpsGET); LuaTable::InsertFunction(mLuaState, "HttpsPOST", lua_HttpsPOST); + LuaTable::InsertFunction(mLuaState, "GetServerVersion", lua_GetServerVersion); LuaTable::End(mLuaState, "MP"); lua_register(mLuaState, "print", lua_Print); diff --git a/src/TNetwork.cpp b/src/TNetwork.cpp index 0935e7b..30c8ad6 100644 --- a/src/TNetwork.cpp +++ b/src/TNetwork.cpp @@ -263,7 +263,7 @@ void TNetwork::Authentication(const TConnection& ClientConnection) { if (Rc.size() > 3 && Rc.substr(0, 2) == "VC") { Rc = Rc.substr(2); - if (Rc.length() > 4 || Rc != Application::ClientVersion()) { + if (Rc.length() > 4 || Rc != Application::ClientVersionString()) { ClientKick(*Client, "Outdated Version!"); return; } diff --git a/src/TServer.cpp b/src/TServer.cpp index 9c294a1..2bb8fc5 100644 --- a/src/TServer.cpp +++ b/src/TServer.cpp @@ -14,7 +14,7 @@ namespace json = rapidjson; TServer::TServer(int argc, char** argv) { - info("BeamMP Server v" + Application::ServerVersion()); + info("BeamMP Server v" + Application::ServerVersionString()); if (argc > 1) { Application::Settings.CustomIP = argv[1]; size_t n = std::count(Application::Settings.CustomIP.begin(), Application::Settings.CustomIP.end(), '.');