Scripting changes + SP support

This commit is contained in:
Federico Cecchetto
2022-04-05 01:30:24 +02:00
parent 7cfa1f8452
commit 81e9ee451a
13 changed files with 249 additions and 125 deletions

View File

@ -212,7 +212,7 @@ namespace logfile
return false;
}
const auto hook = vm_execute_hooks[pos];
const auto& hook = vm_execute_hooks[pos];
const auto state = hook.lua_state();
const scripting::entity self = local_id_to_entity(game::scr_VmPub->function_frame->fs.localId);

View File

@ -2,7 +2,6 @@
#include "loader/component_loader.hpp"
#include "game/game.hpp"
#include <utils/hook.hpp>
#include "game/scripting/entity.hpp"
#include "game/scripting/functions.hpp"
@ -13,6 +12,8 @@
#include "scheduler.hpp"
#include "scripting.hpp"
#include <utils/hook.hpp>
namespace scripting
{
std::unordered_map<int, std::unordered_map<std::string, int>> fields_table;
@ -21,6 +22,7 @@ namespace scripting
namespace
{
utils::hook::detour vm_notify_hook;
utils::hook::detour vm_execute_hook;
utils::hook::detour scr_load_level_hook;
utils::hook::detour g_shutdown_game_hook;
@ -29,7 +31,10 @@ namespace scripting
utils::hook::detour scr_set_thread_position_hook;
utils::hook::detour process_script_hook;
utils::hook::detour sl_get_canonical_string_hook;
std::string current_file;
unsigned int current_file_id{};
void vm_notify_stub(const unsigned int notify_list_owner_id, const game::scr_string_t string_value,
game::VariableValue* top)
@ -48,11 +53,6 @@ namespace scripting
e.arguments.emplace_back(*value);
}
if (e.name == "entitydeleted")
{
scripting::clear_entity_fields(e.entity);
}
lua::engine::notify(e);
}
}
@ -60,6 +60,16 @@ namespace scripting
vm_notify_hook.invoke<void>(notify_list_owner_id, string_value, top);
}
unsigned int vm_execute_stub()
{
if (!lua::engine::is_running())
{
lua::engine::start();
}
return vm_execute_hook.invoke<unsigned int>();
}
void scr_load_level_stub()
{
scr_load_level_hook.invoke<void>();
@ -71,20 +81,25 @@ namespace scripting
void g_shutdown_game_stub(const int free_scripts)
{
if (free_scripts)
{
script_function_table.clear();
}
lua::engine::stop();
return g_shutdown_game_hook.invoke<void>(free_scripts);
}
void scr_add_class_field_stub(unsigned int classnum, game::scr_string_t _name, unsigned int canonicalString, unsigned int offset)
void scr_add_class_field_stub(unsigned int classnum, game::scr_string_t name, unsigned int canonical_string, unsigned int offset)
{
const auto name = game::SL_ConvertToString(_name);
const auto name_str = game::SL_ConvertToString(name);
if (fields_table[classnum].find(name) == fields_table[classnum].end())
if (fields_table[classnum].find(name_str) == fields_table[classnum].end())
{
fields_table[classnum][name] = offset;
fields_table[classnum][name_str] = offset;
}
scr_add_class_field_hook.invoke<void>(classnum, _name, canonicalString, offset);
scr_add_class_field_hook.invoke<void>(classnum, name, canonical_string, offset);
}
void process_script_stub(const char* filename)
@ -92,21 +107,49 @@ namespace scripting
const auto file_id = atoi(filename);
if (file_id)
{
current_file = scripting::find_token(file_id);
current_file_id = file_id;
}
else
{
current_file_id = 0;
current_file = filename;
}
process_script_hook.invoke<void>(filename);
}
void scr_set_thread_position_stub(unsigned int threadName, const char* codePos)
void add_function(const std::string& file, unsigned int id, const char* pos)
{
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);
const auto function_names = scripting::find_token(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::find_token(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<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);
scripting::token_map[str] = result;
return result;
}
}
@ -115,11 +158,6 @@ namespace scripting
public:
void post_unpack() override
{
if (game::environment::is_sp())
{
return;
}
vm_notify_hook.create(SELECT_VALUE(0x140379A00, 0x1404479F0), vm_notify_stub);
scr_add_class_field_hook.create(SELECT_VALUE(0x140370370, 0x14043E2C0), scr_add_class_field_stub);
@ -127,9 +165,19 @@ namespace scripting
scr_set_thread_position_hook.create(SELECT_VALUE(0x14036A180, 0x140437D10), scr_set_thread_position_stub);
process_script_hook.create(SELECT_VALUE(0x1403737E0, 0x1404417E0), process_script_stub);
scr_load_level_hook.create(SELECT_VALUE(0x1402A5BE0, 0x1403727C0), scr_load_level_stub);
if (!game::environment::is_sp())
{
scr_load_level_hook.create(SELECT_VALUE(0x1402A5BE0, 0x1403727C0), scr_load_level_stub);
}
else
{
vm_execute_hook.create(SELECT_VALUE(0x140376590, 0x140444580), vm_execute_stub);
}
g_shutdown_game_hook.create(SELECT_VALUE(0x140277D40, 0x140345A60), g_shutdown_game_stub);
sl_get_canonical_string_hook.create(game::SL_GetCanonicalString, sl_get_canonical_string_stub);
scheduler::loop([]()
{
lua::engine::run_frame();