[Hooking] Optimizie inline-asm hooks

This commit is contained in:
momo5502 2017-02-01 13:44:25 +01:00
parent 7954330418
commit 29acb108be
15 changed files with 196 additions and 94 deletions

View File

@ -84,15 +84,28 @@ namespace Components
push esi push esi
push edi push edi
push eax
pushad
// Check if custom handler should be bypassed // Check if custom handler should be bypassed
call AssetHandler::HasThreadBypass call AssetHandler::HasThreadBypass
mov [esp + 20h], eax
popad
pop eax
test al, al test al, al
jnz finishOriginal jnz finishOriginal
mov ecx, [esp + 18h] // Asset type mov ecx, [esp + 18h] // Asset type
mov ebx, [esp + 1Ch] // Filename mov ebx, [esp + 1Ch] // Filename
push eax
pushad
push ebx push ebx
push ecx push ecx
@ -100,6 +113,11 @@ namespace Components
add esp, 8h add esp, 8h
mov[esp + 20h], eax
popad
pop eax
test eax, eax test eax, eax
jnz finishFound jnz finishFound
@ -196,11 +214,18 @@ namespace Components
{ {
__asm __asm
{ {
push [esp + 8] push eax
push [esp + 8] pushad
push [esp + 2Ch]
push [esp + 2Ch]
call AssetHandler::IsAssetEligible call AssetHandler::IsAssetEligible
add esp, 08h add esp, 08h
mov [esp + 20h], eax
popad
pop eax
test al, al test al, al
jz doNotLoad jz doNotLoad

View File

@ -97,7 +97,7 @@ namespace Components
Network::SendCommand(sock, adr, "connect", connectData.SerializeAsString()); Network::SendCommand(sock, adr, "connect", connectData.SerializeAsString());
} }
void Auth::ParseConnectData(Game::msg_t* msg, Game::netadr_t addr) void Auth::ParseConnectData(Game::msg_t* msg, Game::netadr_t* addr)
{ {
Network::Address address(addr); Network::Address address(addr);
@ -210,12 +210,17 @@ namespace Components
{ {
__asm __asm
{ {
pushad
lea eax, [esp + 20h]
push eax
push esi push esi
call Auth::ParseConnectData call Auth::ParseConnectData
pop esi pop esi
pop eax
popad
mov edi, 6265FEh push 6265FEh
jmp edi retn
} }
} }

View File

@ -47,7 +47,7 @@ namespace Components
static Utils::Cryptography::ECC::Key GuidKey; static Utils::Cryptography::ECC::Key GuidKey;
static void SendConnectDataStub(Game::netsrc_t sock, Game::netadr_t adr, const char *format, int len); static void SendConnectDataStub(Game::netsrc_t sock, Game::netadr_t adr, const char *format, int len);
static void ParseConnectData(Game::msg_t* msg, Game::netadr_t addr); static void ParseConnectData(Game::msg_t* msg, Game::netadr_t* addr);
static void DirectConnectStub(); static void DirectConnectStub();
static void Frame(); static void Frame();

View File

@ -452,7 +452,9 @@ namespace Components
// We need esi preserved here, so we have to backup 'all' registers when storing the safearea // We need esi preserved here, so we have to backup 'all' registers when storing the safearea
call Game::Con_DrawSolidConsole call Game::Con_DrawSolidConsole
pushad
call Console::RestoreSafeArea call Console::RestoreSafeArea
popad
retn retn
} }
} }

View File

@ -67,7 +67,9 @@ namespace Components
{ {
__asm __asm
{ {
pushad
call Dedicated::PostInitialization call Dedicated::PostInitialization
popad
// Start Com_EvenLoop // Start Com_EvenLoop
mov eax, 43D140h mov eax, 43D140h

View File

@ -49,9 +49,11 @@ namespace Components
mov ecx, 5A6510h mov ecx, 5A6510h
call ecx call ecx
pushad
push esi push esi
call Lean::SetLeanFlags call Lean::SetLeanFlags
pop esi pop esi
popad
retn retn
} }
} }

