Skip to content

Commit

Permalink
Bring back binoculars from previous game
Browse files Browse the repository at this point in the history
  • Loading branch information
TheIndra55 committed Feb 5, 2021
1 parent a6982ef commit b977314
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 3 deletions.
51 changes: 51 additions & 0 deletions ControlHooks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <stdio.h>
#include "MinHook.h"
#include "Hooking.hpp"

//int GetNumButtons()
//{
// return 21 /* game default */;
//}

char(__thiscall* UIDataList__AllocateRunData)(DWORD _this, int a2, int a3, int a4, int a5, int a6);

char(__thiscall* UIDataList__AddItem)(DWORD _this, int processFunc, void* data, int getDataFunc, int a5, const char* a6);

const char* GetControlText(void* data, int index, int textIndex)
{
if (textIndex)
{
return "DEL";
}

return "Binoculars";
}

char UIControllerConfigDataList(int a1, int a2)
{
// since the game controller config is complex and hardcoded array size
// we construct the controller config data list ourself and add our own control

if (!UIDataList__AllocateRunData(a1, 22, a2, 0x4EC5E0, 0x4EC5F0, 0x4ECB10))
return 1;

for (int i = 0; i < 21; i++)
{
UIDataList__AddItem(a1, 0x4ECB30, (void*)i, 0x4ECBE0, 0, 0);
}

// hardcoded for now since internal controls array is a fixed size
UIDataList__AddItem(a1, 0, 0, (int)GetControlText, 0, 0);

return 1;
}

void InstallControlHooks()
{
//MH_CreateHook((void*)0x004EC280, GetNumButtons, nullptr);

UIDataList__AllocateRunData = reinterpret_cast<char(__thiscall*)(DWORD, int, int, int, int, int)>(0x00C704C2);
UIDataList__AddItem = reinterpret_cast<char(__thiscall*)(DWORD, int, void*, int, int, const char*)>(0x00C70577);

MH_EnableHook(MH_ALL_HOOKS);
}
3 changes: 3 additions & 0 deletions ControlHooks.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void InstallControlHooks();
41 changes: 41 additions & 0 deletions Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ std::function<char __cdecl(int, int)> Game::f_PushScreen = nullptr;
std::function<int __cdecl()> Game::f_GetTopScreenID = nullptr;
std::function<int __cdecl(const char*, int, int, int)> Game::f_PushOkDialog = nullptr;
std::function<int __cdecl()> Game::f_PopScreen = nullptr;
bool Game::m_binoculars = false;

void(__cdecl* PLAYER_DebugSwitchPlayerCharacter)();
DWORD(__cdecl* sub_C64D3F)(int a1, int a2, int a3);
Expand All @@ -20,6 +21,12 @@ void(__cdecl* IncrHealth)(float amount);
void(__cdecl* UIFadeGroupTrigger)(int group);
void(__cdecl* game_SetGameValue)(int a1, float a2, char a3);

void(__cdecl* PLAYER_SetLookAround)(DWORD instance);
void(__cdecl* CAMERA_StartLookaroundMode)(DWORD camera);
int(__cdecl* SteerSwitchMode)(DWORD instance, int mode);
void(__cdecl* PLAYER_ReSetLookAround)(DWORD instance);
__int16(__cdecl* CAMERA_ForceEndLookaroundMode)(DWORD camera);

void Game::Initialize()
{
f_SwitchChapter = reinterpret_cast<char(__cdecl*)(char*)>(0x422090);
Expand All @@ -41,6 +48,12 @@ void Game::Initialize()
IncrHealth = reinterpret_cast<void(__cdecl*)(float)>(0x005715E0);
UIFadeGroupTrigger = reinterpret_cast<void(__cdecl*)(int)>(0x004EE580);
game_SetGameValue = reinterpret_cast<void(__cdecl*)(int, float, char)>(0x004551A0);

PLAYER_SetLookAround = reinterpret_cast<void(__cdecl*)(DWORD)>(0x00C759A8);
CAMERA_StartLookaroundMode = reinterpret_cast<void(__cdecl*)(DWORD)>(0x0048A300);
SteerSwitchMode = reinterpret_cast<int(__cdecl*)(DWORD, int)>(0x005BAE60);
PLAYER_ReSetLookAround = reinterpret_cast<void(__cdecl*)(DWORD instance)>(0x00C759C7);
CAMERA_ForceEndLookaroundMode = reinterpret_cast<__int16(__cdecl*)(DWORD)>(0x0048A5E0);
}

