From f53444428cc907490180d4443f90042bee7df079 Mon Sep 17 00:00:00 2001 From: knox Date: Wed, 27 Mar 2024 23:31:11 +0100 Subject: [PATCH] fix agent remove handling --- killproof_me/dllmain.cpp | 20 ++++++++++++++------ killproof_me/global.cpp | 28 ++++++++++++++++++++++++++++ killproof_me/global.h | 2 ++ 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/killproof_me/dllmain.cpp b/killproof_me/dllmain.cpp index 8031ea3..dee9f7b 100644 --- a/killproof_me/dllmain.cpp +++ b/killproof_me/dllmain.cpp @@ -196,12 +196,12 @@ void mod_combat(cbtevent* ev, ag* src, ag* dst, const char* skillname, uint64_t /* notify tracking change */ if (!src->elite) { // only run, when names are set and not null - if (src->name != nullptr && src->name[0] != '\0' && dst->name != nullptr && dst->name[0] != '\0') { + if (src->name != nullptr && dst->name != nullptr) { std::string username(dst->name); // remove ':' at the beginning of the name. - if (username.at(0) == ':') { + if (!username.empty() && username.at(0) == ':') { username.erase(0, 1); } @@ -257,12 +257,20 @@ void mod_combat(cbtevent* ev, ag* src, ag* dst, const char* skillname, uint64_t } /* remove */ else { + ARC_LOG(std::format("remove event for user: {}", src->id).c_str()); // do NOT remove yourself - if (username != selfAccountName) { - std::scoped_lock guard(trackedPlayersMutex, instancePlayersMutex); + const auto& playerIt = std::ranges::find_if(cachedPlayers, [pId = src->id](const auto& pPlayer) { + return pPlayer.second.id == pId; + }); + if (playerIt != cachedPlayers.end()) { + ARC_LOG(std::format("found player to remove for user [{}]: {} - {}", src->id, playerIt->second.username, playerIt->second.self).c_str()); - // remove specific user - removePlayer(username, AddedBy::Arcdps); + if (!playerIt->second.self) { + std::scoped_lock guard(trackedPlayersMutex, instancePlayersMutex); + + // remove specific user + removePlayer(playerIt->second, AddedBy::Arcdps); + } } } } diff --git a/killproof_me/global.cpp b/killproof_me/global.cpp index ed16815..f9f737d 100644 --- a/killproof_me/global.cpp +++ b/killproof_me/global.cpp @@ -53,6 +53,34 @@ void loadKillproofs(Player& player) { } } +/** + * lock `trackedPlayersMutex` and `instancePlayersMutex` before calling this + */ +void removePlayer(const Player& pPlayer, AddedBy pAddedByToDelete) { + // check if it should be removed + if (pAddedByToDelete == AddedBy::Miscellaneous || pPlayer.addedBy == pAddedByToDelete) { + // actually remove from tracking + const auto& trackedSub = std::ranges::remove(trackedPlayers, pPlayer.username); + trackedPlayers.erase(trackedSub.begin(), trackedSub.end()); + + const auto& instanceSub = std::ranges::remove(instancePlayers, pPlayer.username); + instancePlayers.erase(instanceSub.begin(), instanceSub.end()); + } +} + +/** + * lock `trackedPlayersMutex` and `instancePlayersMutex` before calling this + */ +void removePlayer(uintptr_t pId, AddedBy pAddedByToDelete) { + const auto& playerIt = std::ranges::find_if(cachedPlayers, [pId](const auto& pPlayer) { + return pPlayer.second.id == pId; + }); + + if (playerIt != cachedPlayers.end()) { + removePlayer(playerIt->second, pAddedByToDelete); + } +} + /** * lock `trackedPlayersMutex` and `instancePlayersMutex` before calling this */ diff --git a/killproof_me/global.h b/killproof_me/global.h index 20c018e..b2dd93d 100644 --- a/killproof_me/global.h +++ b/killproof_me/global.h @@ -36,6 +36,8 @@ inline bool fileExists(const std::string& filename) { return (stat(filename.c_str(), &buffer) == 0); } +void removePlayer(const Player& pPlayer, AddedBy pAddedByToDelete); +void removePlayer(uintptr_t pId, AddedBy pAddedByToDelete); void removePlayer(const std::string& username, AddedBy addedByToDelete); // void removePlayerInstance(const std::string& username); // void removePlayerTracking(const std::string& username);