Skip to content

Commit

Permalink
Add patterns for showing menu in all games
Browse files Browse the repository at this point in the history
  • Loading branch information
TheIndra55 committed Jan 20, 2024
1 parent f9f7a8d commit db47a29
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 14 deletions.
42 changes: 38 additions & 4 deletions src/Hook.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
#include <MinHook.h>
#include <Hooking.Patterns.h>

#include "Hook.h"
#include "input/MessageHook.h"
#include "instance/Instances.h"
#include "game/Game.h"
#include "render/Font.h"

// Modules
#include "modules/MainMenu.h"
#include "modules/InstanceViewer.h"
#include "modules/Skew.h"
#include "modules/Render.h"
#include "modules/Draw.h"
#include "modules/Log.h"

#include "cdc/render/PCDeviceManager.h"

using namespace std::placeholders;

static bool(*s_D3D_Init)();

// Pointer to the cdc::DeviceManager::s_pInstance pointer
static void* s_deviceManager;

static bool D3D_Init()
{
// Initialize the device
Expand All @@ -32,12 +41,19 @@ Hook::Hook() : m_menu(nullptr), m_modules()

void Hook::Initialize()
{
Game::Init();

RegisterModules();

#ifndef TR8
auto match = hook::pattern("A1 ? ? ? ? 8B 0D ? ? ? ? 68 ? ? ? ? 50 E8").count(1);
s_deviceManager = *match.get_first<void*>(7);
#else
auto match = hook::pattern("8B 0D ? ? ? ? 8B 01 8B 15 ? ? ? ? 8B 00 68").count(1);
s_deviceManager = *match.get_first<void*>(2);
#endif

// Create the initial hook
MH_Initialize();
MH_CreateHook((void*)0x4153E0, D3D_Init, (void**)&s_D3D_Init);
MH_CreateHook(match.get_first(), D3D_Init, (void**)&s_D3D_Init);
MH_EnableHook(MH_ALL_HOOKS);
}

Expand All @@ -48,6 +64,10 @@ void Hook::PostInitialize()

// Register the message hook
MessageHook::OnMessage(std::bind(&Hook::OnMessage, this, _1, _2, _3, _4));

#ifndef TR8
Font::OnFlush(std::bind(&Hook::OnFrame, this));
#endif
}

void Hook::OnMessage(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
Expand All @@ -60,10 +80,18 @@ void Hook::OnMessage(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
}
}

void Hook::OnFrame()
{
for (auto& mod : m_modules)
{
mod->OnFrame();
}
}

