feat: game_event component

This commit is contained in:
FutureRave 2023-05-14 10:02:32 +01:00
parent 07885a7920
commit a59462f3c3
4 changed files with 77 additions and 5 deletions

View File

@ -0,0 +1,58 @@
#include <std_include.hpp>
#include "loader/component_loader.hpp"
#include <game/game.hpp>
#include "game_event.hpp"
#include <utils/hook.hpp>
namespace game_event
{
namespace
{
std::vector<std::function<void()>> g_init_game_tasks_;
std::vector<std::function<void()>> g_shutdown_game_tasks_;
void rope_init_ropes_stub()
{
for (const auto& func : g_init_game_tasks_)
{
func();
}
game::Rope_InitRopes();
}
void mantle_shutdown_anims_stub()
{
for (const auto& func : g_init_game_tasks_)
{
func();
}
game::Mantle_ShutdownAnims();
}
}
void on_g_init_game(const std::function<void()>& callback)
{
g_init_game_tasks_.emplace_back(callback);
}
void on_g_shutdown_game(const std::function<void()>& callback)
{
g_shutdown_game_tasks_.emplace_back(callback);
}
class component final : public generic_component
{
public:
void post_unpack() override
{
utils::hook::call(game::select(0x1419DD6EC, 0x1402ABC1B), rope_init_ropes_stub);
utils::hook::call(game::select(0x141A02AAD, 0x1402ADF1D), rope_init_ropes_stub);
}
};
}
REGISTER_COMPONENT(game_event::component)

View File

@ -0,0 +1,7 @@
#pragma once
namespace game_event
{
void on_g_init_game(const std::function<void()>& callback);
void on_g_shutdown_game(const std::function<void()>& callback);
}

View File

@ -2,7 +2,7 @@
#include "loader/component_loader.hpp" #include "loader/component_loader.hpp"
#include "game/game.hpp" #include "game/game.hpp"
#include "scheduler.hpp" #include "game_event.hpp"
#include <utils/hook.hpp> #include <utils/hook.hpp>
#include <utils/string.hpp> #include <utils/string.hpp>
@ -15,7 +15,6 @@ namespace script
constexpr size_t GSC_MAGIC = 0x1C000A0D43534780; constexpr size_t GSC_MAGIC = 0x1C000A0D43534780;
utils::hook::detour db_find_x_asset_header_hook; utils::hook::detour db_find_x_asset_header_hook;
utils::hook::detour g_shutdown_game_hook;
utils::hook::detour gscr_get_bgb_remaining_hook; utils::hook::detour gscr_get_bgb_remaining_hook;
utils::memory::allocator allocator; utils::memory::allocator allocator;
@ -130,11 +129,10 @@ namespace script
return asset_header; return asset_header;
} }
void g_shutdown_game_stub(bool free_scripts) void clear_script_memory()
{ {
loaded_scripts.clear(); loaded_scripts.clear();
allocator.clear(); allocator.clear();
g_shutdown_game_hook.invoke<void>(free_scripts);
} }
void load_gametype_script_stub() void load_gametype_script_stub()
@ -168,7 +166,7 @@ namespace script
db_find_x_asset_header_hook.create(game::select(0x141420ED0, 0x1401D5FB0), db_find_x_asset_header_stub); db_find_x_asset_header_hook.create(game::select(0x141420ED0, 0x1401D5FB0), db_find_x_asset_header_stub);
// Free our scripts when the game ends // Free our scripts when the game ends
g_shutdown_game_hook.create(game::select(0x141A02900, 0x1402ADD70), g_shutdown_game_stub); game_event::on_g_shutdown_game(clear_script_memory);
// Load our scripts when the gametype script is loaded // Load our scripts when the gametype script is loaded
utils::hook::call(game::select(0x141AAF37C, 0x1402D8C7F), load_gametype_script_stub); utils::hook::call(game::select(0x141AAF37C, 0x1402D8C7F), load_gametype_script_stub);

View File

@ -113,6 +113,15 @@ namespace game
WEAK symbol<TLSData*()> Sys_GetTLS{0x1421837B0, 0x140525EB0}; WEAK symbol<TLSData*()> Sys_GetTLS{0x1421837B0, 0x140525EB0};
WEAK symbol<TLSData*()> Sys_IsDatabaseReady{0x142183A60}; WEAK symbol<TLSData*()> Sys_IsDatabaseReady{0x142183A60};
// Rope
WEAK symbol<void()> Rope_InitRopes{0x1420D8D00, 0x1404E4300};
// Glass
WEAK symbol<void()> GlassSv_Shutdown{0x1425AA7A0, 0x14065BCC0};
// Mantle
WEAK symbol<void()> Mantle_ShutdownAnims{0x142678C80, 0x1406A1B50};
// Unnamed // Unnamed
WEAK symbol<const char*(const char* name)> CopyString{0x1422AC220, 0x14056BD70}; WEAK symbol<const char*(const char* name)> CopyString{0x1422AC220, 0x14056BD70};