Skip to content

Commit

Permalink
Add Steam support for TRAE
Browse files Browse the repository at this point in the history
  • Loading branch information
TheIndra55 committed Jun 13, 2021
1 parent cddd334 commit 9103a30
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ void Game::Initialize()
INSTANCE_HideUnhideDrawGroup = reinterpret_cast<void(__cdecl*)(int, int, int)>(0x004319B0);

#if TRAE
OBTABLE_GetObjectID = reinterpret_cast<int(__cdecl*)(char*)>(0x00C63B7D);
OBTABLE_GetObjectID = reinterpret_cast<int(__cdecl*)(char*)>(0x4660C0);

INSTANCE_BirthObjectNoParent = reinterpret_cast<Instance*(__cdecl*)(int, cdc::Vector*, cdc::Vector*, DWORD*, DWORD*, int, int)>(0x0045BA90);
STREAM_GetObjectTrackerByName = reinterpret_cast<ObjectTracker*(__cdecl*)(char*)>(0x00C7D67C);
STREAM_GetObjectTrackerByName = reinterpret_cast<ObjectTracker*(__cdecl*)(char*)>(0x5DA260);
STREAM_PollLoadQueue = reinterpret_cast<bool(__cdecl*)()>(0x005DB190);
#elif TR7
OBTABLE_GetObjectID = reinterpret_cast<int(__cdecl*)(char*)>(0x004655B0);
Expand Down
18 changes: 10 additions & 8 deletions Hooking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
#include "ControlHooks.hpp"
#include "Camera.hpp"

extern Hooking* g_hooking;

LPDIRECT3DDEVICE9 pDevice;
HWND pHwnd;

Hooking::Hooking()
: m_menu(nullptr)
{
MH_Initialize();

// hook into d3d9 creation function and wait for a device
#if TRAE
MH_CreateHook(reinterpret_cast<void*>(0xC5C175), hooked_Direct3DInit, reinterpret_cast<void**>(&original_Direct3DInit));
auto pFound = FindPattern((PBYTE)"\xE8\x00\x00\x00\x00\x85\xC0\x75\x00\xE8\x00\x00\x00\x00\x6A\x00\x6A\x22\xE8", "x????xxx?x????xxxxx");
auto D3D_Init = GetAddress(pFound, 1, 5);

MH_CreateHook(reinterpret_cast<void*>(D3D_Init), hooked_Direct3DInit, reinterpret_cast<void**>(&original_Direct3DInit));
#elif TR8
MH_CreateHook(reinterpret_cast<void*>(0x478640), hooked_Direct3DInit, reinterpret_cast<void**>(&original_Direct3DInit));
#elif TR7
Expand Down Expand Up @@ -91,6 +90,7 @@ void __fastcall PCDeviceManager__ReleaseDevice(DWORD* _this, DWORD _, int status
orginal_PCDeviceManager__ReleaseDevice(_this, status);
}

#if TRAE || TR7
int(__thiscall* origTerrainDrawable_TerrainDrawable)(DWORD _this, int* a2, int a3, int a4, int a5);
int __fastcall TerrainDrawable_TerrainDrawable(DWORD _this, DWORD _, int* a2, int a3, int a4, int a5)
{
Expand All @@ -114,6 +114,7 @@ int __cdecl GetDrawListByTpageId(unsigned int tpageid, bool reflect)

return origGetDrawListByTpageId(tpageid, reflect);
}
#endif

float* (__cdecl* TRANS_RotTransPersVectorf)(DWORD a1, DWORD a2);
void(__cdecl* Font__Print)(DWORD font, const char* a2, ...);
Expand Down Expand Up @@ -466,11 +467,12 @@ void Hooking::GotDevice()
objCheckFamily = reinterpret_cast<bool(__cdecl*)(DWORD instance, unsigned __int16 family)>(0x534660);

MH_CreateHook((void*)0x00434C40, Font__Flush, (void**)&org_Font__Flush);
Font__Print = reinterpret_cast<void(__cdecl*)(DWORD, const char*, ...)>(0x00C5F83D);
Font__Print = reinterpret_cast<void(__cdecl*)(DWORD, const char*, ...)>(0x00434C10);

TRANS_RotTransPersVectorf = reinterpret_cast<float*(__cdecl*)(DWORD, DWORD)>(0x00402B50);

MH_CreateHook((void*)0xC5B896, TerrainDrawable_TerrainDrawable, (void**)&origTerrainDrawable_TerrainDrawable);
MH_CreateHook((void*)0xC5C280, GetDrawListByTpageId, (void**)&origGetDrawListByTpageId);
MH_CreateHook((void*)0x40B9B0, TerrainDrawable_TerrainDrawable, (void**)&origTerrainDrawable_TerrainDrawable);
MH_CreateHook((void*)0x4158E0, GetDrawListByTpageId, (void**)&origGetDrawListByTpageId);

TRANS_TransToDrawVertexV4f = reinterpret_cast<void(__cdecl*)(DRAWVERTEX* v, cdc::Vector * vec)>(0x00402F20);

Expand Down
7 changes: 5 additions & 2 deletions Hooking.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Hooking
};

static int(*original_Direct3DInit)();
static int hooked_Direct3DInit();
int hooked_Direct3DInit();

#if TRAE || TR7
static void(__thiscall* original_PCRenderContext_Present)(DWORD*, int, int, int);
Expand All @@ -45,4 +45,7 @@ static BOOL WINAPI hooked_SetCursorPos(int x, int y);