View File

@ -143,13 +143,17 @@ namespace Components
{ {
__asm __asm
{ {
pushad
push 1 push 1
push [esp + 8h] push [esp + 28h]
call Logger::NetworkLog call Logger::NetworkLog
add esp, 8h add esp, 8h
mov eax, 4576C0h popad
jmp eax
push 4576C0h
retn
} }
} }
@ -161,22 +165,26 @@ namespace Components
test eax, eax test eax, eax
jz returnPrint jz returnPrint
push [esp + 8h] pushad
push [esp + 28h]
call Logger::PrintMessagePipe call Logger::PrintMessagePipe
add esp, 4h add esp, 4h
popad
retn retn
returnPrint: returnPrint:
pushad
push 0 push 0
push [esp + 0Ch] push [esp + 2Ch]
call Logger::NetworkLog call Logger::NetworkLog
add esp, 8h add esp, 8h
popad
push esi push esi
mov esi, [esp + 0Ch] mov esi, [esp + 0Ch]
mov eax, 4AA835h push 4AA835h
jmp eax retn
} }
} }

View File

@ -54,12 +54,19 @@ namespace Components
{ {
__asm __asm
{ {
push eax
pushad
push ecx push ecx
call Materials::ResolveMaterial call Materials::ResolveMaterial
add esp, 4h add esp, 4h
mov edx, 5310F0h mov[esp + 20h], eax
jmp edx popad
pop eax
push 5310F0h
retn
} }
} }
@ -82,13 +89,21 @@ namespace Components
{ {
__asm __asm
{ {
push eax
pushad
push edx // Material push edx // Material
push eax // offset push eax // offset
push ecx // String push ecx // String
call Materials::WriteDeathMessageIcon call Materials::WriteDeathMessageIcon
add esp, 0Ch
add esp, 14h mov[esp + 20h], eax
popad
pop eax
add esp, 8h
retn retn
} }
} }

View File

@ -302,10 +302,16 @@ namespace Components
__asm __asm
{ {
lea eax, [esp + 0C54h] lea eax, [esp + 0C54h]
pushad
push ebp // Command push ebp // Command
push eax // Address pointer push eax // Address pointer
call Network::DeployPacket call Network::DeployPacket
add esp, 8h add esp, 8h
popad
mov al, 1 mov al, 1
pop edi pop edi
pop esi pop esi

View File

@ -52,16 +52,21 @@ namespace Components
{ {
__asm __asm
{ {
lea ecx, [esp + 10h] pushad
lea ecx, [esp + 30h]
push ecx push ecx
call Script::StoreScriptName call Script::StoreScriptName
add esp, 4h add esp, 4h
popad
push ebp push ebp
mov ebp, ds:1CDEAA8h mov ebp, ds:1CDEAA8h
mov ecx, 427DC3h
jmp ecx push 427DC3h
retn
} }
} }
@ -75,12 +80,14 @@ namespace Components
{ {
__asm __asm
{ {
pushad
call Script::RestoreScriptName call Script::RestoreScriptName
popad
mov ds:1CDEAA8h, ebp mov ds:1CDEAA8h, ebp
mov eax, 427E77h push 427E77h
jmp eax retn
} }
} }

View File

@ -99,11 +99,14 @@ namespace Components
{ {
__asm __asm
{ {
pushad
push eax push eax
call ServerInfo::DrawScoreboardInfo call ServerInfo::DrawScoreboardInfo
pop eax pop eax
mov ecx, 591B70h popad
jmp ecx
push 591B70h
retn
} }
} }

View File

@ -85,15 +85,17 @@ namespace Components
{ {
__asm __asm
{ {
pushad
call Theatre::WriteBaseline call Theatre::WriteBaseline
popad
// Restore overwritten operation // Restore overwritten operation
mov ecx, 0A5E9C4h mov ecx, 0A5E9C4h
mov [ecx], 0 mov [ecx], 0
// Return to original code // Return to original code
mov ecx, 5A863Ah push 5A863Ah
jmp ecx retn
} }
} }
@ -110,8 +112,8 @@ namespace Components
retn retn
continue: continue:
mov eax, 5A1AD0h push 5A1AD0h
jmp eax retn
} }
} }
@ -129,8 +131,8 @@ namespace Components
continue: continue:
mov eax, 0B2BB90h mov eax, 0B2BB90h
mov esi, 5A8E08h push 5A8E08h
jmp esi retn
} }
} }
@ -150,8 +152,8 @@ namespace Components
mov ecx, [esp + 10h] mov ecx, [esp + 10h]
push 10h push 10h
push ecx push ecx
mov eax, 4CB3F6h push 4CB3F6h
jmp eax retn
} }
} }

