Skip to content

Commit

Permalink
Add game loop hook and outfit switch
Browse files Browse the repository at this point in the history
  • Loading branch information
TheIndra55 committed Jan 25, 2024
1 parent 5c413bd commit 309d94c
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/Hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "instance/Instances.h"
#include "game/Game.h"
#include "render/Font.h"
#include "game/GameLoop.h"

// Modules
#include "modules/MainMenu.h"
Expand Down Expand Up @@ -76,6 +77,8 @@ void Hook::PostInitialize()
#ifndef TR8
Font::OnFlush(std::bind(&Hook::OnFrame, this));
#endif

GameLoop::OnLoop(std::bind(&Hook::OnLoop, this));
}

void Hook::OnMessage(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
Expand All @@ -96,6 +99,14 @@ void Hook::OnFrame()
}
}

void Hook::OnLoop()
{
for (auto& [hash, mod] : m_modules)
{
mod->OnLoop();
}
}

void Hook::OnDevice()
{
// Assign the DeviceManager instance
Expand Down
1 change: 1 addition & 0 deletions src/Hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Hook

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

public:
Hook();
Expand Down
14 changes: 14 additions & 0 deletions src/game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,18 @@ void GAMELOOP_RequestLevelChangeByName(char* name, GameTracker* gameTracker, int
auto addr = GET_ADDRESS(0x451970, 0xC61CFA, 0x5DF8C0);

Hooking::Call(addr, name, gameTracker, doneType);
}

void PLAYER_DebugSwitchPlayerCharacter()
{
auto addr = GET_ADDRESS(0x5A40B0, 0x5A39A0, 0x79DB50);

Hooking::Call(addr);
}

int OBTABLE_GetObjectID(char* name)
{
auto addr = GET_ADDRESS(0x462590, 0x465DE0, 0x5BF770);

return Hooking::CallReturn<int>(addr, name);
}
14 changes: 11 additions & 3 deletions src/game/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ struct GameTracker
float timeDilation;

int debugTimeMult;

char pad2[752];

int currentPlayerObjectID;
int altPlayerObjectID;
};
#else
struct GameTracker
Expand Down Expand Up @@ -143,8 +148,8 @@ struct GameTracker
int field_134;
int field_138;
int field_13C;
int field_140;
int field_144;
int currentPlayerObjectID;
int altPlayerObjectID;

float timeDilation;
};
Expand All @@ -158,4 +163,7 @@ class Game
static STracker* GetStreamTracker();
};

void GAMELOOP_RequestLevelChangeByName(char* name, GameTracker* gameTracker, int doneType);
void GAMELOOP_RequestLevelChangeByName(char* name, GameTracker* gameTracker, int doneType);

void PLAYER_DebugSwitchPlayerCharacter();
int OBTABLE_GetObjectID(char* name);
25 changes: 25 additions & 0 deletions src/game/GameLoop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <MinHook.h>

#include "GameLoop.h"
#include "Game.h"
#include "util/Hooking.h"

static std::function<void()> s_callback;
static void(*s_GAMELOOP_Process)(GameTracker*);

static void GAMELOOP_Process(GameTracker* gameTracker)
{
s_callback();
s_GAMELOOP_Process(gameTracker);
}

void GameLoop::OnLoop(std::function<void()> callback)
{
if (!s_callback)
{
MH_CreateHook((void*)GET_ADDRESS(0x452140, 0x454AC0, 0x5DFBE0), GAMELOOP_Process, (void**)&s_GAMELOOP_Process);
MH_EnableHook(MH_ALL_HOOKS);
}

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

#include <functional>

class GameLoop
{
public:
static void OnLoop(std::function<void()> callback);
};
42 changes: 42 additions & 0 deletions src/modules/MainMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ void MainMenu::OnDraw()
BirthObject(object);
}

// Player
if (ImGui::CollapsingHeader("Player"))
{
static char outfit[64] = "";
ImGui::InputText("Outfit", outfit, sizeof(outfit));

if (ImGui::Button("Change"))
{
SwitchPlayerCharacter(outfit);
}

ImGui::SameLine();

if (ImGui::Button("Next"))
{
SwitchPlayerCharacter();
}
}

// Time
if (ImGui::CollapsingHeader("Time"))
{
Expand Down Expand Up @@ -57,8 +76,22 @@ void MainMenu::BirthObject(char* name)
INSTANCE_BirthObjectNoParent(game->StreamUnitID, &player->position, &player->rotation, nullptr, tracker->object, 0, 1);
}

void MainMenu::SwitchPlayerCharacter(char* name)
{
auto game = Game::GetGameTracker();

if (name)
{
auto object = OBTABLE_GetObjectID(name);
game->altPlayerObjectID = object;
}

m_switchPlayerNextFrame = true;
}

void MainMenu::OnFrame()
{
// Shows the watermark in th main menu
auto mainState = *(int*)GET_ADDRESS(0x10E5868, 0x838838, 0x000000);

if (mainState == MS_DISPLAY_MAIN_MENU)
Expand All @@ -70,6 +103,15 @@ void MainMenu::OnFrame()
}
}

void MainMenu::OnLoop()
{
if (m_switchPlayerNextFrame)
{
m_switchPlayerNextFrame = false;
PLAYER_DebugSwitchPlayerCharacter();
}
}

void MainMenu::OnInput(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
auto gameTracker = Game::GetGameTracker();
Expand Down
4 changes: 4 additions & 0 deletions src/modules/MainMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
class MainMenu : public Module
{
private:
bool m_switchPlayerNextFrame = false;

void BirthObject(char* name);
void SwitchPlayerCharacter(char* name = nullptr);

public:
void OnDraw();
void OnFrame();
void OnLoop();
void OnInput(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
};
3 changes: 3 additions & 0 deletions src/modules/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class Module
// Called just before a frame ends, Font::Flush to be specific
virtual void OnFrame() { };

// Called every frame before the game loop
virtual void OnLoop() { };

// Called when a message is processed by the window procedure
virtual void OnInput(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { };
};

0 comments on commit 309d94c

Please sign in to comment.