2021-09-06 18:40:37 -04:00
|
|
|
#include <std_include.hpp>
|
2021-04-23 14:57:21 -04:00
|
|
|
#include "loader/component_loader.hpp"
|
2021-04-22 21:46:11 -04:00
|
|
|
|
|
|
|
#include "game/game.hpp"
|
2021-04-23 14:57:21 -04:00
|
|
|
|
2021-09-17 18:44:35 -04:00
|
|
|
#include "command.hpp"
|
2021-04-23 14:57:21 -04:00
|
|
|
#include "scheduler.hpp"
|
2021-08-29 22:58:10 -04:00
|
|
|
#include "scripting.hpp"
|
2021-04-22 21:46:11 -04:00
|
|
|
|
|
|
|
#include "game/scripting/event.hpp"
|
2021-08-29 22:58:10 -04:00
|
|
|
#include "game/scripting/functions.hpp"
|
2021-09-18 12:29:03 -04:00
|
|
|
#include "game/scripting/execution.hpp"
|
2021-04-22 21:46:11 -04:00
|
|
|
#include "game/scripting/lua/engine.hpp"
|
|
|
|
|
2021-04-23 14:57:21 -04:00
|
|
|
#include <utils/hook.hpp>
|
2021-04-22 21:46:11 -04:00
|
|
|
|
|
|
|
namespace scripting
|
|
|
|
{
|
|
|
|
std::unordered_map<int, std::unordered_map<std::string, int>> fields_table;
|
2021-08-29 22:58:10 -04:00
|
|
|
std::unordered_map<std::string, std::unordered_map<std::string, const char*>> script_function_table;
|
2022-01-04 11:57:09 -05:00
|
|
|
utils::concurrency::container<shared_table_t> shared_table;
|
2021-04-22 21:46:11 -04:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
utils::hook::detour vm_notify_hook;
|
|
|
|
|
|
|
|
utils::hook::detour g_shutdown_game_hook;
|
2021-09-18 12:29:03 -04:00
|
|
|
utils::hook::detour player_spawn_hook;
|
2021-04-22 21:46:11 -04:00
|
|
|
|
|
|
|
utils::hook::detour scr_add_class_field_hook;
|
|
|
|
|
2021-08-29 22:58:10 -04:00
|
|
|
utils::hook::detour scr_set_thread_position_hook;
|
|
|
|
utils::hook::detour process_script_hook;
|
|
|
|
|
|
|
|
std::string current_file;
|
|
|
|
|
2021-04-22 21:46:11 -04: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
lua::engine::notify(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
vm_notify_hook.invoke<void>(notify_list_owner_id, string_value, top);
|
|
|
|
}
|
|
|
|
|
2021-09-18 12:29:03 -04:00
|
|
|
void player_spawn_stub(const game::gentity_s* player)
|
2021-04-22 21:46:11 -04:00
|
|
|
{
|
2021-09-18 12:29:03 -04:00
|
|
|
player_spawn_hook.invoke<void>(player);
|
2021-04-22 21:46:11 -04:00
|
|
|
lua::engine::start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void g_shutdown_game_stub(const int free_scripts)
|
|
|
|
{
|
|
|
|
lua::engine::stop();
|
2021-04-24 13:06:34 -04:00
|
|
|
g_shutdown_game_hook.invoke<void>(free_scripts);
|
2021-04-22 21:46:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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<void>(classnum, _name, canonicalString, offset);
|
|
|
|
}
|
2021-08-29 22:58:10 -04:00
|
|
|
|
|
|
|
void process_script_stub(const char* filename)
|
|
|
|
{
|
|
|
|
current_file = filename;
|
|
|
|
|
|
|
|
const auto file_id = atoi(filename);
|
|
|
|
if (file_id)
|
|
|
|
{
|
|
|
|
current_file = scripting::find_token(file_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
process_script_hook.invoke<void>(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
void scr_set_thread_position_stub(unsigned int threadName, const char* codePos)
|
|
|
|
{
|
|
|
|
const auto function_name = scripting::find_token(threadName);
|
|
|
|
script_function_table[current_file][function_name] = codePos;
|
|
|
|
scr_set_thread_position_hook.invoke<void>(threadName, codePos);
|
|
|
|
}
|
2021-12-24 11:51:52 -05:00
|
|
|
|
|
|
|
utils::hook::detour sub_6B2940_hook;
|
|
|
|
char sub_6B2940_stub(void* a1)
|
|
|
|
{
|
|
|
|
const auto result = sub_6B2940_hook.invoke<char>(a1);
|
|
|
|
if (a1 != nullptr)
|
|
|
|
{
|
|
|
|
lua::engine::start();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2021-04-22 21:46:11 -04:00
|
|
|
}
|
|
|
|
|
2021-04-23 14:57:21 -04:00
|
|
|
class component final : public component_interface
|
2021-04-22 21:46:11 -04:00
|
|
|
{
|
2021-04-23 14:57:21 -04:00
|
|
|
public:
|
|
|
|
void post_unpack() override
|
|
|
|
{
|
2022-03-18 17:02:44 -04:00
|
|
|
vm_notify_hook.create(0x1405CC450, vm_notify_stub);
|
2021-04-22 21:46:11 -04:00
|
|
|
|
2022-03-18 17:02:44 -04:00
|
|
|
g_shutdown_game_hook.create(0x1404CBAD0, g_shutdown_game_stub);
|
|
|
|
player_spawn_hook.create(0x1404B0710, player_spawn_stub);
|
2021-04-22 21:46:11 -04:00
|
|
|
|
2022-03-18 17:02:44 -04: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);
|
2021-04-22 21:46:11 -04:00
|
|
|
|
2021-12-24 11:51:52 -05:00
|
|
|
// Loading last checkpoint doesn't call spawn player again (player_spawn_hook)
|
|
|
|
// Not sure what this function does but `a1` is != nullptr when loading
|
|
|
|
// the last checkpoint so we need to start lua in this context
|
2022-03-18 17:02:44 -04:00
|
|
|
sub_6B2940_hook.create(0x1406B2940, sub_6B2940_stub);
|
2021-12-24 11:51:52 -05:00
|
|
|
|
2021-04-23 14:57:21 -04:00
|
|
|
scheduler::loop([]()
|
|
|
|
{
|
|
|
|
lua::engine::run_frame();
|
|
|
|
}, scheduler::pipeline::server);
|
|
|
|
}
|
|
|
|
};
|
2021-04-22 21:46:11 -04:00
|
|
|
}
|
2021-04-23 14:57:21 -04:00
|
|
|
|
|
|
|
REGISTER_COMPONENT(scripting::component)
|