View File

@ -126,9 +126,11 @@ namespace Components
{ {
__asm __asm
{ {
pushad
push edi push edi
call Zones::LoadXModelLodInfo call Zones::LoadXModelLodInfo
add esp, 4h add esp, 4h
popad
mov eax, [esp + 8h] mov eax, [esp + 8h]
push eax push eax

View File

@ -345,13 +345,16 @@ namespace Game
return poolEntry; return poolEntry;
} }
void Menu_FreeItemMemory(Game::itemDef_t* item) __declspec(naked) void Menu_FreeItemMemory(Game::itemDef_t* /*item*/)
{ {
__asm __asm
{ {
mov edi, item pushad
mov edi, [esp + 24h]
mov eax, 63D880h mov eax, 63D880h
call eax call eax
popad
retn
} }
} }
@ -402,20 +405,22 @@ namespace Game
return gameType; return gameType;
} }
float UI_GetScoreboardLeft(void* a1) __declspec(naked) float UI_GetScoreboardLeft(void* /*a1*/)
{ {
// ReSharper disable once CppEntityNeverUsed
static int func = 0x590390;
float result = 0;
__asm __asm
{ {
mov eax, a1 push eax
call func pushad
mov result, eax
}
return result; mov ecx, 590390h
mov eax, [esp + 28h]
call ecx
mov[esp + 20h], eax
popad
pop eax
retn
}
} }
const char *DB_GetXAssetName(XAsset *asset) const char *DB_GetXAssetName(XAsset *asset)
@ -463,32 +468,39 @@ namespace Game
return false; return false;
} }
XAssetHeader DB_FindXAssetDefaultHeaderInternal(XAssetType _type) __declspec(naked) XAssetHeader DB_FindXAssetDefaultHeaderInternal(XAssetType /*type*/)
{
// ReSharper disable once CppEntityNeverUsed
static int func = 0x5BB210;
XAssetHeader result;
__asm
{
push edi
mov edi, _type
call func
pop edi
mov result, eax
}
return result;
}
void FS_AddLocalizedGameDirectory(const char *path, const char *dir)
{ {
__asm __asm
{ {
mov ebx, path push eax
mov eax, dir pushad
mov eax, 5BB210h
mov edi, [esp + 28h]
call eax
mov [esp + 20h], eax
popad
pop eax
retn
}
}
__declspec(naked) void FS_AddLocalizedGameDirectory(const char* /*path*/, const char* /*dir*/)
{
__asm
{
pushad
mov ebx, [esp + 24h]
mov eax, [esp + 28h]
mov ecx, 642EF0h mov ecx, 642EF0h
call ecx call ecx
popad
retn
} }
} }
@ -515,52 +527,57 @@ namespace Game
return hash; return hash;
} }
void R_LoadSunThroughDvars(const char* mapname, sunflare_t* sun) __declspec(naked) void R_LoadSunThroughDvars(const char* /*mapname*/, sunflare_t* /*sun*/)
{ {
__asm __asm
{ {
push ecx pushad
push sun push [esp + 28h]
mov eax, mapname mov eax, [esp + 28h]
mov ecx, 53F990h mov ecx, 53F990h
call ecx call ecx
add esp, 4h add esp, 4h
pop ecx popad
retn
} }
} }
void R_SetSunFromDvars(sunflare_t* sun) __declspec(naked) void R_SetSunFromDvars(sunflare_t* /*sun*/)
{ {
__asm __asm
{ {
push esi pushad
mov esi, sun mov esi, [esp + 24h]
mov eax, 53F6D0h mov eax, 53F6D0h
call ecx call eax
pop esi popad
retn
} }
} }
void SV_KickClient(client_t* client, const char* reason) __declspec(naked) void SV_KickClient(client_t* /*client*/, const char* /*reason*/)
{ {
__asm __asm
{ {
push edi pushad
push esi
mov edi, 0 mov edi, 0
mov esi, client mov esi, [esp + 24h]
push reason push[esp + 28h]
push 0 push 0
push 0 push 0
mov eax, 6249A0h mov eax, 6249A0h
call eax call eax
add esp, 0Ch add esp, 0Ch
pop esi
pop edi popad
retn
} }
} }
@ -568,7 +585,7 @@ namespace Game
{ {
if (client->state < 5) if (client->state < 5)
{ {
Components::Network::Send(client->addr, Utils::String::VA("error\n%s", reason.data())); Components::Network::SendCommand(client->addr, "error", reason);
} }
SV_KickClient(client, reason.data()); SV_KickClient(client, reason.data());
@ -584,27 +601,29 @@ namespace Game
Game::SV_GameSendServerCommand(clientNum, 0, Utils::String::VA("%c \"%s\"", 0x67, message.data())); Game::SV_GameSendServerCommand(clientNum, 0, Utils::String::VA("%c \"%s\"", 0x67, message.data()));
} }
void IN_KeyUp(kbutton_t* button) __declspec(naked) void IN_KeyUp(kbutton_t* /*button*/)
{ {
__asm __asm
{ {
push esi pushad
mov esi, button mov esi, [esp + 24h]
mov eax, 5A5580h mov eax, 5A5580h
call eax call eax
pop esi popad
retn
} }
} }
void IN_KeyDown(kbutton_t* button) __declspec(naked) void IN_KeyDown(kbutton_t* /*button*/)
{ {
__asm __asm
{ {
push esi pushad
mov esi, button mov esi, [esp + 24h]
mov eax, 5A54E0h mov eax, 5A54E0h
call eax call eax
pop esi popad
retn
} }
} }
@ -639,23 +658,23 @@ namespace Game
} }
} }
void Load_VertexBuffer(void* data, IDirect3DVertexBuffer9** where, int len) __declspec(naked) void Load_VertexBuffer(void* /*data*/, IDirect3DVertexBuffer9** /*where*/, int /*len*/)
{ {
__asm __asm
{ {
push edi pushad
push ebx
mov eax, len mov eax, [esp + 2Ch]
mov edi, where mov edi, [esp + 28h]
push data push[esp + 24h]
mov ebx, 5112C0h mov ebx, 5112C0h
call ebx call ebx
add esp, 4 add esp, 4
pop ebx popad
pop edi
retn
} }
} }

View File

@ -65,11 +65,15 @@ BOOL APIENTRY DllMain(HMODULE /*hModule*/, DWORD ul_reason_for_call, LPVOID /*l
{ {
__asm __asm
{ {
pushad
// This has to be called, otherwise the hook is not uninstalled and we're looping into infinity // This has to be called, otherwise the hook is not uninstalled and we're looping into infinity
call Main::Initialize call Main::Initialize
mov eax, 6BAC0Fh popad
jmp eax
push 6BAC0Fh
retn
} }
})->install(); })->install();
} }