void Game::SwitchChapter(char* chapter)
Expand Down Expand Up @@ -142,4 +155,32 @@ void Game::TriggerUiFadeGroup(int group)
void Game::SetGameValue(int key, float val, bool apply)
{
game_SetGameValue(key, val, apply);
}

void Game::ToggleBinoculars()
{
if (!m_binoculars)
{
SteerSwitchMode(PLAYERINSTANCE, 0);

*(__int16*)0x850CAC = 1;
*(bool*)0x850418 = 1;
*(int*)0x86C908 = 72;
*(int*)0x86C818 = 3;

PLAYER_SetLookAround(PLAYERINSTANCE);
Game::InstancePost(PLAYERINSTANCE, 262265, 0);
Game::InstancePost(PLAYERINSTANCE, 262264, 8);
CAMERA_StartLookaroundMode(0x850670);
}
else
{
*(int*)0x6926FC = 0;
*(int*)0x86CD90 = 0;

PLAYER_ReSetLookAround(PLAYERINSTANCE);
CAMERA_ForceEndLookaroundMode(0x850670);
}

m_binoculars = !m_binoculars;
}
6 changes: 6 additions & 0 deletions Game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#include <functional>
#include "pch.h"

#define GAMETRACKER 0x838330
#define PLAYERINSTANCE 0x83833C

class Game
{
public:
Expand All @@ -27,6 +30,9 @@ class Game
static void IncreaseHealth(float amount);
static void TriggerUiFadeGroup(int group);
static void SetGameValue(int key, float val, bool apply);

static bool m_binoculars;
static void ToggleBinoculars();
private:
static std::function<char(char* chapter)> f_SwitchChapter;
static std::function<char __cdecl(int a1)> f_ResetGame;
Expand Down
2 changes: 2 additions & 0 deletions Hooking.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Hooking.hpp"
#include "Game.hpp"
#include "ControlHooks.hpp"

extern Hooking* g_hooking;

Expand All @@ -16,6 +17,7 @@ void Hooking::Initialize()
hooked_Direct3DInit,
reinterpret_cast<void**>(&original_Direct3DInit));

InstallControlHooks();
Game::Initialize();

MH_EnableHook(MH_ALL_HOOKS);
Expand Down
16 changes: 13 additions & 3 deletions Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
#include "Game.hpp"
#include "Hooking.hpp"

#define GAMETRACKER 0x838330
#define PLAYERINSTANCE 0x83833C

extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

static bool shouldInstance = true;
Expand Down Expand Up @@ -47,6 +44,12 @@ void __fastcall hooked_Subtitle_Add(DWORD* _this, void* _, char* str, int durati
orginal_Subtitle_Add(_this, str, duration);
}

char IsPs2()
{
// somewhat the zoom code for binoculars is inside an 'if IsPs2()' in TRAE so we have to hook that function
return Game::m_binoculars;
}

Menu::Menu(LPDIRECT3DDEVICE9 pd3dDevice, HWND hwnd)
{
m_pd3dDevice = pd3dDevice;
Expand All @@ -63,6 +66,8 @@ Menu::Menu(LPDIRECT3DDEVICE9 pd3dDevice, HWND hwnd)
MH_CreateHook((void*)0x4E3C80, localstr_get_hooked, (void**)&localstr_get);
MH_CreateHook((void*)0x4FCB60, pushscreenhooked, (void**)&pushscreen);
MH_CreateHook((void*)0x0046F080, hooked_Subtitle_Add, (void**)&orginal_Subtitle_Add);

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

void Menu::OnDeviceReleased()
Expand Down Expand Up @@ -104,6 +109,11 @@ void Menu::Process(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
Game::SwitchPlayerCharacter();
}

if (msg == WM_KEYUP && wparam == VK_DELETE)
{
Game::ToggleBinoculars();
}

if (m_flight)
{
ProcessFlight(msg, wparam);
Expand Down
2 changes: 2 additions & 0 deletions TRAE-menu-hook.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="ControlHooks.hpp" />
<ClInclude Include="framework.h" />
<ClInclude Include="Game.hpp" />
<ClInclude Include="Hooking.hpp" />
Expand All @@ -171,6 +172,7 @@
<ClInclude Include="pch.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="ControlHooks.cpp" />
<ClCompile Include="dllmain.cpp" />
<ClCompile Include="Game.cpp" />
<ClCompile Include="Hooking.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions TRAE-menu-hook.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
<ClInclude Include="Game.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ControlHooks.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp">
Expand Down Expand Up @@ -89,6 +92,9 @@
<ClCompile Include="Game.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="ControlHooks.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Library Include="d3d9.lib" />
Expand Down

0 comments on commit b977314

Please sign in to comment.