diff --git a/include/Common.h b/include/Common.h index 5feccc0..40ccb99 100644 --- a/include/Common.h +++ b/include/Common.h @@ -8,6 +8,14 @@ #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 @@ -40,6 +48,7 @@ public: std::string CustomIP; [[nodiscard]] bool HasCustomIP() const { return !CustomIP.empty(); } }; + using TShutdownHandler = std::function; // methods @@ -50,9 +59,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.2.0"; } -#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(std::string NewPPS) { mPPS = NewPPS; } @@ -67,6 +76,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(); diff --git a/src/Common.cpp b/src/Common.cpp index 32b5905..0b7e336 100644 --- a/src/Common.cpp +++ b/src/Common.cpp @@ -24,6 +24,10 @@ void Application::GracefullyShutdown() { } } +std::string Application::ServerVersionString() { + return mVersion.AsString(); +} + std::string Comp(std::string Data) { std::array C {}; // obsolete @@ -86,3 +90,14 @@ std::string ThreadName() { 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(); +} diff --git a/src/THeartbeatThread.cpp b/src/THeartbeatThread.cpp index bacb235..607fd94 100644 --- a/src/THeartbeatThread.cpp +++ b/src/THeartbeatThread.cpp @@ -71,8 +71,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 a9af4c2..bfbfc09 100644 --- a/src/TLuaFile.cpp +++ b/src/TLuaFile.cpp @@ -782,6 +782,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: @@ -911,6 +919,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 a5d4624..d24cf6e 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 7be220e..5492b66 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(), '.');