Skip to content

Commit

Permalink
Add call to Release() after a CObject::Find that was causing poor per…
Browse files Browse the repository at this point in the history
…formance (#407)

* Add call to Release() after a CObject::Find that was causing poor performance over time

* Update CHANGELOG.md
  • Loading branch information
oliverpechey authored Feb 17, 2024
1 parent b4e66f4 commit 5e2e6ce
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
69 changes: 37 additions & 32 deletions source/Hooks/SendComm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,57 @@ const std::unique_ptr<FunctionDetour<SendCommType>> func = std::make_unique<Func

int SendComm(uint fromShipId, uint toShipId, uint voiceId, const Costume* costume, uint infocardId, uint* a5, int a6, uint infocardId2, float a8, bool a9)
{
if (const CShip* ship = (CShip*)CObject::Find(toShipId, CObject::Class::CSHIP_OBJECT); ship && ship->is_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<byte, 8> 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<byte, 8> 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;
}
Expand Down

0 comments on commit 5e2e6ce

Please sign in to comment.