Skip to content

Commit

Permalink
Hook TDBID::ToDebugSTRING.
Browse files Browse the repository at this point in the history
  • Loading branch information
gibbed committed Dec 21, 2020
1 parent 1a8fd26 commit 4ee006b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/overlay/Overlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,41 @@ void Overlay::HookLogChannel(ScriptContext* apContext, ScriptStack* apStack, voi
text.Destroy();
}

void Overlay::HookTDBIDToStringDEBUG(ScriptContext* apContext, ScriptStack* apStack, void* result, void*)
{
uint8_t opcode;

#pragma pack(push,1)
struct TDBID
{
uint32_t hash;
uint8_t unk4;
uint16_t unk5;
uint8_t unk7;
};
#pragma pack(pop)
static_assert(sizeof(TDBID) == 8);

TDBID tdbid_value{};
apStack->unk30 = nullptr;
apStack->unk38 = nullptr;
opcode = *(apStack->m_code++);
GetScriptCallArray()[opcode](apStack->m_context, apStack, &tdbid_value, nullptr);
apStack->m_code++; // skip ParamEnd

if (result)
{
std::string tdbid_debug = (tdbid_value.unk5 == 0 && tdbid_value.unk7 == 0)
? fmt::format("<TDBID:{:08X}:{:02X}>",
tdbid_value.hash, tdbid_value.unk4)
: fmt::format("<TDBID:{:08X}:{:02X}:{:04X}:{:02X}>",
tdbid_value.hash, tdbid_value.unk4, tdbid_value.unk5, tdbid_value.unk7);
REDString s(tdbid_debug.c_str());
((REDString*)result)->Copy(&s);
s.Destroy();
}
}

void Overlay::Toggle()
{
struct Singleton
Expand Down
2 changes: 2 additions & 0 deletions src/overlay/Overlay.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ struct Overlay
static LRESULT APIENTRY WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
static void HookLog(ScriptContext* apContext, ScriptStack* apStack, void*, void*);
static void HookLogChannel(ScriptContext* apContext, ScriptStack* apStack, void*, void*);
static void HookTDBIDToStringDEBUG(ScriptContext* apContext, ScriptStack* apStack, void*, void*);

private:

Expand All @@ -68,6 +69,7 @@ struct Overlay
TClipToCenter* m_realClipToCenter{nullptr};
TScriptCall* m_realLog{nullptr};
TScriptCall* m_realLogChannel{ nullptr };
TScriptCall* m_realTDBIDToStringDEBUG{ nullptr };
HWND m_hwnd;
WNDPROC m_wndProc{nullptr};
bool m_enabled{ false };
Expand Down
29 changes: 29 additions & 0 deletions src/overlay/Overlay_Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,35 @@ void Overlay::EarlyHooks(Image* apImage)
else
spdlog::info("\LogChannel function hook complete!");
}

pLocation = FindSignature({
0x48, 0xBF, 0x58, 0xD1, 0x78, 0xA0, 0x18, 0x09,
0xBA, 0xEC, 0x75, 0x16, 0x48, 0x8D, 0x15, 0xCC,
0xCC, 0xCC, 0xCC, 0x48, 0x8B, 0xCF, 0xE8, 0xCC,
0xCC, 0xCC, 0xCC, 0xC6, 0x05, 0xCC, 0xCC, 0xCC,
0xCC, 0x01, 0x41, 0x8B, 0x06, 0x39, 0x05, 0xCC,
0xCC, 0xCC, 0xCC, 0x7F
});

if (pLocation)
{
pLocation = &pLocation[45] + static_cast<int8_t>(pLocation[44]);
pLocation = FindSignature(pLocation, pLocation + 45, {
0x48, 0x8D, 0x0D, 0xCC, 0xCC, 0xCC, 0xCC, 0xE8,
0xCC, 0xCC, 0xCC, 0xCC, 0x83, 0x3D, 0xCC, 0xCC,
0xCC, 0xCC, 0xFF, 0x75, 0xCC, 0x48, 0x8D, 0x05,
});
if (pLocation)
{
pLocation = &pLocation[28] + *reinterpret_cast<int32_t*>(&pLocation[24]);
if (MH_CreateHook(pLocation, &HookTDBIDToStringDEBUG, reinterpret_cast<void**>(&m_realTDBIDToStringDEBUG)) != MH_OK || MH_EnableHook(pLocation) != MH_OK)
{
spdlog::error("\tCould not hook TDBID::ToStringDEBUG function!");
}
else
spdlog::info("\tTDBID::ToStringDEBUG function hook complete!");
}
}
}

long Overlay::PresentD3D12(IDXGISwapChain3* pSwapChain, UINT SyncInterval, UINT Flags)
Expand Down
16 changes: 16 additions & 0 deletions src/reverse/REDString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ REDString::REDString(const char* acpData)
RealStringCtor(this, acpData);
}

REDString* REDString::Copy(REDString* other)
{
static uint8_t* CopyCaller = FindSignature({
0x40, 0x53, 0x48, 0x83, 0xEC, 0x20, 0x48, 0x8B,
0xCA, 0x49, 0x8B, 0xD8, 0xE8, 0xCC, 0xCC, 0xCC,
0xCC, 0x48, 0x8B, 0xD0, 0x48, 0x8B, 0xCB, 0xE8,
0xCC, 0xCC, 0xCC, 0xCC, 0xB0, 0x01, 0x48, 0x83,
0xC4, 0x20, 0x5B, 0xC3
});
using TStringCopy = REDString*(REDString*, REDString*);
static TStringCopy* RealStringCopy = reinterpret_cast<TStringCopy*>(
&CopyCaller[28] + *reinterpret_cast<int32_t*>(&CopyCaller[24]));

return RealStringCopy(this, other);
}

void REDString::Destroy()
{
using TStringDtor = void(REDString*);
Expand Down
1 change: 1 addition & 0 deletions src/reverse/REDString.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ struct REDString
{
REDString();
REDString(const char* acpData);
REDString* Copy(REDString* other);
void Destroy();

char* ToString()
Expand Down

0 comments on commit 4ee006b

Please sign in to comment.