Add game_module component

This commit is contained in:
fed 2023-10-01 18:18:02 +02:00
parent 7c8236ada5
commit 39e87d401a
No known key found for this signature in database
GPG Key ID: 1D2C630F04722996
3 changed files with 141 additions and 6 deletions

View File

@ -0,0 +1,125 @@
#include <std_include.hpp>
#include "loader/component_loader.hpp"
#include "game/game.hpp"
#include "game_module.hpp"
#include <utils/hook.hpp>
namespace game_module
{
namespace
{
utils::hook::detour handle_a_hook;
utils::hook::detour handle_w_hook;
utils::hook::detour handle_ex_a_hook;
utils::hook::detour handle_ex_w_hook;
utils::hook::detour file_name_a_hook;
utils::hook::detour file_name_w_hook;
HMODULE __stdcall get_module_handle_a(const LPCSTR module_name)
{
if (!module_name)
{
return get_game_module();
}
return handle_a_hook.invoke<HMODULE>(module_name);
}
HMODULE __stdcall get_module_handle_w(const LPWSTR module_name)
{
if (!module_name)
{
return get_game_module();
}
return handle_w_hook.invoke<HMODULE>(module_name);
}
BOOL __stdcall get_module_handle_ex_a(const DWORD flags, const LPCSTR module_name, HMODULE* hmodule)
{
if (!module_name)
{
*hmodule = get_game_module();
return TRUE;
}
return handle_ex_a_hook.invoke<BOOL>(flags, module_name, hmodule);
}
BOOL __stdcall get_module_handle_ex_w(const DWORD flags, const LPCWSTR module_name, HMODULE* hmodule)
{
if (!module_name)
{
*hmodule = get_game_module();
return TRUE;
}
return handle_ex_w_hook.invoke<BOOL>(flags, module_name, hmodule);
}
DWORD __stdcall get_module_file_name_a(HMODULE hmodule, const LPSTR filename, const DWORD size)
{
if (!hmodule || utils::nt::library(hmodule) == get_game_module())
{
hmodule = get_host_module();
}
return file_name_a_hook.invoke<DWORD>(hmodule, filename, size);
}
DWORD __stdcall get_module_file_name_w(HMODULE hmodule, const LPWSTR filename, const DWORD size)
{
if (!hmodule || utils::nt::library(hmodule) == get_game_module())
{
hmodule = get_host_module();
}
return file_name_w_hook.invoke<DWORD>(hmodule, filename, size);
}
void hook_module_resolving()
{
handle_a_hook.create(&GetModuleHandleA, &get_module_handle_a);
handle_w_hook.create(&GetModuleHandleW, &get_module_handle_w);
handle_ex_w_hook.create(&GetModuleHandleExA, &get_module_handle_ex_a);
handle_ex_w_hook.create(&GetModuleHandleExW, &get_module_handle_ex_w);
file_name_a_hook.create(&GetModuleFileNameA, &get_module_file_name_a);
file_name_w_hook.create(&GetModuleFileNameW, &get_module_file_name_w);
}
}
utils::nt::library get_game_module()
{
static utils::nt::library game{HMODULE(BASE_ADDRESS)};
return game;
}
utils::nt::library get_host_module()
{
static utils::nt::library host{};
return host;
}
class component final : public component_interface
{
public:
void post_start() override
{
get_host_module();
}
void post_load() override
{
#ifdef INJECT_HOST_AS_LIB
hook_module_resolving();
#else
assert(get_host_module() == get_game_module());
#endif
}
};
}
REGISTER_COMPONENT(game_module::component)

View File

@ -0,0 +1,9 @@
#pragma once
#include <utils/nt.hpp>
namespace game_module
{
utils::nt::library get_game_module();
utils::nt::library get_host_module();
}

View File

@ -23,8 +23,7 @@ namespace patches
DECLSPEC_NORETURN void quit_stub() DECLSPEC_NORETURN void quit_stub()
{ {
component_loader::pre_destroy(); utils::hook::invoke<void>(0x1408B1BA0);
exit(0);
} }
void gscr_set_save_dvar_stub() void gscr_set_save_dvar_stub()
@ -80,12 +79,14 @@ namespace patches
public: public:
void post_unpack() override void post_unpack() override
{ {
// Fix startup crashes // Fix startup crash (bnet)
utils::hook::set(0x140633080, 0xC301B0);
utils::hook::set(0x140272F70, 0xC301B0); utils::hook::set(0x140272F70, 0xC301B0);
utils::hook::jump(0x140046148, sub_46148);
utils::hook::jump(0x1408B1CD0, quit_stub); // Fix shutdown crash
utils::hook::jump(0x1408B1CD0, 0x1408B1BA0);
// Disable splash screen
utils::hook::nop(0x14064F546, 5);
// Unlock fps in main menu // Unlock fps in main menu
utils::hook::set<BYTE>(0x1403D8E1B, 0xEB); utils::hook::set<BYTE>(0x1403D8E1B, 0xEB);