From e92b5d13005d9973982aa7a397ba4b312dfe890e Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Tue, 9 Aug 2022 15:19:12 +0200 Subject: [PATCH] General cleanup Use c++ methods, remove unnecessary casts, unnecessary use of auto, etc.. --- r5dev/engine/cmodel_bsp.cpp | 8 ++++---- r5dev/engine/net.cpp | 2 +- r5dev/engine/sys_utils.cpp | 11 ++++++----- r5dev/filesystem/basefilesystem.cpp | 4 ++-- r5dev/game/server/ai_network.cpp | 2 +- r5dev/game/server/ai_networkmanager.cpp | 6 +++--- r5dev/gameui/IConsole.cpp | 2 +- r5dev/naveditor/GameUtils.cpp | 18 +++++++++--------- r5dev/naveditor/Sample_TileMesh.cpp | 2 +- r5dev/sdklauncher/basepanel.cpp | 6 +++--- r5dev/squirrel/sqvm.cpp | 2 +- r5dev/thirdparty/imgui/src/imgui_utility.cpp | 4 ++-- r5dev/tier1/NetAdr2.cpp | 6 +++--- r5dev/vgui/vgui_debugpanel.cpp | 2 +- r5dev/vgui/vgui_debugpanel.h | 12 +++++++++--- r5dev/vpc/interfaces.cpp | 2 +- r5dev/vphysics/QHull.cpp | 2 +- r5dev/vpklib/packedstore.cpp | 12 ++++++------ 18 files changed, 55 insertions(+), 48 deletions(-) diff --git a/r5dev/engine/cmodel_bsp.cpp b/r5dev/engine/cmodel_bsp.cpp index ceb644e0..ad8287ce 100644 --- a/r5dev/engine/cmodel_bsp.cpp +++ b/r5dev/engine/cmodel_bsp.cpp @@ -41,16 +41,16 @@ void MOD_GetAllInstalledMaps() for (const fs::directory_entry& dEntry : fs::directory_iterator("vpk")) { - std::string svFileName = dEntry.path().string(); + std::string svFileName = dEntry.path().u8string(); std::regex_search(svFileName, smRegexMatches, rgArchiveRegex); if (smRegexMatches.size() > 0) { - if (strcmp(smRegexMatches[1].str().c_str(), "frontend") == 0) + if (smRegexMatches[1].str().compare("frontend") == 0) { continue; } - else if (strcmp(smRegexMatches[1].str().c_str(), "mp_common") == 0) + else if (smRegexMatches[1].str().compare("mp_common") == 0) { if (std::find(g_vAllMaps.begin(), g_vAllMaps.end(), "mp_lobby") == g_vAllMaps.end()) g_vAllMaps.push_back("mp_lobby"); @@ -354,7 +354,7 @@ void MOD_PreloadPakFile(const string& svLevelName) nlohmann::json jsIn; try { - ifstream iPakLoadDefFile(fsPath.string().c_str(), std::ios::binary); // Load prerequisites file. + ifstream iPakLoadDefFile(fsPath.u8string(), std::ios::binary); // Load prerequisites file. jsIn = nlohmann::json::parse(iPakLoadDefFile); iPakLoadDefFile.close(); diff --git a/r5dev/engine/net.cpp b/r5dev/engine/net.cpp index d59dd5bd..5bec4067 100644 --- a/r5dev/engine/net.cpp +++ b/r5dev/engine/net.cpp @@ -125,7 +125,7 @@ void NET_PrintFunc(const char* fmt, ...) vsnprintf(buf, sizeof(buf), fmt, args); - buf[sizeof(buf) - 1] = 0; + buf[sizeof(buf) - 1] = '\0'; va_end(args); DevMsg(eDLL_T::CLIENT, "%s", buf); diff --git a/r5dev/engine/sys_utils.cpp b/r5dev/engine/sys_utils.cpp index d1e6b6e7..3152eb38 100644 --- a/r5dev/engine/sys_utils.cpp +++ b/r5dev/engine/sys_utils.cpp @@ -35,7 +35,7 @@ void HSys_Error(char* fmt, ...) buf[sizeof(buf) -1] = 0; va_end(args); - Error(eDLL_T::ENGINE, "%s\n", buf); + Error(eDLL_T::ENGINE, "%s", buf); return v_Sys_Error(buf); } @@ -54,11 +54,11 @@ void* HSys_Warning(int level, char* fmt, ...) vsnprintf(buf, sizeof(buf), fmt, args); - buf[sizeof(buf) - 1] = 0; + buf[sizeof(buf) - 1] = '\0'; va_end(args); }///////////////////////////// - Warning(eDLL_T::COMMON, "Warning(%d):%s\n", level, buf); + Warning(eDLL_T::COMMON, "Warning(%d):%s", level, buf); return v_Sys_Warning(level, buf); } @@ -107,11 +107,12 @@ void HCon_NPrintf(int pos, const char* fmt, ...) vsnprintf(buf, sizeof(buf), fmt, args); - buf[sizeof(buf) - 1] = 0; + buf[sizeof(buf) - 1] = '\0'; va_end(args); }///////////////////////////// - snprintf((char*)g_pLogSystem.m_pszCon_NPrintf_Buf, 4096, buf); + snprintf(g_pLogSystem.m_pszCon_NPrintf_Buf, + sizeof(g_pLogSystem.m_pszCon_NPrintf_Buf), buf); } } #endif // !DEDICATED diff --git a/r5dev/filesystem/basefilesystem.cpp b/r5dev/filesystem/basefilesystem.cpp index c04a02cd..5eae6e29 100644 --- a/r5dev/filesystem/basefilesystem.cpp +++ b/r5dev/filesystem/basefilesystem.cpp @@ -111,7 +111,7 @@ FileHandle_t CBaseFileSystem::VReadFromVPK(CBaseFileSystem* pFileSystem, FileHan { std::string svFilePath = ConvertToWinPath(pszFilePath); - if (strstr(svFilePath.c_str(), "\\\*\\")) + if (svFilePath.find("\\\*\\") != string::npos) { // Erase '//*/'. svFilePath.erase(0, 4); @@ -139,7 +139,7 @@ bool CBaseFileSystem::VReadFromCache(CBaseFileSystem* pFileSystem, char* pszFile { std::string svFilePath = ConvertToWinPath(pszFilePath); - if (strstr(svFilePath.c_str(), "\\\*\\")) + if (svFilePath.find("\\\*\\") != string::npos) { // Erase '//*/'. svFilePath.erase(0, 4); diff --git a/r5dev/game/server/ai_network.cpp b/r5dev/game/server/ai_network.cpp index 395bab2d..a0e50846 100644 --- a/r5dev/game/server/ai_network.cpp +++ b/r5dev/game/server/ai_network.cpp @@ -31,7 +31,7 @@ void CAI_Network::DebugConnectMsg(int node1, int node2, const char* pszFormat, . vsnprintf(buf, sizeof(buf), pszFormat, args); - buf[sizeof(buf) - 1] = 0; + buf[sizeof(buf) - 1] = '\0'; va_end(args); }///////////////////////////// diff --git a/r5dev/game/server/ai_networkmanager.cpp b/r5dev/game/server/ai_networkmanager.cpp index 06f7b82f..55e6fe11 100644 --- a/r5dev/game/server/ai_networkmanager.cpp +++ b/r5dev/game/server/ai_networkmanager.cpp @@ -312,12 +312,12 @@ void CAI_NetworkManager::LoadNetworkGraph(CAI_NetworkManager* pAINetworkManager, if (nAiNetVersion > AINET_VERSION_NUMBER) { Warning(eDLL_T::SERVER, "AI node graph '%s' deviates expectations (net version: '%d' expected: '%d')\n", - fsGraphPath.string().c_str(), nAiNetVersion, AINET_VERSION_NUMBER); + fsGraphPath.relative_path().u8string().c_str(), nAiNetVersion, AINET_VERSION_NUMBER); } else if (nAiMapVersion != g_ServerGlobalVariables->m_nMapVersion) { Warning(eDLL_T::SERVER, "AI node graph '%s' is out of date (map version: '%d' expected: '%d')\n", - fsGraphPath.string().c_str(), nAiMapVersion, g_ServerGlobalVariables->m_nMapVersion); + fsGraphPath.relative_path().u8string().c_str(), nAiMapVersion, g_ServerGlobalVariables->m_nMapVersion); } else { @@ -336,7 +336,7 @@ void CAI_NetworkManager::LoadNetworkGraph(CAI_NetworkManager* pAINetworkManager, if (nNavMeshHash != nAiGraphHash) { Warning(eDLL_T::SERVER, "AI node graph '%s' is out of date (checksum: '0x%X' expected: '0x%X')\n", - fsGraphPath.string().c_str(), nNavMeshHash, nAiGraphHash); + fsGraphPath.relative_path().u8string().c_str(), nNavMeshHash, nAiGraphHash); } iNavMesh.close(); diff --git a/r5dev/gameui/IConsole.cpp b/r5dev/gameui/IConsole.cpp index 1fcd40fc..665cab4e 100644 --- a/r5dev/gameui/IConsole.cpp +++ b/r5dev/gameui/IConsole.cpp @@ -535,7 +535,7 @@ void CConsole::ProcessCommand(const char* pszCommand) t.detach(); // Detatch from render thread. m_nHistoryPos = -1; - for (ssize_t i = static_cast(m_vHistory.size()) - 1; i >= 0; i--) + for (size_t i = m_vHistory.size(); i-- > 0; ) { if (m_vHistory[i].compare(pszCommand) == 0) { diff --git a/r5dev/naveditor/GameUtils.cpp b/r5dev/naveditor/GameUtils.cpp index e2cfcce9..2dbd2af2 100644 --- a/r5dev/naveditor/GameUtils.cpp +++ b/r5dev/naveditor/GameUtils.cpp @@ -105,10 +105,10 @@ void buildLinkTable(dtNavMesh* mesh, LinkTableData& data) { dtMeshTile* tile = mesh->getTile(i); if (!tile || !tile->header || !tile->dataSize) continue; - auto pcount = tile->header->polyCount; + int pcount = tile->header->polyCount; for (int j = 0; j < pcount; j++) { - auto& poly = tile->polys[j]; + dtPoly& poly = tile->polys[j]; poly.disjointSetId = -1; } } @@ -118,14 +118,14 @@ void buildLinkTable(dtNavMesh* mesh, LinkTableData& data) { dtMeshTile* tile = mesh->getTile(i); if (!tile || !tile->header || !tile->dataSize) continue; - auto pcount = tile->header->polyCount; + int pcount = tile->header->polyCount; for (int j = 0; j < pcount; j++) { - auto& poly = tile->polys[j]; - auto plink = poly.firstLink; + dtPoly& poly = tile->polys[j]; + unsigned int plink = poly.firstLink; while (plink != DT_NULL_LINK) { - auto l = tile->links[plink]; + const dtLink l = tile->links[plink]; const dtMeshTile* t; const dtPoly* p; mesh->getTileAndPolyByRefUnsafe(l.ref, &t, &p); @@ -153,11 +153,11 @@ void buildLinkTable(dtNavMesh* mesh, LinkTableData& data) { dtMeshTile* tile = mesh->getTile(i); if (!tile || !tile->header || !tile->dataSize) continue; - auto pcount = tile->header->polyCount; + int pcount = tile->header->polyCount; for (int j = 0; j < pcount; j++) { - auto& poly = tile->polys[j]; - auto id = data.find(poly.disjointSetId); + dtPoly& poly = tile->polys[j]; + int id = data.find(poly.disjointSetId); poly.disjointSetId = id; } } diff --git a/r5dev/naveditor/Sample_TileMesh.cpp b/r5dev/naveditor/Sample_TileMesh.cpp index 603f5868..278c5d13 100644 --- a/r5dev/naveditor/Sample_TileMesh.cpp +++ b/r5dev/naveditor/Sample_TileMesh.cpp @@ -784,7 +784,7 @@ void Sample_TileMesh::removeAllTiles() void Sample_TileMesh::buildAllHulls() { - for (auto& h : hulls) + for (const hulldef& h : hulls) { m_agentRadius = h.radius; m_agentMaxClimb = h.climb_height; diff --git a/r5dev/sdklauncher/basepanel.cpp b/r5dev/sdklauncher/basepanel.cpp index e499eb1d..ff9a2c54 100644 --- a/r5dev/sdklauncher/basepanel.cpp +++ b/r5dev/sdklauncher/basepanel.cpp @@ -550,18 +550,18 @@ void CUIBaseSurface::ParseMaps() { std::regex rgArchiveRegex{ R"([^_]*_(.*)(.bsp.pak000_dir).*)" }; std::smatch smRegexMatches; - for (const auto& dEntry : fs::directory_iterator("vpk")) + for (const fs::directory_entry& dEntry : fs::directory_iterator("vpk")) { std::string svFileName = dEntry.path().string(); std::regex_search(svFileName, smRegexMatches, rgArchiveRegex); if (smRegexMatches.size() > 0) { - if (strcmp(smRegexMatches[1].str().c_str(), "frontend") == 0) + if (smRegexMatches[1].str().compare("frontend") == 0) { continue; } - else if (strcmp(smRegexMatches[1].str().c_str(), "mp_common") == 0) + else if (smRegexMatches[1].str().compare("mp_common") == 0) { if (!this->m_MapCombo->Items.Contains("mp_lobby")) { diff --git a/r5dev/squirrel/sqvm.cpp b/r5dev/squirrel/sqvm.cpp index 79ce099c..a1eac93c 100644 --- a/r5dev/squirrel/sqvm.cpp +++ b/r5dev/squirrel/sqvm.cpp @@ -91,7 +91,7 @@ SQRESULT SQVM_PrintFunc(HSQUIRRELVM v, SQChar* fmt, ...) vsnprintf(buf, sizeof(buf), fmt, args); - buf[sizeof(buf) - 1] = 0; + buf[sizeof(buf) - 1] = '\0'; va_end(args); }///////////////////////////// diff --git a/r5dev/thirdparty/imgui/src/imgui_utility.cpp b/r5dev/thirdparty/imgui/src/imgui_utility.cpp index 5d21069a..f445d9ef 100644 --- a/r5dev/thirdparty/imgui/src/imgui_utility.cpp +++ b/r5dev/thirdparty/imgui/src/imgui_utility.cpp @@ -45,7 +45,7 @@ void Strtrim(char* s) void ImGuiConfig::Load() { fs::path fsPath = "platform\\imgui.json"; - DevMsg(eDLL_T::MS, "Loading ImGui config file '%s'\n", fsPath.relative_path().string().c_str()); + DevMsg(eDLL_T::MS, "Loading ImGui config file '%s'\n", fsPath.relative_path().u8string().c_str()); if (fs::exists(fsPath)) { @@ -93,7 +93,7 @@ void ImGuiConfig::Save() fs::path fsPath = "platform\\imgui.json"; - DevMsg(eDLL_T::MS, "Saving ImGui config file '%s'\n", fsPath.string().c_str()); + DevMsg(eDLL_T::MS, "Saving ImGui config file '%s'\n", fsPath.relative_path().u8string().c_str()); std::ofstream outFile(fsPath, std::ios::out | std::ios::trunc); // Write config file. outFile << jsOut.dump(4); // Dump it into config file. diff --git a/r5dev/tier1/NetAdr2.cpp b/r5dev/tier1/NetAdr2.cpp index ff32d70b..1b15923f 100644 --- a/r5dev/tier1/NetAdr2.cpp +++ b/r5dev/tier1/NetAdr2.cpp @@ -62,12 +62,12 @@ void CNetAdr2::SetPort(const string& svInPort) void CNetAdr2::SetIPAndPort(string svInAdr) { SetType(netadrtype_t::NA_IP); - if (strstr(svInAdr.c_str(), "loopback") || strstr(svInAdr.c_str(), "::1")) + if (svInAdr.find("loopback") != string::npos || svInAdr.find("::1") != string::npos) { SetType(netadrtype_t::NA_LOOPBACK); svInAdr = "[127.0.0.1]:" + GetPort(svInAdr); } - else if (strstr(svInAdr.c_str(), "localhost")) + else if (svInAdr.find("localhost") != string::npos) { svInAdr = "[127.0.0.1]:" + GetPort(svInAdr); } @@ -102,7 +102,7 @@ void CNetAdr2::SetIPAndPort(string svInAdr, string svInPort) svInAdr = "127.0.0.1"; } - if (strstr(svInAdr.c_str(), "[") || strstr(svInAdr.c_str(), "]")) + if (svInAdr.find("[") != string::npos || svInAdr.find("]") != string::npos) { svInAdr = GetBase(svInAdr); } diff --git a/r5dev/vgui/vgui_debugpanel.cpp b/r5dev/vgui/vgui_debugpanel.cpp index e845d981..6aeb31a3 100644 --- a/r5dev/vgui/vgui_debugpanel.cpp +++ b/r5dev/vgui/vgui_debugpanel.cpp @@ -135,7 +135,7 @@ void CLogSystem::DrawHostStats(void) const nHeight = g_nWindowHeight - nHeight; } - CMatSystemSurface_DrawColoredText(g_pMatSystemSurface, v_Rui_GetFontFace(), m_nFontHeight, nWidth, nHeight, c.r(), c.g(), c.b(), c.a(), (char*)m_pszCon_NPrintf_Buf); + CMatSystemSurface_DrawColoredText(g_pMatSystemSurface, v_Rui_GetFontFace(), m_nFontHeight, nWidth, nHeight, c.r(), c.g(), c.b(), c.a(), m_pszCon_NPrintf_Buf); } //----------------------------------------------------------------------------- diff --git a/r5dev/vgui/vgui_debugpanel.h b/r5dev/vgui/vgui_debugpanel.h index 6ec1b2f6..4a6144c4 100644 --- a/r5dev/vgui/vgui_debugpanel.h +++ b/r5dev/vgui/vgui_debugpanel.h @@ -37,6 +37,12 @@ struct LogMsg_t class CLogSystem { public: + CLogSystem() + { + m_nFontHeight = 16; + memset(m_pszCon_NPrintf_Buf, '\0', sizeof(m_pszCon_NPrintf_Buf)); + } + void Update(void); void AddLog(LogType_t type, string svText); void DrawLog(void); @@ -48,11 +54,11 @@ public: private: Color GetLogColorForType(LogType_t type) const; - vector m_vLogs{}; - int m_nFontHeight = 16; + vector m_vLogs; + int m_nFontHeight; public: - char* m_pszCon_NPrintf_Buf[4096]{}; + char m_pszCon_NPrintf_Buf[4096]{}; }; /////////////////////////////////////////////////////////////////////////////// diff --git a/r5dev/vpc/interfaces.cpp b/r5dev/vpc/interfaces.cpp index 8a4c273f..94d238fe 100644 --- a/r5dev/vpc/interfaces.cpp +++ b/r5dev/vpc/interfaces.cpp @@ -74,7 +74,7 @@ void CFactory::GetFactoriesFromRegister(void) //--------------------------------------------------------------------------------- CMemory CFactory::GetFactoryPtr(const string& svFactoryName, bool bVersionLess) const { - for (auto& it : m_vFactories) // Loop through the whole vector. + for (const FactoryInfo& it : m_vFactories) // Loop through the whole vector. { if (bVersionLess) { diff --git a/r5dev/vphysics/QHull.cpp b/r5dev/vphysics/QHull.cpp index 219ec19c..3dcec081 100644 --- a/r5dev/vphysics/QHull.cpp +++ b/r5dev/vphysics/QHull.cpp @@ -23,7 +23,7 @@ int HQHull_PrintFunc(const char* fmt, ...) vsnprintf(buf, sizeof(buf), fmt, args); - buf[sizeof(buf) - 1] = 0; + buf[sizeof(buf) - 1] = '\0'; va_end(args); }///////////////////////////// diff --git a/r5dev/vpklib/packedstore.cpp b/r5dev/vpklib/packedstore.cpp index fa60d486..ed37ed1c 100644 --- a/r5dev/vpklib/packedstore.cpp +++ b/r5dev/vpklib/packedstore.cpp @@ -55,14 +55,14 @@ VPKDir_t CPackedStore::GetDirectoryFile(string svPackDirFile) const for (size_t i = 0; i < DIR_LOCALE.size(); i++) { - if (strstr(svPackDirFile.c_str(), DIR_CONTEXT[i].c_str())) + if (svPackDirFile.find(DIR_CONTEXT[i]) != string::npos) { for (size_t j = 0; j < DIR_CONTEXT.size(); j++) { - if (strstr(svPackDirFile.c_str(), DIR_CONTEXT[j].c_str())) + if (svPackDirFile.find(DIR_CONTEXT[j]) != string::npos) { string svPackDirPrefix = DIR_LOCALE[i] + DIR_LOCALE[i]; - StringReplace(svPackDirFile, DIR_LOCALE[i].c_str(), svPackDirPrefix.c_str()); + StringReplace(svPackDirFile, DIR_LOCALE[i], svPackDirPrefix); goto escape; } } @@ -234,7 +234,7 @@ nlohmann::json CPackedStore::GetManifest(const string& svWorkSpace, const string { try { - ifstream iManifest(fsPath.string().c_str(), std::ios::binary); + ifstream iManifest(fsPath.u8string(), std::ios::binary); jsOut = nlohmann::json::parse(iManifest); return jsOut; @@ -306,9 +306,9 @@ string CPackedStore::StripLocalePrefix(const string& svDirectoryFile) const for (size_t i = 0; i < DIR_LOCALE.size(); i++) { - if (strstr(svFileName.c_str(), DIR_LOCALE[i].c_str())) + if (svFileName.find(DIR_LOCALE[i]) != string::npos) { - StringReplace(svFileName, DIR_LOCALE[i].c_str(), ""); + StringReplace(svFileName, DIR_LOCALE[i], ""); break; } }