#include #include "loader/component_loader.hpp" #include "game/game.hpp" #include "game/dvars.hpp" #include "command.hpp" #include "scheduler.hpp" #include "scripting.hpp" #include "game/scripting/event.hpp" #include "game/scripting/functions.hpp" #include "game/scripting/execution.hpp" #include "game/scripting/lua/engine.hpp" #include #include namespace scripting { std::unordered_map> fields_table; std::unordered_map> script_function_table; utils::concurrency::container shared_table; namespace { utils::hook::detour vm_notify_hook; utils::hook::detour g_shutdown_game_hook; utils::hook::detour client_spawn_hook; utils::hook::detour sv_check_load_level_hook; utils::hook::detour scr_add_class_field_hook; utils::hook::detour scr_set_thread_position_hook; utils::hook::detour process_script_hook; utils::hook::detour sl_get_canonical_string_hook; utils::hook::detour respawn_hook; utils::hook::detour scr_run_current_threads_hook; game::dvar_t* scr_auto_respawn = nullptr; std::string current_file; unsigned int current_file_id{}; std::unordered_map canonical_string_table; using notify_list = std::vector; utils::concurrency::container scheduled_notifies; void vm_notify_stub(const unsigned int notify_list_owner_id, const game::scr_string_t string_value, game::VariableValue* top) { const auto* string = game::SL_ConvertToString(string_value); if (string) { event e; e.name = string; e.entity = notify_list_owner_id; for (auto* value = top; value->type != game::SCRIPT_END; --value) { e.arguments.emplace_back(*value); } scheduled_notifies.access([&](notify_list& list) { list.push_back(e); }); } vm_notify_hook.invoke(notify_list_owner_id, string_value, top); } void clear_scheduler_notifies() { scheduled_notifies.access([](notify_list& list) { list.clear(); }); } void client_spawn_stub(const game::gentity_s* client) { client_spawn_hook.invoke(client); scr_auto_respawn->current.enabled = true; clear_scheduler_notifies(); lua::engine::start(); } void g_shutdown_game_stub(const int free_scripts) { if (free_scripts) { canonical_string_table.clear(); } clear_scheduler_notifies(); lua::engine::stop(); g_shutdown_game_hook.invoke(free_scripts); } void scr_add_class_field_stub(unsigned int classnum, game::scr_string_t name, unsigned int canonicalString, unsigned int offset) { const auto name_ = game::SL_ConvertToString(name); if (fields_table[classnum].find(name_) == fields_table[classnum].end()) { fields_table[classnum][name_] = offset; } scr_add_class_field_hook.invoke(classnum, name, canonicalString, offset); } void process_script_stub(const char* filename) { const auto file_id = atoi(filename); if (file_id) { current_file_id = file_id; } else { current_file_id = 0; current_file = filename; } process_script_hook.invoke(filename); } std::vector get_token_names(unsigned int id) { auto result = scripting::find_token(id); if (canonical_string_table.find(id) != canonical_string_table.end()) { result.push_back(canonical_string_table[id]); } return result; } void add_function(const std::string& file, unsigned int id, const char* pos) { const auto function_names = scripting::get_token_names(id); for (const auto& name : function_names) { script_function_table[file][name] = pos; } } void scr_set_thread_position_stub(unsigned int thread_name, const char* code_pos) { if (current_file_id) { const auto names = scripting::get_token_names(current_file_id); for (const auto& name : names) { add_function(name, thread_name, code_pos); } } else { add_function(current_file, thread_name, code_pos); } scr_set_thread_position_hook.invoke(thread_name, code_pos); } char sv_check_load_level_stub(void* save_game) { const auto result = sv_check_load_level_hook.invoke(save_game); if (save_game != nullptr) { scr_auto_respawn->current.enabled = true; clear_scheduler_notifies(); lua::engine::start(); } return result; } unsigned int sl_get_canonical_string_stub(const char* str) { const auto result = sl_get_canonical_string_hook.invoke(str); canonical_string_table[result] = str; return result; } void respawn_stub() { if (!scr_auto_respawn->current.enabled) { return; } respawn_hook.invoke(); } void scr_run_current_threads_stub() { notify_list list_copy{}; scheduled_notifies.access([&](notify_list& list) { list_copy = list; list.clear(); }); for (const auto& e : list_copy) { lua::engine::notify(e); } scr_run_current_threads_hook.invoke(); } } class component final : public component_interface { public: void post_unpack() override { vm_notify_hook.create(0x1405CC450, vm_notify_stub); g_shutdown_game_hook.create(0x1404CBAD0, g_shutdown_game_stub); client_spawn_hook.create(0x1404B0710, client_spawn_stub); sv_check_load_level_hook.create(0x1406B2940, sv_check_load_level_stub); scr_add_class_field_hook.create(0x1405C2C30, scr_add_class_field_stub); scr_set_thread_position_hook.create(0x1405BC7E0, scr_set_thread_position_stub); process_script_hook.create(0x1405C6160, process_script_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); scr_run_current_threads_hook.create(0x1405C8370, scr_run_current_threads_stub); scheduler::loop([]() { lua::engine::run_frame(); }, scheduler::pipeline::server); } }; } REGISTER_COMPONENT(scripting::component)