614 lines
15 KiB
C++
Raw Normal View History

2024-05-06 23:30:27 +08:00
2024-02-07 20:59:24 +08:00
#include "MinHook.h"
extern WinMutex viewMutex;
// - Unnamed helpers -
2024-07-21 21:04:12 +08:00
namespace
{ // unnamed
2024-02-07 20:59:24 +08:00
#ifndef _WIN64
BYTE common_hook[] = {
2024-07-21 21:04:12 +08:00
0x9c, // pushfd
0x60, // pushad
0x9c, // pushfd ; Artikash 11/4/2018: not sure why pushfd happens twice. Anyway, after this a total of 0x28 bytes are pushed
2024-02-07 20:59:24 +08:00
0x8d, 0x44, 0x24, 0x28, // lea eax,[esp+0x28]
2024-07-21 21:04:12 +08:00
0x50, // push eax ; lpDatabase
0xb9, 0, 0, 0, 0, // mov ecx,@this
0xbb, 0, 0, 0, 0, // mov ebx,@TextHook::Send
0xff, 0xd3, // call ebx
0x9d, // popfd
0x61, // popad
0x9d, // popfd
0x68, 0, 0, 0, 0, // push @original
0xc3 // ret ; basically absolute jmp to @original
2024-02-07 20:59:24 +08:00
};
int this_offset = 9, send_offset = 14, original_offset = 24;
#else
BYTE common_hook[] = {
2024-07-21 21:04:12 +08:00
0x9c, // push rflags
0x50, // push rax
0x53, // push rbx
0x51, // push rcx
0x52, // push rdx
0x54, // push rsp
0x55, // push rbp
0x56, // push rsi
0x57, // push rdi
2024-02-07 20:59:24 +08:00
0x41, 0x50, // push r8
0x41, 0x51, // push r9
0x41, 0x52, // push r10
0x41, 0x53, // push r11
0x41, 0x54, // push r12
0x41, 0x55, // push r13
0x41, 0x56, // push r14
0x41, 0x57, // push r15
// https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention
// https://stackoverflow.com/questions/43358429/save-value-of-xmm-registers
2024-07-21 21:04:12 +08:00
0x48, 0x83, 0xec, 0x20, // sub rsp,0x20
0xf3, 0x0f, 0x7f, 0x24, 0x24, // movdqu [rsp],xmm4
0xf3, 0x0f, 0x7f, 0x6c, 0x24, 0x10, // movdqu [rsp+0x10],xmm5
2024-02-07 20:59:24 +08:00
0x48, 0x8d, 0x94, 0x24, 0xa8, 0x00, 0x00, 0x00, // lea rdx,[rsp+0xa8]
2024-07-21 21:04:12 +08:00
0x48, 0xb9, 0, 0, 0, 0, 0, 0, 0, 0, // mov rcx,@this
0x48, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, // mov rax,@TextHook::Send
0x48, 0x89, 0xe3, // mov rbx,rsp
0x48, 0x83, 0xe4, 0xf0, // and rsp,0xfffffffffffffff0 ; align stack
0xff, 0xd0, // call rax
0x48, 0x89, 0xdc, // mov rsp,rbx
0xf3, 0x0f, 0x6f, 0x6c, 0x24, 0x10, // movdqu xmm5,XMMWORD PTR[rsp + 0x10]
0xf3, 0x0f, 0x6f, 0x24, 0x24, // movdqu xmm4,XMMWORD PTR[rsp]
0x48, 0x83, 0xc4, 0x20, // add rsp,0x20
0x41, 0x5f, // pop r15
0x41, 0x5e, // pop r14
0x41, 0x5d, // pop r13
0x41, 0x5c, // pop r12
0x41, 0x5b, // pop r11
0x41, 0x5a, // pop r10
0x41, 0x59, // pop r9
0x41, 0x58, // pop r8
0x5f, // pop rdi
0x5e, // pop rsi
0x5d, // pop rbp
0x5c, // pop rsp
0x5a, // pop rdx
0x59, // pop rcx
0x5b, // pop rbx
0x58, // pop rax
0x9d, // pop rflags
0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp qword ptr [rip]
0, 0, 0, 0, 0, 0, 0, 0 // @original
2024-02-07 20:59:24 +08:00
};
int this_offset = 50, send_offset = 60, original_offset = 126;
#endif
2024-07-21 21:04:12 +08:00
// thread_local BYTE buffer[PIPE_BUFFER_SIZE];
// thread_local will crush on windowsxp
2024-02-07 20:59:24 +08:00
} // unnamed namespace
// - TextHook methods -
2024-07-21 21:04:12 +08:00
uintptr_t getasbaddr(const HookParam &hp)
{
auto address = hp.address;
if (hp.type & MODULE_OFFSET)
{
if (hp.type & FUNCTION_OFFSET)
{
2024-10-30 13:39:02 +08:00
FARPROC function = NULL;
try
{
auto ordinal = std::stoi(hp.function);
function = GetProcAddress(GetModuleHandleW(hp.module), (LPCSTR)ordinal);
}
catch (...)
{
function = GetProcAddress(GetModuleHandleW(hp.module), hp.function);
}
if (function)
2024-07-21 21:04:12 +08:00
address += (uint64_t)function;
else
return ConsoleOutput(FUNC_MISSING), 0;
}
else
{
2024-07-21 21:04:12 +08:00
if (HMODULE moduleBase = GetModuleHandleW(hp.module))
address += (uint64_t)moduleBase;
else
return ConsoleOutput(MODULE_MISSING), 0;
}
}
return address;
}
2024-02-07 20:59:24 +08:00
bool TextHook::Insert(HookParam hp)
{
2024-07-21 21:04:12 +08:00
auto addr = getasbaddr(hp);
if (!addr)
return false;
RemoveHook(addr, 0);
2024-07-21 21:04:12 +08:00
ConsoleOutput(INSERTING_HOOK, hp.name, addr);
local_buffer = new BYTE[PIPE_BUFFER_SIZE];
2024-02-07 20:59:24 +08:00
{
std::scoped_lock lock(viewMutex);
this->hp = hp;
address = addr;
2024-02-07 20:59:24 +08:00
}
2024-07-21 21:04:12 +08:00
savetypeforremove = hp.type;
if (hp.type & DIRECT_READ)
return InsertReadCode();
if (hp.type & BREAK_POINT)
2024-10-03 14:53:59 +08:00
{
if (InsertBreakPoint())
return true;
if (safeautoleaveveh)
return InsertBreakPoint(); // 搜索特殊码后不会释放导致virtualprotect查询失败重试。
return false;
}
2024-02-07 20:59:24 +08:00
return InsertHookCode();
}
2024-07-21 21:04:12 +08:00
uintptr_t win64find0000(uintptr_t addr)
{
uintptr_t r = 0;
__try
{
addr &= ~0xf;
for (uintptr_t i = addr, j = addr - 0x10000; i > j; i -= 0x10)
{
DWORD k = *(DWORD *)(i - 4);
if (k == 0x00000000)
return i;
}
return 0;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
2024-05-09 07:23:06 +08:00
}
2024-07-21 21:04:12 +08:00
return r;
2024-05-09 07:23:06 +08:00
}
2024-07-21 21:04:12 +08:00
Synchronized<std::unordered_map<uintptr_t, uintptr_t>> retaddr2relative; // 很奇怪这个放到函数里用static在xp上会报错。
uintptr_t queryrelativeret(HookParam &hp, uintptr_t retaddr)
{
// 不需要区分是相对于哪个module的偏移只需要得到偏移就可以了用来确保重启程序后ret值恒定
auto &re = retaddr2relative.Acquire().contents;
if (re.find(retaddr) != re.end())
return re.at(retaddr);
uintptr_t relative = retaddr;
if (hp.jittype == JITTYPE::UNITY)
{
#ifndef _WIN64
relative = retaddr - SafeFindEnclosingAlignedFunction(retaddr, 0x10000);
#else
relative = retaddr - win64find0000(retaddr);
#endif
2024-05-09 07:23:06 +08:00
}
2024-07-21 21:04:12 +08:00
else
{
if (MEMORY_BASIC_INFORMATION info = {}; VirtualQuery((LPCVOID)retaddr, &info, sizeof(info)))
relative -= (uintptr_t)info.AllocationBase;
2024-05-09 07:23:06 +08:00
}
2024-07-21 21:04:12 +08:00
re.insert(std::make_pair(retaddr, relative));
2024-03-12 22:02:31 +08:00
return relative;
}
2024-07-21 21:04:12 +08:00
uintptr_t jitgetaddr(hook_stack *stack, HookParam *hp)
{
switch (hp->jittype)
{
2024-07-21 21:04:12 +08:00
#ifdef _WIN64
2024-04-07 18:35:50 +08:00
case JITTYPE::RPCS3:
return RPCS3::emu_arg(stack)[hp->argidx];
case JITTYPE::VITA3K:
return VITA3K::emu_arg(stack)[hp->argidx];
case JITTYPE::YUZU:
2024-10-11 18:42:53 +08:00
return YUZU::emu_arg(stack, hp->emu_addr)[hp->argidx];
2024-07-21 21:04:12 +08:00
#endif
case JITTYPE::PPSSPP:
2024-04-01 12:34:24 +08:00
return PPSSPP::emu_arg(stack)[hp->argidx];
default:
2024-04-01 00:46:05 +08:00
return 0;
}
}
2024-07-21 21:04:12 +08:00
bool checklengthembedable(const HookParam &hp, size_t size)
{
2024-05-11 13:01:02 +08:00
size_t len;
if (hp.type & CODEC_UTF16)
2024-07-21 21:04:12 +08:00
len = 2;
else if (hp.type & CODEC_UTF32)
len = 4;
else
{
2024-08-31 18:23:06 +08:00
len = 2;
2024-07-21 21:04:12 +08:00
}
return size > len;
2024-05-11 13:01:02 +08:00
}
2024-07-21 21:04:12 +08:00
bool commonfilter(void *data, size_t *len, HookParam *hp)
{
if (hp->type & CODEC_UTF16)
;
else if (hp->type & CODEC_UTF32)
;
else if (hp->type & CODEC_UTF8)
;
else
{
if (*len == 2)
{
StringFilter((char *)data, len, "\x81\xa4", 2);
StringFilter((char *)data, len, "\x81\xa5", 2);
2024-07-09 23:49:59 +08:00
}
}
return true;
}
2024-02-07 20:59:24 +08:00
void TextHook::Send(uintptr_t lpDataBase)
{
2024-07-21 21:04:12 +08:00
auto buffer = (TextOutput_T *)local_buffer;
2024-02-07 20:59:24 +08:00
auto pbData = buffer->data;
2024-07-21 21:04:12 +08:00
_InterlockedIncrement((long *)&useCount);
2024-02-07 20:59:24 +08:00
__try
{
2024-07-21 21:04:12 +08:00
auto stack = get_hook_stack(lpDataBase);
2024-02-07 20:59:24 +08:00
if (auto current_trigger_fun = trigger_fun.exchange(nullptr))
2024-07-21 21:04:12 +08:00
if (!current_trigger_fun(location, stack))
trigger_fun = current_trigger_fun;
if (hp.type & HOOK_RETURN)
{
hp.type &= ~HOOK_RETURN;
hp.address = stack->retaddr;
strcat(hp.name, "_Return");
// 清除jit hook特征防止手动插入
strcpy(hp.unityfunctioninfo, "");
hp.emu_addr = 0;
// 清除module
2024-05-10 15:39:46 +08:00
hp.type &= ~MODULE_OFFSET;
hp.type &= ~FUNCTION_OFFSET;
2024-07-21 21:04:12 +08:00
strcpy(hp.function, "");
wcscpy(hp.module, L"");
2024-05-10 14:00:53 +08:00
2024-07-21 21:04:12 +08:00
NewHook(hp, hp.name);
hp.type |= HOOK_EMPTY;
2024-05-10 14:00:53 +08:00
__leave;
}
2024-07-21 21:04:12 +08:00
if (hp.type & HOOK_EMPTY)
__leave; // jichi 10/24/2014: dummy hook only for dynamic hook
2024-02-07 20:59:24 +08:00
size_t lpCount = 0;
uintptr_t lpSplit = 0,
2024-07-21 21:04:12 +08:00
lpRetn = stack->retaddr,
plpdatain = (lpDataBase + hp.offset),
lpDataIn = *(uintptr_t *)plpdatain;
bool isstring = false;
if (hp.jittype != JITTYPE::PC && hp.jittype != JITTYPE::UNITY)
2024-04-01 00:46:05 +08:00
{
2024-07-21 21:04:12 +08:00
lpDataIn = jitgetaddr(stack, &hp);
plpdatain = (uintptr_t)&lpDataIn;
2024-04-01 00:46:05 +08:00
}
2024-07-21 21:04:12 +08:00
else if (hp.jittype == JITTYPE::UNITY)
{
plpdatain = (uintptr_t)argidx(stack, hp.argidx);
lpDataIn = *(uintptr_t *)plpdatain;
2024-05-10 02:11:33 +08:00
}
2024-02-17 14:33:26 +08:00
2024-07-21 21:04:12 +08:00
auto use_custom_embed_fun = (hp.type & EMBED_ABLE) && !(hp.type & EMBED_BEFORE_SIMPLE);
if (use_custom_embed_fun)
2024-02-07 20:59:24 +08:00
{
2024-07-21 21:04:12 +08:00
isstring = true;
lpSplit = Engine::ScenarioRole;
lpRetn = 0;
if (hp.hook_before(stack, pbData, &lpCount, &lpSplit) == false)
__leave;
2024-02-07 20:59:24 +08:00
}
2024-07-21 21:04:12 +08:00
else if (hp.text_fun)
2024-02-07 20:59:24 +08:00
{
2024-07-21 21:04:12 +08:00
isstring = true;
2024-02-17 12:56:49 +08:00
hp.text_fun(stack, &hp, &lpDataIn, &lpSplit, &lpCount);
}
2024-07-21 21:04:12 +08:00
else if (hp.type & SPECIAL_JIT_STRING)
2024-05-09 07:23:06 +08:00
{
2024-07-21 21:04:12 +08:00
if (hp.jittype == JITTYPE::UNITY)
commonsolvemonostring(lpDataIn, &lpDataIn, &lpCount);
2024-05-09 07:23:06 +08:00
}
2024-07-21 21:04:12 +08:00
else
2024-02-17 12:56:49 +08:00
{
2024-07-21 21:04:12 +08:00
if (hp.type & FIXING_SPLIT)
lpSplit = FIXED_SPLIT_VALUE; // fuse all threads, and prevent floating
else if (hp.type & USING_SPLIT)
{
2024-02-17 12:56:49 +08:00
lpSplit = *(uintptr_t *)(lpDataBase + hp.split);
2024-07-21 21:04:12 +08:00
if (hp.type & SPLIT_INDIRECT)
lpSplit = *(uintptr_t *)(lpSplit + hp.split_index);
2024-02-07 20:59:24 +08:00
}
2024-07-21 21:04:12 +08:00
if (hp.type & DATA_INDIRECT)
{
plpdatain = (lpDataIn + hp.index);
2024-02-17 12:56:49 +08:00
lpDataIn = *(uintptr_t *)plpdatain;
2024-02-07 20:59:24 +08:00
}
2024-02-17 12:56:49 +08:00
lpDataIn += hp.padding;
lpCount = GetLength(stack, lpDataIn);
2024-02-17 12:06:27 +08:00
}
2024-07-21 21:04:12 +08:00
if (lpCount <= 0)
__leave;
2024-02-17 12:06:27 +08:00
if (lpCount > TEXT_BUFFER_SIZE)
{
ConsoleOutput(InvalidLength, lpCount, hp.name);
lpCount = TEXT_BUFFER_SIZE;
}
2024-07-21 21:04:12 +08:00
if (!use_custom_embed_fun)
2024-02-17 12:06:27 +08:00
{
2024-07-21 21:04:12 +08:00
if ((!(hp.type & USING_CHAR)) && (isstring || (hp.type & USING_STRING)))
2024-02-07 20:59:24 +08:00
{
2024-07-21 21:04:12 +08:00
if (lpDataIn == 0)
__leave;
::memcpy(pbData, (void *)lpDataIn, lpCount);
2024-02-07 20:59:24 +08:00
}
2024-02-17 12:56:49 +08:00
else
{
2024-07-21 21:04:12 +08:00
if (hp.type & CODEC_UTF32)
2024-02-07 20:59:24 +08:00
{
2024-07-21 21:04:12 +08:00
*(uint32_t *)pbData = lpDataIn & 0xffffffff;
2024-02-07 20:59:24 +08:00
}
else
2024-07-21 21:04:12 +08:00
{ // CHAR_LITTEL_ENDIAN,CODEC_ANSI_BE,CODEC_UTF16
2024-02-07 20:59:24 +08:00
lpDataIn &= 0xffff;
2024-07-21 21:04:12 +08:00
if ((hp.type & CODEC_ANSI_BE) && (lpDataIn >> 8))
lpDataIn = _byteswap_ushort(lpDataIn & 0xffff);
if (lpCount == 1)
lpDataIn &= 0xff;
*(WORD *)pbData = lpDataIn & 0xffff;
2024-02-07 20:59:24 +08:00
}
2024-07-21 21:04:12 +08:00
}
2024-02-07 20:59:24 +08:00
}
2024-07-21 21:04:12 +08:00
if (!commonfilter(pbData, &lpCount, &hp) || lpCount <= 0)
__leave;
if (hp.filter_fun && !hp.filter_fun(pbData, &lpCount, &hp) || lpCount <= 0)
__leave;
if (hp.type & (NO_CONTEXT | FIXING_SPLIT))
lpRetn = 0;
2024-02-17 12:06:27 +08:00
2024-07-21 21:04:12 +08:00
buffer->type = hp.type;
lpRetn = queryrelativeret(hp, lpRetn);
ThreadParam tp{GetCurrentProcessId(), address, lpRetn, lpSplit};
2024-02-28 23:33:52 +08:00
parsenewlineseperator(pbData, &lpCount);
2024-07-21 21:04:12 +08:00
bool canembed;
if (hp.type & EMBED_ABLE)
{
if (!checklengthembedable(hp, lpCount))
{
buffer->type &= (~EMBED_ABLE);
canembed = false;
2024-05-11 13:01:02 +08:00
}
2024-07-21 21:04:12 +08:00
else if (checktranslatedok(pbData, lpCount))
{
buffer->type &= (~EMBED_ABLE);
canembed = true;
2024-05-11 13:01:02 +08:00
}
2024-07-21 21:04:12 +08:00
else
{
canembed = true;
2024-05-11 13:01:02 +08:00
}
}
2024-02-28 23:33:52 +08:00
2024-07-21 21:04:12 +08:00
TextOutput(tp, hp, buffer, lpCount);
2024-02-28 23:33:52 +08:00
2024-07-21 21:04:12 +08:00
if (canembed && (check_embed_able(tp)))
2024-02-07 20:59:24 +08:00
{
2024-07-21 21:04:12 +08:00
auto lpCountsave = lpCount;
if (waitfornotify(buffer, pbData, &lpCount, tp))
2024-02-07 20:59:24 +08:00
{
2024-07-21 21:04:12 +08:00
if (hp.type & EMBED_AFTER_NEW)
2024-02-07 20:59:24 +08:00
{
2024-07-21 21:04:12 +08:00
auto _ = new char[max(lpCountsave, lpCount) + 10];
memcpy(_, pbData, lpCount);
for (int i = lpCount; i < max(lpCountsave, lpCount) + 10; i++)
_[i] = 0;
*(uintptr_t *)plpdatain = (uintptr_t)_;
2024-02-07 20:59:24 +08:00
}
2024-07-21 21:04:12 +08:00
else if (hp.type & EMBED_AFTER_OVERWRITE)
2024-02-07 20:59:24 +08:00
{
2024-07-21 21:04:12 +08:00
memcpy((void *)lpDataIn, pbData, lpCount);
for (int i = lpCount; i < lpCountsave; i++)
((BYTE *)(lpDataIn))[i] = 0;
2024-02-07 20:59:24 +08:00
}
2024-07-21 21:04:12 +08:00
else if (hp.hook_after)
hp.hook_after(stack, pbData, lpCount);
else if (hp.type & SPECIAL_JIT_STRING)
{
if (hp.jittype == JITTYPE::UNITY)
unity_ui_string_hook_after(argidx(stack, hp.argidx), pbData, lpCount);
2024-05-09 07:23:06 +08:00
}
2024-02-07 20:59:24 +08:00
}
}
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
if (!err && !(hp.type & KNOWN_UNSTABLE))
{
err = true;
ConsoleOutput(SEND_ERROR, hp.name);
}
}
2024-07-21 21:04:12 +08:00
_InterlockedDecrement((long *)&useCount);
2024-02-07 20:59:24 +08:00
}
2024-07-21 21:04:12 +08:00
bool TextHook::breakpointcontext(PCONTEXT context)
{
auto stack = std::make_unique<hook_stack>();
context_get(stack.get(), context);
auto lpDataBase = stack->get_base();
Send(lpDataBase);
2024-07-21 21:04:12 +08:00
context_set(stack.get(), context);
return true;
}
bool TextHook::InsertBreakPoint()
{
2024-07-21 21:04:12 +08:00
// MH_CreateHook 64位unity/yuzu-emu经常 MH_ERROR_MEMORY_ALLOC
return add_veh_hook(location, std::bind(&TextHook::breakpointcontext, this, std::placeholders::_1));
}
bool TextHook::RemoveBreakPoint()
{
return remove_veh_hook(location);
}
2024-02-07 20:59:24 +08:00
bool TextHook::InsertHookCode()
{
VirtualProtect(location, 10, PAGE_EXECUTE_READWRITE, DUMMY);
2024-07-21 21:04:12 +08:00
void *original;
2024-02-07 20:59:24 +08:00
MH_STATUS error;
while ((error = MH_CreateHook(location, trampoline, &original)) != MH_OK)
2024-07-21 21:04:12 +08:00
if (error == MH_ERROR_ALREADY_CREATED)
RemoveHook(address);
else
return ConsoleOutput(MH_StatusToString(error)), false;
2024-02-07 20:59:24 +08:00
2024-07-21 21:04:12 +08:00
*(TextHook **)(common_hook + this_offset) = this;
2024-02-07 20:59:24 +08:00
*(void(TextHook::**)(uintptr_t))(common_hook + send_offset) = &TextHook::Send;
2024-07-21 21:04:12 +08:00
*(void **)(common_hook + original_offset) = original;
2024-02-07 20:59:24 +08:00
memcpy(trampoline, common_hook, sizeof(common_hook));
return MH_EnableHook(location) == MH_OK;
}
void TextHook::Read()
{
size_t dataLen = 1;
2024-07-21 21:04:12 +08:00
// BYTE(*buffer)[PIPE_BUFFER_SIZE] = &::buffer, *pbData = *buffer + sizeof(ThreadParam);
auto buffer = (TextOutput_T *)local_buffer;
2024-02-07 20:59:24 +08:00
auto pbData = buffer->data;
2024-07-21 21:04:12 +08:00
buffer->type = hp.type;
2024-02-07 20:59:24 +08:00
__try
{
2024-07-21 21:04:12 +08:00
if (hp.text_fun)
2024-02-07 20:59:24 +08:00
{
2024-04-07 18:35:50 +08:00
while (WaitForSingleObject(readerEvent, 500) == WAIT_TIMEOUT)
2024-07-21 21:04:12 +08:00
hp.text_fun(0, 0, 0, 0, 0);
2024-02-07 20:59:24 +08:00
}
2024-04-07 18:35:50 +08:00
else
{
2024-05-23 16:29:46 +08:00
while (WaitForSingleObject(readerEvent, 500) == WAIT_TIMEOUT)
2024-04-07 18:35:50 +08:00
{
2024-07-21 21:04:12 +08:00
if (!location)
continue;
int currentLen = HookStrlen((BYTE *)location);
bool changed = memcmp(pbData, location, dataLen) != 0;
if (changed || (currentLen != dataLen))
2024-05-23 16:29:46 +08:00
{
dataLen = min(currentLen, TEXT_BUFFER_SIZE);
memcpy(pbData, location, dataLen);
2024-07-21 21:04:12 +08:00
if (hp.filter_fun && !hp.filter_fun(pbData, &dataLen, &hp) || dataLen <= 0)
continue;
TextOutput({GetCurrentProcessId(), address, 0, 0}, hp, buffer, dataLen);
2024-09-07 21:34:41 +08:00
dataLen = min(currentLen, TEXT_BUFFER_SIZE);
2024-05-23 16:29:46 +08:00
memcpy(pbData, location, dataLen);
}
2024-07-21 21:04:12 +08:00
}
2024-04-07 18:35:50 +08:00
}
2024-02-07 20:59:24 +08:00
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
ConsoleOutput(READ_ERROR, hp.name);
Clear();
}
}
bool TextHook::InsertReadCode()
{
2024-07-21 21:04:12 +08:00
readerThread = CreateThread(nullptr, 0, [](void *This)
{ ((TextHook*)This)->Read(); return 0UL; }, this, 0, nullptr);
2024-02-07 20:59:24 +08:00
readerEvent = CreateEventW(nullptr, FALSE, FALSE, NULL);
return true;
}
void TextHook::RemoveHookCode()
{
MH_DisableHook(location);
2024-07-21 21:04:12 +08:00
while (useCount != 0)
;
2024-02-07 20:59:24 +08:00
MH_RemoveHook(location);
}
void TextHook::RemoveReadCode()
{
SetEvent(readerEvent);
2024-07-21 21:04:12 +08:00
if (GetThreadId(readerThread) != GetCurrentThreadId())
WaitForSingleObject(readerThread, 1000);
2024-02-07 20:59:24 +08:00
CloseHandle(readerEvent);
CloseHandle(readerThread);
}
void TextHook::Clear()
{
2024-07-21 21:04:12 +08:00
if (address == 0)
return;
if (savetypeforremove & DIRECT_READ)
RemoveReadCode();
else if (savetypeforremove & BREAK_POINT)
RemoveBreakPoint();
else
RemoveHookCode();
2024-02-07 20:59:24 +08:00
NotifyHookRemove(address, hp.name);
std::scoped_lock lock(viewMutex);
memset(&hp, 0, sizeof(HookParam));
address = 0;
2024-07-21 21:04:12 +08:00
if (local_buffer)
delete[] local_buffer;
2024-02-07 20:59:24 +08:00
}
2024-07-21 21:04:12 +08:00
int TextHook::GetLength(hook_stack *stack, uintptr_t in)
2024-02-07 20:59:24 +08:00
{
int len;
2024-07-21 21:04:12 +08:00
if (hp.type & USING_STRING)
2024-02-07 20:59:24 +08:00
{
2024-07-21 21:04:12 +08:00
if (hp.length_offset)
2024-02-07 20:59:24 +08:00
{
2024-07-21 21:04:12 +08:00
len = *((uintptr_t *)stack->base + hp.length_offset);
if (len >= 0)
2024-02-07 20:59:24 +08:00
{
if (hp.type & CODEC_UTF16)
len <<= 1;
2024-07-21 21:04:12 +08:00
else if (hp.type & CODEC_UTF32)
2024-02-07 20:59:24 +08:00
len <<= 2;
}
else if (len != -1)
{
}
else
2024-07-21 21:04:12 +08:00
{ // len==-1
len = HookStrlen((BYTE *)in);
2024-02-07 20:59:24 +08:00
}
}
else
{
2024-07-21 21:04:12 +08:00
len = HookStrlen((BYTE *)in);
2024-02-07 20:59:24 +08:00
}
}
else
{
if (hp.type & CODEC_UTF16)
len = 2;
2024-07-21 21:04:12 +08:00
else if (hp.type & CODEC_UTF32)
2024-02-07 20:59:24 +08:00
len = 4;
2024-07-21 21:04:12 +08:00
else
{ // CODEC_ANSI_BE,CHAR_LITTLE_ENDIAN
2024-02-07 20:59:24 +08:00
if (hp.type & CODEC_ANSI_BE)
in >>= 8;
len = !!IsDBCSLeadByteEx(hp.codepage, in & 0xff) + 1;
2024-07-21 21:04:12 +08:00
}
2024-02-07 20:59:24 +08:00
}
return max(0, len);
}
2024-07-21 21:04:12 +08:00
int TextHook::HookStrlen(BYTE *data)
2024-02-07 20:59:24 +08:00
{
2024-07-21 21:04:12 +08:00
return HookStrLen(&hp, data);
2024-02-07 20:59:24 +08:00
}
// EOF