From 6c76136c5c599adad7b94a2f240031d66e57c6a3 Mon Sep 17 00:00:00 2001 From: Diavolo Date: Tue, 31 May 2022 18:38:09 +0200 Subject: [PATCH 1/2] [ISO] Follow the c++20 standard --- premake5.lua | 2 +- src/Components/Modules/AssetHandler.cpp | 14 +++++++------- .../Modules/AssetInterfaces/IMaterial.cpp | 3 ++- src/Components/Modules/Chat.cpp | 4 ++-- src/Components/Modules/Command.cpp | 4 ++-- src/Components/Modules/Friends.cpp | 4 ++-- src/Components/Modules/IPCPipe.cpp | 2 +- src/Components/Modules/Localization.cpp | 12 ++++++------ src/Components/Modules/Maps.cpp | 2 +- src/Components/Modules/ModelSurfs.cpp | 4 ++-- src/Components/Modules/Party.cpp | 7 ++++--- src/Components/Modules/Script.cpp | 6 +++--- src/Components/Modules/StringTable.cpp | 2 +- src/Components/Modules/StructuredData.cpp | 2 +- src/Components/Modules/UIFeeder.cpp | 8 ++++---- src/Components/Modules/UIScript.cpp | 2 +- src/Components/Modules/ZoneBuilder.cpp | 4 ++-- src/Steam/Proxy.cpp | 2 +- src/Steam/Steam.cpp | 2 +- src/Utils/Entities.cpp | 2 +- src/Utils/Memory.hpp | 2 +- 21 files changed, 46 insertions(+), 44 deletions(-) diff --git a/premake5.lua b/premake5.lua index 4a5fbe51..1b4be6a9 100644 --- a/premake5.lua +++ b/premake5.lua @@ -206,7 +206,7 @@ workspace "iw4x" configurations {"Debug", "Release"} language "C++" - cppdialect "C++17" + cppdialect "C++20" architecture "x86" platforms "Win32" diff --git a/src/Components/Modules/AssetHandler.cpp b/src/Components/Modules/AssetHandler.cpp index 06a03471..f5df53b9 100644 --- a/src/Components/Modules/AssetHandler.cpp +++ b/src/Components/Modules/AssetHandler.cpp @@ -23,7 +23,7 @@ namespace Components return; } - if (AssetHandler::AssetInterfaces.find(iAsset->getType()) != AssetHandler::AssetInterfaces.end()) + if (AssetHandler::AssetInterfaces.contains(iAsset->getType())) { Logger::Print("Duplicate asset interface: %s\n", Game::DB_GetXAssetTypeName(iAsset->getType())); delete AssetHandler::AssetInterfaces[iAsset->getType()]; @@ -58,7 +58,7 @@ namespace Components // Allow call DB_FindXAssetHeader within the hook AssetHandler::SetBypassState(true); - if (AssetHandler::TypeCallbacks.find(type) != AssetHandler::TypeCallbacks.end()) + if (AssetHandler::TypeCallbacks.contains(type)) { header = AssetHandler::TypeCallbacks[type](type, filename); } @@ -329,17 +329,17 @@ namespace Components { void* pointer = (*Game::g_streamBlocks)[offset->getUnpackedBlock()].data + offset->getUnpackedOffset(); - if (AssetHandler::Relocations.find(pointer) != AssetHandler::Relocations.end()) + if (AssetHandler::Relocations.contains(pointer)) { pointer = AssetHandler::Relocations[pointer]; } - offset->pointer = *reinterpret_cast(pointer); + offset->pointer = *static_cast(pointer); } void AssetHandler::ZoneSave(Game::XAsset asset, ZoneBuilder::Zone* builder) { - if (AssetHandler::AssetInterfaces.find(asset.type) != AssetHandler::AssetInterfaces.end()) + if (AssetHandler::AssetInterfaces.contains(asset.type)) { AssetHandler::AssetInterfaces[asset.type]->save(asset.header, builder); } @@ -351,7 +351,7 @@ namespace Components void AssetHandler::ZoneMark(Game::XAsset asset, ZoneBuilder::Zone* builder) { - if (AssetHandler::AssetInterfaces.find(asset.type) != AssetHandler::AssetInterfaces.end()) + if (AssetHandler::AssetInterfaces.contains(asset.type)) { AssetHandler::AssetInterfaces[asset.type]->mark(asset.header, builder); } @@ -375,7 +375,7 @@ namespace Components return { entry->second }; } - if (AssetHandler::AssetInterfaces.find(type) != AssetHandler::AssetInterfaces.end()) + if (AssetHandler::AssetInterfaces.contains(type)) { AssetHandler::AssetInterfaces[type]->load(&header, filename, builder); diff --git a/src/Components/Modules/AssetInterfaces/IMaterial.cpp b/src/Components/Modules/AssetInterfaces/IMaterial.cpp index 9ccddf71..1ac097d5 100644 --- a/src/Components/Modules/AssetInterfaces/IMaterial.cpp +++ b/src/Components/Modules/AssetInterfaces/IMaterial.cpp @@ -243,7 +243,8 @@ namespace Assets { Components::Logger::Print("No replacement found for material %s with techset %s\n", asset->info.name, asset->techniqueSet->name); std::string techName = asset->techniqueSet->name; - if (techSetCorrespondance.find(techName) != techSetCorrespondance.end()) { + if (techSetCorrespondance.contains(techName)) + { auto iw4TechSetName = techSetCorrespondance[techName]; Game::XAssetEntry* iw4TechSet = Game::DB_FindXAssetEntry(Game::XAssetType::ASSET_TYPE_TECHNIQUE_SET, iw4TechSetName.data()); diff --git a/src/Components/Modules/Chat.cpp b/src/Components/Modules/Chat.cpp index 9979fb89..9a79ea1c 100644 --- a/src/Components/Modules/Chat.cpp +++ b/src/Components/Modules/Chat.cpp @@ -23,7 +23,7 @@ namespace Components } std::unique_lock lock(Chat::AccessMutex); - if (Chat::MuteList.find(Game::svs_clients[player->s.number].steamID) != Chat::MuteList.end()) + if (Chat::MuteList.contains(Game::svs_clients[player->s.number].steamID)) { lock.unlock(); Chat::SendChat = false; @@ -217,7 +217,7 @@ namespace Components { std::unique_lock lock(Chat::AccessMutex); - if (Chat::MuteList.find(client->steamID) == Chat::MuteList.end()) + if (!Chat::MuteList.contains(client->steamID)) { Chat::MuteList.insert(client->steamID); lock.unlock(); diff --git a/src/Components/Modules/Command.cpp b/src/Components/Modules/Command.cpp index 83dbd377..6144d1e6 100644 --- a/src/Components/Modules/Command.cpp +++ b/src/Components/Modules/Command.cpp @@ -64,7 +64,7 @@ namespace Components { const auto command = Utils::String::ToLower(name); - if (Command::FunctionMap.find(command) == Command::FunctionMap.end()) + if (!Command::FunctionMap.contains(command)) { Command::AddRaw(name, Command::MainCallback); } @@ -87,7 +87,7 @@ namespace Components const auto command = Utils::String::ToLower(name); - if (Command::FunctionMapSV.find(command) == Command::FunctionMapSV.end()) + if (!Command::FunctionMapSV.contains(command)) { Command::AddRawSV(name, Command::MainCallbackSV); diff --git a/src/Components/Modules/Friends.cpp b/src/Components/Modules/Friends.cpp index ea15a68b..b64fd46e 100644 --- a/src/Components/Modules/Friends.cpp +++ b/src/Components/Modules/Friends.cpp @@ -190,10 +190,10 @@ namespace Components { std::lock_guard _(Friends::Mutex); - const unsigned int modId = *reinterpret_cast("IW4x") | 0x80000000; + const auto modId = *reinterpret_cast("IW4x") | 0x80000000; // Split up the list - for (auto entry : Friends::FriendsList) + for (const auto& entry : Friends::FriendsList) { Steam::FriendGameInfo info; if (Steam::Proxy::SteamFriends->GetFriendGamePlayed(entry.userId, &info) && info.m_gameID.modID == modId) diff --git a/src/Components/Modules/IPCPipe.cpp b/src/Components/Modules/IPCPipe.cpp index bcd8185b..76b4de01 100644 --- a/src/Components/Modules/IPCPipe.cpp +++ b/src/Components/Modules/IPCPipe.cpp @@ -161,7 +161,7 @@ namespace Components if (bResult && cbBytes) { - if (pipe->packetCallbacks.find(pipe->packet.command) != pipe->packetCallbacks.end()) + if (pipe->packetCallbacks.contains(pipe->packet.command)) { pipe->packetCallbacks[pipe->packet.command](pipe->packet.buffer); } diff --git a/src/Components/Modules/Localization.cpp b/src/Components/Modules/Localization.cpp index 641f1b0f..a3c87efb 100644 --- a/src/Components/Modules/Localization.cpp +++ b/src/Components/Modules/Localization.cpp @@ -12,7 +12,7 @@ namespace Components std::lock_guard _(Localization::LocalizeMutex); Utils::Memory::Allocator* allocator = Utils::Memory::GetAllocator(); - if (Localization::LocalizeMap.find(key) != Localization::LocalizeMap.end()) + if (Localization::LocalizeMap.contains(key)) { Game::LocalizeEntry* entry = Localization::LocalizeMap[key]; @@ -52,11 +52,11 @@ namespace Components { std::lock_guard _(Localization::LocalizeMutex); - if (Localization::TempLocalizeMap.find(key) != Localization::TempLocalizeMap.end()) + if (Localization::TempLocalizeMap.contains(key)) { entry = Localization::TempLocalizeMap[key]; } - else if (Localization::LocalizeMap.find(key) != Localization::LocalizeMap.end()) + else if (Localization::LocalizeMap.contains(key)) { entry = Localization::LocalizeMap[key]; } @@ -80,7 +80,7 @@ namespace Components std::lock_guard _(Localization::LocalizeMutex); Utils::Memory::Allocator* allocator = Utils::Memory::GetAllocator(); - if (Localization::TempLocalizeMap.find(key) != Localization::TempLocalizeMap.end()) + if (Localization::TempLocalizeMap.contains(key)) { Game::LocalizeEntry* entry = Localization::TempLocalizeMap[key]; if (entry->value) allocator->free(entry->value); @@ -256,11 +256,11 @@ namespace Components Game::XAssetHeader header = { nullptr }; std::lock_guard _(Localization::LocalizeMutex); - if (Localization::TempLocalizeMap.find(filename) != Localization::TempLocalizeMap.end()) + if (Localization::TempLocalizeMap.contains(filename)) { header.localize = Localization::TempLocalizeMap[filename]; } - else if (Localization::LocalizeMap.find(filename) != Localization::LocalizeMap.end()) + else if (Localization::LocalizeMap.contains(filename)) { header.localize = Localization::LocalizeMap[filename]; } diff --git a/src/Components/Modules/Maps.cpp b/src/Components/Modules/Maps.cpp index 34a819b2..1b82de10 100644 --- a/src/Components/Modules/Maps.cpp +++ b/src/Components/Modules/Maps.cpp @@ -920,7 +920,7 @@ namespace Components { std::string name = gameWorld->dpvs.smodelDrawInsts[i].model->name; - if (models.find(name) == models.end()) models[name] = 1; + if (!models.contains(name)) models[name] = 1; else models[name]++; } } diff --git a/src/Components/Modules/ModelSurfs.cpp b/src/Components/Modules/ModelSurfs.cpp index 374b028a..4378068a 100644 --- a/src/Components/Modules/ModelSurfs.cpp +++ b/src/Components/Modules/ModelSurfs.cpp @@ -109,7 +109,7 @@ namespace Components Game::XSurface* tempSurfaces = allocator.allocateArray(modelSurfs->numsurfs); char* surfaceData = reinterpret_cast(modelSurfs->surfs); - if (ModelSurfs::AllocMap.find(modelSurfs->name) != ModelSurfs::AllocMap.end()) + if (ModelSurfs::AllocMap.contains(modelSurfs->name)) { Game::CModelAllocData* allocData = ModelSurfs::AllocMap[modelSurfs->name]; @@ -210,7 +210,7 @@ namespace Components if (hasCustomSurface && !ModelSurfs::AllocMap.empty()) { - auto allocData = ModelSurfs::AllocMap.find(header.modelSurfs->name); + const auto allocData = ModelSurfs::AllocMap.find(header.modelSurfs->name); if (allocData != ModelSurfs::AllocMap.end()) { Utils::Memory::FreeAlign(allocData->second->indexBuffer); diff --git a/src/Components/Modules/Party.cpp b/src/Components/Modules/Party.cpp index 6be17612..83924ab8 100644 --- a/src/Components/Modules/Party.cpp +++ b/src/Components/Modules/Party.cpp @@ -39,7 +39,7 @@ namespace Components const char* Party::GetLobbyInfo(SteamID lobby, const std::string& key) { - if (Party::LobbyMap.find(lobby.bits) != Party::LobbyMap.end()) + if (Party::LobbyMap.contains(lobby.bits)) { Network::Address address = Party::LobbyMap[lobby.bits]; @@ -58,9 +58,10 @@ namespace Components void Party::RemoveLobby(SteamID lobby) { - if (Party::LobbyMap.find(lobby.bits) != Party::LobbyMap.end()) + const auto got = Party::LobbyMap.find(lobby.bits); + if (got != Party::LobbyMap.end()) { - Party::LobbyMap.erase(Party::LobbyMap.find(lobby.bits)); + Party::LobbyMap.erase(got); } } diff --git a/src/Components/Modules/Script.cpp b/src/Components/Modules/Script.cpp index dbe79ccc..0beb6ce0 100644 --- a/src/Components/Modules/Script.cpp +++ b/src/Components/Modules/Script.cpp @@ -461,7 +461,7 @@ namespace Components void Script::GetReplacedPos(const char* pos) { - if (Script::ReplacedFunctions.find(pos) != Script::ReplacedFunctions.end()) + if (Script::ReplacedFunctions.contains(pos)) { Script::ReplacedPos = Script::ReplacedFunctions[pos]; } @@ -475,7 +475,7 @@ namespace Components return; } - if (Script::ReplacedFunctions.find(what) != Script::ReplacedFunctions.end()) + if (Script::ReplacedFunctions.contains(what)) { Logger::Print("Warning: ReplacedFunctions already contains codePosValue for a function\n"); } @@ -667,7 +667,7 @@ namespace Components return; } - Game::Scr_AddBool(static_cast(Script::ScriptStorage.count(key))); // Until C++17 + Game::Scr_AddInt(static_cast(Script::ScriptStorage.count(key))); }); Script::AddFunction("StorageClear", [] // gsc: StorageClear(); diff --git a/src/Components/Modules/StringTable.cpp b/src/Components/Modules/StringTable.cpp index 8f1d590d..e9c82836 100644 --- a/src/Components/Modules/StringTable.cpp +++ b/src/Components/Modules/StringTable.cpp @@ -64,7 +64,7 @@ namespace Components std::string filename = Utils::String::ToLower(_filename); - if (StringTable::StringTableMap.find(filename) != StringTable::StringTableMap.end()) + if (StringTable::StringTableMap.contains(filename)) { header.stringTable = StringTable::StringTableMap[filename]; } diff --git a/src/Components/Modules/StructuredData.cpp b/src/Components/Modules/StructuredData.cpp index 983ee2e7..db5a514e 100644 --- a/src/Components/Modules/StructuredData.cpp +++ b/src/Components/Modules/StructuredData.cpp @@ -285,7 +285,7 @@ namespace Components // No need to patch version 155 if (newData[i].version == 155) continue; - if (patchDefinitions.find(newData[i].version) != patchDefinitions.end()) + if (patchDefinitions.contains(newData[i].version)) { auto patchData = patchDefinitions[newData[i].version]; auto otherData = otherPatchDefinitions[newData[i].version]; diff --git a/src/Components/Modules/UIFeeder.cpp b/src/Components/Modules/UIFeeder.cpp index b0e554bf..cc07d4c8 100644 --- a/src/Components/Modules/UIFeeder.cpp +++ b/src/Components/Modules/UIFeeder.cpp @@ -12,7 +12,7 @@ namespace Components const char* UIFeeder::GetItemText() { - if (UIFeeder::Feeders.find(UIFeeder::Current.feeder) != UIFeeder::Feeders.end()) + if (UIFeeder::Feeders.contains(UIFeeder::Current.feeder)) { return UIFeeder::Feeders[UIFeeder::Current.feeder].getItemText(UIFeeder::Current.index, UIFeeder::Current.column); } @@ -22,7 +22,7 @@ namespace Components unsigned int UIFeeder::GetItemCount() { - if (UIFeeder::Feeders.find(UIFeeder::Current.feeder) != UIFeeder::Feeders.end()) + if (UIFeeder::Feeders.contains(UIFeeder::Current.feeder)) { return UIFeeder::Feeders[UIFeeder::Current.feeder].getItemCount(); } @@ -313,7 +313,7 @@ namespace Components void UIFeeder::ApplyMap(UIScript::Token) { - std::string mapname = Dvar::Var("ui_map_name").get(); + const auto mapname = Dvar::Var("ui_map_name").get(); Dvar::Var("ui_mapname").set(mapname); Utils::Hook::Call(0x503B50)(mapname.data()); // Party_SetDisplayMapName @@ -321,7 +321,7 @@ namespace Components void UIFeeder::ApplyInitialMap(UIScript::Token) { - std::string mapname = Dvar::Var("ui_mapname").get(); + const auto mapname = Dvar::Var("ui_mapname").get(); Game::UI_UpdateArenas(); Game::UI_SortArenas(); diff --git a/src/Components/Modules/UIScript.cpp b/src/Components/Modules/UIScript.cpp index cd9b713c..2a591a8f 100644 --- a/src/Components/Modules/UIScript.cpp +++ b/src/Components/Modules/UIScript.cpp @@ -55,7 +55,7 @@ namespace Components bool UIScript::RunMenuScript(const char* name, const char** args) { - if (UIScript::UIScripts.find(name) != UIScript::UIScripts.end()) + if (UIScript::UIScripts.contains(name)) { UIScript::UIScripts[name](UIScript::Token(args)); return true; diff --git a/src/Components/Modules/ZoneBuilder.cpp b/src/Components/Modules/ZoneBuilder.cpp index 1715ebe7..9e060357 100644 --- a/src/Components/Modules/ZoneBuilder.cpp +++ b/src/Components/Modules/ZoneBuilder.cpp @@ -530,7 +530,7 @@ namespace Components // Check if the given pointer has already been mapped bool ZoneBuilder::Zone::hasPointer(const void* pointer) { - return (this->pointerMap.find(pointer) != this->pointerMap.end()); + return this->pointerMap.contains(pointer); } // Get stored offset for given file pointer @@ -644,7 +644,7 @@ namespace Components { if (type < Game::XAssetType::ASSET_TYPE_COUNT && type >= 0) { - if (this->renameMap[type].find(asset) != this->renameMap[type].end()) + if (this->renameMap[type].contains(asset)) { return this->renameMap[type][asset]; } diff --git a/src/Steam/Proxy.cpp b/src/Steam/Proxy.cpp index f3a7e72f..4ac7e709 100644 --- a/src/Steam/Proxy.cpp +++ b/src/Steam/Proxy.cpp @@ -36,7 +36,7 @@ namespace Steam std::pair Interface::getMethod(const std::string& method) { - if(this->methodCache.find(method) != this->methodCache.end()) + if(this->methodCache.contains(method)) { return this->methodCache[method]; } diff --git a/src/Steam/Steam.cpp b/src/Steam/Steam.cpp index 23501def..a03cec07 100644 --- a/src/Steam/Steam.cpp +++ b/src/Steam/Steam.cpp @@ -54,7 +54,7 @@ namespace Steam for (auto result : results) { - if (Callbacks::ResultHandlers.find(result.call) != Callbacks::ResultHandlers.end()) + if (Callbacks::ResultHandlers.contains(result.call)) { Callbacks::ResultHandlers[result.call]->Run(result.data, false, result.call); } diff --git a/src/Utils/Entities.cpp b/src/Utils/Entities.cpp index 04e24761..f2a38b86 100644 --- a/src/Utils/Entities.cpp +++ b/src/Utils/Entities.cpp @@ -70,7 +70,7 @@ namespace Utils { for (auto& entity : this->entities) { - if (entity.find("classname") != entity.end()) + if (entity.contains("classname")) { if (entity["classname"] == "misc_turret"s) { diff --git a/src/Utils/Memory.hpp b/src/Utils/Memory.hpp index 88bb7007..fde99eb7 100644 --- a/src/Utils/Memory.hpp +++ b/src/Utils/Memory.hpp @@ -108,7 +108,7 @@ namespace Utils bool isPointerMapped(void* ptr) { - return this->ptrMap.find(ptr) != this->ptrMap.end(); + return this->ptrMap.contains(ptr); } template T* getPointer(void* oldPtr) From 92a9eb58ad221c6c9aa4d29fb7e01dbc6ad3213b Mon Sep 17 00:00:00 2001 From: Diavolo Date: Fri, 3 Jun 2022 14:45:14 +0200 Subject: [PATCH 2/2] Address review --- src/Components/Modules/Party.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Components/Modules/Party.cpp b/src/Components/Modules/Party.cpp index 83924ab8..3c51a7f2 100644 --- a/src/Components/Modules/Party.cpp +++ b/src/Components/Modules/Party.cpp @@ -58,11 +58,7 @@ namespace Components void Party::RemoveLobby(SteamID lobby) { - const auto got = Party::LobbyMap.find(lobby.bits); - if (got != Party::LobbyMap.end()) - { - Party::LobbyMap.erase(got); - } + Party::LobbyMap.erase(lobby.bits); } void Party::ConnectError(const std::string& message)