From 88b33367581a888d5532786b70cb323e1dfe6baa Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Thu, 11 Aug 2022 11:07:45 +0200 Subject: [PATCH] Many small code improvements and optimizations * Use c++ methods as much as possible. * Use enum types for accessing NavMesh objects from array. * Use size_t for for loops when testing against size types. * Don't compute strlen twice of more on the same string. * Don't use unnecessary c string casts if there is a method with a std::string overload. * Don't create string objects from string pointers if we could use them directly. * Don't initialize RCON password twice on each change, and don't set if the new password equals the old. --- r5dev/core/dllmain.cpp | 4 +- r5dev/core/logdef.cpp | 4 +- r5dev/datacache/mdlcache.cpp | 4 +- r5dev/engine/client/cl_rcon.cpp | 7 +- r5dev/engine/cmodel_bsp.cpp | 2 +- r5dev/engine/net_chan.cpp | 22 +++-- r5dev/engine/net_chan.h | 5 +- r5dev/engine/server/sv_rcon.cpp | 17 +++- r5dev/engine/server/sv_rcon.h | 2 + r5dev/game/server/ai_networkmanager.cpp | 10 +-- r5dev/gameui/IConsole.cpp | 4 +- r5dev/launcher/IApplication.cpp | 2 +- r5dev/launcher/launcher.cpp | 2 +- r5dev/materialsystem/cmaterialsystem.cpp | 8 +- r5dev/materialsystem/cmaterialsystem.h | 2 + r5dev/mathlib/sha1.cpp | 2 +- r5dev/netconsole/netconsole.cpp | 2 +- r5dev/networksystem/bansystem.cpp | 2 +- r5dev/public/utility/memaddr.cpp | 4 +- r5dev/rtech/rtech_utils.cpp | 4 +- r5dev/rtech/rtech_utils.h | 2 + r5dev/sdklauncher/basepanel.cpp | 2 +- r5dev/tier1/NetAdr2.cpp | 34 +++---- r5dev/tier1/NetAdr2.h | 18 ++-- r5dev/vstdlib/callback.cpp | 107 ++++++++++------------- r5dev/windows/console.cpp | 2 +- 26 files changed, 146 insertions(+), 128 deletions(-) diff --git a/r5dev/core/dllmain.cpp b/r5dev/core/dllmain.cpp index 1072cee0..7c42baea 100644 --- a/r5dev/core/dllmain.cpp +++ b/r5dev/core/dllmain.cpp @@ -26,7 +26,7 @@ void R5Dev_Init() g_svCmdLine = LoadConfigFile(SDK_DEFAULT_CFG); } #ifndef DEDICATED - if (strstr(g_svCmdLine.c_str(), "-wconsole")) + if (g_svCmdLine.find("-wconsole") != std::string::npos) { Console_Init(); } @@ -38,7 +38,7 @@ void R5Dev_Init() for (size_t i = 0; i < SDK_ARRAYSIZE(R5R_EMBLEM); i++) { std::string svEscaped = StringEscape(R5R_EMBLEM[i]); - spdlog::info("{:s}{:s}{:s}\n", g_svRedF.c_str(), svEscaped.c_str(), g_svReset.c_str()); + spdlog::info("{:s}{:s}{:s}\n", g_svRedF, svEscaped, g_svReset); } spdlog::info("\n"); diff --git a/r5dev/core/logdef.cpp b/r5dev/core/logdef.cpp index 9246affe..55b62c38 100644 --- a/r5dev/core/logdef.cpp +++ b/r5dev/core/logdef.cpp @@ -31,7 +31,7 @@ void SpdLog_Init(void) auto wconsole = spdlog::stdout_logger_mt("win_console"); // Determine if user wants ansi-color logging in the terminal. - if (strstr(g_svCmdLine.c_str(), "-ansiclr")) + if (g_svCmdLine.find("-ansiclr") != string::npos) { wconsole->set_pattern("[0.000] %v\u001b[0m"); g_bSpdLog_UseAnsiClr = true; @@ -71,7 +71,7 @@ void SpdLog_PostInit() iconsole->set_pattern("%v"); - if (strstr(g_svCmdLine.c_str(), "-ansiclr")) + if (g_svCmdLine.find("-ansiclr") != string::npos) { wconsole->set_pattern("%v\u001b[0m"); g_bSpdLog_UseAnsiClr = true; diff --git a/r5dev/datacache/mdlcache.cpp b/r5dev/datacache/mdlcache.cpp index d772b21f..27a13396 100644 --- a/r5dev/datacache/mdlcache.cpp +++ b/r5dev/datacache/mdlcache.cpp @@ -39,12 +39,12 @@ studiohdr_t* CMDLCache::FindMDL(CMDLCache* cache, MDLHandle_t handle, void* a3) if (pStudioHDR) { string svStudio = ConvertToUnixPath(string(pStudioHDR->name)); - if (svStudio.compare(ERROR_MODEL) == 0) + if (svStudio.compare(ERROR_MODEL) == NULL) { g_pMDLFallback->m_pErrorHDR = pStudioHDR; g_pMDLFallback->m_hErrorMDL = handle; } - if (svStudio.compare(EMPTY_MODEL) == 0) + if (svStudio.compare(EMPTY_MODEL) == NULL) { g_pMDLFallback->m_pEmptyHDR = pStudioHDR; g_pMDLFallback->m_hEmptyMDL = handle; diff --git a/r5dev/engine/client/cl_rcon.cpp b/r5dev/engine/client/cl_rcon.cpp index 12fdb2f9..67c860e6 100644 --- a/r5dev/engine/client/cl_rcon.cpp +++ b/r5dev/engine/client/cl_rcon.cpp @@ -66,9 +66,10 @@ void CRConClient::Shutdown(void) //----------------------------------------------------------------------------- bool CRConClient::SetPassword(const char* pszPassword) { - if (std::strlen(pszPassword) < 8) + size_t nLen = std::strlen(pszPassword); + if (nLen < 8) { - if (std::strlen(pszPassword) > 0) + if (nLen > 0) { DevMsg(eDLL_T::CLIENT, "Remote server access requires a password of at least 8 characters\n"); } @@ -119,7 +120,7 @@ bool CRConClient::Connect(void) //----------------------------------------------------------------------------- bool CRConClient::Connect(const std::string& svInAdr, const std::string& svInPort) { - if (svInAdr.size() > 0 && svInPort.size() > 0) + if (!svInAdr.empty() && !svInPort.empty()) { // Default is [127.0.0.1]:37015 m_pNetAdr2->SetIPAndPort(svInAdr, svInPort); diff --git a/r5dev/engine/cmodel_bsp.cpp b/r5dev/engine/cmodel_bsp.cpp index ad8287ce..c8181b02 100644 --- a/r5dev/engine/cmodel_bsp.cpp +++ b/r5dev/engine/cmodel_bsp.cpp @@ -44,7 +44,7 @@ void MOD_GetAllInstalledMaps() std::string svFileName = dEntry.path().u8string(); std::regex_search(svFileName, smRegexMatches, rgArchiveRegex); - if (smRegexMatches.size() > 0) + if (!smRegexMatches.empty()) { if (smRegexMatches[1].str().compare("frontend") == 0) { diff --git a/r5dev/engine/net_chan.cpp b/r5dev/engine/net_chan.cpp index f034598e..9467d2c6 100644 --- a/r5dev/engine/net_chan.cpp +++ b/r5dev/engine/net_chan.cpp @@ -13,25 +13,31 @@ // Purpose: gets the netchannel name // Output : const char* //----------------------------------------------------------------------------- -string CNetChan::GetName(void) const +const char* CNetChan::GetName(void) const { - // [0x1A8D + 0x1] (first char in array is a null character!). - const char* pszName = this->m_Name + 1; - return string(pszName, NET_CHANNELNAME_MAXLEN); + return this->m_Name; } //----------------------------------------------------------------------------- // Purpose: gets the netchannel address // Output : const char* //----------------------------------------------------------------------------- -string CNetChan::GetAddress(void) const +const char* CNetChan::GetAddress(void) const { - char szAdr[INET6_ADDRSTRLEN]{}; - if (!inet_ntop(AF_INET6, &this->remote_address.adr, szAdr, INET6_ADDRSTRLEN)) + // Select a static buffer + static char s[4][INET6_ADDRSTRLEN]; + static int slot = 0; + int useSlot = (slot++) % 4; + + // Render into it + + if (!inet_ntop(AF_INET6, &this->remote_address.adr, s[useSlot], sizeof(s[0]))) { Warning(eDLL_T::ENGINE, "%s - Address conversion failed: %s", __FUNCTION__, NET_ErrorString(WSAGetLastError())); } - return szAdr; + + // Pray the caller uses it before it gets clobbered + return s[useSlot]; } //----------------------------------------------------------------------------- diff --git a/r5dev/engine/net_chan.h b/r5dev/engine/net_chan.h index fbf6676a..f5f70513 100644 --- a/r5dev/engine/net_chan.h +++ b/r5dev/engine/net_chan.h @@ -79,8 +79,8 @@ enum EBufType class CNetChan { public: - string GetName(void) const; - string GetAddress(void) const; + const char* GetName(void) const; + const char* GetAddress(void) const; int GetDataRate(void) const; int GetBufferSize(void) const; @@ -162,6 +162,7 @@ private: int m_nSequencesSkipped_MAYBE; int m_nSessionRecvs; uint32_t m_nLiftimeRecvs; + bool m_bPad; char m_Name[NET_CHANNELNAME_MAXLEN]; uint8_t m_bRetrySendLong; v_netadr_t remote_address; diff --git a/r5dev/engine/server/sv_rcon.cpp b/r5dev/engine/server/sv_rcon.cpp index 04999cee..a0516b58 100644 --- a/r5dev/engine/server/sv_rcon.cpp +++ b/r5dev/engine/server/sv_rcon.cpp @@ -114,15 +114,18 @@ bool CRConServer::SetPassword(const char* pszPassword) m_bInitialized = false; m_pSocket->CloseAllAcceptedSockets(); - if (std::strlen(pszPassword) < 8) + size_t nLen = std::strlen(pszPassword); + if (nLen < 8) { - if (std::strlen(pszPassword) > 0) + if (nLen > 0) { Warning(eDLL_T::SERVER, "Remote server access requires a password of at least 8 characters\n"); } + this->Shutdown(); return false; } + m_svPasswordHash = sha256(pszPassword); DevMsg(eDLL_T::SERVER, "Password hash ('%s')\n", m_svPasswordHash.c_str()); @@ -569,6 +572,16 @@ void CRConServer::CloseNonAuthConnection(void) } } } + +//----------------------------------------------------------------------------- +// Purpose: checks if server rcon is initialized +// Output : true if initialized, false otherwise +//----------------------------------------------------------------------------- +bool CRConServer::IsInitialized(void) const +{ + return m_bInitialized; +} + /////////////////////////////////////////////////////////////////////////////// CRConServer* g_pRConServer = new CRConServer(); CRConServer* RCONServer() diff --git a/r5dev/engine/server/sv_rcon.h b/r5dev/engine/server/sv_rcon.h index 1274e652..56027224 100644 --- a/r5dev/engine/server/sv_rcon.h +++ b/r5dev/engine/server/sv_rcon.h @@ -41,6 +41,8 @@ public: void CloseConnection(void); void CloseNonAuthConnection(void); + bool IsInitialized(void) const; + private: bool m_bInitialized; diff --git a/r5dev/game/server/ai_networkmanager.cpp b/r5dev/game/server/ai_networkmanager.cpp index 6a1d8d00..c2e83afa 100644 --- a/r5dev/game/server/ai_networkmanager.cpp +++ b/r5dev/game/server/ai_networkmanager.cpp @@ -34,7 +34,7 @@ void CAI_NetworkBuilder::SaveNetworkGraph(CAI_Network* pNetwork) const string svMeshDir = "maps\\navmesh\\"; const string svGraphDir = "maps\\graphs\\"; - fs::path fsMeshPath(svMeshDir + g_pHostState->m_levelName + "_" + SHULL_SIZE[3] + ".nm"); + fs::path fsMeshPath(svMeshDir + g_pHostState->m_levelName + "_" + SHULL_SIZE[EHULL_SIZE::LARGE] + ".nm"); fs::path fsGraphPath(svGraphDir + g_pHostState->m_levelName + ".ain"); CFastTimer masterTimer; @@ -69,7 +69,7 @@ void CAI_NetworkBuilder::SaveNetworkGraph(CAI_Network* pNetwork) } else { - Warning(eDLL_T::SERVER, "%s - No %s NavMesh found. Unable to calculate CRC for AI Network.\n", __FUNCTION__, SHULL_SIZE[3].c_str()); + Warning(eDLL_T::SERVER, "%s - No %s NavMesh found. Unable to calculate CRC for AI Network.\n", __FUNCTION__, SHULL_SIZE[EHULL_SIZE::LARGE].c_str()); } // Large NavMesh CRC. @@ -169,7 +169,7 @@ void CAI_NetworkBuilder::SaveNetworkGraph(CAI_Network* pNetwork) // Don't know what this is, it's likely a block from tf1 that got deprecated? should just be 1 int per node. DevMsg(eDLL_T::SERVER, " |-- Writing '%d' bytes for node block at '0x%zX'\n", pNetwork->m_iNumNodes * sizeof(uint32_t), writer.GetPosition()); - if (static_cast(pNetwork->m_iNumNodes) > 0) + if (pNetwork->m_iNumNodes > 0) { uint32_t* unkNodeBlock = new uint32_t[pNetwork->m_iNumNodes]; memset(&unkNodeBlock, '\0', pNetwork->m_iNumNodes * sizeof(uint32_t)); @@ -179,7 +179,7 @@ void CAI_NetworkBuilder::SaveNetworkGraph(CAI_Network* pNetwork) // TODO: This is traverse nodes i think? these aren't used in r2 ains so we can get away with just writing count=0 and skipping // but ideally should actually dump these. - DevMsg(eDLL_T::SERVER, " |-- Writing '%d' traversal nodes at '0x%zX'\n", 0, static_cast(writer.GetPosition())); + DevMsg(eDLL_T::SERVER, " |-- Writing '%d' traversal nodes at '0x%zX'\n", 0, writer.GetPosition()); short traverseNodeCount = 0; // Only write count since count=0 means we don't have to actually do anything here. writer.Write(&traverseNodeCount, sizeof(short)); @@ -293,7 +293,7 @@ void CAI_NetworkManager::LoadNetworkGraph(CAI_NetworkManager* pAINetworkManager, string svMeshDir = "maps\\navmesh\\"; string svGraphDir = "maps\\graphs\\"; - fs::path fsMeshPath(svMeshDir + g_pHostState->m_levelName + "_" + SHULL_SIZE[3] + ".nm"); + fs::path fsMeshPath(svMeshDir + g_pHostState->m_levelName + "_" + SHULL_SIZE[EHULL_SIZE::LARGE] + ".nm"); fs::path fsGraphPath(svGraphDir + g_pHostState->m_levelName + ".ain"); int nAiNetVersion = NULL; diff --git a/r5dev/gameui/IConsole.cpp b/r5dev/gameui/IConsole.cpp index 665cab4e..de60a55c 100644 --- a/r5dev/gameui/IConsole.cpp +++ b/r5dev/gameui/IConsole.cpp @@ -249,7 +249,7 @@ void CConsole::BasePanel(void) { // Remove the default value from ConVar before assigning it to the input buffer. string svConVar = m_vSuggest[m_nSuggestPos].m_svName.substr(0, m_vSuggest[m_nSuggestPos].m_svName.find(' ')) + ' '; - memmove(m_szInputBuf, svConVar.c_str(), svConVar.size() + 1); + memmove(m_szInputBuf, svConVar.data(), svConVar.size() + 1); ResetAutoComplete(); BuildSummary(svConVar); @@ -366,7 +366,7 @@ void CConsole::SuggestPanel(void) // Remove the default value from ConVar before assigning it to the input buffer. string svConVar = m_vSuggest[i].m_svName.substr(0, m_vSuggest[i].m_svName.find(' ')) + ' '; - memmove(m_szInputBuf, svConVar.c_str(), svConVar.size() + 1); + memmove(m_szInputBuf, svConVar.data(), svConVar.size() + 1); ResetAutoComplete(); BuildSummary(svConVar); diff --git a/r5dev/launcher/IApplication.cpp b/r5dev/launcher/IApplication.cpp index 79c7c7a9..1e0d2736 100644 --- a/r5dev/launcher/IApplication.cpp +++ b/r5dev/launcher/IApplication.cpp @@ -64,7 +64,7 @@ bool CModAppSystemGroup::Create(CModAppSystemGroup* pModAppSystemGroup) for (auto& map : g_pCVar->DumpToMap()) { g_pConsole->m_vsvCommandBases.push_back( - CSuggest(map.first.c_str(), map.second->GetFlags())); + CSuggest(map.first, map.second->GetFlags())); } #endif // !DEDICATED if (pModAppSystemGroup->IsServerOnly()) diff --git a/r5dev/launcher/launcher.cpp b/r5dev/launcher/launcher.cpp index 216b9d0e..fceb4b53 100644 --- a/r5dev/launcher/launcher.cpp +++ b/r5dev/launcher/launcher.cpp @@ -125,7 +125,7 @@ void ParseAndApplyConfigFile(const string& svConfig) stringstream ss(svConfig); string svInput; - if (strlen(svConfig.c_str()) > 0) + if (!svConfig.empty()) { while (std::getline(ss, svInput, '\n')) { diff --git a/r5dev/materialsystem/cmaterialsystem.cpp b/r5dev/materialsystem/cmaterialsystem.cpp index 8618d7b9..a08f4011 100644 --- a/r5dev/materialsystem/cmaterialsystem.cpp +++ b/r5dev/materialsystem/cmaterialsystem.cpp @@ -29,10 +29,10 @@ void StreamDB_Init(const char* pszLevelName) if (!jsIn.is_null()) { - if (!jsIn["stbsp"].is_null()) + if (!jsIn[STREAM_DB_EXT].is_null()) { - string svStreamDBFile = jsIn["stbsp"].get(); - DevMsg(eDLL_T::MS, "%s: Loading override STBSP file '%s.stbsp'\n", __FUNCTION__, svStreamDBFile.c_str()); + string svStreamDBFile = jsIn[STREAM_DB_EXT].get(); + DevMsg(eDLL_T::MS, "%s: Loading override STBSP file '%s.%s'\n", __FUNCTION__, svStreamDBFile.c_str(), STREAM_DB_EXT); v_StreamDB_Init(svStreamDBFile.c_str()); return; } @@ -43,7 +43,7 @@ void StreamDB_Init(const char* pszLevelName) Warning(eDLL_T::MS, "%s: Exception while parsing STBSP override: '%s'\n", __FUNCTION__, ex.what()); } } - DevMsg(eDLL_T::MS, "%s: Loading STBSP file '%s.stbsp'\n", __FUNCTION__, pszLevelName); + DevMsg(eDLL_T::MS, "%s: Loading STBSP file '%s.%s'\n", __FUNCTION__, pszLevelName, STREAM_DB_EXT); v_StreamDB_Init(pszLevelName); } diff --git a/r5dev/materialsystem/cmaterialsystem.h b/r5dev/materialsystem/cmaterialsystem.h index 522eef78..0129f5e7 100644 --- a/r5dev/materialsystem/cmaterialsystem.h +++ b/r5dev/materialsystem/cmaterialsystem.h @@ -1,5 +1,7 @@ #pragma once +#define STREAM_DB_EXT "stbsp" + /* ==== MATERIALSYSTEM ================================================================================================================================================== */ inline CMemory p_CMaterialSystem__Init; inline auto CMaterialSystem__Init = p_CMaterialSystem__Init.RCast(); diff --git a/r5dev/mathlib/sha1.cpp b/r5dev/mathlib/sha1.cpp index a8c871a7..e907c806 100644 --- a/r5dev/mathlib/sha1.cpp +++ b/r5dev/mathlib/sha1.cpp @@ -109,7 +109,7 @@ std::string SHA1::final() std::string SHA1::from_file(const std::string &filename) { - std::ifstream stream(filename.c_str(), std::ios::binary); + std::ifstream stream(filename, std::ios::binary); SHA1 checksum; checksum.update(stream); return checksum.final(); diff --git a/r5dev/netconsole/netconsole.cpp b/r5dev/netconsole/netconsole.cpp index 225244b6..5528d735 100644 --- a/r5dev/netconsole/netconsole.cpp +++ b/r5dev/netconsole/netconsole.cpp @@ -234,7 +234,7 @@ bool CNetCon::ShouldQuit(void) const //----------------------------------------------------------------------------- bool CNetCon::Connect(const std::string& svInAdr, const std::string& svInPort) { - if (svInAdr.size() > 0 && svInPort.size() > 0) + if (!svInAdr.empty() && !svInPort.empty()) { // Default is [127.0.0.1]:37015 m_pNetAdr2->SetIPAndPort(svInAdr, svInPort); diff --git a/r5dev/networksystem/bansystem.cpp b/r5dev/networksystem/bansystem.cpp index 3ac517c8..edb88b74 100644 --- a/r5dev/networksystem/bansystem.cpp +++ b/r5dev/networksystem/bansystem.cpp @@ -186,7 +186,7 @@ void CBanSystem::BanListCheck(void) Warning(eDLL_T::SERVER, "Connection rejected for '%s' ('%llu' is banned from this server!)\n", svIpAddress.c_str(), pClient->GetOriginID()); AddEntry(svIpAddress, pClient->GetOriginID()); Save(); // Save banlist to file. - NET_DisconnectClient(pClient, c, m_vRefuseList[i].first.c_str(), false, true); + NET_DisconnectClient(pClient, c, m_vRefuseList[i].first.c_str(), 0, true); } } } diff --git a/r5dev/public/utility/memaddr.cpp b/r5dev/public/utility/memaddr.cpp index 92210585..ef201578 100644 --- a/r5dev/public/utility/memaddr.cpp +++ b/r5dev/public/utility/memaddr.cpp @@ -40,7 +40,7 @@ void CMemory::Patch(const vector vOpcodeArray) const SIZE_T dwSize = vOpcodeArray.size(); VirtualProtect(reinterpret_cast(ptr), dwSize, PAGE_EXECUTE_READWRITE, &oldProt); // Patch page to be able to read and write to it. - for (int i = 0; i < vOpcodeArray.size(); i++) + for (size_t i = 0; i < vOpcodeArray.size(); i++) { *reinterpret_cast(ptr + i) = vOpcodeArray[i]; // Write opcodes to Address. } @@ -62,7 +62,7 @@ void CMemory::PatchString(const string& svString) const VirtualProtect(reinterpret_cast(ptr), dwSize, PAGE_EXECUTE_READWRITE, &oldProt); // Patch page to be able to read and write to it. - for (int i = 0; i < svString.size(); i++) + for (size_t i = 0; i < svString.size(); i++) { *reinterpret_cast(ptr + i) = bytes[i]; // Write string to Address. } diff --git a/r5dev/rtech/rtech_utils.cpp b/r5dev/rtech/rtech_utils.cpp index bc2c4204..dbe25597 100644 --- a/r5dev/rtech/rtech_utils.cpp +++ b/r5dev/rtech/rtech_utils.cpp @@ -646,7 +646,7 @@ int32_t RTech::OpenFile(const char* szFilePath, void* unused, int64_t* fileSizeO //----------------------------------------------------------------------------- RPakLoadedInfo_t* RTech::GetPakLoadedInfo(int nPakId) { - for (int i = 0; i < *s_pLoadedPakCount; ++i) + for (int16_t i = 0; i < *s_pLoadedPakCount; ++i) { RPakLoadedInfo_t* info = &g_pLoadedPakInfo[i]; if (!info) @@ -667,7 +667,7 @@ RPakLoadedInfo_t* RTech::GetPakLoadedInfo(int nPakId) //----------------------------------------------------------------------------- RPakLoadedInfo_t* RTech::GetPakLoadedInfo(const char* szPakName) { - for (int i = 0; i < *s_pLoadedPakCount; ++i) + for (int16_t i = 0; i < *s_pLoadedPakCount; ++i) { RPakLoadedInfo_t* info = &g_pLoadedPakInfo[i]; if (!info) diff --git a/r5dev/rtech/rtech_utils.h b/r5dev/rtech/rtech_utils.h index 98e7443f..7670954e 100644 --- a/r5dev/rtech/rtech_utils.h +++ b/r5dev/rtech/rtech_utils.h @@ -3,6 +3,8 @@ #define PAK_PARAM_SIZE 0xB0 #define DCMP_BUF_SIZE 0x400000 +#define RPAKHEADER (('k'<<24)+('a'<<16)+('P'<<8)+'R') + /*unk_141313180*/ // LUT_0 redacted now, split LUT into multiple parts. #pragma warning( push ) diff --git a/r5dev/sdklauncher/basepanel.cpp b/r5dev/sdklauncher/basepanel.cpp index ff9a2c54..f8db9498 100644 --- a/r5dev/sdklauncher/basepanel.cpp +++ b/r5dev/sdklauncher/basepanel.cpp @@ -555,7 +555,7 @@ void CUIBaseSurface::ParseMaps() std::string svFileName = dEntry.path().string(); std::regex_search(svFileName, smRegexMatches, rgArchiveRegex); - if (smRegexMatches.size() > 0) + if (!smRegexMatches.empty()) { if (smRegexMatches[1].str().compare("frontend") == 0) { diff --git a/r5dev/tier1/NetAdr2.cpp b/r5dev/tier1/NetAdr2.cpp index 1b15923f..e1fb1158 100644 --- a/r5dev/tier1/NetAdr2.cpp +++ b/r5dev/tier1/NetAdr2.cpp @@ -136,7 +136,7 @@ void CNetAdr2::SetVersion(void) { if (inet_pton(reinterpret_cast(&m_sadr)->sin_family, GetBase().c_str(), &reinterpret_cast(m_sadr)->sin_addr) && - !strstr(GetBase().c_str(), "::")) + GetBase().find("::") == string::npos) { m_version = netadrversion_t::NA_V4; return; @@ -203,13 +203,13 @@ string CNetAdr2::GetBase(void) const std::smatch smRegexMatches; std::regex_search(m_svip, smRegexMatches, rx); - if (smRegexMatches.size() > 0) + if (smRegexMatches.empty()) { - return smRegexMatches[1].str(); + return "127.0.0.1"; } else { - return "127.0.0.1"; + return smRegexMatches[1].str(); } } @@ -223,13 +223,13 @@ string CNetAdr2::GetBase(const string& svInAdr) const std::smatch smRegexMatches; std::regex_search(svInAdr, smRegexMatches, rx); - if (smRegexMatches.size() > 0) + if (smRegexMatches.empty()) { - return smRegexMatches[1].str(); + return "127.0.0.1"; } else { - return "127.0.0.1"; + return smRegexMatches[1].str(); } } @@ -498,7 +498,7 @@ bool CNetAdr2::IsValidPort(const string& svInPort) const //----------------------------------------------------------------------------- bool CNetAdr2::IsLocalhost(void) const { - return (strcmp(GetBase().c_str(), "127.0.0.1") == 0); + return (GetBase().compare("127.0.0.1") == NULL); } //----------------------------------------------------------------------------- @@ -506,7 +506,7 @@ bool CNetAdr2::IsLocalhost(void) const //----------------------------------------------------------------------------- bool CNetAdr2::IsLoopback(void) const { - return GetType() == netadrtype_t::NA_LOOPBACK; + return (GetType() == netadrtype_t::NA_LOOPBACK); } //----------------------------------------------------------------------------- @@ -558,12 +558,12 @@ bool CNetAdr2::CompareAdr(const CNetAdr2& netAdr2, bool bBaseOnly) const if (GetType() == netadrtype_t::NA_IP) { if (!bBaseOnly && - (strcmp(netAdr2.GetPort().c_str(), GetPort().c_str()) != 0)) + (netAdr2.GetPort().compare(GetPort()) != NULL)) { return false; } - if (strcmp(netAdr2.GetBase().c_str(), GetBase().c_str()) == 0) + if (netAdr2.GetBase().compare(GetBase()) == NULL) { return true; } @@ -599,8 +599,8 @@ bool CNetAdr2::CompareClassBAdr(const CNetAdr2& netAdr2) const vector v0 = netAdr2.GetParts(); vector v1 = GetParts(); - if (strcmp(v0[0].c_str(), v1[0].c_str()) == 0 && - strcmp(v0[1].c_str(), v1[1].c_str()) == 0) + if (v0[0].compare(v1[0]) == NULL && + v0[1].compare(v1[1]) == NULL) { return true; } @@ -636,9 +636,9 @@ bool CNetAdr2::CompareClassCAdr(const CNetAdr2& netAdr2) const vector v0 = netAdr2.GetParts(); vector v1 = GetParts(); - if (strcmp(v0[0].c_str(), v1[0].c_str()) == 0 && - strcmp(v0[1].c_str(), v1[1].c_str()) == 0 && - strcmp(v0[2].c_str(), v1[2].c_str()) == 0) + if (v0[0].compare(v1[0]) == NULL && + v0[1].compare(v1[1]) == NULL && + v0[2].compare(v1[2]) == NULL) { return true; } @@ -655,5 +655,5 @@ void CNetAdr2::Clear(void) m_svip.clear(); m_type = netadrtype_t::NA_NULL; m_version = netadrversion_t::NA_INVALID; - m_sadr = {}; + m_sadr = nullptr; } diff --git a/r5dev/tier1/NetAdr2.h b/r5dev/tier1/NetAdr2.h index 94bffa5c..0996d66b 100644 --- a/r5dev/tier1/NetAdr2.h +++ b/r5dev/tier1/NetAdr2.h @@ -60,9 +60,9 @@ public: private: string m_svip; - netadrtype_t m_type{}; - netadrversion_t m_version{}; - sockaddr_storage* m_sadr{}; + netadrtype_t m_type; + netadrversion_t m_version; + sockaddr_storage* m_sadr; }; class v_netadr_t // !TODO: Move this to 'NetAdr.h' instead and adjust existing class to new system. @@ -74,10 +74,16 @@ public: } inline string GetAddress(void) const { - char szAdr[INET6_ADDRSTRLEN]{}; - inet_ntop(AF_INET6, &this->adr, szAdr, INET6_ADDRSTRLEN); + // Select a static buffer + static char s[4][INET6_ADDRSTRLEN]; + static int slot = 0; + int useSlot = (slot++) % 4; - return szAdr; + // Render into it + inet_ntop(AF_INET6, &this->adr, s[useSlot], sizeof(s[0])); + + // Pray the caller uses it before it gets clobbered + return s[useSlot]; } inline uint16_t GetPort(void) const { diff --git a/r5dev/vstdlib/callback.cpp b/r5dev/vstdlib/callback.cpp index b1c529a1..403ccea1 100644 --- a/r5dev/vstdlib/callback.cpp +++ b/r5dev/vstdlib/callback.cpp @@ -93,27 +93,19 @@ void Host_Kick_f(const CCommand& args) for (int i = 0; i < MAX_PLAYERS; i++) { - CClient* pClient = g_pClient->GetClient(i); - if (!pClient) - continue; - - CNetChan* pNetChan = pClient->GetNetChan(); - if (!pNetChan) - continue; - - string svClientName = pNetChan->GetName(); // Get full name. - - if (svClientName.empty()) + if (CClient* pClient = g_pClient->GetClient(i)) { - continue; + if (CNetChan* pNetChan = pClient->GetNetChan()) + { + if (strlen(pNetChan->GetName()) > 0) + { + if (strcmp(args.Arg(1), pNetChan->GetName()) == NULL) // Our wanted name? + { + NET_DisconnectClient(pClient, i, "Kicked from server", 0, true); + } + } + } } - - if (strcmp(args.Arg(1), svClientName.c_str()) != 0) // Our wanted name? - { - continue; - } - - NET_DisconnectClient(pClient, i, "Kicked from server", 0, 1); } } #ifndef CLIENT_DLL @@ -142,8 +134,6 @@ void Host_KickID_f(const CCommand& args) if (!pNetChan) continue; - string svIpAddress = pNetChan->GetAddress(); // If this stays null they modified the packet somehow. - if (bOnlyDigits) { uint64_t nTargetID = static_cast(std::stoll(args.Arg(1))); @@ -164,16 +154,16 @@ void Host_KickID_f(const CCommand& args) } } - NET_DisconnectClient(pClient, i, "Kicked from server", 0, 1); + NET_DisconnectClient(pClient, i, "Kicked from server", 0, true); } else { - if (string(args.Arg(1)).compare(svIpAddress) != NULL) + if (strcmp(args.Arg(1), pNetChan->GetAddress()) != NULL) { continue; } - NET_DisconnectClient(pClient, i, "Kicked from server", 0, 1); + NET_DisconnectClient(pClient, i, "Kicked from server", 0, true); } } } @@ -198,31 +188,21 @@ void Host_Ban_f(const CCommand& args) for (int i = 0; i < MAX_PLAYERS; i++) { - CClient* pClient = g_pClient->GetClient(i); - if (!pClient) - continue; - - CNetChan* pNetChan = pClient->GetNetChan(); - if (!pNetChan) - continue; - - string svClientName = pNetChan->GetName(); // Get full name. - - if (svClientName.empty()) + if (CClient* pClient = g_pClient->GetClient(i)) { - continue; + if (CNetChan* pNetChan = pClient->GetNetChan()) + { + if (strlen(pNetChan->GetName()) > 0) + { + if (strcmp(args.Arg(1), pNetChan->GetName()) == NULL) // Our wanted name? + { + g_pBanSystem->AddEntry(pNetChan->GetAddress(), pClient->GetOriginID()); + g_pBanSystem->Save(); + NET_DisconnectClient(pClient, i, "Banned from server", 0, true); + } + } + } } - - if (strcmp(args.Arg(1), svClientName.c_str()) != 0) - { - continue; - } - - string svIpAddress = pNetChan->GetAddress(); // If this stays empty they modified the packet somehow. - - g_pBanSystem->AddEntry(svIpAddress, pClient->GetOriginID()); - g_pBanSystem->Save(); - NET_DisconnectClient(pClient, i, "Banned from server", 0, 1); } } @@ -251,8 +231,6 @@ void Host_BanID_f(const CCommand& args) if (!pNetChan) continue; - string svIpAddress = pNetChan->GetAddress(); // If this stays empty they modified the packet somehow. - if (bOnlyDigits) { uint64_t nTargetID = static_cast(std::stoll(args.Arg(1))); @@ -273,20 +251,20 @@ void Host_BanID_f(const CCommand& args) } } - g_pBanSystem->AddEntry(svIpAddress, pClient->GetOriginID()); + g_pBanSystem->AddEntry(pNetChan->GetAddress(), pClient->GetOriginID()); g_pBanSystem->Save(); - NET_DisconnectClient(pClient, i, "Banned from server", 0, 1); + NET_DisconnectClient(pClient, i, "Banned from server", 0, true); } else { - if (string(args.Arg(1)).compare(svIpAddress) != NULL) + if (strcmp(args.Arg(1), pNetChan->GetAddress()) != NULL) { continue; } - g_pBanSystem->AddEntry(svIpAddress, pClient->GetOriginID()); + g_pBanSystem->AddEntry(pNetChan->GetAddress(), pClient->GetOriginID()); g_pBanSystem->Save(); - NET_DisconnectClient(pClient, i, "Banned from server", 0, 1); + NET_DisconnectClient(pClient, i, "Banned from server", 0, true); } } } @@ -439,9 +417,9 @@ void Pak_Swap_f(const CCommand& args) { try { + string pakName; RPakHandle_t nPakId = 0; RPakLoadedInfo_t* pakInfo = nullptr; - string pakName = std::string(); if (args.HasOnlyDigits(1)) { @@ -551,7 +529,7 @@ void RTech_Decompress_f(const CCommand& args) DevMsg(eDLL_T::RTECH, " | | |-- Size decp: '%llu'\n", rheader.m_nSizeMemory); DevMsg(eDLL_T::RTECH, " | | |-- Ratio : '%.02f'\n", (rheader.m_nSizeDisk * 100.f) / rheader.m_nSizeMemory); - if (rheader.m_nMagic != 'kaPR') + if (rheader.m_nMagic != RPAKHEADER) { Error(eDLL_T::RTECH, "%s - pak file '%s' has invalid magic!\n", __FUNCTION__, pakNameIn.c_str()); return; @@ -821,12 +799,19 @@ void RCON_PasswordChanged_f(IConVar* pConVar, const char* pOldString, float flOl { if (ConVar* pConVarRef = g_pCVar->FindVar(pConVar->GetName())) { + if (strcmp(pOldString, pConVarRef->GetString()) == NULL) + return; // Same password. + #ifndef DEDICATED - RCONClient()->SetPassword(pConVarRef->GetString()); - RCONClient()->Init(); -#elif DEDICATED - RCONServer()->SetPassword(pConVarRef->GetString()); - RCONServer()->Init(); + if (RCONClient()->IsInitialized()) + RCONClient()->SetPassword(pConVarRef->GetString()); + else + RCONClient()->Init(); // Initialize first. +#elif DEDICATED + if (RCONServer()->IsInitialized()) + RCONServer()->SetPassword(pConVarRef->GetString()); + else + RCONServer()->Init(); // Initialize first. #endif // DEDICATED } } diff --git a/r5dev/windows/console.cpp b/r5dev/windows/console.cpp index 21f02b47..68b8e7db 100644 --- a/r5dev/windows/console.cpp +++ b/r5dev/windows/console.cpp @@ -98,7 +98,7 @@ void Console_Init() CloseHandle(hThread); } - if (strstr(g_svCmdLine.c_str(), "-ansiclr")) + if (g_svCmdLine.find("-ansiclr") != string::npos) { GetConsoleMode(hOutput, &dwMode); dwMode |= ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING;