From 5e2e6cef08ac735904f039be8eccf634ba29b1f7 Mon Sep 17 00:00:00 2001 From: Oliver Pechey <40522943+oliverpechey@users.noreply.github.com> Date: Sat, 17 Feb 2024 15:42:01 +0000 Subject: [PATCH] Add call to Release() after a CObject::Find that was causing poor performance (#407) * Add call to Release() after a CObject::Find that was causing poor performance over time * Update CHANGELOG.md --- CHANGELOG.md | 3 ++ source/Hooks/SendComm.cpp | 69 +++++++++++++++++++++------------------ 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b316a1d1c..09a0d6e13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 4.0.22 +- Add call to Release() after a CObject::Find that was causing poor performance. + ## 4.0.21 - Create 'daily_tasks' a plugin that assigns basic daily tasks to player accounts for them to complete. diff --git a/source/Hooks/SendComm.cpp b/source/Hooks/SendComm.cpp index f9527e804..c56ff82b5 100644 --- a/source/Hooks/SendComm.cpp +++ b/source/Hooks/SendComm.cpp @@ -7,52 +7,57 @@ const std::unique_ptr> func = std::make_uniqueis_player()) + if (auto* ship = (CShip*)CObject::Find(toShipId, CObject::Class::CSHIP_OBJECT); ship) { - const auto client = ship->GetOwnerPlayer(); - - const auto& ci = ClientInfo[client]; - const auto* conf = FLHookConfig::c(); + if (ship->is_player()) + { + const auto client = ship->GetOwnerPlayer(); - static std::array num1RewriteBytes = {0xBA, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90, 0x90}; + const auto& ci = ClientInfo[client]; + const auto* conf = FLHookConfig::c(); - const auto content = DWORD(GetModuleHandle("content.dll")); - constexpr DWORD factionOffset = 0x6fb632c + 18 - 0x6ea0000; - constexpr DWORD numberOffset1 = 0x6eeb49b - 0x6ea0000; - constexpr DWORD numberOffset2 = 0x6eeb523 + 1 - 0x6ea0000; - constexpr DWORD formationOffset = 0x6fb7524 + 25 - 0x6ea0000; + static std::array num1RewriteBytes = {0xBA, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90, 0x90}; - auto playerFactionAddr = PVOID(content + factionOffset); - auto playerNumber1 = PVOID(content + numberOffset1); - auto playerNumber2 = PVOID(content + numberOffset2); - auto playerFormation = PCHAR(content + formationOffset); + const auto content = DWORD(GetModuleHandle("content.dll")); + constexpr DWORD factionOffset = 0x6fb632c + 18 - 0x6ea0000; + constexpr DWORD numberOffset1 = 0x6eeb49b - 0x6ea0000; + constexpr DWORD numberOffset2 = 0x6eeb523 + 1 - 0x6ea0000; + constexpr DWORD formationOffset = 0x6fb7524 + 25 - 0x6ea0000; - if (!conf->callsign.disableRandomisedFormations) - { - *(int*)(num1RewriteBytes.data() + 1) = ci.formationNumber1; - WriteProcMem(playerNumber1, num1RewriteBytes.data(), num1RewriteBytes.size()); - WriteProcMem(playerNumber2, &ci.formationNumber2, 1); - DWORD _; - VirtualProtect(playerFormation, 2, PAGE_READWRITE, &_); - std::sprintf(playerFormation, "%02d", ci.formationTag); - } + const auto playerFactionAddr = PVOID(content + factionOffset); + const auto playerNumber1 = PVOID(content + numberOffset1); + const auto playerNumber2 = PVOID(content + numberOffset2); + const auto playerFormation = PCHAR(content + formationOffset); - if (!conf->callsign.disableUsingAffiliationForCallsign) - { - if (auto repGroupNick = Hk::Ini::GetFromPlayerFile(client, L"rep_group"); repGroupNick.has_value() && repGroupNick.value().length() - 4 <= 6) + if (!conf->callsign.disableRandomisedFormations) { - auto val = wstos(repGroupNick.value()); - WriteProcMem(playerFactionAddr, val.erase(val.size() - 4).c_str(), val.size()); + *(int*)(num1RewriteBytes.data() + 1) = ci.formationNumber1; + WriteProcMem(playerNumber1, num1RewriteBytes.data(), num1RewriteBytes.size()); + WriteProcMem(playerNumber2, &ci.formationNumber2, 1); + DWORD _; + VirtualProtect(playerFormation, 2, PAGE_READWRITE, &_); + std::sprintf(playerFormation, "%02d", ci.formationTag); } - else + + if (!conf->callsign.disableUsingAffiliationForCallsign) { - WriteProcMem(playerFactionAddr, "player", 6); + if (auto repGroupNick = Hk::Ini::GetFromPlayerFile(client, L"rep_group"); repGroupNick.has_value() && repGroupNick.value().length() - 4 <= 6) + { + auto val = wstos(repGroupNick.value()); + WriteProcMem(playerFactionAddr, val.erase(val.size() - 4).c_str(), val.size()); + } + else + { + WriteProcMem(playerFactionAddr, "player", 6); + } } } + + ship->Release(); } func->UnDetour(); - int res = func->GetOriginalFunc()(fromShipId, toShipId, voiceId, costume, infocardId, a5, a6, infocardId2, a8, a9); + const int res = func->GetOriginalFunc()(fromShipId, toShipId, voiceId, costume, infocardId, a5, a6, infocardId2, a8, a9); func->Detour(SendComm); return res; }