2023-06-06 07:40:40 +00:00
|
|
|
#include "lua_module.hpp"
|
|
|
|
|
|
|
|
#include "bindings/command.hpp"
|
|
|
|
#include "bindings/event.hpp"
|
2023-07-15 22:07:10 +02:00
|
|
|
#include "bindings/global_table.hpp"
|
2023-06-06 07:40:40 +00:00
|
|
|
#include "bindings/globals.hpp"
|
|
|
|
#include "bindings/gui.hpp"
|
|
|
|
#include "bindings/locals.hpp"
|
|
|
|
#include "bindings/log.hpp"
|
|
|
|
#include "bindings/memory.hpp"
|
|
|
|
#include "bindings/native.hpp"
|
|
|
|
#include "bindings/network.hpp"
|
|
|
|
#include "bindings/script.hpp"
|
|
|
|
#include "bindings/tunables.hpp"
|
|
|
|
#include "bindings/vector.hpp"
|
2023-07-17 14:55:42 +02:00
|
|
|
#include "bindings/imgui.hpp"
|
2023-06-06 07:40:40 +00:00
|
|
|
#include "file_manager.hpp"
|
|
|
|
#include "script_mgr.hpp"
|
|
|
|
|
|
|
|
namespace big
|
|
|
|
{
|
2023-07-16 23:32:34 +02:00
|
|
|
// https://sol2.readthedocs.io/en/latest/exceptions.html
|
|
|
|
int exception_handler(lua_State* L, sol::optional<const std::exception&> maybe_exception, sol::string_view description)
|
2023-06-06 07:40:40 +00:00
|
|
|
{
|
2023-07-16 23:32:34 +02:00
|
|
|
// L is the lua state, which you can wrap in a state_view if necessary
|
|
|
|
// maybe_exception will contain exception, if it exists
|
|
|
|
// description will either be the what() of the exception or a description saying that we hit the general-case catch(...)
|
|
|
|
if (maybe_exception)
|
|
|
|
{
|
|
|
|
const std::exception& ex = *maybe_exception;
|
|
|
|
LOG(FATAL) << ex.what();
|
|
|
|
}
|
2023-06-06 07:40:40 +00:00
|
|
|
else
|
2023-07-16 23:32:34 +02:00
|
|
|
{
|
|
|
|
LOG(FATAL) << description;
|
|
|
|
}
|
|
|
|
Logger::FlushQueue();
|
2023-06-06 07:40:40 +00:00
|
|
|
|
2023-07-16 23:32:34 +02:00
|
|
|
// you must push 1 element onto the stack to be
|
|
|
|
// transported through as the error object in Lua
|
|
|
|
// note that Lua -- and 99.5% of all Lua users and libraries -- expects a string
|
|
|
|
// so we push a single string (in our case, the description of the error)
|
|
|
|
return sol::stack::push(L, description);
|
2023-06-06 07:40:40 +00:00
|
|
|
}
|
|
|
|
|
2023-07-16 23:32:34 +02:00
|
|
|
inline void panic_handler(sol::optional<std::string> maybe_msg)
|
2023-06-06 07:40:40 +00:00
|
|
|
{
|
2023-07-16 23:32:34 +02:00
|
|
|
LOG(FATAL) << "Lua is in a panic state and will now abort() the application";
|
|
|
|
if (maybe_msg)
|
2023-06-06 07:40:40 +00:00
|
|
|
{
|
2023-07-16 23:32:34 +02:00
|
|
|
const std::string& msg = maybe_msg.value();
|
|
|
|
LOG(FATAL) << "error message: " << msg;
|
2023-06-06 07:40:40 +00:00
|
|
|
}
|
2023-07-16 23:32:34 +02:00
|
|
|
Logger::FlushQueue();
|
|
|
|
|
|
|
|
// When this function exits, Lua will exhibit default behavior and abort()
|
2023-06-06 07:40:40 +00:00
|
|
|
}
|
|
|
|
|
2023-07-18 13:07:33 +02:00
|
|
|
lua_module::lua_module(std::string module_name, folder& scripts_folder) :
|
2023-06-06 07:40:40 +00:00
|
|
|
m_module_name(module_name),
|
|
|
|
m_module_id(rage::joaat(module_name))
|
|
|
|
{
|
2023-07-13 09:36:13 +02:00
|
|
|
m_state = std::make_unique<sol::state>();
|
|
|
|
|
|
|
|
auto& state = *m_state;
|
|
|
|
|
2023-07-15 22:07:10 +02:00
|
|
|
// clang-format off
|
2023-07-13 09:36:13 +02:00
|
|
|
state.open_libraries(
|
2023-07-11 09:24:44 +02:00
|
|
|
sol::lib::base,
|
|
|
|
sol::lib::package,
|
|
|
|
sol::lib::coroutine,
|
|
|
|
sol::lib::string,
|
2023-07-13 09:36:13 +02:00
|
|
|
sol::lib::os,
|
2023-07-11 09:24:44 +02:00
|
|
|
sol::lib::math,
|
|
|
|
sol::lib::table,
|
|
|
|
sol::lib::bit32,
|
|
|
|
sol::lib::utf8
|
|
|
|
);
|
2023-07-15 22:07:10 +02:00
|
|
|
// clang-format on
|
2023-06-06 07:40:40 +00:00
|
|
|
|
2023-07-18 13:07:33 +02:00
|
|
|
init_lua_api(scripts_folder);
|
2023-06-06 07:40:40 +00:00
|
|
|
|
2023-07-13 09:36:13 +02:00
|
|
|
state["!module_name"] = module_name;
|
|
|
|
state["!this"] = this;
|
2023-06-06 07:40:40 +00:00
|
|
|
|
2023-07-16 23:32:34 +02:00
|
|
|
state.set_exception_handler(exception_handler);
|
|
|
|
state.set_panic(sol::c_call<decltype(&panic_handler), &panic_handler>);
|
2023-06-06 07:40:40 +00:00
|
|
|
|
2023-07-18 13:07:33 +02:00
|
|
|
const auto script_file_path = scripts_folder.get_file(module_name).get_path();
|
2023-07-02 22:32:46 +02:00
|
|
|
m_last_write_time = std::filesystem::last_write_time(script_file_path);
|
2023-07-16 23:32:34 +02:00
|
|
|
|
|
|
|
auto result = state.safe_script_file(script_file_path.string(), &sol::script_pass_on_error, sol::load_mode::text);
|
2023-06-06 07:40:40 +00:00
|
|
|
|
|
|
|
if (!result.valid())
|
|
|
|
{
|
2023-07-16 23:32:34 +02:00
|
|
|
LOG(FATAL) << module_name << " failed to load: " << result.get<sol::error>().what();
|
|
|
|
Logger::FlushQueue();
|
2023-06-06 07:40:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_module::~lua_module()
|
|
|
|
{
|
2023-07-05 00:30:57 +02:00
|
|
|
for (const auto owned_tab : m_owned_tabs)
|
|
|
|
{
|
|
|
|
big::g_gui_service->remove_from_nav(owned_tab);
|
|
|
|
}
|
|
|
|
|
2023-06-06 07:40:40 +00:00
|
|
|
for (auto script : m_registered_scripts)
|
2023-07-13 09:36:13 +02:00
|
|
|
{
|
2023-06-06 07:40:40 +00:00
|
|
|
g_script_mgr.remove_script(script);
|
2023-07-13 09:36:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// the lua state is about to be destroyed,
|
|
|
|
// but we need to keep it around a little bit longer
|
|
|
|
// until the script manager properly finish executing any potential lua script.
|
|
|
|
// There are most likely much better ways of doing all this, feel free to refactor I guess.
|
|
|
|
std::shared_ptr<sol::state> lua_state_shared = std::shared_ptr<sol::state>(std::move(m_state));
|
|
|
|
g_script_mgr.add_on_script_batch_removed([lua_state_shared] {
|
|
|
|
});
|
2023-06-06 07:40:40 +00:00
|
|
|
|
|
|
|
for (auto memory : m_allocated_memory)
|
|
|
|
delete[] memory;
|
|
|
|
}
|
|
|
|
|
2023-07-02 22:32:46 +02:00
|
|
|
rage::joaat_t lua_module::module_id() const
|
2023-06-06 07:40:40 +00:00
|
|
|
{
|
|
|
|
return m_module_id;
|
|
|
|
}
|
|
|
|
|
2023-07-02 22:32:46 +02:00
|
|
|
const std::string& lua_module::module_name() const
|
2023-06-06 07:40:40 +00:00
|
|
|
{
|
|
|
|
return m_module_name;
|
|
|
|
}
|
2023-06-27 20:13:05 +02:00
|
|
|
|
2023-07-02 22:32:46 +02:00
|
|
|
const std::chrono::time_point<std::chrono::file_clock> lua_module::last_write_time() const
|
|
|
|
{
|
|
|
|
return m_last_write_time;
|
|
|
|
}
|
|
|
|
|
2023-07-18 13:07:33 +02:00
|
|
|
void lua_module::set_folder_for_lua_require(folder& scripts_folder)
|
2023-07-13 09:36:13 +02:00
|
|
|
{
|
|
|
|
auto& state = *m_state;
|
|
|
|
|
2023-07-18 13:07:33 +02:00
|
|
|
const auto scripts_search_path = scripts_folder.get_path() / "?.lua";
|
2023-07-13 09:36:13 +02:00
|
|
|
state["package"]["path"] = scripts_search_path.string();
|
|
|
|
}
|
|
|
|
|
|
|
|
void lua_module::sandbox_lua_os_library()
|
2023-06-27 20:13:05 +02:00
|
|
|
{
|
2023-07-13 09:36:13 +02:00
|
|
|
auto& state = *m_state;
|
|
|
|
|
|
|
|
const auto& os = state["os"];
|
|
|
|
sol::table sandbox_os(state, sol::create);
|
|
|
|
|
2023-07-15 22:07:10 +02:00
|
|
|
sandbox_os["clock"] = os["clock"];
|
|
|
|
sandbox_os["date"] = os["date"];
|
|
|
|
sandbox_os["difftime"] = os["difftime"];
|
|
|
|
sandbox_os["time"] = os["time"];
|
2023-07-13 09:36:13 +02:00
|
|
|
|
|
|
|
state["os"] = sandbox_os;
|
|
|
|
}
|
|
|
|
|
2023-07-15 22:07:10 +02:00
|
|
|
template<size_t N>
|
|
|
|
static constexpr auto not_supported_lua_function(const char (&function_name)[N])
|
|
|
|
{
|
|
|
|
return [function_name](sol::this_state current_state, sol::variadic_args args) {
|
|
|
|
const auto module = sol::state_view(current_state)["!this"].get<big::lua_module*>();
|
|
|
|
|
|
|
|
LOG(FATAL) << module->module_name() << " tried calling a currently not supported lua function: " << function_name;
|
2023-07-16 23:32:34 +02:00
|
|
|
Logger::FlushQueue();
|
2023-07-15 22:07:10 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-07-18 13:07:33 +02:00
|
|
|
void lua_module::sandbox_lua_loads(folder& scripts_folder)
|
2023-07-13 09:36:13 +02:00
|
|
|
{
|
|
|
|
auto& state = *m_state;
|
|
|
|
|
|
|
|
// That's from lua base lib, luaB
|
2023-07-15 22:07:10 +02:00
|
|
|
state["load"] = not_supported_lua_function("load");
|
|
|
|
state["loadstring"] = not_supported_lua_function("loadstring");
|
|
|
|
state["loadfile"] = not_supported_lua_function("loadfile");
|
|
|
|
state["dofile"] = not_supported_lua_function("dofile");
|
2023-07-13 09:36:13 +02:00
|
|
|
|
|
|
|
// That's from lua package lib.
|
|
|
|
// We only allow dependencies between .lua files, no DLLs.
|
2023-07-15 22:07:10 +02:00
|
|
|
state["package"]["loadlib"] = not_supported_lua_function("package.loadlib");
|
2023-07-13 09:36:13 +02:00
|
|
|
state["package"]["cpath"] = "";
|
|
|
|
|
|
|
|
// 1 2 3 4
|
|
|
|
// {searcher_preload, searcher_Lua, searcher_C, searcher_Croot, NULL};
|
2023-07-15 22:07:10 +02:00
|
|
|
state["package"]["searchers"][3] = not_supported_lua_function("package.searcher C");
|
|
|
|
state["package"]["searchers"][4] = not_supported_lua_function("package.searcher Croot");
|
2023-07-13 09:36:13 +02:00
|
|
|
|
2023-07-18 13:07:33 +02:00
|
|
|
set_folder_for_lua_require(scripts_folder);
|
2023-06-27 20:13:05 +02:00
|
|
|
}
|
|
|
|
|
2023-07-18 13:07:33 +02:00
|
|
|
void lua_module::init_lua_api(folder& scripts_folder)
|
2023-06-27 20:13:05 +02:00
|
|
|
{
|
2023-07-13 09:36:13 +02:00
|
|
|
auto& state = *m_state;
|
|
|
|
|
|
|
|
// https://blog.rubenwardy.com/2020/07/26/sol3-script-sandbox/
|
|
|
|
// https://www.lua.org/manual/5.4/manual.html#pdf-require
|
|
|
|
sandbox_lua_os_library();
|
2023-07-18 13:07:33 +02:00
|
|
|
sandbox_lua_loads(scripts_folder);
|
2023-07-13 09:36:13 +02:00
|
|
|
|
|
|
|
lua::log::bind(state);
|
|
|
|
lua::globals::bind(state);
|
|
|
|
lua::script::bind(state);
|
|
|
|
lua::native::bind(state);
|
|
|
|
lua::memory::bind(state);
|
|
|
|
lua::gui::bind(state);
|
|
|
|
lua::network::bind(state);
|
|
|
|
lua::command::bind(state);
|
|
|
|
lua::tunables::bind(state);
|
|
|
|
lua::locals::bind(state);
|
|
|
|
lua::event::bind(state);
|
|
|
|
lua::vector::bind(state);
|
|
|
|
lua::global_table::bind(state);
|
2023-07-17 14:55:42 +02:00
|
|
|
lua::imgui::bind(state, state.globals());
|
2023-06-27 20:13:05 +02:00
|
|
|
}
|
2023-06-06 07:40:40 +00:00
|
|
|
}
|