Hook all steam imports

This commit is contained in:
momo5502 2022-06-26 10:07:46 +02:00
parent 89b188a1ca
commit 70ad1b5823
3 changed files with 60 additions and 50 deletions

View File

@ -5,54 +5,73 @@
#include <utils/finally.hpp> #include <utils/finally.hpp>
#include <utils/hook.hpp> #include <utils/hook.hpp>
#include <utils/nt.hpp> #include <utils/nt.hpp>
#include <utils/io.hpp>
#include <steam/steam.hpp>
namespace namespace
{ {
std::pair<void**, void*> g_original_import{};
DECLSPEC_NORETURN void WINAPI exit_hook(const int code) DECLSPEC_NORETURN void WINAPI exit_hook(const int code)
{ {
component_loader::pre_destroy(); component_loader::pre_destroy();
exit(code); exit(code);
} }
void patch_imports() std::pair<void**, void*> patch_steam_import(const std::string& func, void* function)
{ {
const utils::nt::library game{}; static const utils::nt::library game{};
const auto self = utils::nt::library::get_by_address(patch_imports);
auto patch_steam_import = [&](const std::string& func)
{
const auto game_entry = game.get_iat_entry("steam_api64.dll", func); const auto game_entry = game.get_iat_entry("steam_api64.dll", func);
if (!game_entry) if (!game_entry)
{ {
throw std::runtime_error("Import '" + func + "' not found!"); throw std::runtime_error("Import '" + func + "' not found!");
} }
const auto self_proc = self.get_proc<void*>(func); const auto original_import = game_entry;
if (!self_proc) utils::hook::set(game_entry, function);
{ return {game_entry, original_import};
throw std::runtime_error(func + " export not found");
} }
utils::hook::set(game_entry, self_proc);
};
patch_steam_import("SteamAPI_RegisterCallback"); bool restart_app_if_necessary_stub()
patch_steam_import("SteamAPI_RegisterCallResult"); {
patch_steam_import("SteamGameServer_Shutdown"); const std::string steam_path = steam::SteamAPI_GetSteamInstallPath();
patch_steam_import("SteamGameServer_RunCallbacks"); if (steam_path.empty() || !::utils::io::file_exists(steam_path + "/steam.exe"))
patch_steam_import("SteamGameServer_GetHSteamPipe"); {
patch_steam_import("SteamGameServer_GetHSteamUser"); MessageBoxA(nullptr, "Steam must be installed for the game to run. Please install Steam!", "Error",
patch_steam_import("SteamInternal_GameServer_Init"); MB_ICONERROR);
patch_steam_import("SteamAPI_UnregisterCallResult"); ShellExecuteA(nullptr, "open", "https://store.steampowered.com/about/", nullptr, nullptr, SW_SHOWNORMAL);
patch_steam_import("SteamAPI_UnregisterCallback"); TerminateProcess(GetCurrentProcess(), 1);
patch_steam_import("SteamAPI_RunCallbacks"); }
//patch_steam_import("SteamAPI_Shutdown");
patch_steam_import("SteamInternal_CreateInterface");
patch_steam_import("SteamAPI_GetHSteamUser");
patch_steam_import("SteamAPI_GetHSteamPipe");
patch_steam_import("SteamAPI_Init");
patch_steam_import("SteamAPI_RestartAppIfNecessary");
utils::hook::set(game.get_iat_entry("kernel32.dll", "ExitProcess"), exit_hook); utils::hook::set(g_original_import.first, g_original_import.second);
patch_steam_import("SteamAPI_Shutdown", steam::SteamAPI_Shutdown);
component_loader::post_unpack();
return steam::SteamAPI_RestartAppIfNecessary();
}
void patch_imports()
{
patch_steam_import("SteamAPI_RegisterCallback", steam::SteamAPI_RegisterCallback);
patch_steam_import("SteamAPI_RegisterCallResult", steam::SteamAPI_RegisterCallResult);
patch_steam_import("SteamGameServer_Shutdown", steam::SteamGameServer_Shutdown);
patch_steam_import("SteamGameServer_RunCallbacks", steam::SteamGameServer_RunCallbacks);
patch_steam_import("SteamGameServer_GetHSteamPipe", steam::SteamGameServer_GetHSteamPipe);
patch_steam_import("SteamGameServer_GetHSteamUser", steam::SteamGameServer_GetHSteamUser);
patch_steam_import("SteamInternal_GameServer_Init", steam::SteamInternal_GameServer_Init);
patch_steam_import("SteamAPI_UnregisterCallResult", steam::SteamAPI_UnregisterCallResult);
patch_steam_import("SteamAPI_UnregisterCallback", steam::SteamAPI_UnregisterCallback);
patch_steam_import("SteamAPI_RunCallbacks", steam::SteamAPI_RunCallbacks);
patch_steam_import("SteamInternal_CreateInterface", steam::SteamInternal_CreateInterface);
patch_steam_import("SteamAPI_GetHSteamUser", steam::SteamAPI_GetHSteamUser);
patch_steam_import("SteamAPI_GetHSteamPipe", steam::SteamAPI_GetHSteamPipe);
patch_steam_import("SteamAPI_Init", steam::SteamAPI_Init);
//patch_steam_import("SteamAPI_Shutdown", steam::SteamAPI_Shutdown);
g_original_import = patch_steam_import("SteamAPI_RestartAppIfNecessary", restart_app_if_necessary_stub);
utils::hook::set(utils::nt::library{}.get_iat_entry("kernel32.dll", "ExitProcess"), exit_hook);
} }
bool run() bool run()

View File

@ -104,23 +104,13 @@ namespace steam
results_.clear(); results_.clear();
} }
extern "C" { //extern "C" {
bool SteamAPI_RestartAppIfNecessary() bool SteamAPI_RestartAppIfNecessary()
{ {
const std::string steam_path = SteamAPI_GetSteamInstallPath();
if (!steam_path.empty() && ::utils::io::file_exists(steam_path + "/steam.exe"))
{
component_loader::post_unpack();
return false; return false;
} }
MessageBoxA(nullptr, "Steam must be installed for the game to run. Please install Steam!", "Error", MB_ICONERROR);
ShellExecuteA(nullptr, "open", "https://store.steampowered.com/about/", nullptr, nullptr, SW_SHOWNORMAL);
TerminateProcess(GetCurrentProcess(), 1);
return true;
}
bool SteamAPI_Init() bool SteamAPI_Init()
{ {
const std::filesystem::path steam_path = SteamAPI_GetSteamInstallPath(); const std::filesystem::path steam_path = SteamAPI_GetSteamInstallPath();
@ -293,5 +283,5 @@ namespace steam
return &user_stats; return &user_stats;
} }
} //}
} }

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#define STEAM_EXPORT extern "C" __declspec(dllexport) //#define STEAM_EXPORT extern "C" __declspec(dllexport)
#define STEAM_EXPORT
struct raw_steam_id final struct raw_steam_id final
{ {