From e1f5d81550d11345a3181e9cf2ded40e4e2b46dc Mon Sep 17 00:00:00 2001 From: mckaygerhard Date: Thu, 7 Sep 2023 17:10:05 -0400 Subject: [PATCH] implements client enhanced information * closes https://codeberg.org/minenux/minetest-engine-multicraft2#49 * closes https://codeberg.org/minenux/minetest-engine-minetest/issues/26 * it takes from https://github.com/MultiCraft/MultiCraft2/pull/50 * backported from "Add platform information to get_player_information" --- doc/lua_api.txt | 2 +- src/client.cpp | 12 ++++++++++-- src/clientiface.h | 14 ++++++++++++-- src/network/networkprotocol.h | 2 ++ src/script/lua_api/l_server.cpp | 16 +++++++++++++--- src/server.cpp | 8 ++++++-- src/server.h | 2 +- 7 files changed, 45 insertions(+), 11 deletions(-) diff --git a/doc/lua_api.txt b/doc/lua_api.txt index a20abdc64..1359fb2df 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -2280,7 +2280,7 @@ Helper functions major = 0, -- major version number minor = 4, -- minor version number patch = 10, -- patch version number - vers_string = "0.4.9-git", -- full version string + version_string = "0.4.9", -- full version string state = "Active" -- current client state } * `minetest.mkdir(path)`: returns success. diff --git a/src/client.cpp b/src/client.cpp index 1cbb6953d..489f21e8f 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -1272,15 +1272,23 @@ void Client::sendRespawn() void Client::sendReady() { + const char *platform_name = porting::getPlatformName(); + const std::string sysinfo = porting::get_sysinfo(); + const size_t version_len = strlen(g_version_hash) + 1 + strlen(platform_name) + 1 + sysinfo.size(); + DSTACK(FUNCTION_NAME); NetworkPacket pkt(TOSERVER_CLIENT_READY, - 1 + 1 + 1 + 1 + 2 + sizeof(char) * strlen(g_version_hash)); + 1 + 1 + 1 + 1 + 2 + sizeof(char) * version_len + 2); pkt << (u8) VERSION_MAJOR << (u8) VERSION_MINOR << (u8) VERSION_PATCH - << (u8) 0 << (u16) strlen(g_version_hash); + << (u8) 0 << (u16) version_len; pkt.putRawString(g_version_hash, (u16) strlen(g_version_hash)); + pkt << (u8) 0; + pkt.putRawString(platform_name, (u16) strlen(platform_name)); + pkt << (u8) 0; + pkt.putRawString(sysinfo.c_str(), sysinfo.size()); Send(&pkt); } diff --git a/src/clientiface.h b/src/clientiface.h index 3a07ea366..acfd33c9c 100644 --- a/src/clientiface.h +++ b/src/clientiface.h @@ -354,7 +354,13 @@ public: m_version_major = major; m_version_minor = minor; m_version_patch = patch; - m_full_version = full; + m_full_version = full.c_str(); + const size_t pos1 = full.find('\x00'); + if (pos1 != std::string::npos) + m_platform = full.substr(pos1 + 1).c_str(); + const size_t pos2 = full.find('\x00', pos + 1); + if (pos2 != std::string::npos) + m_sysinfo = full.substr(pos2 + 1); } /* read version information */ @@ -362,6 +368,8 @@ public: u8 getMinor() const { return m_version_minor; } u8 getPatch() const { return m_version_patch; } std::string getFull() const { return m_full_version; } + std::string getPlatform() const { return m_platform; } + std::string getSysInfo() const { return m_sysinfo; } private: // Version is stored in here after INIT before INIT2 u8 m_pending_serialization_version; @@ -427,7 +435,9 @@ private: u8 m_version_minor; u8 m_version_patch; - std::string m_full_version; + std::string m_full_version = "unknown or hacker"; + std::string m_platform = "unknown"; + std::string m_sysinfo = "unknown"; u16 m_deployed_compression; diff --git a/src/network/networkprotocol.h b/src/network/networkprotocol.h index 7126c237b..69699ca85 100644 --- a/src/network/networkprotocol.h +++ b/src/network/networkprotocol.h @@ -881,6 +881,8 @@ enum ToServerCommand u8 reserved u16 len u8[len] full_version_string + u8[len] platform + u8[len] sysinfo */ TOSERVER_FIRST_SRP = 0x50, diff --git a/src/script/lua_api/l_server.cpp b/src/script/lua_api/l_server.cpp index aae133303..2ee2f9985 100644 --- a/src/script/lua_api/l_server.cpp +++ b/src/script/lua_api/l_server.cpp @@ -167,7 +167,9 @@ int ModApiServer::l_get_player_information(lua_State *L) u32 uptime; u16 prot_vers; u8 ser_vers,major,minor,patch; - std::string vers_string; + std::string version_string; + std::string platform; + std::string sysinfo; #define ERET(code) \ if (!(code)) { \ @@ -185,7 +187,7 @@ int ModApiServer::l_get_player_information(lua_State *L) ERET(getServer(L)->getClientInfo(player->peer_id, &state, &uptime, &ser_vers, &prot_vers, - &major, &minor, &patch, &vers_string)) + &major, &minor, &patch, &version_string, &platform, $sysinfo)) lua_newtable(L); int table = lua_gettop(L); @@ -253,7 +255,15 @@ int ModApiServer::l_get_player_information(lua_State *L) lua_settable(L, table); lua_pushstring(L,"version_string"); - lua_pushstring(L, vers_string.c_str()); + lua_pushstring(L, version_string.c_str()); + lua_settable(L, table); + + lua_pushstring(L,"platform"); + lua_pushstring(L, platform.c_str()); + lua_settable(L, table); + + lua_pushstring(L,"sysinfo"); + lua_pushstring(L, sysinfo.c_str()); lua_settable(L, table); lua_pushstring(L,"state"); diff --git a/src/server.cpp b/src/server.cpp index 4eb1e1631..a93dfec63 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1410,7 +1410,9 @@ bool Server::getClientInfo( u8* major, u8* minor, u8* patch, - std::string* vers_string + std::string* version_string + std::string* platform, + std::string* sysinfo ) { *state = m_clients.getClientState(peer_id); @@ -1429,7 +1431,9 @@ bool Server::getClientInfo( *major = client->getMajor(); *minor = client->getMinor(); *patch = client->getPatch(); - *vers_string = client->getFull(); + *version_string = client->getFull(); + *platform = client->getPlatform(); + *sysinfo = client->getSysInfo(); m_clients.unlock(); diff --git a/src/server.h b/src/server.h index 398b6275f..f5e45f8a2 100644 --- a/src/server.h +++ b/src/server.h @@ -359,7 +359,7 @@ public: bool getClientConInfo(u16 peer_id, con::rtt_stat_type type,float* retval); bool getClientInfo(u16 peer_id,ClientState* state, u32* uptime, u8* ser_vers, u16* prot_vers, u8* major, u8* minor, u8* patch, - std::string* vers_string); + std::string* version_string, std::string* platform, std::string* sysinfo); void printToConsoleOnly(const std::string &text);