h2-mod/src/client/component/scripting.cpp

370 lines
9.1 KiB
C++
Raw Normal View History

2021-09-07 00:40:37 +02:00
#include <std_include.hpp>
#include "loader/component_loader.hpp"
2021-04-23 03:46:11 +02:00
#include "game/game.hpp"
2022-06-21 03:30:41 +02:00
#include "game/dvars.hpp"
#include "scheduler.hpp"
#include "scripting.hpp"
2022-08-24 19:07:14 +02:00
#include "gsc.hpp"
2022-10-23 06:40:36 +02:00
#include "console.hpp"
2021-04-23 03:46:11 +02:00
#include "game/scripting/event.hpp"
#include "game/scripting/functions.hpp"
2021-09-18 18:29:03 +02:00
#include "game/scripting/execution.hpp"
2021-04-23 03:46:11 +02:00
#include "game/scripting/lua/engine.hpp"
#include <utils/hook.hpp>
2022-07-05 03:36:17 +02:00
#include <utils/concurrency.hpp>
2021-04-23 03:46:11 +02:00
namespace scripting
{
std::unordered_map<int, std::unordered_map<std::string, int>> fields_table;
std::unordered_map<std::string, std::unordered_map<std::string, const char*>> script_function_table;
2022-08-24 19:07:14 +02:00
std::unordered_map<std::string, std::vector<std::pair<std::string, const char*>>> script_function_table_sort;
utils::concurrency::container<shared_table_t> shared_table;
2021-04-23 03:46:11 +02:00
std::unordered_map<std::string, int> get_dvar_int_overrides;
2022-08-24 19:07:14 +02:00
std::string current_file;
2021-04-23 03:46:11 +02:00
namespace
{
utils::hook::detour vm_notify_hook;
utils::hook::detour g_shutdown_game_hook;
2022-03-20 19:51:29 +01:00
utils::hook::detour client_spawn_hook;
utils::hook::detour sv_check_load_level_hook;
2021-04-23 03:46:11 +02:00
utils::hook::detour scr_add_class_field_hook;
utils::hook::detour scr_set_thread_position_hook;
utils::hook::detour process_script_hook;
2022-06-19 21:21:09 +02:00
utils::hook::detour sl_get_canonical_string_hook;
2022-06-21 03:30:41 +02:00
utils::hook::detour respawn_hook;
2022-07-05 03:36:17 +02:00
utils::hook::detour scr_run_current_threads_hook;
2022-12-18 04:15:25 +01:00
utils::hook::detour scr_delete_hook;
2022-06-21 03:30:41 +02:00
game::dvar_t* scr_auto_respawn = nullptr;
2022-08-24 19:07:14 +02:00
std::string current_scriptfile;
2022-06-19 21:21:09 +02:00
unsigned int current_file_id{};
2022-11-13 19:16:48 +01:00
std::vector<std::function<void(bool, bool)>> shutdown_callbacks;
2022-08-24 19:07:14 +02:00
2022-06-26 02:27:57 +02:00
std::unordered_map<unsigned int, std::string> canonical_string_table;
2022-07-05 03:36:17 +02:00
using notify_list = std::vector<event>;
utils::concurrency::container<notify_list> scheduled_notifies;
2021-04-23 03:46:11 +02:00
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);
}
2022-07-17 20:26:39 +02:00
lua::engine::handle_endon_conditions(e);
2022-07-05 03:36:17 +02:00
scheduled_notifies.access([&](notify_list& list)
{
list.push_back(e);
});
2021-04-23 03:46:11 +02:00
}
vm_notify_hook.invoke<void>(notify_list_owner_id, string_value, top);
}
2022-07-17 20:26:39 +02:00
void clear_scheduled_notifies()
2022-07-05 03:36:17 +02:00
{
get_dvar_int_overrides.clear();
2022-07-05 03:36:17 +02:00
scheduled_notifies.access([](notify_list& list)
{
list.clear();
});
}
2022-03-20 19:51:29 +01:00
void client_spawn_stub(const game::gentity_s* client)
2021-04-23 03:46:11 +02:00
{
2022-03-20 19:51:29 +01:00
client_spawn_hook.invoke<void>(client);
2022-06-21 03:30:41 +02:00
scr_auto_respawn->current.enabled = true;
2022-07-17 20:26:39 +02:00
clear_scheduled_notifies();
2021-04-23 03:46:11 +02:00
lua::engine::start();
}
void g_shutdown_game_stub(const int free_scripts)
{
2022-06-26 02:27:57 +02:00
if (free_scripts)
{
2022-08-24 19:07:14 +02:00
script_function_table_sort.clear();
script_function_table.clear();
2022-06-26 02:27:57 +02:00
canonical_string_table.clear();
}
2022-08-24 19:07:14 +02:00
for (const auto& callback : shutdown_callbacks)
{
2022-11-13 19:16:48 +01:00
callback(free_scripts, false);
2022-08-24 19:07:14 +02:00
}
2022-07-17 20:26:39 +02:00
clear_scheduled_notifies();
2021-04-23 03:46:11 +02:00
lua::engine::stop();
2021-04-24 19:06:34 +02:00
g_shutdown_game_hook.invoke<void>(free_scripts);
2022-11-13 19:16:48 +01:00
for (const auto& callback : shutdown_callbacks)
{
callback(free_scripts, true);
}
2021-04-23 03:46:11 +02:00
}
2022-03-20 19:51:29 +01:00
void scr_add_class_field_stub(unsigned int classnum, game::scr_string_t name, unsigned int canonicalString, unsigned int offset)
2021-04-23 03:46:11 +02:00
{
2022-03-20 19:51:29 +01:00
const auto name_ = game::SL_ConvertToString(name);
2021-04-23 03:46:11 +02:00
2022-03-20 19:51:29 +01:00
if (fields_table[classnum].find(name_) == fields_table[classnum].end())
2021-04-23 03:46:11 +02:00
{
2022-03-20 19:51:29 +01:00
fields_table[classnum][name_] = offset;
2021-04-23 03:46:11 +02:00
}
2022-03-20 19:51:29 +01:00
scr_add_class_field_hook.invoke<void>(classnum, name, canonicalString, offset);
2021-04-23 03:46:11 +02:00
}
void process_script_stub(const char* filename)
{
2022-08-24 19:07:14 +02:00
current_scriptfile = filename;
const auto file_id = atoi(filename);
if (file_id)
{
2022-06-19 21:21:09 +02:00
current_file_id = file_id;
}
else
{
current_file_id = 0;
current_file = filename;
}
process_script_hook.invoke<void>(filename);
}
2022-06-26 02:27:57 +02:00
std::vector<std::string> 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;
}
2022-08-24 19:07:14 +02:00
void add_function_sort(unsigned int id, const char* pos)
{
std::string filename = current_file;
if (current_file_id)
{
2022-08-30 21:47:29 +02:00
filename = scripting::get_token_single(current_file_id);
2022-08-24 19:07:14 +02:00
}
if (script_function_table_sort.find(filename) == script_function_table_sort.end())
{
const auto script = gsc::find_script(game::ASSET_TYPE_SCRIPTFILE, current_scriptfile.data(), false);
if (script)
{
const auto end = &script->bytecode[script->bytecodeLen];
script_function_table_sort[filename].emplace_back("__end__", end);
}
}
const auto name = get_token_single(id);
auto& itr = script_function_table_sort[filename];
itr.insert(itr.end() - 1, {name, pos});
}
2022-06-19 21:21:09 +02:00
void add_function(const std::string& file, unsigned int id, const char* pos)
{
2022-06-26 02:27:57 +02:00
const auto function_names = scripting::get_token_names(id);
2022-06-19 21:21:09 +02:00
for (const auto& name : function_names)
{
script_function_table[file][name] = pos;
}
}
2021-12-24 17:51:52 +01:00
2022-06-19 21:21:09 +02:00
void scr_set_thread_position_stub(unsigned int thread_name, const char* code_pos)
{
2022-08-24 19:07:14 +02:00
add_function_sort(thread_name, code_pos);
2022-06-19 21:21:09 +02:00
if (current_file_id)
{
2022-06-26 02:27:57 +02:00
const auto names = scripting::get_token_names(current_file_id);
2022-06-19 21:21:09 +02:00
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<void>(thread_name, code_pos);
}
2022-03-20 19:51:29 +01:00
char sv_check_load_level_stub(void* save_game)
2021-12-24 17:51:52 +01:00
{
2022-03-20 19:51:29 +01:00
const auto result = sv_check_load_level_hook.invoke<char>(save_game);
if (save_game != nullptr)
2021-12-24 17:51:52 +01:00
{
2022-06-21 03:30:41 +02:00
scr_auto_respawn->current.enabled = true;
2022-07-17 20:26:39 +02:00
clear_scheduled_notifies();
2021-12-24 17:51:52 +01:00
lua::engine::start();
}
return result;
}
2022-06-19 21:21:09 +02:00
unsigned int sl_get_canonical_string_stub(const char* str)
{
const auto result = sl_get_canonical_string_hook.invoke<unsigned int>(str);
2022-06-26 02:27:57 +02:00
canonical_string_table[result] = str;
2022-06-19 21:21:09 +02:00
return result;
}
2022-06-21 03:30:41 +02:00
void respawn_stub()
{
if (!scr_auto_respawn->current.enabled)
{
return;
}
respawn_hook.invoke<void>();
}
2022-07-05 03:36:17 +02:00
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<void>();
}
utils::hook::detour scr_get_dvar_int_hook;
void scr_get_dvar_int_stub()
{
const auto dvar = game::Scr_GetString(0);
if (get_dvar_int_overrides.find(dvar) != get_dvar_int_overrides.end())
{
game::Scr_AddInt(get_dvar_int_overrides[dvar]);
return;
}
scr_get_dvar_int_hook.invoke<void>();
}
2022-10-23 06:40:36 +02:00
void* get_spawn_point_stub()
{
const auto spawn_point = utils::hook::invoke<void*>(0x1404B1670);
if (spawn_point == nullptr)
{
console::warn("No spawnpoint found for this map, using (0, 0, 0)\n");
return &game::g_entities[0];
}
return spawn_point;
}
2022-12-18 04:15:25 +01:00
void scr_delete_stub(game::scr_entref_t ref)
{
if (ref.entnum == 0 && ref.classnum == 0)
{
console::warn("Script tried to delete entity 0\n");
return;
}
scr_delete_hook.invoke<void>(ref);
}
2021-04-23 03:46:11 +02:00
}
2022-08-30 21:47:29 +02:00
std::string get_token_single(unsigned int id)
{
if (canonical_string_table.find(id) != canonical_string_table.end())
{
return canonical_string_table[id];
}
return scripting::find_token_single(id);
}
2022-11-13 19:16:48 +01:00
void on_shutdown(const std::function<void(bool, bool)>& callback)
2022-08-24 19:07:14 +02:00
{
shutdown_callbacks.push_back(callback);
}
std::optional<std::string> get_canonical_string(const unsigned int id)
{
if (canonical_string_table.find(id) == canonical_string_table.end())
{
return {};
}
return {canonical_string_table[id]};
}
class component final : public component_interface
2021-04-23 03:46:11 +02:00
{
public:
void post_unpack() override
{
2022-03-18 22:02:44 +01:00
vm_notify_hook.create(0x1405CC450, vm_notify_stub);
2021-04-23 03:46:11 +02:00
2022-03-18 22:02:44 +01:00
g_shutdown_game_hook.create(0x1404CBAD0, g_shutdown_game_stub);
2022-03-20 19:51:29 +01:00
client_spawn_hook.create(0x1404B0710, client_spawn_stub);
sv_check_load_level_hook.create(0x1406B2940, sv_check_load_level_stub);
2021-04-23 03:46:11 +02:00
2022-03-18 22:02:44 +01:00
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);
2022-06-19 21:21:09 +02:00
sl_get_canonical_string_hook.create(game::SL_GetCanonicalString, sl_get_canonical_string_stub);
2021-04-23 03:46:11 +02:00
2022-06-21 03:30:41 +02:00
scr_auto_respawn = dvars::register_bool("scr_autoRespawn", true, 0, "Automatically respawn player on death");
respawn_hook.create(0x1404B1E00, respawn_stub);
2022-07-05 03:36:17 +02:00
scr_run_current_threads_hook.create(0x1405C8370, scr_run_current_threads_stub);
scr_get_dvar_int_hook.create(0x1404F0730, scr_get_dvar_int_stub);
2022-12-18 04:15:25 +01:00
scr_delete_hook.create(0x1404F0460, scr_delete_stub);
2022-10-23 06:40:36 +02:00
utils::hook::call(0x1404B07D2, get_spawn_point_stub);
scheduler::loop([]()
{
lua::engine::run_frame();
}, scheduler::pipeline::server);
}
};
2021-04-23 03:46:11 +02:00
}
REGISTER_COMPONENT(scripting::component)