Scripting changes + SP support

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

View File

@ -264,6 +264,7 @@ filter {}
filter "configurations:Debug"
optimize "Debug"
buildoptions {"/bigobj"}
defines {"DEBUG", "_DEBUG"}
filter {}

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();

View File

@ -145,47 +145,6 @@ namespace scripting
return exec_ent_thread(entity, pos, arguments);
}
static std::unordered_map<unsigned int, std::unordered_map<std::string, script_value>> custom_fields;
script_value get_custom_field(const entity& entity, const std::string& field)
{
auto& fields = custom_fields[entity.get_entity_id()];
const auto _field = fields.find(field);
if (_field != fields.end())
{
return _field->second;
}
return {};
}
void set_custom_field(const entity& entity, const std::string& field, const script_value& value)
{
const auto id = entity.get_entity_id();
if (custom_fields[id].find(field) != custom_fields[id].end())
{
custom_fields[id][field] = value;
return;
}
custom_fields[id].insert(std::make_pair(field, value));
}
void clear_entity_fields(const entity& entity)
{
const auto id = entity.get_entity_id();
if (custom_fields.find(id) != custom_fields.end())
{
custom_fields[id].clear();
}
}
void clear_custom_fields()
{
custom_fields.clear();
}
void set_entity_field(const entity& entity, const std::string& field, const script_value& value)
{
const auto entref = entity.get_entity_reference();
@ -206,8 +165,7 @@ namespace scripting
}
else
{
// Read custom fields
set_custom_field(entity, field, value);
set_object_variable(entity.get_entity_id(), field, value);
}
}
@ -234,8 +192,7 @@ namespace scripting
return value;
}
// Add custom fields
return get_custom_field(entity, field);
return get_object_variable(entity.get_entity_id(), field);
}
unsigned int make_array()
@ -248,4 +205,47 @@ namespace scripting
return index;
}
void set_object_variable(const unsigned int parent_id, const unsigned int id, const script_value& value)
{
const auto offset = 0xFA00 * (parent_id & 3);
const auto variable_id = game::GetVariable(parent_id, id);
const auto variable = &game::scr_VarGlob->childVariableValue[variable_id + offset];
const auto& raw_value = value.get_raw();
game::AddRefToValue(raw_value.type, raw_value.u);
game::RemoveRefToValue(variable->type, variable->u.u);
variable->type = static_cast<char>(raw_value.type);
variable->u.u = raw_value.u;
}
void set_object_variable(const unsigned int parent_id, const std::string& name, const script_value& value)
{
const auto id = scripting::find_token_id(name);
set_object_variable(parent_id, id, value);
}
script_value get_object_variable(const unsigned int parent_id, const unsigned int id)
{
const auto offset = 0xFA00 * (parent_id & 3);
const auto variable_id = game::FindVariable(parent_id, id);
if (!variable_id)
{
return {};
}
const auto variable = &game::scr_VarGlob->childVariableValue[variable_id + offset];
game::VariableValue value{};
value.type = static_cast<int>(variable->type);
value.u = variable->u.u;
return value;
}
script_value get_object_variable(const unsigned int parent_id, const std::string& name)
{
const auto id = scripting::find_token_id(name);
return get_object_variable(parent_id, id);
}
}

View File

@ -27,13 +27,16 @@ namespace scripting
script_value call_script_function(const entity& entity, const std::string& filename,
const std::string& function, const std::vector<script_value>& arguments);
void clear_entity_fields(const entity& entity);
void clear_custom_fields();
void set_entity_field(const entity& entity, const std::string& field, const script_value& value);
script_value get_entity_field(const entity& entity, const std::string& field);
void notify(const entity& entity, const std::string& event, const std::vector<script_value>& arguments);
unsigned int make_array();
script_value get_object_variable(const unsigned int parent_id, const unsigned int id);
script_value get_object_variable(const unsigned int parent_id, const std::string& name);
void set_object_variable(const unsigned int parent_id, const std::string& name, const script_value& value);
void set_object_variable(const unsigned int parent_id, const unsigned int id, const script_value& value);
}

View File