void Hook::OnDevice()
{
// Assign the DeviceManager instance
cdc::PCDeviceManager::s_pInstance = *(cdc::PCDeviceManager**)0xA6669C;
cdc::PCDeviceManager::s_pInstance = *(cdc::PCDeviceManager**)s_deviceManager;

// Initialize the hook
PostInitialize();
Expand All @@ -79,8 +107,14 @@ void Hook::RegisterModule()

void Hook::RegisterModules()
{
RegisterModule<MainMenu>();
RegisterModule<InstanceViewer>();
RegisterModule<Skew>();
#ifndef TR8
RegisterModule<Render>();
RegisterModule<Draw>();
RegisterModule<Log>();
#endif
}

Hook& Hook::GetInstance()
Expand Down
1 change: 1 addition & 0 deletions src/Hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Hook
void RegisterModules();

void OnMessage(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
void OnFrame();

public:
Hook();
Expand Down
9 changes: 8 additions & 1 deletion src/cdc/render/PCDeviceManager.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <Hooking.Patterns.h>

#include "PCDeviceManager.h"

cdc::PCDeviceManager* cdc::PCDeviceManager::s_pInstance = nullptr;
Expand All @@ -24,7 +26,12 @@ HWND cdc::PCDeviceManager::GetWindow()

void cdc::PCDeviceManager::AddDeviceResource(PCInternalResource* resource)
{
auto func = reinterpret_cast<void(__thiscall*)(PCDeviceManager*, PCInternalResource*)>(0x00616F10);
#ifndef TR8
auto match = hook::pattern("8B 41 08 33 D2 3B C2 8B 44 24 04").count(1);
#else
auto match = hook::pattern("8B 44 24 04 33 D2 39 51 08 89 50").count(1);
#endif

auto func = (void(__thiscall*)(PCDeviceManager*, PCInternalResource*))match.get_first();
func(this, resource);
}
9 changes: 6 additions & 3 deletions src/game/Game.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "Game.h"

void Game::Init()
{
}
#include "util/Hooking.h"

Instance* Game::GetPlayerInstance()
{
Expand All @@ -12,4 +10,9 @@ Instance* Game::GetPlayerInstance()
GameTracker* Game::GetGameTracker()
{
return (GameTracker*)0x838330;
}

void GAMELOOP_ExitGame(char* name, GameTracker* gameTracker, int doneType)
{
Hooking::Call(0xC61CFA, name, gameTracker, doneType);
}
6 changes: 3 additions & 3 deletions src/game/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ struct GameTracker
class Game
{
public:
static void Init();

static Instance* GetPlayerInstance();
static GameTracker* GetGameTracker();
};
};

void GAMELOOP_ExitGame(char* name, GameTracker* gameTracker, int doneType);
8 changes: 8 additions & 0 deletions src/modules/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@
class Module
{
public:
// Called during drawing of the main menu bar, can be used to add menu items
virtual void OnMenu() { };

// Called during menu drawing, used for ImGui draw code
virtual void OnDraw() { };

// Called just before a frame ends, Font::Flush to be specific
virtual void OnFrame() { };

// Called when a message is processed by the window procedure
virtual void OnInput(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { };
};
62 changes: 62 additions & 0 deletions src/render/Font.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <stdio.h>
#include <stdarg.h>

#include <MinHook.h>

#include "Font.h"
#include "util/Hooking.h"

static std::function<void()> s_callback;

// Font::Flush
static void(* s_Flush)();

static void Flush()
{
s_callback();
s_Flush();
}

char Font::s_formatted[1024];

Font* Font::GetMainFont()
{
return *(Font**)0x7D1800;
}

void Font::SetCursor(float x, float y)
{
Hooking::Call(0x433C70, x, y);
}

void Font::SetScale(float scaleX, float scaleY)
{
Hooking::Call(0x433E60, scaleX, scaleY);
}

void Font::Print(const char* fmt, ...)
{
va_list va;

va_start(va, fmt);
vsprintf_s(s_formatted, fmt, va);
va_end(va);

PrintFormatted(s_formatted);
}

void Font::PrintFormatted(const char* formatted, int backdrop)
{
Hooking::ThisCall(0x434A70, this, formatted, backdrop);
}

void Font::OnFlush(std::function<void()> callback)
{
if (!s_callback)
{
MH_CreateHook((void*)0x434C40, Flush, (void**)&s_Flush);
MH_EnableHook(MH_ALL_HOOKS);
}

s_callback = callback;
}
20 changes: 20 additions & 0 deletions src/render/Font.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <functional>

class Font
{
private:
static char s_formatted[1024];

public:
static Font* GetMainFont();

static void SetCursor(float x, float y);
static void SetScale(float scaleX, float scaleY);

void Print(const char* fmt, ...);
void PrintFormatted(const char* formatted, int backdrop = 0);

static void OnFlush(std::function<void()> callback);
};
10 changes: 8 additions & 2 deletions src/render/RenderContext.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <Windows.h>

#include <Hooking.Patterns.h>
#include <MinHook.h>

#include "RenderContext.h"
Expand All @@ -19,7 +19,13 @@ void RenderContext::OnPresent(std::function<void()> callback)
{
if (!s_callback)
{
MH_CreateHook((void*)0x61BB80, Present, (void**)&s_Present);
#ifndef TR8
auto match = hook::pattern("8B 41 14 85 C0 74 19 8B 54 24 0C").count(1);
#else
auto match = hook::pattern("A1 ? ? ? ? 83 78 10 00 75 39 80 B8").count(1);
#endif

MH_CreateHook(match.get_first(), Present, (void**)&s_Present);
MH_EnableHook(MH_ALL_HOOKS);
}

Expand Down
16 changes: 15 additions & 1 deletion src/util/Hooking.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,18 @@ class Hooking
{
reinterpret_cast<T(*)(Args...)>(address)(args...);
}
};

// Calls a class function
template<typename... Args>
static inline void ThisCall(unsigned int address, Args... args)
{
reinterpret_cast<void(__thiscall*)(Args...)>(address)(args...);
}

// Calls a class function with a return value
template<typename T, typename... Args>
static inline T ThisCallReturn(unsigned int address, Args... args)
{
reinterpret_cast<T(__thiscall*)(Args...)>(address)(args...);
}
};

0 comments on commit db47a29

Please sign in to comment.