This commit is contained in:
Maurice Heumann 2023-02-21 17:48:00 +01:00
parent 6d3ec0b824
commit 77abb1a7e1
2 changed files with 30 additions and 33 deletions

View File

@ -26,7 +26,7 @@ namespace ui_scripting
utils::hook::detour lua_cod_getrawfile_hook; utils::hook::detour lua_cod_getrawfile_hook;
game::dvar_t* dvar_cg_enable_unsafe_lua_functions; game::dvar_t* dvar_cg_enable_unsafe_lua_functions;
static bool unsafe_function_called_message_shown = false; bool unsafe_function_called_message_shown = false;
struct globals_t struct globals_t
{ {
@ -171,10 +171,7 @@ namespace ui_scripting
void setup_functions() void setup_functions()
{ {
const auto lua = get_globals(); const auto lua = get_globals();
lua["game"] = table();
using game = table;
auto game_type = game();
lua["game"] = game_type;
} }
void enable_globals() void enable_globals()
@ -207,10 +204,8 @@ namespace ui_scripting
lua["table"]["unpack"] = lua["unpack"]; lua["table"]["unpack"] = lua["unpack"];
lua["luiglobals"] = lua; lua["luiglobals"] = lua;
utils::nt::library host{}; const utils::nt::library host{};
std::string folder = "ui_scripts/"; const auto folder = game::is_server() ? "lobby_scripts/" : "ui_scripts/";
if (game::is_server())
folder = "lobby_scripts/";
load_scripts((game::get_appdata_path() / "data" / folder).string()); load_scripts((game::get_appdata_path() / "data" / folder).string());
load_scripts((host.get_folder() / "boiii" / folder).string()); load_scripts((host.get_folder() / "boiii" / folder).string());
} }
@ -227,7 +222,7 @@ namespace ui_scripting
} }
} }
void ui_cod_init_stub(bool frontend) void ui_cod_init_stub(const bool frontend)
{ {
ui_cod_init_hook.invoke(frontend); ui_cod_init_hook.invoke(frontend);
@ -235,7 +230,8 @@ namespace ui_scripting
{ {
// Fetch the names of the local files so file overrides are already handled // Fetch the names of the local files so file overrides are already handled
globals = {}; globals = {};
utils::nt::library host{}; const utils::nt::library host{};
load_local_script_files((game::get_appdata_path() / "data/ui_scripts/").string()); load_local_script_files((game::get_appdata_path() / "data/ui_scripts/").string());
load_local_script_files((host.get_folder() / "boiii/ui_scripts/").string()); load_local_script_files((host.get_folder() / "boiii/ui_scripts/").string());
return; return;
@ -274,14 +270,13 @@ namespace ui_scripting
return load_buffer(globals.raw_script_name, utils::io::read_file(globals.raw_script_name)); return load_buffer(globals.raw_script_name, utils::io::read_file(globals.raw_script_name));
} }
return utils::hook::invoke<int>(game::select(0x141D3AFB0, 0x1403E4090), state, compiler_options, reader, reader_data, debug_reader, return utils::hook::invoke<int>(game::select(0x141D3AFB0, 0x1403E4090), state, compiler_options, reader,
reader_data, debug_reader,
debug_reader_data, chunk_name); debug_reader_data, chunk_name);
} }
game::XAssetHeader lua_cod_getrawfile_stub(char* filename) game::XAssetHeader lua_cod_getrawfile_stub(char* filename)
{ {
game::XAssetHeader header{.luaFile = nullptr};
if (!is_loaded_script(globals.in_require_script) && !is_local_script(filename)) if (!is_loaded_script(globals.in_require_script) && !is_local_script(filename))
{ {
return lua_cod_getrawfile_hook.invoke<game::XAssetHeader>(filename); return lua_cod_getrawfile_hook.invoke<game::XAssetHeader>(filename);
@ -303,9 +298,10 @@ namespace ui_scripting
{ {
globals.load_raw_script = true; globals.load_raw_script = true;
globals.raw_script_name = target_script; globals.raw_script_name = target_script;
header.luaFile = reinterpret_cast<game::LuaFile*>(1);
return header; return game::XAssetHeader{
.luaFile = reinterpret_cast<game::LuaFile*>(1) //
};
} }
return lua_cod_getrawfile_hook.invoke<game::XAssetHeader>(filename); return lua_cod_getrawfile_hook.invoke<game::XAssetHeader>(filename);
@ -392,7 +388,9 @@ namespace ui_scripting
lua_cod_getrawfile_hook.create(game::select(0x141F0F880, 0x1404BCB70), lua_cod_getrawfile_stub); lua_cod_getrawfile_hook.create(game::select(0x141F0F880, 0x1404BCB70), lua_cod_getrawfile_stub);
if (game::is_server()) if (game::is_server())
{
return; return;
}
dvar_cg_enable_unsafe_lua_functions = game::Dvar_RegisterBool( dvar_cg_enable_unsafe_lua_functions = game::Dvar_RegisterBool(
game::Dvar_GenerateHash("cg_enable_unsafe_lua_functions"), "cg_enable_unsafe_lua_functions", false, game::Dvar_GenerateHash("cg_enable_unsafe_lua_functions"), "cg_enable_unsafe_lua_functions", false,
@ -402,19 +400,17 @@ namespace ui_scripting
scheduler::once([]() scheduler::once([]()
{ {
game::dvar_t* dvar_callstack_ship = game::Dvar_FindVar("ui_error_callstack_ship"); game::dvar_t* dvar_callstack_ship = game::Dvar_FindVar("ui_error_callstack_ship");
dvar_callstack_ship->flags = (game::dvarFlags_e)0; dvar_callstack_ship->flags = static_cast<game::dvarFlags_e>(0);
game::dvar_t* dvar_report_delay = game::Dvar_FindVar("ui_error_report_delay"); game::dvar_t* dvar_report_delay = game::Dvar_FindVar("ui_error_report_delay");
dvar_report_delay->flags = (game::dvarFlags_e)0; dvar_report_delay->flags = static_cast<game::dvarFlags_e>(0);
game::Dvar_SetFromStringByName("ui_error_callstack_ship", "1", true); game::Dvar_SetFromStringByName("ui_error_callstack_ship", "1", true);
game::Dvar_SetFromStringByName("ui_error_report_delay", "0", true); game::Dvar_SetFromStringByName("ui_error_report_delay", "0", true);
}, scheduler::pipeline::renderer); }, scheduler::pipeline::renderer);
command::add("luiReload", [](auto& params) command::add("luiReload", []
{ {
auto frontend = game::Com_IsRunningUILevel(); if (game::Com_IsRunningUILevel())
if (frontend)
{ {
converted_functions.clear(); converted_functions.clear();

View File

@ -78,7 +78,7 @@ namespace ui_scripting
void assign(const game::hks::HksObject& value); void assign(const game::hks::HksObject& value);
void release(); void release();
game::hks::HksObject value_{ game::hks::TNONE, {} }; game::hks::HksObject value_{game::hks::TNONE, {}};
int ref_{}; int ref_{};
}; };
@ -133,25 +133,25 @@ namespace ui_scripting
bool operator==(const script_value& other) const; bool operator==(const script_value& other) const;
arguments operator()() const; [[maybe_unused]] arguments operator()() const;
arguments operator()(const arguments& arguments) const; [[maybe_unused]] arguments operator()(const arguments& arguments) const;
template<class ...T> template <class ...T>
arguments operator()(T... arguments) const [[maybe_unused]] arguments operator()(T ... arguments) const
{ {
return this->as<function>().call({ arguments... }); return this->as<function>().call({arguments...});
} }
template <size_t Size> template <size_t Size>
table_value operator[](const char(&key)[Size]) const table_value operator[](const char (&key)[Size]) const
{ {
return { this->as<table>(), key }; return {this->as<table>(), key};
} }
template <typename T = script_value> template <typename T = script_value>
table_value operator[](const T& key) const table_value operator[](const T& key) const
{ {
return { this->as<table>(), key }; return {this->as<table>(), key};
} }
template <typename T> template <typename T>
@ -240,11 +240,12 @@ namespace ui_scripting
{ {
if (static_cast<std::size_t>(index) >= values_.size()) if (static_cast<std::size_t>(index) >= values_.size())
{ {
return { values_, {}, index }; return {values_, {}, index};
} }
return { values_, values_[index], index }; return {values_, values_[index], index};
} }
private: private:
arguments values_{}; arguments values_{};
}; };