@ -69,19 +69,40 @@ namespace scripting
return reinterpret_cast<script_function*>(method_table)[index - 0x8000];
}
unsigned int parse_token_id(const std::string& name)
{
if (name.starts_with("_ID"))
{
return static_cast<unsigned int>(std::strtol(name.substr(3).data(), nullptr, 10));
}
if (name.starts_with("_id_"))
{
return static_cast<unsigned int>(std::strtol(name.substr(4).data(), nullptr, 16));
}
return 0;
}
}
std::string find_token(unsigned int id)
std::vector<std::string> find_token(unsigned int id)
{
std::vector<std::string> results;
results.push_back(utils::string::va("_id_%X", id));
results.push_back(utils::string::va("_ID%i", id));
for (const auto& token : token_map)
{
if (token.second == id)
{
return token.first;
results.push_back(token.first);
break;
}
}
return utils::string::va("_ID%i", id);
return results;
}
unsigned int find_token_id(const std::string& name)
@ -93,7 +114,13 @@ namespace scripting
return result->second;
}
return 0;
const auto parsed_id = parse_token_id(name);
if (parsed_id)
{
return parsed_id;
}
return game::SL_GetCanonicalString(name.data());
}
script_function find_function(const std::string& name, const bool prefer_global)

View File

@ -9,7 +9,7 @@ namespace scripting
using script_function = void(*)(game::scr_entref_t);
std::string find_token(unsigned int id);
std::vector<std::string> find_token(unsigned int id);
unsigned int find_token_id(const std::string& name);
script_function find_function(const std::string& name, const bool prefer_global);

View File

