add GetServerVersion

This commit is contained in:
Lion Kortlepel 2021-07-15 00:04:44 +02:00
parent 9595ef164e
commit e11211f201
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
6 changed files with 42 additions and 7 deletions

View File

@ -8,6 +8,14 @@
#include "TConsole.h" #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. // static class handling application start, shutdown, etc.
// yes, static classes, singletons, globals are all pretty // yes, static classes, singletons, globals are all pretty
// bad idioms. In this case we need a central way to access // bad idioms. In this case we need a central way to access
@ -40,6 +48,7 @@ public:
std::string CustomIP; std::string CustomIP;
[[nodiscard]] bool HasCustomIP() const { return !CustomIP.empty(); } [[nodiscard]] bool HasCustomIP() const { return !CustomIP.empty(); }
}; };
using TShutdownHandler = std::function<void()>; using TShutdownHandler = std::function<void()>;
// methods // methods
@ -50,9 +59,9 @@ public:
// Causes all threads to finish up and exit gracefull gracefully // Causes all threads to finish up and exit gracefull gracefully
static void GracefullyShutdown(); static void GracefullyShutdown();
static TConsole& Console() { return *mConsole; } static TConsole& Console() { return *mConsole; }
static std::string ServerVersion() { return "2.2.0"; } static std::string ServerVersionString();
#warning "change version from 2.2.0 to real version" static const Version& ServerVersion() { return mVersion; }
static std::string ClientVersion() { return "2.0"; } static std::string ClientVersionString() { return "2.0"; }
static std::string PPS() { return mPPS; } static std::string PPS() { return mPPS; }
static void SetPPS(std::string NewPPS) { mPPS = NewPPS; } static void SetPPS(std::string NewPPS) { mPPS = NewPPS; }
@ -67,6 +76,8 @@ private:
static std::unique_ptr<TConsole> mConsole; static std::unique_ptr<TConsole> mConsole;
static inline std::mutex mShutdownHandlersMutex {}; static inline std::mutex mShutdownHandlersMutex {};
static inline std::deque<TShutdownHandler> mShutdownHandlers {}; static inline std::deque<TShutdownHandler> mShutdownHandlers {};
static inline Version mVersion { 2, 2, 0 };
}; };
std::string ThreadName(); std::string ThreadName();

View File

@ -24,6 +24,10 @@ void Application::GracefullyShutdown() {
} }
} }
std::string Application::ServerVersionString() {
return mVersion.AsString();
}
std::string Comp(std::string Data) { std::string Comp(std::string Data) {
std::array<char, Biggest> C {}; std::array<char, Biggest> C {};
// obsolete // obsolete
@ -86,3 +90,14 @@ std::string ThreadName() {
void RegisterThread(const std::string str) { void RegisterThread(const std::string str) {
threadNameMap[std::this_thread::get_id()] = 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();
}

View File

@ -71,8 +71,8 @@ std::string THeartbeatThread::GenerateCall() {
<< "&port=" << Application::Settings.Port << "&port=" << Application::Settings.Port
<< "&map=" << Application::Settings.MapName << "&map=" << Application::Settings.MapName
<< "&private=" << (Application::Settings.Private ? "true" : "false") << "&private=" << (Application::Settings.Private ? "true" : "false")
<< "&version=" << Application::ServerVersion() << "&version=" << Application::ServerVersionString()
<< "&clientversion=" << Application::ClientVersion() << "&clientversion=" << Application::ClientVersionString()
<< "&name=" << Application::Settings.ServerName << "&name=" << Application::Settings.ServerName
<< "&modlist=" << mResourceManager.TrimmedList() << "&modlist=" << mResourceManager.TrimmedList()
<< "&modstotalsize=" << mResourceManager.MaxModSize() << "&modstotalsize=" << mResourceManager.MaxModSize()

View File

@ -782,6 +782,14 @@ int lua_GetOSName(lua_State* L) {
return 1; 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) // status, body = HttpsGET(host, port, target)
// example usage: // example usage:
// send a GET https://example.com:443/index.html: // 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, "GetOSName", lua_GetOSName);
LuaTable::InsertFunction(mLuaState, "HttpsGET", lua_HttpsGET); LuaTable::InsertFunction(mLuaState, "HttpsGET", lua_HttpsGET);
LuaTable::InsertFunction(mLuaState, "HttpsPOST", lua_HttpsPOST); LuaTable::InsertFunction(mLuaState, "HttpsPOST", lua_HttpsPOST);
LuaTable::InsertFunction(mLuaState, "GetServerVersion", lua_GetServerVersion);
LuaTable::End(mLuaState, "MP"); LuaTable::End(mLuaState, "MP");
lua_register(mLuaState, "print", lua_Print); lua_register(mLuaState, "print", lua_Print);

View File

@ -263,7 +263,7 @@ void TNetwork::Authentication(const TConnection& ClientConnection) {
if (Rc.size() > 3 && Rc.substr(0, 2) == "VC") { if (Rc.size() > 3 && Rc.substr(0, 2) == "VC") {
Rc = Rc.substr(2); Rc = Rc.substr(2);
if (Rc.length() > 4 || Rc != Application::ClientVersion()) { if (Rc.length() > 4 || Rc != Application::ClientVersionString()) {
ClientKick(*Client, "Outdated Version!"); ClientKick(*Client, "Outdated Version!");
return; return;
} }

View File

@ -14,7 +14,7 @@
namespace json = rapidjson; namespace json = rapidjson;
TServer::TServer(int argc, char** argv) { TServer::TServer(int argc, char** argv) {
info("BeamMP Server v" + Application::ServerVersion()); info("BeamMP Server v" + Application::ServerVersionString());
if (argc > 1) { if (argc > 1) {
Application::Settings.CustomIP = argv[1]; Application::Settings.CustomIP = argv[1];
size_t n = std::count(Application::Settings.CustomIP.begin(), Application::Settings.CustomIP.end(), '.'); size_t n = std::count(Application::Settings.CustomIP.begin(), Application::Settings.CustomIP.end(), '.');