From deb249469278131c9268d679b7d47d89b7bee053 Mon Sep 17 00:00:00 2001 From: def Date: Wed, 27 Feb 2019 20:24:31 +0100 Subject: [PATCH] Don't suggest to downgrade, clean up versioning (fixes #1440) --- CMakeLists.txt | 2 +- src/base/system.c | 11 +++++++++++ src/base/system.h | 1 + src/engine/client/client.cpp | 33 ++++++++++++++++++++++++++++++--- src/engine/client/http.cpp | 2 +- src/engine/client/updater.cpp | 2 +- src/engine/server/server.cpp | 2 +- src/engine/shared/netban.cpp | 14 +------------- src/engine/shared/netban.h | 3 --- src/game/version.h | 3 +-- 10 files changed, 48 insertions(+), 25 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b92102d33..66c9efcc7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8) file(STRINGS src/game/version.h VERSION_LINE LIMIT_COUNT 1 - REGEX GAME_RELEASE_VERSION + REGEX "^#define GAME_VERSION " ) if(VERSION_LINE MATCHES "\"([0-9]+)\\.([0-9]+)\\.([0-9]+)\"") diff --git a/src/base/system.c b/src/base/system.c index 58e6c4005..33f62aaa0 100644 --- a/src/base/system.c +++ b/src/base/system.c @@ -2717,6 +2717,17 @@ char str_uppercase(char c) return c; } +int str_isallnum(const char *str) +{ + while(*str) + { + if(!(*str >= '0' && *str <= '9')) + return 0; + str++; + } + return 1; +} + int str_toint(const char *str) { return atoi(str); } int str_toint_base(const char *str, int base) { return strtol(str, NULL, base); } float str_tofloat(const char *str) { return atof(str); } diff --git a/src/base/system.h b/src/base/system.h index e6765bc01..2cbc011ef 100644 --- a/src/base/system.h +++ b/src/base/system.h @@ -1612,6 +1612,7 @@ int str_toint_base(const char *str, int base); float str_tofloat(const char *str); int str_isspace(char c); char str_uppercase(char c); +int str_isallnum(const char *str); unsigned str_quickhash(const char *str); struct SKELETON; diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index 2c527ad44..9417678f1 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -2259,6 +2260,29 @@ void CClient::FinishDDNetInfo() LoadDDNetInfo(); } +typedef std::tuple Version; +static const Version InvalidVersion = std::make_tuple(-1, -1, -1); + +Version ToVersion(char *pStr) +{ + int version[3] = {0, 0, 0}; + const char *p = strtok(pStr, "."); + + for(int i = 0; i < 3 && p; ++i) + { + if(!str_isallnum(p)) + return InvalidVersion; + + version[i] = str_toint(p); + p = strtok(NULL, "."); + } + + if(p) + return InvalidVersion; + + return std::make_tuple(version[0], version[1], version[2]); +} + void CClient::LoadDDNetInfo() { const json_value *pDDNetInfo = m_ServerBrowser.LoadDDNetInfo(); @@ -2269,10 +2293,13 @@ void CClient::LoadDDNetInfo() const json_value *pVersion = json_object_get(pDDNetInfo, "version"); if(pVersion->type == json_string) { - const char *pVersionString = json_string_get(pVersion); - if(str_comp(pVersionString, GAME_RELEASE_VERSION)) + char aNewVersionStr[64]; + str_copy(aNewVersionStr, json_string_get(pVersion), sizeof(aNewVersionStr)); + char aCurVersionStr[64]; + str_copy(aCurVersionStr, GAME_VERSION, sizeof(aCurVersionStr)); + if(ToVersion(aNewVersionStr) > ToVersion(aCurVersionStr)) { - str_copy(m_aVersionStr, pVersionString, sizeof(m_aVersionStr)); + str_copy(m_aVersionStr, json_string_get(pVersion), sizeof(m_aVersionStr)); } else { diff --git a/src/engine/client/http.cpp b/src/engine/client/http.cpp index 188864837..9aa1a40c3 100644 --- a/src/engine/client/http.cpp +++ b/src/engine/client/http.cpp @@ -122,7 +122,7 @@ void CRequest::Run() curl_easy_setopt(pHandle, CURLOPT_FAILONERROR, 1L); curl_easy_setopt(pHandle, CURLOPT_URL, m_aUrl); curl_easy_setopt(pHandle, CURLOPT_NOSIGNAL, 1L); - curl_easy_setopt(pHandle, CURLOPT_USERAGENT, "DDNet " GAME_RELEASE_VERSION " (" CONF_PLATFORM_STRING "; " CONF_ARCH_STRING ")"); + curl_easy_setopt(pHandle, CURLOPT_USERAGENT, "DDNet " GAME_VERSION " (" CONF_PLATFORM_STRING "; " CONF_ARCH_STRING ")"); // We only trust our own custom-selected CAs for our own servers. // Other servers can use any CA trusted by the system. diff --git a/src/engine/client/updater.cpp b/src/engine/client/updater.cpp index 68ccd8cb3..affe418a2 100644 --- a/src/engine/client/updater.cpp +++ b/src/engine/client/updater.cpp @@ -233,7 +233,7 @@ void CUpdater::ParseUpdate() { const json_value *pTemp; const json_value *pCurrent = json_array_get(pVersions, i); - if(str_comp(json_string_get(json_object_get(pCurrent, "version")), GAME_RELEASE_VERSION)) + if(str_comp(json_string_get(json_object_get(pCurrent, "version")), GAME_VERSION)) { if(json_boolean_get(json_object_get(pCurrent, "client"))) m_ClientUpdate = true; diff --git a/src/engine/server/server.cpp b/src/engine/server/server.cpp index 1fc79cf72..d028aa6c6 100644 --- a/src/engine/server/server.cpp +++ b/src/engine/server/server.cpp @@ -229,7 +229,7 @@ void CServerBan::ConBanExt(IConsole::IResult *pResult, void *pUser) int Minutes = pResult->NumArguments()>1 ? clamp(pResult->GetInteger(1), 0, 44640) : 30; const char *pReason = pResult->NumArguments()>2 ? pResult->GetString(2) : "No reason given"; - if(StrAllnum(pStr)) + if(str_isallnum(pStr)) { int ClientID = str_toint(pStr); if(ClientID < 0 || ClientID >= MAX_CLIENTS || pThis->Server()->m_aClients[ClientID].m_State == CServer::CClient::STATE_EMPTY) diff --git a/src/engine/shared/netban.cpp b/src/engine/shared/netban.cpp index 80e6c57cd..23b3f27b9 100644 --- a/src/engine/shared/netban.cpp +++ b/src/engine/shared/netban.cpp @@ -7,18 +7,6 @@ #include "netban.h" -bool CNetBan::StrAllnum(const char *pStr) -{ - while(*pStr) - { - if(!(*pStr >= '0' && *pStr <= '9')) - return false; - pStr++; - } - return true; -} - - CNetBan::CNetHash::CNetHash(const NETADDR *pAddr) { if(pAddr->type==NETTYPE_IPV4) @@ -478,7 +466,7 @@ void CNetBan::ConUnban(IConsole::IResult *pResult, void *pUser) CNetBan *pThis = static_cast(pUser); const char *pStr = pResult->GetString(0); - if(StrAllnum(pStr)) + if(str_isallnum(pStr)) pThis->UnbanByIndex(str_toint(pStr)); else { diff --git a/src/engine/shared/netban.h b/src/engine/shared/netban.h index 11aed131f..4df3b3e7f 100644 --- a/src/engine/shared/netban.h +++ b/src/engine/shared/netban.h @@ -58,9 +58,6 @@ protected: return pBuffer; } - // todo: move? - static bool StrAllnum(const char *pStr); - class CNetHash { public: diff --git a/src/game/version.h b/src/game/version.h index 043071ba8..310abaf21 100644 --- a/src/game/version.h +++ b/src/game/version.h @@ -2,9 +2,8 @@ /* If you are missing that file, acquire a complete release at teeworlds.com. */ #ifndef GAME_VERSION_H #define GAME_VERSION_H -#define GAME_VERSION "0.6.4, 11.8" #define GAME_NETVERSION "0.6 626fce9a778df4d4" -#define GAME_RELEASE_VERSION "11.8" +#define GAME_VERSION "11.8" #define CLIENT_VERSIONNR 11080 extern const char *GIT_SHORTREV_HASH; #endif