From 70ad1b58232718439fda24daa5b9aa08dfb3bbe1 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sun, 26 Jun 2022 10:07:46 +0200 Subject: [PATCH] Hook all steam imports --- src/client/main.cpp | 91 +++++++++++++++++++++++--------------- src/client/steam/steam.cpp | 16 ++----- src/client/steam/steam.hpp | 3 +- 3 files changed, 60 insertions(+), 50 deletions(-) diff --git a/src/client/main.cpp b/src/client/main.cpp index 5651eacd..45cc4484 100644 --- a/src/client/main.cpp +++ b/src/client/main.cpp @@ -5,54 +5,73 @@ #include #include #include +#include + +#include namespace { + std::pair g_original_import{}; + DECLSPEC_NORETURN void WINAPI exit_hook(const int code) { component_loader::pre_destroy(); exit(code); } + std::pair patch_steam_import(const std::string& func, void* function) + { + static const utils::nt::library game{}; + + const auto game_entry = game.get_iat_entry("steam_api64.dll", func); + if (!game_entry) + { + throw std::runtime_error("Import '" + func + "' not found!"); + } + + const auto original_import = game_entry; + utils::hook::set(game_entry, function); + return {game_entry, original_import}; + } + + bool restart_app_if_necessary_stub() + { + const std::string steam_path = steam::SteamAPI_GetSteamInstallPath(); + if (steam_path.empty() || !::utils::io::file_exists(steam_path + "/steam.exe")) + { + 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); + } + + 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() { - const utils::nt::library game{}; - const auto self = utils::nt::library::get_by_address(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); - auto patch_steam_import = [&](const std::string& func) - { - const auto game_entry = game.get_iat_entry("steam_api64.dll", func); - if (!game_entry) - { - throw std::runtime_error("Import '" + func + "' not found!"); - } - - const auto self_proc = self.get_proc(func); - if (!self_proc) - { - throw std::runtime_error(func + " export not found"); - } - utils::hook::set(game_entry, self_proc); - }; - - patch_steam_import("SteamAPI_RegisterCallback"); - patch_steam_import("SteamAPI_RegisterCallResult"); - patch_steam_import("SteamGameServer_Shutdown"); - patch_steam_import("SteamGameServer_RunCallbacks"); - patch_steam_import("SteamGameServer_GetHSteamPipe"); - patch_steam_import("SteamGameServer_GetHSteamUser"); - patch_steam_import("SteamInternal_GameServer_Init"); - patch_steam_import("SteamAPI_UnregisterCallResult"); - patch_steam_import("SteamAPI_UnregisterCallback"); - 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(utils::nt::library{}.get_iat_entry("kernel32.dll", "ExitProcess"), exit_hook); } bool run() diff --git a/src/client/steam/steam.cpp b/src/client/steam/steam.cpp index 686a7adf..1d4f500c 100644 --- a/src/client/steam/steam.cpp +++ b/src/client/steam/steam.cpp @@ -104,21 +104,11 @@ namespace steam results_.clear(); } - extern "C" { + //extern "C" { 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; - } - - 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; + return false; } bool SteamAPI_Init() @@ -293,5 +283,5 @@ namespace steam return &user_stats; } - } + //} } diff --git a/src/client/steam/steam.hpp b/src/client/steam/steam.hpp index 8a0f8958..5999b2d1 100644 --- a/src/client/steam/steam.hpp +++ b/src/client/steam/steam.hpp @@ -1,6 +1,7 @@ #pragma once -#define STEAM_EXPORT extern "C" __declspec(dllexport) +//#define STEAM_EXPORT extern "C" __declspec(dllexport) +#define STEAM_EXPORT struct raw_steam_id final {