From 73d96fd4f777d1a730953a52ba9a47c2e0c7a503 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 6 Oct 2024 18:23:33 +0300 Subject: [PATCH] move generator script execution to an isolated Lua state --- src/logic/scripting/lua/lua_engine.cpp | 36 +++++--- src/logic/scripting/lua/lua_engine.hpp | 10 ++- src/logic/scripting/lua/lua_util.cpp | 2 +- src/logic/scripting/lua/lua_util.hpp | 6 +- src/logic/scripting/scripting.cpp | 87 +++++++++---------- src/logic/scripting/scripting_functional.cpp | 6 +- src/logic/scripting/scripting_hud.cpp | 10 +-- .../scripting/scripting_world_generation.cpp | 19 +++- 8 files changed, 101 insertions(+), 75 deletions(-) diff --git a/src/logic/scripting/lua/lua_engine.cpp b/src/logic/scripting/lua/lua_engine.cpp index df8cffe6..b6cd8446 100644 --- a/src/logic/scripting/lua/lua_engine.cpp +++ b/src/logic/scripting/lua/lua_engine.cpp @@ -17,7 +17,7 @@ luaerror::luaerror(const std::string& message) : std::runtime_error(message) { } static void remove_lib_funcs( - lua::State* L, const char* libname, const char* funcs[] + State* L, const char* libname, const char* funcs[] ) { if (getglobal(L, libname)) { for (uint i = 0; funcs[i]; i++) { @@ -28,7 +28,15 @@ static void remove_lib_funcs( } } -static void create_libs(lua::State* L, StateType stateType) { +[[nodiscard]] scriptenv lua::create_environment(State* L) { + int id = lua::create_environment(L, 0); + return std::shared_ptr(new int(id), [=](int* id) { //-V508 + lua::remove_environment(L, *id); + delete id; + }); +} + +static void create_libs(State* L, StateType stateType) { openlib(L, "block", blocklib); openlib(L, "core", corelib); openlib(L, "file", filelib); @@ -65,7 +73,7 @@ static void create_libs(lua::State* L, StateType stateType) { addfunc(L, "print", lua::wrap); } -void lua::init_state(lua::State* L, StateType stateType) { +void lua::init_state(State* L, StateType stateType) { // Allowed standard libraries pop(L, luaopen_base(L)); pop(L, luaopen_math(L)); @@ -100,20 +108,15 @@ void lua::initialize() { logger.info() << LUA_VERSION; logger.info() << LUAJIT_VERSION; - auto L = luaL_newstate(); - if (L == nullptr) { - throw luaerror("could not to initialize Lua"); - } - main_thread = L; - init_state(L, StateType::BASE); + main_thread = create_state(StateType::BASE); } void lua::finalize() { - lua_close(main_thread); + lua::close(main_thread); } bool lua::emit_event( - lua::State* L, const std::string& name, std::function args + State* L, const std::string& name, std::function args ) { getglobal(L, "events"); getfield(L, "emit"); @@ -124,6 +127,15 @@ bool lua::emit_event( return result; } -lua::State* lua::get_main_thread() { +State* lua::get_main_state() { return main_thread; } + +State* lua::create_state(StateType stateType) { + auto L = luaL_newstate(); + if (L == nullptr) { + throw luaerror("could not initialize Lua state"); + } + init_state(L, stateType); + return L; +} diff --git a/src/logic/scripting/lua/lua_engine.hpp b/src/logic/scripting/lua/lua_engine.hpp index b046fb6b..494de626 100644 --- a/src/logic/scripting/lua/lua_engine.hpp +++ b/src/logic/scripting/lua/lua_engine.hpp @@ -17,11 +17,13 @@ namespace lua { void finalize(); bool emit_event( - lua::State*, + State*, const std::string& name, - std::function args = [](auto*) { return 0; } + std::function args = [](auto*) { return 0; } ); - lua::State* get_main_thread(); + State* get_main_state(); + State* create_state(StateType stateType); + [[nodiscard]] scriptenv create_environment(State* L); - void init_state(lua::State* L, StateType stateType); + void init_state(State* L, StateType stateType); } diff --git a/src/logic/scripting/lua/lua_util.cpp b/src/logic/scripting/lua/lua_util.cpp index 6152a254..44583e4c 100644 --- a/src/logic/scripting/lua/lua_util.cpp +++ b/src/logic/scripting/lua/lua_util.cpp @@ -277,7 +277,7 @@ int lua::create_environment(State* L, int parent) { return id; } -void lua::removeEnvironment(State* L, int id) { +void lua::remove_environment(State* L, int id) { if (id == 0) { return; } diff --git a/src/logic/scripting/lua/lua_util.hpp b/src/logic/scripting/lua/lua_util.hpp index f365cdba..05b37c1d 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -546,9 +546,13 @@ namespace lua { return 0; } int create_environment(lua::State*, int parent); - void removeEnvironment(lua::State*, int id); + void remove_environment(lua::State*, int id); void dump_stack(lua::State*); + inline void close(lua::State* L) { + lua_close(L); + } + inline void addfunc( lua::State* L, const std::string& name, lua_CFunction func ) { diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index 412bd403..b06e7258 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -43,7 +43,7 @@ void scripting::load_script(const fs::path& name, bool throwable) { auto paths = scripting::engine->getPaths(); fs::path file = paths->getResourcesFolder() / fs::path("scripts") / name; std::string src = files::read_string(file); - auto L = lua::get_main_thread(); + auto L = lua::get_main_state(); lua::loadbuffer(L, 0, src, file.u8string()); if (throwable) { lua::call(L, 0, 0); @@ -57,7 +57,7 @@ int scripting::load_script( ) { std::string src = files::read_string(file); logger.info() << "script (" << type << ") " << file.u8string(); - return lua::execute(lua::get_main_thread(), env, src, file.u8string()); + return lua::execute(lua::get_main_state(), env, src, file.u8string()); } void scripting::initialize(Engine* engine) { @@ -74,18 +74,13 @@ void scripting::initialize(Engine* engine) { } [[nodiscard]] scriptenv scripting::create_environment() { - auto L = lua::get_main_thread(); - int id = lua::create_environment(L, 0); - return std::shared_ptr(new int(id), [=](int* id) { //-V508 - lua::removeEnvironment(L, *id); - delete id; - }); + return lua::create_environment(lua::get_main_state()); } [[nodiscard]] scriptenv scripting::create_pack_environment( const ContentPack& pack ) { - auto L = lua::get_main_thread(); + auto L = lua::get_main_state(); int id = lua::create_environment(L, 0); lua::pushenv(L, id); lua::pushvalue(L, -1); @@ -94,7 +89,7 @@ void scripting::initialize(Engine* engine) { lua::setfield(L, "PACK_ID"); lua::pop(L); return std::shared_ptr(new int(id), [=](int* id) { //-V508 - lua::removeEnvironment(L, *id); + lua::remove_environment(L, *id); delete id; }); } @@ -102,7 +97,7 @@ void scripting::initialize(Engine* engine) { [[nodiscard]] scriptenv scripting::create_doc_environment( const scriptenv& parent, const std::string& name ) { - auto L = lua::get_main_thread(); + auto L = lua::get_main_state(); int id = lua::create_environment(L, *parent); lua::pushenv(L, id); lua::pushvalue(L, -1); @@ -121,7 +116,7 @@ void scripting::initialize(Engine* engine) { } lua::pop(L); return std::shared_ptr(new int(id), [=](int* id) { //-V508 - lua::removeEnvironment(L, *id); + lua::remove_environment(L, *id); delete id; }); } @@ -129,7 +124,7 @@ void scripting::initialize(Engine* engine) { [[nodiscard]] static scriptenv create_component_environment( const scriptenv& parent, int entityIdx, const std::string& name ) { - auto L = lua::get_main_thread(); + auto L = lua::get_main_state(); int id = lua::create_environment(L, *parent); lua::pushvalue(L, entityIdx); @@ -151,13 +146,13 @@ void scripting::initialize(Engine* engine) { lua::pop(L); return std::shared_ptr(new int(id), [=](int* id) { //-V508 - lua::removeEnvironment(L, *id); + lua::remove_environment(L, *id); delete id; }); } void scripting::process_post_runnables() { - auto L = lua::get_main_thread(); + auto L = lua::get_main_state(); if (lua::getglobal(L, "__process_post_runnables")) { lua::call_nothrow(L, 0); } @@ -171,28 +166,28 @@ void scripting::on_world_load(LevelController* controller) { scripting::controller = controller; load_script("world.lua", false); - auto L = lua::get_main_thread(); + auto L = lua::get_main_state(); for (auto& pack : scripting::engine->getContentPacks()) { lua::emit_event(L, pack.id + ".worldopen"); } } void scripting::on_world_tick() { - auto L = lua::get_main_thread(); + auto L = lua::get_main_state(); for (auto& pack : scripting::engine->getContentPacks()) { lua::emit_event(L, pack.id + ".worldtick"); } } void scripting::on_world_save() { - auto L = lua::get_main_thread(); + auto L = lua::get_main_state(); for (auto& pack : scripting::engine->getContentPacks()) { lua::emit_event(L, pack.id + ".worldsave"); } } void scripting::on_world_quit() { - auto L = lua::get_main_thread(); + auto L = lua::get_main_state(); for (auto& pack : scripting::engine->getContentPacks()) { lua::emit_event(L, pack.id + ".worldquit"); } @@ -217,21 +212,21 @@ void scripting::on_world_quit() { void scripting::on_blocks_tick(const Block& block, int tps) { std::string name = block.name + ".blockstick"; - lua::emit_event(lua::get_main_thread(), name, [tps](auto L) { + lua::emit_event(lua::get_main_state(), name, [tps](auto L) { return lua::pushinteger(L, tps); }); } void scripting::update_block(const Block& block, int x, int y, int z) { std::string name = block.name + ".update"; - lua::emit_event(lua::get_main_thread(), name, [x, y, z](auto L) { + lua::emit_event(lua::get_main_state(), name, [x, y, z](auto L) { return lua::pushivec_stack(L, glm::ivec3(x, y, z)); }); } void scripting::random_update_block(const Block& block, int x, int y, int z) { std::string name = block.name + ".randupdate"; - lua::emit_event(lua::get_main_thread(), name, [x, y, z](auto L) { + lua::emit_event(lua::get_main_state(), name, [x, y, z](auto L) { return lua::pushivec_stack(L, glm::ivec3(x, y, z)); }); } @@ -240,7 +235,7 @@ void scripting::on_block_placed( Player* player, const Block& block, int x, int y, int z ) { std::string name = block.name + ".placed"; - lua::emit_event(lua::get_main_thread(), name, [x, y, z, player](auto L) { + lua::emit_event(lua::get_main_state(), name, [x, y, z, player](auto L) { lua::pushivec_stack(L, glm::ivec3(x, y, z)); lua::pushinteger(L, player ? player->getId() : -1); return 4; @@ -254,7 +249,7 @@ void scripting::on_block_placed( for (auto& [packid, pack] : content->getPacks()) { if (pack->worldfuncsset.onblockplaced) { lua::emit_event( - lua::get_main_thread(), + lua::get_main_state(), packid + ".blockplaced", world_event_args ); @@ -268,7 +263,7 @@ void scripting::on_block_broken( if (block.rt.funcsset.onbroken) { std::string name = block.name + ".broken"; lua::emit_event( - lua::get_main_thread(), + lua::get_main_state(), name, [x, y, z, player](auto L) { lua::pushivec_stack(L, glm::ivec3(x, y, z)); @@ -286,7 +281,7 @@ void scripting::on_block_broken( for (auto& [packid, pack] : content->getPacks()) { if (pack->worldfuncsset.onblockbroken) { lua::emit_event( - lua::get_main_thread(), + lua::get_main_state(), packid + ".blockbroken", world_event_args ); @@ -298,7 +293,7 @@ bool scripting::on_block_interact( Player* player, const Block& block, glm::ivec3 pos ) { std::string name = block.name + ".interact"; - return lua::emit_event(lua::get_main_thread(), name, [pos, player](auto L) { + return lua::emit_event(lua::get_main_state(), name, [pos, player](auto L) { lua::pushivec_stack(L, pos); lua::pushinteger(L, player->getId()); return 4; @@ -308,7 +303,7 @@ bool scripting::on_block_interact( bool scripting::on_item_use(Player* player, const ItemDef& item) { std::string name = item.name + ".use"; return lua::emit_event( - lua::get_main_thread(), + lua::get_main_state(), name, [player](lua::State* L) { return lua::pushinteger(L, player->getId()); } ); @@ -319,7 +314,7 @@ bool scripting::on_item_use_on_block( ) { std::string name = item.name + ".useon"; return lua::emit_event( - lua::get_main_thread(), + lua::get_main_state(), name, [ipos, normal, player](auto L) { lua::pushivec_stack(L, ipos); @@ -335,7 +330,7 @@ bool scripting::on_item_break_block( ) { std::string name = item.name + ".blockbreakby"; return lua::emit_event( - lua::get_main_thread(), + lua::get_main_state(), name, [x, y, z, player](auto L) { lua::pushivec_stack(L, glm::ivec3(x, y, z)); @@ -348,7 +343,7 @@ bool scripting::on_item_break_block( dv::value scripting::get_component_value( const scriptenv& env, const std::string& name ) { - auto L = lua::get_main_thread(); + auto L = lua::get_main_state(); lua::pushenv(L, *env); if (lua::getfield(L, name)) { return lua::tovalue(L, -1); @@ -363,7 +358,7 @@ void scripting::on_entity_spawn( const dv::value& args, const dv::value& saved ) { - auto L = lua::get_main_thread(); + auto L = lua::get_main_state(); lua::requireglobal(L, STDCOMP); if (lua::getfield(L, "new_Entity")) { lua::pushinteger(L, eid); @@ -431,7 +426,7 @@ static void process_entity_callback( const std::string& name, std::function args ) { - auto L = lua::get_main_thread(); + auto L = lua::get_main_state(); lua::pushenv(L, *env); if (lua::getfield(L, name)) { if (args) { @@ -461,7 +456,7 @@ void scripting::on_entity_despawn(const Entity& entity) { process_entity_callback( entity, "on_despawn", &entity_funcs_set::on_despawn, nullptr ); - auto L = lua::get_main_thread(); + auto L = lua::get_main_state(); lua::get_from(L, "stdcomp", "remove_Entity", true); lua::pushinteger(L, entity.getUID()); lua::call(L, 1, 0); @@ -561,7 +556,7 @@ void scripting::on_entity_used(const Entity& entity, Player* player) { } void scripting::on_entities_update(int tps, int parts, int part) { - auto L = lua::get_main_thread(); + auto L = lua::get_main_state(); lua::get_from(L, STDCOMP, "update", true); lua::pushinteger(L, tps); lua::pushinteger(L, parts); @@ -571,7 +566,7 @@ void scripting::on_entities_update(int tps, int parts, int part) { } void scripting::on_entities_render(float delta) { - auto L = lua::get_main_thread(); + auto L = lua::get_main_state(); lua::get_from(L, STDCOMP, "render", true); lua::pushnumber(L, delta); lua::call_nothrow(L, 1, 0); @@ -584,7 +579,7 @@ void scripting::on_ui_open( auto argsptr = std::make_shared>(std::move(args)); std::string name = layout->getId() + ".open"; - lua::emit_event(lua::get_main_thread(), name, [=](auto L) { + lua::emit_event(lua::get_main_state(), name, [=](auto L) { for (const auto& value : *argsptr) { lua::pushvalue(L, value); } @@ -596,7 +591,7 @@ void scripting::on_ui_progress( UiDocument* layout, int workDone, int workTotal ) { std::string name = layout->getId() + ".progress"; - lua::emit_event(lua::get_main_thread(), name, [=](auto L) { + lua::emit_event(lua::get_main_state(), name, [=](auto L) { lua::pushinteger(L, workDone); lua::pushinteger(L, workTotal); return 2; @@ -605,7 +600,7 @@ void scripting::on_ui_progress( void scripting::on_ui_close(UiDocument* layout, Inventory* inventory) { std::string name = layout->getId() + ".close"; - lua::emit_event(lua::get_main_thread(), name, [inventory](auto L) { + lua::emit_event(lua::get_main_state(), name, [inventory](auto L) { return lua::pushinteger(L, inventory ? inventory->getId() : 0); }); } @@ -613,7 +608,7 @@ void scripting::on_ui_close(UiDocument* layout, Inventory* inventory) { bool scripting::register_event( int env, const std::string& name, const std::string& id ) { - auto L = lua::get_main_thread(); + auto L = lua::get_main_state(); if (lua::pushenv(L, env) == 0) { lua::pushglobals(L); } @@ -635,7 +630,7 @@ bool scripting::register_event( } int scripting::get_values_on_stack() { - return lua::gettop(lua::get_main_thread()); + return lua::gettop(lua::get_main_state()); } void scripting::load_block_script( @@ -645,7 +640,7 @@ void scripting::load_block_script( block_funcs_set& funcsset ) { int env = *senv; - lua::pop(lua::get_main_thread(), load_script(env, "block", file)); + lua::pop(lua::get_main_state(), load_script(env, "block", file)); funcsset.init = register_event(env, "init", prefix + ".init"); funcsset.update = register_event(env, "on_update", prefix + ".update"); funcsset.randupdate = @@ -665,7 +660,7 @@ void scripting::load_item_script( item_funcs_set& funcsset ) { int env = *senv; - lua::pop(lua::get_main_thread(), load_script(env, "item", file)); + lua::pop(lua::get_main_state(), load_script(env, "item", file)); funcsset.init = register_event(env, "init", prefix + ".init"); funcsset.on_use = register_event(env, "on_use", prefix + ".use"); funcsset.on_use_on_block = @@ -677,7 +672,7 @@ void scripting::load_item_script( void scripting::load_entity_component( const std::string& name, const fs::path& file ) { - auto L = lua::get_main_thread(); + auto L = lua::get_main_state(); std::string src = files::read_string(file); logger.info() << "script (component) " << file.u8string(); lua::loadbuffer(L, 0, src, "C!" + name); @@ -691,7 +686,7 @@ void scripting::load_world_script( world_funcs_set& funcsset ) { int env = *senv; - lua::pop(lua::get_main_thread(), load_script(env, "world", file)); + lua::pop(lua::get_main_state(), load_script(env, "world", file)); register_event(env, "init", prefix + ".init"); register_event(env, "on_world_open", prefix + ".worldopen"); register_event(env, "on_world_tick", prefix + ".worldtick"); @@ -710,7 +705,7 @@ void scripting::load_layout_script( uidocscript& script ) { int env = *senv; - lua::pop(lua::get_main_thread(), load_script(env, "layout", file)); + lua::pop(lua::get_main_state(), load_script(env, "layout", file)); script.onopen = register_event(env, "on_open", prefix + ".open"); script.onprogress = register_event(env, "on_progress", prefix + ".progress"); diff --git a/src/logic/scripting/scripting_functional.cpp b/src/logic/scripting/scripting_functional.cpp index 02398b49..d09c9432 100644 --- a/src/logic/scripting/scripting_functional.cpp +++ b/src/logic/scripting/scripting_functional.cpp @@ -12,7 +12,7 @@ static debug::Logger logger("scripting_func"); runnable scripting::create_runnable( const scriptenv& env, const std::string& src, const std::string& file ) { - auto L = lua::get_main_thread(); + auto L = lua::get_main_state(); try { lua::loadbuffer(L, *env, src, file); return lua::create_runnable(L); @@ -25,7 +25,7 @@ runnable scripting::create_runnable( static lua::State* process_callback( const scriptenv& env, const std::string& src, const std::string& file ) { - auto L = lua::get_main_thread(); + auto L = lua::get_main_state(); try { if (lua::eval(L, *env, src, file) != 0) { return L; @@ -163,7 +163,7 @@ vec2supplier scripting::create_vec2_supplier( value_to_string_func scripting::create_tostring( const scriptenv& env, const std::string& src, const std::string& file ) { - auto L = lua::get_main_thread(); + auto L = lua::get_main_state(); try { lua::loadbuffer(L, *env, src, file); lua::call(L, 0, 1); diff --git a/src/logic/scripting/scripting_hud.cpp b/src/logic/scripting/scripting_hud.cpp index fa1cfe12..a711453c 100644 --- a/src/logic/scripting/scripting_hud.cpp +++ b/src/logic/scripting/scripting_hud.cpp @@ -17,11 +17,11 @@ Hud* scripting::hud = nullptr; void scripting::on_frontend_init(Hud* hud) { scripting::hud = hud; - lua::openlib(lua::get_main_thread(), "hud", hudlib); + lua::openlib(lua::get_main_state(), "hud", hudlib); for (auto& pack : engine->getContentPacks()) { lua::emit_event( - lua::get_main_thread(), + lua::get_main_state(), pack.id + ".hudopen", [&](lua::State* L) { return lua::pushinteger(L, hud->getPlayer()->getId()); @@ -33,7 +33,7 @@ void scripting::on_frontend_init(Hud* hud) { void scripting::on_frontend_render() { for (auto& pack : engine->getContentPacks()) { lua::emit_event( - lua::get_main_thread(), + lua::get_main_state(), pack.id + ".hudrender", [&](lua::State* L) { return 0; } ); @@ -43,7 +43,7 @@ void scripting::on_frontend_render() { void scripting::on_frontend_close() { for (auto& pack : engine->getContentPacks()) { lua::emit_event( - lua::get_main_thread(), + lua::get_main_state(), pack.id + ".hudclose", [&](lua::State* L) { return lua::pushinteger(L, hud->getPlayer()->getId()); @@ -60,7 +60,7 @@ void scripting::load_hud_script( std::string src = files::read_string(file); logger.info() << "loading script " << file.u8string(); - lua::execute(lua::get_main_thread(), env, src, file.u8string()); + lua::execute(lua::get_main_state(), env, src, file.u8string()); register_event(env, "init", packid + ".init"); register_event(env, "on_hud_open", packid + ".hudopen"); diff --git a/src/logic/scripting/scripting_world_generation.cpp b/src/logic/scripting/scripting_world_generation.cpp index e935d818..c8bb2799 100644 --- a/src/logic/scripting/scripting_world_generation.cpp +++ b/src/logic/scripting/scripting_world_generation.cpp @@ -13,6 +13,10 @@ #include "data/dv.hpp" #include "world/generator/GeneratorDef.hpp" #include "util/timeutil.hpp" +#include "files/files.hpp" +#include "debug/Logger.hpp" + +static debug::Logger logger("generator-scripting"); class LuaGeneratorScript : public GeneratorScript { lua::State* L; @@ -28,6 +32,13 @@ public: env(std::move(env)) {} + virtual ~LuaGeneratorScript() { + env.reset(); + if (L != lua::get_main_state()) { + lua::close(L); + } + } + std::shared_ptr generateHeightmap( const glm::ivec2& offset, const glm::ivec2& size, uint64_t seed, uint bpd ) override { @@ -130,8 +141,8 @@ public: std::unique_ptr scripting::load_generator( const GeneratorDef& def, const fs::path& file, const std::string& dirPath ) { - auto env = create_environment(); - auto L = lua::get_main_thread(); + auto L = lua::create_state(lua::StateType::GENERATOR); + auto env = lua::create_environment(L); lua::stackguard _(L); lua::pushenv(L, *env); @@ -143,7 +154,9 @@ std::unique_ptr scripting::load_generator( lua::pop(L); if (fs::exists(file)) { - lua::pop(L, load_script(*env, "generator", file)); + std::string src = files::read_string(file); + logger.info() << "script (generator) " << file.u8string(); + lua::pop(L, lua::execute(L, *env, src, file.u8string())); } else { // Use default (empty) script lua::pop(L, lua::execute(L, *env, "", ""));