Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save twist84/78d9725e39ad0e8b4d87c0bf39401fe4 to your computer and use it in GitHub Desktop.
Save twist84/78d9725e39ad0e8b4d87c0bf39401fe4 to your computer and use it in GitHub Desktop.
#include "LoadingScreen.hpp"
#include "../Patch.hpp"
#include "../ElDorito.hpp"
#include "../Blam/BlamTypes.hpp"
#include "../Console.hpp"
using namespace Patches::LoadingScreen;
namespace
{
void ShowLoadingScreenHook(const char *mapPath, short unk);
void BeginLoadingHook(uint32_t totalBytes);
void UpdateLoadingProgressHook(uint32_t bytes);
void HideLoadingScreenHook();
void ChangeTick();
bool OnLoadingScreen = false;
std::shared_ptr<LoadingScreenUi> ActiveUi;
}
namespace Patches::LoadingScreen
{
void ApplyAll()
{
Hook(0x167935, ShowLoadingScreenHook, HookFlags::IsCall).Apply();
Hook(0x161E1E, BeginLoadingHook, HookFlags::IsCall).Apply();
Hook(0x2EBA07, UpdateLoadingProgressHook, HookFlags::IsCall).Apply();
Hook(0x167ABA, HideLoadingScreenHook, HookFlags::IsCall).Apply();
// Force a jump so that the loading screen never renders
Patch(0x1064E7, { 0xEB }).Apply();
// fixes a rare issue were the game wouldn't render anything before the mainmenu finished loading
// See issue #323
Patch(0x167899, { 0xEB }).Apply();
}
void SetUi(std::shared_ptr<LoadingScreenUi> ui)
{
ActiveUi = ui;
}
}
namespace
{
void ShowLoadingScreenHook(const char *mapPath, short unk)
{
OnLoadingScreen = true;
typedef void(*ShowLoadingScreenPtr)(const char *mapPath, short unk);
auto ShowLoadingScreen = reinterpret_cast<ShowLoadingScreenPtr>(0x52EE40);
ShowLoadingScreen(mapPath, unk);
if (ActiveUi)
ActiveUi->Show(mapPath);
ChangeTick();
}
void BeginLoadingHook(uint32_t totalBytes)
{
typedef void(*BeginLoadingPtr)(uint32_t totalBytes);
auto BeginLoading = reinterpret_cast<BeginLoadingPtr>(0x711C00);
BeginLoading(totalBytes);
if (ActiveUi)
ActiveUi->Begin(totalBytes);
}
void UpdateLoadingProgressHook(uint32_t bytes)
{
typedef void(*UpdateLoadingProgressPtr)(uint32_t bytes);
auto UpdateLoadingProgress = reinterpret_cast<UpdateLoadingProgressPtr>(0x711C30);
UpdateLoadingProgress(bytes);
if (ActiveUi)
ActiveUi->UpdateProgress(bytes);
}
void HideLoadingScreenHook()
{
OnLoadingScreen = false;
typedef void(*HideLoadingScreenPtr)();
auto HideLoadingScreen = reinterpret_cast<HideLoadingScreenPtr>(0x52EE20);
HideLoadingScreen();
if (ActiveUi)
ActiveUi->Hide();
}
void ChangeTick()
{
if (ElDorito::Instance().GameHasMenuShown && OnLoadingScreen)
{
int tick = 1024;
auto gameTimeGlobalsPtr = ElDorito::GetMainTls(GameGlobals::Time::TLSOffset)[0];
gameTimeGlobalsPtr(GameGlobals::Time::FpsIndex).Write(tick);
gameTimeGlobalsPtr(GameGlobals::Time::DTInverseIndex).Write(1.0f / tick);
std::stringstream ss;
ss << "FpsIndex: " << gameTimeGlobalsPtr(GameGlobals::Time::FpsIndex).Read<int>() << std::endl;
ss << "DTInverseIndex: " << gameTimeGlobalsPtr(GameGlobals::Time::DTInverseIndex).Read<float>() << std::endl;
Console::WriteLine(ss.str());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment