Some changes

This commit is contained in:
Federico Cecchetto 2022-04-29 21:51:25 +02:00
parent 232d06d837
commit b25119effc
3 changed files with 48 additions and 61 deletions

View File

@ -146,6 +146,7 @@ namespace logger
utils::hook::jump(0x14032C630, print_warning, true);
utils::hook::jump(0x14032AEF0, lui_print, true);
com_error_hook.create(0x1405A2D80, com_error_stub);
utils::hook::jump(0x14013A98C, print);
}
};
}

View File

@ -13,6 +13,7 @@
#include "fastfiles.hpp"
#include "mods.hpp"
#include "updater.hpp"
#include "game_console.hpp"
#include "game/ui_scripting/execution.hpp"
#include "game/scripting/execution.hpp"
@ -87,14 +88,14 @@ namespace ui_scripting
void print_error(const std::string& error)
{
printf("************** UI Script execution error **************\n");
printf("%s\n", error.data());
printf("****************************************************\n");
game_console::print(game_console::con_type_error, "************** LUI script execution error **************\n");
game_console::print(game_console::con_type_error, "%s\n", error.data());
game_console::print(game_console::con_type_error, "********************************************************\n");
}
void print_loading_script(const std::string& name)
{
printf("Loading LUI script '%s'\n", name.data());
game_console::print(game_console::con_type_info, "Loading LUI script '%s'\n", name.data());
}
std::string get_current_script()
@ -371,17 +372,11 @@ namespace ui_scripting
hks_package_require_hook.invoke<void>(state);
}
utils::hook::detour db_find_xasset_header_hook;
game::XAssetHeader db_find_xasset_header_stub(game::XAssetType type, const char* name, int allow_create_default)
{
if (type != game::XAssetType::ASSET_TYPE_LUAFILE)
{
return db_find_xasset_header_hook.invoke<game::XAssetHeader>(type, name, allow_create_default);
}
if (!is_loaded_script(globals.in_require_script))
{
return db_find_xasset_header_hook.invoke<game::XAssetHeader>(type, name, allow_create_default);
return game::DB_FindXAssetHeader(type, name, allow_create_default);
}
const auto folder = globals.in_require_script.substr(0, globals.in_require_script.find_last_of("/\\"));
@ -396,7 +391,7 @@ namespace ui_scripting
}
else if (name_.starts_with("ui/LUI/"))
{
return db_find_xasset_header_hook.invoke<game::XAssetHeader>(type, name, allow_create_default);
return game::DB_FindXAssetHeader(type, name, allow_create_default);
}
return static_cast<game::XAssetHeader>(nullptr);
@ -470,11 +465,9 @@ namespace ui_scripting
void post_unpack() override
{
utils::hook::call(0x14030BF2B, db_find_xasset_header_stub);
utils::hook::call(0x14030C079, db_find_xasset_header_stub);
utils::hook::call(0x14030C104, hks_load_stub);
utils::hook::jump(0x14013A98C, printf);
db_find_xasset_header_hook.create(game::DB_FindXAssetHeader, db_find_xasset_header_stub);
hks_package_require_hook.create(0x1402B4DA0, hks_package_require_stub);
hks_start_hook.create(0x140328BE0, hks_start_stub);

View File

@ -6,11 +6,41 @@
namespace ui_scripting
{
namespace
{
script_value get_field(void* ptr, game::hks::HksObjectType type, const script_value& key)
{
const auto state = *game::hks::lua_state;
const auto top = state->m_apistack.top;
push_value(key);
game::hks::HksObject value{};
game::hks::HksObject obj{};
obj.t = type;
obj.v.ptr = ptr;
game::hks::hks_obj_gettable(&value, state, &obj, &state->m_apistack.top[-1]);
state->m_apistack.top = top;
return value;
}
void set_field(void* ptr, game::hks::HksObjectType type, const script_value& key, const script_value& value)
{
const auto state = *game::hks::lua_state;
game::hks::HksObject obj{};
obj.t = type;
obj.v.ptr = ptr;
game::hks::hks_obj_settable(state, &obj, &key.get_raw(), &value.get_raw());
}
}
void push_value(const script_value& value)
{
const auto state = *game::hks::lua_state;
const auto value_ = value.get_raw();
*state->m_apistack.top = value_;
*state->m_apistack.top = value.get_raw();
state->m_apistack.top++;
}
@ -60,8 +90,8 @@ namespace ui_scripting
const auto globals = table((*::game::hks::lua_state)->globals.v.table);
const auto engine = globals.get("Engine").as<table>();
const auto root = engine.get("GetLuiRoot").as<function>().call({})[0].as<userdata>();
const auto process_event = root.get("processEvent").as<function>();
const auto root = engine.get("GetLuiRoot")()[0].as<userdata>();
const auto process_event = root.get("processEvent");
table event{};
event.set("name", name);
@ -72,14 +102,13 @@ namespace ui_scripting
event.set(arg.first, arg.second);
}
process_event.call({root, event});
process_event(root, event);
return true;
}
arguments call_script_function(const function& function, const arguments& arguments)
{
const auto state = *game::hks::lua_state;
state->m_apistack.top = state->m_apistack.base;
const auto top = state->m_apistack.top;
push_value(function);
@ -98,57 +127,21 @@ namespace ui_scripting
script_value get_field(const userdata& self, const script_value& key)
{
const auto state = *game::hks::lua_state;
const auto top = state->m_apistack.top;
push_value(key);
game::hks::HksObject value{};
game::hks::HksObject userdata{};
userdata.t = game::hks::TUSERDATA;
userdata.v.ptr = self.ptr;
game::hks::hks_obj_gettable(&value, state, &userdata, &state->m_apistack.top[-1]);
state->m_apistack.top = top;
return value;
return get_field(self.ptr, game::hks::TUSERDATA, key);
}
script_value get_field(const table& self, const script_value& key)
{
const auto state = *game::hks::lua_state;
const auto top = state->m_apistack.top;
push_value(key);
game::hks::HksObject value{};
game::hks::HksObject userdata{};
userdata.t = game::hks::TTABLE;
userdata.v.ptr = self.ptr;
game::hks::hks_obj_gettable(&value, state, &userdata, &state->m_apistack.top[-1]);
state->m_apistack.top = top;
return value;
return get_field(self.ptr, game::hks::TTABLE, key);
}
void set_field(const userdata& self, const script_value& key, const script_value& value)
{
const auto state = *game::hks::lua_state;
game::hks::HksObject userdata{};
userdata.t = game::hks::TUSERDATA;
userdata.v.ptr = self.ptr;
game::hks::hks_obj_settable(state, &userdata, &key.get_raw(), &value.get_raw());
set_field(self.ptr, game::hks::TUSERDATA, key, value);
}
void set_field(const table& self, const script_value& key, const script_value& value)
{
const auto state = *game::hks::lua_state;
game::hks::HksObject userdata{};
userdata.t = game::hks::TTABLE;
userdata.v.ptr = self.ptr;
game::hks::hks_obj_settable(state, &userdata, &key.get_raw(), &value.get_raw());
set_field(self.ptr, game::hks::TTABLE, key, value);
}
}