extern bool isDiskFS;

void NOP(void* address, int num);
void NOP(void* address, int num);

uint8_t __declspec(noinline)* FindPattern(BYTE* bMask, char* szMask);
uint8_t __declspec(noinline)* GetAddress(uint8_t* ptr, uint8_t offset, uint8_t instr_size);
13 changes: 6 additions & 7 deletions Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,27 +207,26 @@ Menu::Menu(LPDIRECT3DDEVICE9 pd3dDevice, HWND hwnd)
ImGui_ImplDX9_Init(m_pd3dDevice);

#if TRAE
MH_CreateHook((void*)0x00C62479, newinstance, (void**)&INSTANCE_NewInstance);
MH_CreateHook((void*)0x00457580, newinstance, (void**)&INSTANCE_NewInstance);
MH_CreateHook((void*)0x4FCB60, pushscreenhooked, (void**)&pushscreen);
MH_CreateHook((void*)0x0046F080, hooked_Subtitle_Add, (void**)&orginal_Subtitle_Add);

MH_CreateHook((void*)0x004E6EC0, IsPs2, nullptr);

MH_CreateHook((void*)0x00C7DC5B, STREAM_LoadLevel, (void**)&origSTREAM_LoadLevel);
MH_CreateHook((void*)0x005DBBA0, STREAM_LoadLevel, (void**)&origSTREAM_LoadLevel);
MH_CreateHook((void*)0x005DB680, STREAM_FinishLoad, (void**)&origSTREAM_FinishLoad);

MH_CreateHook((void*)0x00424FE0, CinematicHandlerImpl_NextFrame, (void**)&origCinematicHandlerImpl_NextFrame);
#endif

#if TRAE
MH_CreateHook((void*)0x0045F640, getFS, nullptr);
MH_CreateHook((void*)0x00C63241, unitFileName, (void**)&origUnitFileName);

MH_CreateHook((void*)0x00C7D980, InsertGlobalObject, (void**)&origInsertGlobalObject);
MSFileSystem_FileExists = reinterpret_cast<int(__thiscall*)(int _this, const char* file)>(0x005E52C0);

MH_CreateHook((void*)0x00401480, IMAGE_LoadImage, (void**)&origIMAGE_LoadImage);
MH_CreateHook((void*)0x00C63280, imageFileName, (void**)&origImageFileName);

MH_CreateHook((void*)0x45F650, unitFileName, (void**)&origUnitFileName);
MH_CreateHook((void*)0x5DB360, InsertGlobalObject, (void**)&origInsertGlobalObject);
MH_CreateHook((void*)0x0045F6A0, imageFileName, (void**)&origImageFileName);

INSTANCE_ReallyRemoveInstance = reinterpret_cast<int(__cdecl*)(Instance*, int, char)>(0x0045A3A0);
INSTANCE_SetModel = reinterpret_cast<void(__cdecl*)(Instance * instance, int model)>(0x00458A90);
Expand Down
26 changes: 26 additions & 0 deletions dllmain.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
#include "Hooking.hpp"

static bool hit = false;

BOOL (WINAPI* dGetVersionExA)(LPOSVERSIONINFOA lpStartupInfo);

BOOL WINAPI hGetVersionExA(LPOSVERSIONINFOA lpStartupInfo)
{
if (!hit)
{
Hooking::GetInstance(); // Will call the ctor
hit = true;

// TODO MH_RemoveHook
}

return dGetVersionExA(lpStartupInfo);
}

DWORD WINAPI Hook(LPVOID lpParam)
{
MH_Initialize();

#if TRAE
// we cannot insert our hooks now since game is not done yet unpacking
// hook one of the first functions called from unpacked code and insert our hooks then
MH_CreateHookApi(L"Kernel32", "GetStartupInfoW", hGetVersionExA, reinterpret_cast<void**>(&dGetVersionExA));
MH_EnableHook(MH_ALL_HOOKS);
#else
Hooking::GetInstance(); // Will call the ctor
#endif

while (true) Sleep(0);
return 0;
Expand Down
36 changes: 36 additions & 0 deletions util.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <Windows.h>
#include <psapi.h>
#include "Hooking.hpp"

// taken from sunbeam906
uint8_t __declspec(noinline)* GetAddress(uint8_t* ptr, uint8_t offset, uint8_t instr_size)
{
return (ptr + *(int32_t*)(ptr + offset) + instr_size);
}

bool __declspec(noinline) bDataCompare(const BYTE* pData, const BYTE* bMask, const char* szMask)
{
for (; *szMask; ++szMask, ++pData, ++bMask)
{
if (*szMask == 'x' && *pData != *bMask)
return FALSE;
}

return (*szMask) == NULL;
}

uint8_t __declspec(noinline)* FindPattern(BYTE* bMask, char* szMask)
{
MODULEINFO moduleInfo = { 0 };
GetModuleInformation(GetCurrentProcess(), GetModuleHandle(NULL), &moduleInfo, sizeof(MODULEINFO));

uint8_t* dwBaseAddress = (uint8_t*)moduleInfo.lpBaseOfDll + 0x1000; // start from first section
int dwModuleSize = (int)moduleInfo.SizeOfImage - 0x1000; // scan size - 0x1000, due to the above

for (int i = 0; i < dwModuleSize; i++)
{
if (bDataCompare((BYTE*)(dwBaseAddress + i), bMask, szMask))
return (uint8_t*)(dwBaseAddress + i);
}
return 0;
}

0 comments on commit 9103a30

Please sign in to comment.