Add scr_autoRespawn

This commit is contained in:
Federico Cecchetto 2022-06-21 03:30:41 +02:00
parent c83c736ce2
commit dd0f60ed48

View File

@ -2,6 +2,7 @@
#include "loader/component_loader.hpp" #include "loader/component_loader.hpp"
#include "game/game.hpp" #include "game/game.hpp"
#include "game/dvars.hpp"
#include "command.hpp" #include "command.hpp"
#include "scheduler.hpp" #include "scheduler.hpp"
@ -35,6 +36,10 @@ namespace scripting
utils::hook::detour sl_get_canonical_string_hook; utils::hook::detour sl_get_canonical_string_hook;
utils::hook::detour respawn_hook;
game::dvar_t* scr_auto_respawn = nullptr;
std::string current_file; std::string current_file;
unsigned int current_file_id{}; unsigned int current_file_id{};
@ -62,6 +67,7 @@ namespace scripting
void client_spawn_stub(const game::gentity_s* client) void client_spawn_stub(const game::gentity_s* client)
{ {
client_spawn_hook.invoke<void>(client); client_spawn_hook.invoke<void>(client);
scr_auto_respawn->current.enabled = true;
lua::engine::start(); lua::engine::start();
} }
@ -132,6 +138,7 @@ namespace scripting
const auto result = sv_check_load_level_hook.invoke<char>(save_game); const auto result = sv_check_load_level_hook.invoke<char>(save_game);
if (save_game != nullptr) if (save_game != nullptr)
{ {
scr_auto_respawn->current.enabled = true;
lua::engine::start(); lua::engine::start();
} }
return result; return result;
@ -143,6 +150,16 @@ namespace scripting
scripting::token_map[str] = result; scripting::token_map[str] = result;
return result; return result;
} }
void respawn_stub()
{
if (!scr_auto_respawn->current.enabled)
{
return;
}
respawn_hook.invoke<void>();
}
} }
class component final : public component_interface class component final : public component_interface
@ -161,6 +178,9 @@ namespace scripting
process_script_hook.create(0x1405C6160, process_script_stub); process_script_hook.create(0x1405C6160, process_script_stub);
sl_get_canonical_string_hook.create(game::SL_GetCanonicalString, sl_get_canonical_string_stub); sl_get_canonical_string_hook.create(game::SL_GetCanonicalString, sl_get_canonical_string_stub);
scr_auto_respawn = dvars::register_bool("scr_autoRespawn", true, 0, "Automatically respawn player on death");
respawn_hook.create(0x1404B1E00, respawn_stub);
scheduler::loop([]() scheduler::loop([]()
{ {
lua::engine::run_frame(); lua::engine::run_frame();