2022-03-01 15:14:41 -05:00
|
|
|
#include <std_include.hpp>
|
|
|
|
#include "loader/component_loader.hpp"
|
|
|
|
|
2022-10-21 18:15:58 -04:00
|
|
|
#include "component/gsc/script_extension.hpp"
|
|
|
|
#include "component/gsc/script_loading.hpp"
|
|
|
|
#include "component/scheduler.hpp"
|
|
|
|
#include "component/scripting.hpp"
|
2022-03-01 15:14:41 -05:00
|
|
|
|
2022-10-23 17:31:32 -04:00
|
|
|
#include "console.hpp"
|
|
|
|
|
2022-10-18 06:38:06 -04:00
|
|
|
#include "game/game.hpp"
|
|
|
|
|
|
|
|
#include "game/scripting/event.hpp"
|
|
|
|
#include "game/scripting/execution.hpp"
|
|
|
|
#include "game/scripting/functions.hpp"
|
|
|
|
#include "game/scripting/lua/engine.hpp"
|
2022-09-11 13:14:42 -04:00
|
|
|
|
2022-04-04 19:30:24 -04:00
|
|
|
#include <utils/hook.hpp>
|
|
|
|
|
2022-03-01 15:14:41 -05:00
|
|
|
namespace scripting
|
|
|
|
{
|
2022-03-02 20:09:13 -05:00
|
|
|
std::unordered_map<int, std::unordered_map<std::string, int>> fields_table;
|
2022-10-18 06:38:06 -04:00
|
|
|
|
2022-03-01 15:14:41 -05:00
|
|
|
std::unordered_map<std::string, std::unordered_map<std::string, const char*>> script_function_table;
|
2022-09-11 13:14:42 -04:00
|
|
|
std::unordered_map<std::string, std::vector<std::pair<std::string, const char*>>> script_function_table_sort;
|
2022-10-18 06:38:06 -04:00
|
|
|
std::unordered_map<const char*, std::pair<std::string, std::string>> script_function_table_rev;
|
|
|
|
|
2022-04-04 19:54:13 -04:00
|
|
|
utils::concurrency::container<shared_table_t> shared_table;
|
2022-03-01 15:14:41 -05:00
|
|
|
|
2022-09-11 13:14:42 -04:00
|
|
|
std::string current_file;
|
|
|
|
|
2022-03-01 15:14:41 -05:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
utils::hook::detour vm_notify_hook;
|
2022-04-04 19:30:24 -04:00
|
|
|
utils::hook::detour vm_execute_hook;
|
2022-09-11 13:14:42 -04:00
|
|
|
utils::hook::detour g_load_structs_hook;
|
2022-03-01 15:14:41 -05:00
|
|
|
utils::hook::detour scr_load_level_hook;
|
|
|
|
utils::hook::detour g_shutdown_game_hook;
|
|
|
|
|
2022-03-02 20:09:13 -05:00
|
|
|
utils::hook::detour scr_add_class_field_hook;
|
|
|
|
|
2022-03-01 15:14:41 -05:00
|
|
|
utils::hook::detour scr_set_thread_position_hook;
|
|
|
|
utils::hook::detour process_script_hook;
|
|
|
|
|
2022-04-04 19:30:24 -04:00
|
|
|
utils::hook::detour sl_get_canonical_string_hook;
|
|
|
|
|
2022-04-10 15:40:20 -04:00
|
|
|
utils::hook::detour db_find_xasset_header_hook;
|
|
|
|
|
2022-10-18 06:38:06 -04:00
|
|
|
std::string current_script_file;
|
2022-04-04 19:30:24 -04:00
|
|
|
unsigned int current_file_id{};
|
2022-03-01 15:14:41 -05:00
|
|
|
|
2022-04-10 15:40:20 -04:00
|
|
|
game::dvar_t* g_dump_scripts;
|
|
|
|
|
2022-09-11 13:14:42 -04:00
|
|
|
std::vector<std::function<void(bool)>> shutdown_callbacks;
|
|
|
|
|
|
|
|
std::unordered_map<unsigned int, std::string> canonical_string_table;
|
2022-06-30 11:03:41 -04:00
|
|
|
|
2022-03-01 15:14:41 -05:00
|
|
|
void vm_notify_stub(const unsigned int notify_list_owner_id, const game::scr_string_t string_value,
|
|
|
|
game::VariableValue* top)
|
|
|
|
{
|
|
|
|
if (!game::VirtualLobby_Loaded())
|
|
|
|
{
|
|
|
|
const auto* string = game::SL_ConvertToString(string_value);
|
|
|
|
if (string)
|
|
|
|
{
|
|
|
|
event e;
|
|
|
|
e.name = string;
|
|
|
|
e.entity = notify_list_owner_id;
|
|
|
|
|
2022-11-03 13:43:50 -04:00
|
|
|
for (auto* value = top; value->type != game::VAR_PRECODEPOS; --value)
|
2022-03-01 15:14:41 -05:00
|
|
|
{
|
|
|
|
e.arguments.emplace_back(*value);
|
|
|
|
}
|
|
|
|
|
|
|
|
lua::engine::notify(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vm_notify_hook.invoke<void>(notify_list_owner_id, string_value, top);
|
|
|
|
}
|
|
|
|
|
2022-04-04 19:30:24 -04:00
|
|
|
unsigned int vm_execute_stub()
|
|
|
|
{
|
|
|
|
if (!lua::engine::is_running())
|
|
|
|
{
|
|
|
|
lua::engine::start();
|
|
|
|
}
|
|
|
|
|
|
|
|
return vm_execute_hook.invoke<unsigned int>();
|
|
|
|
}
|
|
|
|
|
2022-09-11 13:14:42 -04:00
|
|
|
void g_load_structs_stub()
|
2022-03-01 15:14:41 -05:00
|
|
|
{
|
|
|
|
if (!game::VirtualLobby_Loaded())
|
|
|
|
{
|
2022-09-11 13:14:42 -04:00
|
|
|
game::G_LogPrintf("------------------------------------------------------------\n");
|
|
|
|
game::G_LogPrintf("InitGame\n");
|
|
|
|
|
2022-03-01 15:14:41 -05:00
|
|
|
lua::engine::start();
|
2022-09-11 13:14:42 -04:00
|
|
|
|
|
|
|
gsc::load_main_handles();
|
2022-03-01 15:14:41 -05:00
|
|
|
}
|
2022-09-11 13:14:42 -04:00
|
|
|
|
|
|
|
g_load_structs_hook.invoke<void>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void scr_load_level_stub()
|
|
|
|
{
|
|
|
|
if (!game::VirtualLobby_Loaded())
|
|
|
|
{
|
|
|
|
gsc::load_init_handles();
|
|
|
|
}
|
|
|
|
|
|
|
|
scr_load_level_hook.invoke<void>();
|
2022-03-01 15:14:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void g_shutdown_game_stub(const int free_scripts)
|
|
|
|
{
|
2022-04-04 19:30:24 -04:00
|
|
|
if (free_scripts)
|
|
|
|
{
|
2022-09-11 13:14:42 -04:00
|
|
|
script_function_table_sort.clear();
|
2022-04-04 19:30:24 -04:00
|
|
|
script_function_table.clear();
|
2022-09-11 13:14:42 -04:00
|
|
|
canonical_string_table.clear();
|
2022-04-04 19:30:24 -04:00
|
|
|
}
|
|
|
|
|
2022-06-30 11:03:41 -04:00
|
|
|
for (const auto& callback : shutdown_callbacks)
|
|
|
|
{
|
2022-09-11 13:14:42 -04:00
|
|
|
callback(free_scripts);
|
2022-06-30 11:03:41 -04:00
|
|
|
}
|
|
|
|
|
2022-05-24 15:55:53 -04:00
|
|
|
scripting::notify(*game::levelEntityId, "shutdownGame_called", {1});
|
2022-03-01 15:14:41 -05:00
|
|
|
lua::engine::stop();
|
2022-09-11 13:14:42 -04:00
|
|
|
|
|
|
|
game::G_LogPrintf("ShutdownGame:\n");
|
|
|
|
game::G_LogPrintf("------------------------------------------------------------\n");
|
|
|
|
|
2022-10-18 06:38:06 -04:00
|
|
|
g_shutdown_game_hook.invoke<void>(free_scripts);
|
2022-03-01 15:14:41 -05:00
|
|
|
}
|
|
|
|
|
2022-04-04 19:30:24 -04:00
|
|
|
void scr_add_class_field_stub(unsigned int classnum, game::scr_string_t name, unsigned int canonical_string, unsigned int offset)
|
2022-03-02 20:09:13 -05:00
|
|
|
{
|
2022-04-04 19:30:24 -04:00
|
|
|
const auto name_str = game::SL_ConvertToString(name);
|
2022-03-02 20:09:13 -05:00
|
|
|
|
2022-04-04 19:30:24 -04:00
|
|
|
if (fields_table[classnum].find(name_str) == fields_table[classnum].end())
|
2022-03-02 20:09:13 -05:00
|
|
|
{
|
2022-04-04 19:30:24 -04:00
|
|
|
fields_table[classnum][name_str] = offset;
|
2022-03-02 20:09:13 -05:00
|
|
|
}
|
|
|
|
|
2022-04-04 19:30:24 -04:00
|
|
|
scr_add_class_field_hook.invoke<void>(classnum, name, canonical_string, offset);
|
2022-03-02 20:09:13 -05:00
|
|
|
}
|
|
|
|
|
2022-03-01 15:14:41 -05:00
|
|
|
void process_script_stub(const char* filename)
|
|
|
|
{
|
2022-10-18 06:38:06 -04:00
|
|
|
current_script_file = filename;
|
2022-09-11 13:14:42 -04:00
|
|
|
|
2022-03-01 15:14:41 -05:00
|
|
|
const auto file_id = atoi(filename);
|
|
|
|
if (file_id)
|
|
|
|
{
|
2022-09-11 13:14:42 -04:00
|
|
|
current_file_id = static_cast<std::uint16_t>(file_id);
|
2022-03-01 15:14:41 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-04-04 19:30:24 -04:00
|
|
|
current_file_id = 0;
|
2022-03-01 15:14:41 -05:00
|
|
|
current_file = filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
process_script_hook.invoke<void>(filename);
|
|
|
|
}
|
|
|
|
|
2022-09-11 13:14:42 -04:00
|
|
|
void add_function_sort(unsigned int id, const char* pos)
|
2022-03-01 15:14:41 -05:00
|
|
|
{
|
2022-09-11 13:14:42 -04:00
|
|
|
std::string filename = current_file;
|
|
|
|
if (current_file_id)
|
2022-04-04 19:30:24 -04:00
|
|
|
{
|
2022-09-11 13:14:42 -04:00
|
|
|
filename = scripting::get_token(current_file_id);
|
2022-04-04 19:30:24 -04:00
|
|
|
}
|
2022-09-11 13:14:42 -04:00
|
|
|
|
2022-10-18 06:38:06 -04:00
|
|
|
if (!script_function_table_sort.contains(filename))
|
2022-09-11 13:14:42 -04:00
|
|
|
{
|
2022-10-18 06:38:06 -04:00
|
|
|
const auto script = gsc::find_script(game::ASSET_TYPE_SCRIPTFILE, current_script_file.data(), false);
|
2022-09-11 13:14:42 -04:00
|
|
|
if (script)
|
|
|
|
{
|
|
|
|
const auto end = &script->bytecode[script->bytecodeLen];
|
|
|
|
script_function_table_sort[filename].emplace_back("__end__", end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto name = scripting::get_token(id);
|
|
|
|
auto& itr = script_function_table_sort[filename];
|
|
|
|
itr.insert(itr.end() - 1, {name, pos});
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_function(const std::string& file, unsigned int id, const char* pos)
|
|
|
|
{
|
|
|
|
const auto name = get_token(id);
|
|
|
|
script_function_table[file][name] = pos;
|
2022-10-18 06:38:06 -04:00
|
|
|
script_function_table_rev[pos] = {file, name};
|
2022-04-04 19:30:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void scr_set_thread_position_stub(unsigned int thread_name, const char* code_pos)
|
|
|
|
{
|
2022-09-11 13:14:42 -04:00
|
|
|
add_function_sort(thread_name, code_pos);
|
|
|
|
|
2022-04-04 19:30:24 -04:00
|
|
|
if (current_file_id)
|
|
|
|
{
|
2022-09-11 13:14:42 -04:00
|
|
|
const auto name = get_token(current_file_id);
|
|
|
|
add_function(name, thread_name, code_pos);
|
2022-04-04 19:30:24 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
add_function(current_file, thread_name, code_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
scr_set_thread_position_hook.invoke<void>(thread_name, code_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int sl_get_canonical_string_stub(const char* str)
|
|
|
|
{
|
|
|
|
const auto result = sl_get_canonical_string_hook.invoke<unsigned int>(str);
|
2022-09-11 13:14:42 -04:00
|
|
|
canonical_string_table[result] = str;
|
2022-04-04 19:30:24 -04:00
|
|
|
return result;
|
2022-03-01 15:14:41 -05:00
|
|
|
}
|
2022-10-23 17:14:59 -04:00
|
|
|
|
|
|
|
void* get_spawn_point_stub()
|
|
|
|
{
|
|
|
|
const auto spawn_point = utils::hook::invoke<void*>(0x28BD50_b);
|
|
|
|
if (spawn_point == nullptr)
|
|
|
|
{
|
|
|
|
console::warn("No spawnpoint found for this map, using (0, 0, 0)\n");
|
|
|
|
return &game::sp::g_entities[0];
|
|
|
|
}
|
|
|
|
return spawn_point;
|
|
|
|
}
|
2022-03-01 15:14:41 -05:00
|
|
|
}
|
|
|
|
|
2022-09-11 13:14:42 -04:00
|
|
|
std::string get_token(unsigned int id)
|
|
|
|
{
|
|
|
|
if (canonical_string_table.find(id) != canonical_string_table.end())
|
|
|
|
{
|
|
|
|
return canonical_string_table[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
return scripting::find_token(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_shutdown(const std::function<void(bool)>& callback)
|
2022-06-30 11:03:41 -04:00
|
|
|
{
|
|
|
|
shutdown_callbacks.push_back(callback);
|
|
|
|
}
|
|
|
|
|
2022-09-11 13:14:42 -04:00
|
|
|
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]};
|
|
|
|
}
|
|
|
|
|
2022-03-01 15:14:41 -05:00
|
|
|
class component final : public component_interface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void post_unpack() override
|
|
|
|
{
|
2022-05-25 17:56:10 -04:00
|
|
|
vm_notify_hook.create(SELECT_VALUE(0x3CD500_b, 0x514560_b), vm_notify_stub);
|
2022-03-02 20:09:13 -05:00
|
|
|
|
2022-05-25 17:56:10 -04:00
|
|
|
scr_add_class_field_hook.create(SELECT_VALUE(0x3C3CE0_b, 0x50AE20_b), scr_add_class_field_stub);
|
2022-03-01 15:14:41 -05:00
|
|
|
|
2022-05-25 17:56:10 -04:00
|
|
|
scr_set_thread_position_hook.create(SELECT_VALUE(0x3BD890_b, 0x504870_b), scr_set_thread_position_stub);
|
|
|
|
process_script_hook.create(SELECT_VALUE(0x3C7200_b, 0x50E340_b), process_script_stub);
|
2022-04-10 15:40:20 -04:00
|
|
|
sl_get_canonical_string_hook.create(game::SL_GetCanonicalString, sl_get_canonical_string_stub);
|
2022-03-02 20:09:13 -05:00
|
|
|
|
2022-09-11 13:14:42 -04:00
|
|
|
g_load_structs_hook.create(SELECT_VALUE(0x2E7970_b, 0x458520_b), g_load_structs_stub);
|
|
|
|
scr_load_level_hook.create(SELECT_VALUE(0x2D4CD0_b, 0x450FC0_b), scr_load_level_stub);
|
|
|
|
if (game::environment::is_sp())
|
2022-04-04 19:30:24 -04:00
|
|
|
{
|
2022-05-25 17:56:10 -04:00
|
|
|
vm_execute_hook.create(0x3CA080_b, vm_execute_stub);
|
2022-04-04 19:30:24 -04:00
|
|
|
}
|
|
|
|
|
2022-05-25 17:56:10 -04:00
|
|
|
g_shutdown_game_hook.create(SELECT_VALUE(0x2A5130_b, 0x422F30_b), g_shutdown_game_stub);
|
2022-03-01 15:14:41 -05:00
|
|
|
|
2022-10-23 17:14:59 -04:00
|
|
|
if (game::environment::is_sp())
|
|
|
|
{
|
|
|
|
utils::hook::call(0x28AE82_b, get_spawn_point_stub);
|
|
|
|
}
|
|
|
|
|
2022-10-18 06:38:06 -04:00
|
|
|
scheduler::loop([]
|
2022-03-02 20:09:13 -05:00
|
|
|
{
|
|
|
|
lua::engine::run_frame();
|
|
|
|
}, scheduler::pipeline::server);
|
2022-03-01 15:14:41 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-24 15:55:53 -04:00
|
|
|
REGISTER_COMPONENT(scripting::component)
|