@ -17,7 +17,6 @@ namespace scripting::lua
{
namespace
{
vector normalize_vector(const vector& vec)
{
const auto length = sqrt(
@ -155,12 +154,52 @@ namespace scripting::lua
{
return normalize_vector(a);
};
vector_type["normalize"] = [](const vector& a)
{
return normalize_vector(a);
};
vector_type["toangles"] = [](const vector& a)
{
return call("vectortoangles", {a}).as<vector>();
};
vector_type["toyaw"] = [](const vector& a)
{
return call("vectortoyaw", {a}).as<vector>();
};
vector_type["tolerp"] = [](const vector& a)
{
return call("vectortolerp", {a}).as<vector>();
};
vector_type["toup"] = [](const vector& a)
{
return call("anglestoup", {a}).as<vector>();
};
vector_type["toright"] = [](const vector& a)
{
return call("anglestoright", {a}).as<vector>();
};
vector_type["toforward"] = [](const vector& a)
{
return call("anglestoforward", {a}).as<vector>();
};
}
void setup_entity_type(sol::state& state, event_handler& handler, scheduler& scheduler)
{
state["level"] = entity{*game::levelEntityId};
if (game::environment::is_sp())
{
state["player"] = call("getentbynum", {0}).as<entity>();
}
auto entity_type = state.new_usertype<entity>("entity");
for (const auto& func : method_map)

View File

@ -11,6 +11,8 @@ namespace scripting::lua::engine
{
namespace
{
bool running = false;
auto& get_scripts()
{
static std::vector<std::unique_ptr<context>> scripts{};
@ -38,21 +40,30 @@ namespace scripting::lua::engine
void stop()
{
running = false;
logfile::clear_callbacks();
get_scripts().clear();
}
void start()
{
// No SP until there is a concept
if (game::environment::is_sp())
{
return;
}
stop();
load_scripts("h1-mod/scripts/");
load_scripts("data/scripts/");
if (game::environment::is_sp())
{
load_scripts("h1-mod/scripts/sp/");
load_scripts("data/scripts/sp/");
}
else
{
load_scripts("h1-mod/scripts/mp/");
load_scripts("data/scripts/mp/");
}
running = true;
}
void notify(const event& e)
@ -70,4 +81,9 @@ namespace scripting::lua::engine
script->run_frame();
}
}
bool is_running()
{
return running;
}
}

View File

@ -8,4 +8,5 @@ namespace scripting::lua::engine
void stop();
void notify(const event& e);
void run_frame();
bool is_running();
}

View File

@ -165,56 +165,33 @@ namespace scripting::lua
auto table = sol::table::create(state);
auto metatable = sol::table::create(state);
const auto offset = 64000 * (parent_id & 3);
metatable[sol::meta_function::new_index] = [offset, parent_id](const sol::table t, const sol::this_state s,
metatable[sol::meta_function::new_index] = [parent_id](const sol::table t, const sol::this_state s,
const sol::lua_value& field, const sol::lua_value& value)
{
const auto id = field.is<std::string>()
? scripting::find_token_id(field.as<std::string>())
: field.as<int>();
if (!id)
const auto new_variable = convert({s, value});
if (field.is<unsigned int>())
{
return;
scripting::set_object_variable(parent_id, field.as<unsigned int>(), new_variable);
}
else if (field.is<std::string>())
{
scripting::set_object_variable(parent_id, field.as<std::string>(), new_variable);
}
const auto variable_id = game::GetVariable(parent_id, id);
const auto variable = &game::scr_VarGlob->childVariableValue[variable_id + offset];
const auto new_variable = convert({s, value}).get_raw();
game::AddRefToValue(new_variable.type, new_variable.u);
game::RemoveRefToValue(variable->type, variable->u.u);
variable->type = (char)new_variable.type;
variable->u.u = new_variable.u;
};
metatable[sol::meta_function::index] = [offset, parent_id](const sol::table t, const sol::this_state s,
metatable[sol::meta_function::index] = [parent_id](const sol::table t, const sol::this_state s,
const sol::lua_value& field)
{
const auto id = field.is<std::string>()
? scripting::find_token_id(field.as<std::string>())
: field.as<int>();
if (!id)
if (field.is<unsigned int>())
{
return sol::lua_value{s, sol::lua_nil};
return convert(s, scripting::get_object_variable(parent_id, field.as<unsigned int>()));
}
else if (field.is<std::string>())
{
return convert(s, scripting::get_object_variable(parent_id, field.as<std::string>()));
}
const auto variable_id = game::FindVariable(parent_id, id);
if (!variable_id)
{
return sol::lua_value{s, sol::lua_nil};
}
const auto variable = game::scr_VarGlob->childVariableValue[variable_id + offset];
game::VariableValue result{};
result.u = variable.u.u;
result.type = (game::scriptType_e)variable.type;
return convert(s, result);
return sol::lua_value{s, sol::lua_nil};
};
table[sol::metatable_key] = metatable;

View File

@ -70,7 +70,7 @@ namespace game
WEAK symbol<void(const char* gameName)> FS_Startup{0x1403B85D0, 0x1404EDD30};
WEAK symbol<void(const char* path, const char* dir)> FS_AddLocalizedGameDirectory{0x1403B6030, 0x1404EBE20};
WEAK symbol<unsigned int(unsigned int, unsigned int)> GetVariable{0x14036FDD0, 0x1403F3730};
WEAK symbol<unsigned int(unsigned int, unsigned int)> GetVariable{0x14036FDD0, 0x14043DD70};
WEAK symbol<unsigned int(unsigned int parentId, unsigned int unsignedValue)> GetNewVariable{0x14036FA00, 0x14043D990};
WEAK symbol<unsigned int(unsigned int parentId, unsigned int unsignedValue)> GetNewArrayVariable{0x14036F880, 0x14043D810};
WEAK symbol<void()> GScr_LoadConsts{0x1402D13E0, 0x140393810};
@ -136,6 +136,7 @@ namespace game
WEAK symbol<void()> Scr_ClearOutParams{0x140374460, 0x140442510};
WEAK symbol<scr_entref_t(unsigned int entId)> Scr_GetEntityIdRef{0x140372D50, 0x140440D80};
WEAK symbol<unsigned int(int classnum, unsigned int entnum)> Scr_GetEntityId{0x140372CA0, 0x140440CD0};
WEAK symbol<int(unsigned int classnum, int entnum, int offset)> Scr_SetObjectField{0x1402B9F60, 0x140385330};
WEAK symbol<ScreenPlacement* ()> ScrPlace_GetViewPlacement{0x1401981F0, 0x140288550};
@ -155,8 +156,8 @@ namespace game
WEAK symbol<scr_string_t(const char* str)> SL_FindString{0x14036D700, 0x14043B470};
WEAK symbol<scr_string_t(const char* str, unsigned int user)> SL_GetString{0x14036D9A0, 0x14043B840};
WEAK symbol<const char* (scr_string_t stringValue)> SL_ConvertToString{0x14036D420, 0x14043B170};
WEAK symbol<int(unsigned int classnum, int entnum, int offset)> Scr_SetObjectField{0x1402B9F60, 0x140385330};
WEAK symbol<const char*(scr_string_t stringValue)> SL_ConvertToString{0x14036D420, 0x14043B170};
WEAK symbol<unsigned int(const char* str)> SL_GetCanonicalString{0x14036A310, 0x140437EA0};
WEAK symbol<void(netadr_s* from)> SV_DirectConnect{0, 0x140480860};
WEAK symbol<void(int arg, char* buffer, int bufferLength)> SV_Cmd_ArgvBuffer{0x1403446C0, 0x140404CA0};

View File

@ -54,6 +54,17 @@ namespace ui_scripting::lua::engine
load_scripts("h1-mod/ui_scripts/");
load_scripts("data/ui_scripts/");
if (game::environment::is_sp())
{
load_scripts("h1-mod/ui_scripts/sp/");
load_scripts("data/ui_scripts/sp/");
}
else
{
load_scripts("h1-mod/ui_scripts/mp/");
load_scripts("data/ui_scripts/mp/");
}
}
void stop()