From 736fdbf96444db62916197835daee4487b0748ef Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sat, 5 Oct 2024 00:53:41 +0300 Subject: [PATCH 1/6] move lua libs to /logic/scripting/lua/libs --- src/logic/scripting/lua/{ => libs}/api_lua.hpp | 2 +- src/logic/scripting/lua/{ => libs}/lib__rigidbody.cpp | 0 src/logic/scripting/lua/{ => libs}/lib__skeleton.cpp | 0 src/logic/scripting/lua/{ => libs}/lib__transform.cpp | 0 src/logic/scripting/lua/{ => libs}/libaudio.cpp | 0 src/logic/scripting/lua/{ => libs}/libblock.cpp | 0 src/logic/scripting/lua/{ => libs}/libcamera.cpp | 0 src/logic/scripting/lua/{ => libs}/libconsole.cpp | 0 src/logic/scripting/lua/{ => libs}/libcore.cpp | 0 src/logic/scripting/lua/{ => libs}/libentity.cpp | 0 src/logic/scripting/lua/{ => libs}/libentity.hpp | 0 src/logic/scripting/lua/{ => libs}/libfile.cpp | 0 src/logic/scripting/lua/{ => libs}/libgeneration.cpp | 2 +- src/logic/scripting/lua/{ => libs}/libgui.cpp | 0 src/logic/scripting/lua/{ => libs}/libhud.cpp | 0 src/logic/scripting/lua/{ => libs}/libinput.cpp | 0 src/logic/scripting/lua/{ => libs}/libinventory.cpp | 0 src/logic/scripting/lua/{ => libs}/libitem.cpp | 0 src/logic/scripting/lua/{ => libs}/libjson.cpp | 0 src/logic/scripting/lua/{ => libs}/libmat4.cpp | 0 src/logic/scripting/lua/{ => libs}/libpack.cpp | 0 src/logic/scripting/lua/{ => libs}/libplayer.cpp | 0 src/logic/scripting/lua/{ => libs}/libquat.cpp | 0 src/logic/scripting/lua/{ => libs}/libtime.cpp | 0 src/logic/scripting/lua/{ => libs}/libtoml.cpp | 0 src/logic/scripting/lua/{ => libs}/libvecn.cpp | 0 src/logic/scripting/lua/{ => libs}/libworld.cpp | 0 src/logic/scripting/lua/lua_engine.cpp | 2 +- src/logic/scripting/lua/lua_extensions.cpp | 2 +- src/logic/scripting/lua/lua_overrides.cpp | 2 +- src/logic/scripting/scripting_hud.cpp | 2 +- 31 files changed, 6 insertions(+), 6 deletions(-) rename src/logic/scripting/lua/{ => libs}/api_lua.hpp (98%) rename src/logic/scripting/lua/{ => libs}/lib__rigidbody.cpp (100%) rename src/logic/scripting/lua/{ => libs}/lib__skeleton.cpp (100%) rename src/logic/scripting/lua/{ => libs}/lib__transform.cpp (100%) rename src/logic/scripting/lua/{ => libs}/libaudio.cpp (100%) rename src/logic/scripting/lua/{ => libs}/libblock.cpp (100%) rename src/logic/scripting/lua/{ => libs}/libcamera.cpp (100%) rename src/logic/scripting/lua/{ => libs}/libconsole.cpp (100%) rename src/logic/scripting/lua/{ => libs}/libcore.cpp (100%) rename src/logic/scripting/lua/{ => libs}/libentity.cpp (100%) rename src/logic/scripting/lua/{ => libs}/libentity.hpp (100%) rename src/logic/scripting/lua/{ => libs}/libfile.cpp (100%) rename src/logic/scripting/lua/{ => libs}/libgeneration.cpp (98%) rename src/logic/scripting/lua/{ => libs}/libgui.cpp (100%) rename src/logic/scripting/lua/{ => libs}/libhud.cpp (100%) rename src/logic/scripting/lua/{ => libs}/libinput.cpp (100%) rename src/logic/scripting/lua/{ => libs}/libinventory.cpp (100%) rename src/logic/scripting/lua/{ => libs}/libitem.cpp (100%) rename src/logic/scripting/lua/{ => libs}/libjson.cpp (100%) rename src/logic/scripting/lua/{ => libs}/libmat4.cpp (100%) rename src/logic/scripting/lua/{ => libs}/libpack.cpp (100%) rename src/logic/scripting/lua/{ => libs}/libplayer.cpp (100%) rename src/logic/scripting/lua/{ => libs}/libquat.cpp (100%) rename src/logic/scripting/lua/{ => libs}/libtime.cpp (100%) rename src/logic/scripting/lua/{ => libs}/libtoml.cpp (100%) rename src/logic/scripting/lua/{ => libs}/libvecn.cpp (100%) rename src/logic/scripting/lua/{ => libs}/libworld.cpp (100%) diff --git a/src/logic/scripting/lua/api_lua.hpp b/src/logic/scripting/lua/libs/api_lua.hpp similarity index 98% rename from src/logic/scripting/lua/api_lua.hpp rename to src/logic/scripting/lua/libs/api_lua.hpp index 6a8f3dd2..f856f5f4 100644 --- a/src/logic/scripting/lua/api_lua.hpp +++ b/src/logic/scripting/lua/libs/api_lua.hpp @@ -3,7 +3,7 @@ #include #include -#include "lua_util.hpp" +#include "../lua_util.hpp" /// Definitions can be found in local .cpp files /// having same names as declarations diff --git a/src/logic/scripting/lua/lib__rigidbody.cpp b/src/logic/scripting/lua/libs/lib__rigidbody.cpp similarity index 100% rename from src/logic/scripting/lua/lib__rigidbody.cpp rename to src/logic/scripting/lua/libs/lib__rigidbody.cpp diff --git a/src/logic/scripting/lua/lib__skeleton.cpp b/src/logic/scripting/lua/libs/lib__skeleton.cpp similarity index 100% rename from src/logic/scripting/lua/lib__skeleton.cpp rename to src/logic/scripting/lua/libs/lib__skeleton.cpp diff --git a/src/logic/scripting/lua/lib__transform.cpp b/src/logic/scripting/lua/libs/lib__transform.cpp similarity index 100% rename from src/logic/scripting/lua/lib__transform.cpp rename to src/logic/scripting/lua/libs/lib__transform.cpp diff --git a/src/logic/scripting/lua/libaudio.cpp b/src/logic/scripting/lua/libs/libaudio.cpp similarity index 100% rename from src/logic/scripting/lua/libaudio.cpp rename to src/logic/scripting/lua/libs/libaudio.cpp diff --git a/src/logic/scripting/lua/libblock.cpp b/src/logic/scripting/lua/libs/libblock.cpp similarity index 100% rename from src/logic/scripting/lua/libblock.cpp rename to src/logic/scripting/lua/libs/libblock.cpp diff --git a/src/logic/scripting/lua/libcamera.cpp b/src/logic/scripting/lua/libs/libcamera.cpp similarity index 100% rename from src/logic/scripting/lua/libcamera.cpp rename to src/logic/scripting/lua/libs/libcamera.cpp diff --git a/src/logic/scripting/lua/libconsole.cpp b/src/logic/scripting/lua/libs/libconsole.cpp similarity index 100% rename from src/logic/scripting/lua/libconsole.cpp rename to src/logic/scripting/lua/libs/libconsole.cpp diff --git a/src/logic/scripting/lua/libcore.cpp b/src/logic/scripting/lua/libs/libcore.cpp similarity index 100% rename from src/logic/scripting/lua/libcore.cpp rename to src/logic/scripting/lua/libs/libcore.cpp diff --git a/src/logic/scripting/lua/libentity.cpp b/src/logic/scripting/lua/libs/libentity.cpp similarity index 100% rename from src/logic/scripting/lua/libentity.cpp rename to src/logic/scripting/lua/libs/libentity.cpp diff --git a/src/logic/scripting/lua/libentity.hpp b/src/logic/scripting/lua/libs/libentity.hpp similarity index 100% rename from src/logic/scripting/lua/libentity.hpp rename to src/logic/scripting/lua/libs/libentity.hpp diff --git a/src/logic/scripting/lua/libfile.cpp b/src/logic/scripting/lua/libs/libfile.cpp similarity index 100% rename from src/logic/scripting/lua/libfile.cpp rename to src/logic/scripting/lua/libs/libfile.cpp diff --git a/src/logic/scripting/lua/libgeneration.cpp b/src/logic/scripting/lua/libs/libgeneration.cpp similarity index 98% rename from src/logic/scripting/lua/libgeneration.cpp rename to src/logic/scripting/lua/libs/libgeneration.cpp index ec425345..d24fae0c 100644 --- a/src/logic/scripting/lua/libgeneration.cpp +++ b/src/logic/scripting/lua/libs/libgeneration.cpp @@ -7,7 +7,7 @@ #include "world/generator/VoxelFragment.hpp" #include "content/ContentLoader.hpp" #include "engine.hpp" -#include "lua_custom_types.hpp" +#include "../lua_custom_types.hpp" using namespace scripting; diff --git a/src/logic/scripting/lua/libgui.cpp b/src/logic/scripting/lua/libs/libgui.cpp similarity index 100% rename from src/logic/scripting/lua/libgui.cpp rename to src/logic/scripting/lua/libs/libgui.cpp diff --git a/src/logic/scripting/lua/libhud.cpp b/src/logic/scripting/lua/libs/libhud.cpp similarity index 100% rename from src/logic/scripting/lua/libhud.cpp rename to src/logic/scripting/lua/libs/libhud.cpp diff --git a/src/logic/scripting/lua/libinput.cpp b/src/logic/scripting/lua/libs/libinput.cpp similarity index 100% rename from src/logic/scripting/lua/libinput.cpp rename to src/logic/scripting/lua/libs/libinput.cpp diff --git a/src/logic/scripting/lua/libinventory.cpp b/src/logic/scripting/lua/libs/libinventory.cpp similarity index 100% rename from src/logic/scripting/lua/libinventory.cpp rename to src/logic/scripting/lua/libs/libinventory.cpp diff --git a/src/logic/scripting/lua/libitem.cpp b/src/logic/scripting/lua/libs/libitem.cpp similarity index 100% rename from src/logic/scripting/lua/libitem.cpp rename to src/logic/scripting/lua/libs/libitem.cpp diff --git a/src/logic/scripting/lua/libjson.cpp b/src/logic/scripting/lua/libs/libjson.cpp similarity index 100% rename from src/logic/scripting/lua/libjson.cpp rename to src/logic/scripting/lua/libs/libjson.cpp diff --git a/src/logic/scripting/lua/libmat4.cpp b/src/logic/scripting/lua/libs/libmat4.cpp similarity index 100% rename from src/logic/scripting/lua/libmat4.cpp rename to src/logic/scripting/lua/libs/libmat4.cpp diff --git a/src/logic/scripting/lua/libpack.cpp b/src/logic/scripting/lua/libs/libpack.cpp similarity index 100% rename from src/logic/scripting/lua/libpack.cpp rename to src/logic/scripting/lua/libs/libpack.cpp diff --git a/src/logic/scripting/lua/libplayer.cpp b/src/logic/scripting/lua/libs/libplayer.cpp similarity index 100% rename from src/logic/scripting/lua/libplayer.cpp rename to src/logic/scripting/lua/libs/libplayer.cpp diff --git a/src/logic/scripting/lua/libquat.cpp b/src/logic/scripting/lua/libs/libquat.cpp similarity index 100% rename from src/logic/scripting/lua/libquat.cpp rename to src/logic/scripting/lua/libs/libquat.cpp diff --git a/src/logic/scripting/lua/libtime.cpp b/src/logic/scripting/lua/libs/libtime.cpp similarity index 100% rename from src/logic/scripting/lua/libtime.cpp rename to src/logic/scripting/lua/libs/libtime.cpp diff --git a/src/logic/scripting/lua/libtoml.cpp b/src/logic/scripting/lua/libs/libtoml.cpp similarity index 100% rename from src/logic/scripting/lua/libtoml.cpp rename to src/logic/scripting/lua/libs/libtoml.cpp diff --git a/src/logic/scripting/lua/libvecn.cpp b/src/logic/scripting/lua/libs/libvecn.cpp similarity index 100% rename from src/logic/scripting/lua/libvecn.cpp rename to src/logic/scripting/lua/libs/libvecn.cpp diff --git a/src/logic/scripting/lua/libworld.cpp b/src/logic/scripting/lua/libs/libworld.cpp similarity index 100% rename from src/logic/scripting/lua/libworld.cpp rename to src/logic/scripting/lua/libs/libworld.cpp diff --git a/src/logic/scripting/lua/lua_engine.cpp b/src/logic/scripting/lua/lua_engine.cpp index 6ae427e3..8055fc54 100644 --- a/src/logic/scripting/lua/lua_engine.cpp +++ b/src/logic/scripting/lua/lua_engine.cpp @@ -5,7 +5,7 @@ #include "debug/Logger.hpp" #include "util/stringutil.hpp" -#include "api_lua.hpp" +#include "libs/api_lua.hpp" #include "lua_custom_types.hpp" static debug::Logger logger("lua-state"); diff --git a/src/logic/scripting/lua/lua_extensions.cpp b/src/logic/scripting/lua/lua_extensions.cpp index 51185898..e1d7a230 100644 --- a/src/logic/scripting/lua/lua_extensions.cpp +++ b/src/logic/scripting/lua/lua_extensions.cpp @@ -1,6 +1,6 @@ #include -#include "api_lua.hpp" +#include "libs/api_lua.hpp" #include "debug/Logger.hpp" static debug::Logger logger("lua-debug"); diff --git a/src/logic/scripting/lua/lua_overrides.cpp b/src/logic/scripting/lua/lua_overrides.cpp index 8233d95a..12b0bbff 100644 --- a/src/logic/scripting/lua/lua_overrides.cpp +++ b/src/logic/scripting/lua/lua_overrides.cpp @@ -1,6 +1,6 @@ #include -#include "api_lua.hpp" +#include "libs/api_lua.hpp" /// @brief Modified version of luaB_print from lbaselib.c int l_print(lua::State* L) { diff --git a/src/logic/scripting/scripting_hud.cpp b/src/logic/scripting/scripting_hud.cpp index f48e42da..fa1cfe12 100644 --- a/src/logic/scripting/scripting_hud.cpp +++ b/src/logic/scripting/scripting_hud.cpp @@ -5,7 +5,7 @@ #include "files/files.hpp" #include "frontend/hud.hpp" #include "objects/Player.hpp" -#include "lua/api_lua.hpp" +#include "lua/libs/api_lua.hpp" #include "lua/lua_engine.hpp" #include "scripting.hpp" From 756de7b0b1873b375fd0275488b430ed143bdc4f Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sat, 5 Oct 2024 17:32:03 +0300 Subject: [PATCH 2/6] refactor lua_engine --- src/logic/scripting/lua/lua_engine.cpp | 55 +++++++++++++++----------- src/logic/scripting/lua/lua_engine.hpp | 7 ++++ 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/logic/scripting/lua/lua_engine.cpp b/src/logic/scripting/lua/lua_engine.cpp index 8055fc54..df8cffe6 100644 --- a/src/logic/scripting/lua/lua_engine.cpp +++ b/src/logic/scripting/lua/lua_engine.cpp @@ -28,49 +28,44 @@ static void remove_lib_funcs( } } -static void create_libs(lua::State* L) { - openlib(L, "audio", audiolib); +static void create_libs(lua::State* L, StateType stateType) { openlib(L, "block", blocklib); - openlib(L, "console", consolelib); openlib(L, "core", corelib); openlib(L, "file", filelib); - openlib(L, "gui", guilib); - openlib(L, "input", inputlib); - openlib(L, "inventory", inventorylib); openlib(L, "generation", generationlib); openlib(L, "item", itemlib); openlib(L, "json", jsonlib); openlib(L, "mat4", mat4lib); openlib(L, "pack", packlib); - openlib(L, "player", playerlib); openlib(L, "quat", quatlib); openlib(L, "time", timelib); openlib(L, "toml", tomllib); openlib(L, "vec2", vec2lib); openlib(L, "vec3", vec3lib); openlib(L, "vec4", vec4lib); - openlib(L, "world", worldlib); - openlib(L, "entities", entitylib); - openlib(L, "cameras", cameralib); + if (stateType == StateType::BASE) { + openlib(L, "gui", guilib); + openlib(L, "input", inputlib); + openlib(L, "inventory", inventorylib); + openlib(L, "world", worldlib); + openlib(L, "audio", audiolib); + openlib(L, "console", consolelib); + openlib(L, "player", playerlib); - // components - openlib(L, "__skeleton", skeletonlib); - openlib(L, "__rigidbody", rigidbodylib); - openlib(L, "__transform", transformlib); + openlib(L, "entities", entitylib); + openlib(L, "cameras", cameralib); + + // components + openlib(L, "__skeleton", skeletonlib); + openlib(L, "__rigidbody", rigidbodylib); + openlib(L, "__transform", transformlib); + } addfunc(L, "print", lua::wrap); } -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; +void lua::init_state(lua::State* L, StateType stateType) { // Allowed standard libraries pop(L, luaopen_base(L)); pop(L, luaopen_math(L)); @@ -83,7 +78,7 @@ void lua::initialize() { const char* removed_os[] { "execute", "exit", "remove", "rename", "setlocale", "tmpname", nullptr}; remove_lib_funcs(L, "os", removed_os); - create_libs(L); + create_libs(L, stateType); pushglobals(L); setglobal(L, env_name(0)); @@ -101,6 +96,18 @@ void lua::initialize() { newusertype(L); } +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); +} + void lua::finalize() { lua_close(main_thread); } diff --git a/src/logic/scripting/lua/lua_engine.hpp b/src/logic/scripting/lua/lua_engine.hpp index 1690fbbd..b046fb6b 100644 --- a/src/logic/scripting/lua/lua_engine.hpp +++ b/src/logic/scripting/lua/lua_engine.hpp @@ -8,6 +8,11 @@ #include "lua_util.hpp" namespace lua { + enum class StateType { + BASE, + GENERATOR, + }; + void initialize(); void finalize(); @@ -17,4 +22,6 @@ namespace lua { std::function args = [](auto*) { return 0; } ); lua::State* get_main_thread(); + + void init_state(lua::State* L, StateType stateType); } From 7c73515d2d10a89acf38500a6be551ba1d87288e Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sat, 5 Oct 2024 17:35:31 +0300 Subject: [PATCH 3/6] update workflows --- .github/workflows/build.yml | 2 +- .github/workflows/macos.yml | 2 +- .github/workflows/windows.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6d6c9c9b..f5155aab 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -4,7 +4,7 @@ on: push: branches: [ "main" ] pull_request: - branches: [ "main" ] + branches: [ "main", "heightmaps" ] # TODO: remove 'heightmaps' after merge jobs: build: diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index 1f9675ca..fefbbee7 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -4,7 +4,7 @@ on: push: branches: [ "main", "release-**"] pull_request: - branches: [ "main" ] + branches: [ "main", "heightmaps" ] # TODO: remove 'heightmaps' after merge jobs: build-dmg: diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index f93b5ac9..349b1447 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -4,7 +4,7 @@ on: push: branches: [ "main" ] pull_request: - branches: [ "main" ] + branches: [ "main", "heightmaps" ] # TODO: remove 'heightmaps' after merge jobs: build-windows: From 091805a16e9e827cc687d6f39c0de7744df2eb0c Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 6 Oct 2024 17:28:44 +0300 Subject: [PATCH 4/6] refactor scripting_world_generation.cpp --- .../scripting/scripting_world_generation.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/logic/scripting/scripting_world_generation.cpp b/src/logic/scripting/scripting_world_generation.cpp index dd4d8ad1..e935d818 100644 --- a/src/logic/scripting/scripting_world_generation.cpp +++ b/src/logic/scripting/scripting_world_generation.cpp @@ -15,20 +15,22 @@ #include "util/timeutil.hpp" class LuaGeneratorScript : public GeneratorScript { + lua::State* L; const GeneratorDef& def; scriptenv env; public: LuaGeneratorScript( + lua::State* L, const GeneratorDef& def, scriptenv env) - : def(def), + : L(L), + def(def), env(std::move(env)) {} std::shared_ptr generateHeightmap( const glm::ivec2& offset, const glm::ivec2& size, uint64_t seed, uint bpd ) override { - auto L = lua::get_main_thread(); lua::pushenv(L, *env); if (lua::getfield(L, "generate_heightmap")) { lua::pushivec_stack(L, offset); @@ -51,7 +53,6 @@ public: std::vector> maps; uint biomeParameters = def.biomeParameters; - auto L = lua::get_main_thread(); lua::pushenv(L, *env); if (lua::getfield(L, "generate_biome_parameters")) { lua::pushivec_stack(L, offset); @@ -80,7 +81,6 @@ public: ) override { std::vector placements; - auto L = lua::get_main_thread(); lua::stackguard _(L); lua::pushenv(L, *env); if (lua::getfield(L, "place_structures")) { @@ -97,7 +97,9 @@ public: lua::rawgeti(L, 1); int structIndex = 0; if (lua::isstring(L, -1)) { - const auto& found = def.structuresIndices.find(lua::require_string(L, -1)); + const auto& found = def.structuresIndices.find( + lua::require_string(L, -1) + ); if (found != def.structuresIndices.end()) { structIndex = found->second; } @@ -144,11 +146,10 @@ std::unique_ptr scripting::load_generator( lua::pop(L, load_script(*env, "generator", file)); } else { // Use default (empty) script - lua::pop(L, lua::execute(lua::get_main_thread(), *env, "", "")); + lua::pop(L, lua::execute(L, *env, "", "")); } return std::make_unique( - def, - std::move(env) + L, def, std::move(env) ); } From 73d96fd4f777d1a730953a52ba9a47c2e0c7a503 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 6 Oct 2024 18:23:33 +0300 Subject: [PATCH 5/6] 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, "", "")); From d9a44f3b87a355e605321238091c8fd28efa4f3b Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 6 Oct 2024 21:07:27 +0300 Subject: [PATCH 6/6] minor refactor --- src/logic/scripting/scripting.cpp | 4 - src/logic/scripting/scripting.hpp | 1 - .../scripting/scripting_world_generation.cpp | 133 +++++++++--------- 3 files changed, 64 insertions(+), 74 deletions(-) diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index b06e7258..eaab27d3 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -73,10 +73,6 @@ void scripting::initialize(Engine* engine) { return std::make_shared(0); } -[[nodiscard]] scriptenv scripting::create_environment() { - return lua::create_environment(lua::get_main_state()); -} - [[nodiscard]] scriptenv scripting::create_pack_environment( const ContentPack& pack ) { diff --git a/src/logic/scripting/scripting.hpp b/src/logic/scripting/scripting.hpp index 8a1a39cd..a34c21cf 100644 --- a/src/logic/scripting/scripting.hpp +++ b/src/logic/scripting/scripting.hpp @@ -52,7 +52,6 @@ namespace scripting { scriptenv get_root_environment(); scriptenv create_pack_environment(const ContentPack& pack); - scriptenv create_environment(); scriptenv create_doc_environment( const scriptenv& parent, const std::string& name ); diff --git a/src/logic/scripting/scripting_world_generation.cpp b/src/logic/scripting/scripting_world_generation.cpp index c8bb2799..c7655084 100644 --- a/src/logic/scripting/scripting_world_generation.cpp +++ b/src/logic/scripting/scripting_world_generation.cpp @@ -16,45 +16,42 @@ #include "files/files.hpp" #include "debug/Logger.hpp" +using namespace lua; + static debug::Logger logger("generator-scripting"); class LuaGeneratorScript : public GeneratorScript { - lua::State* L; + State* L; const GeneratorDef& def; scriptenv env; public: - LuaGeneratorScript( - lua::State* L, - const GeneratorDef& def, - scriptenv env) - : L(L), - def(def), - env(std::move(env)) - {} + LuaGeneratorScript(State* L, const GeneratorDef& def, scriptenv env) + : L(L), def(def), env(std::move(env)) { + } virtual ~LuaGeneratorScript() { env.reset(); - if (L != lua::get_main_state()) { - lua::close(L); + if (L != get_main_state()) { + close(L); } } std::shared_ptr generateHeightmap( const glm::ivec2& offset, const glm::ivec2& size, uint64_t seed, uint bpd ) override { - lua::pushenv(L, *env); - if (lua::getfield(L, "generate_heightmap")) { - lua::pushivec_stack(L, offset); - lua::pushivec_stack(L, size); - lua::pushinteger(L, seed); - lua::pushinteger(L, bpd); - if (lua::call_nothrow(L, 6)) { - auto map = lua::touserdata(L, -1)->getHeightmap(); - lua::pop(L, 2); + pushenv(L, *env); + if (getfield(L, "generate_heightmap")) { + pushivec_stack(L, offset); + pushivec_stack(L, size); + pushinteger(L, seed); + pushinteger(L, bpd); + if (call_nothrow(L, 6)) { + auto map = touserdata(L, -1)->getHeightmap(); + pop(L, 2); return map; } } - lua::pop(L); + pop(L); return std::make_shared(size.x, size.y); } @@ -64,22 +61,22 @@ public: std::vector> maps; uint biomeParameters = def.biomeParameters; - lua::pushenv(L, *env); - if (lua::getfield(L, "generate_biome_parameters")) { - lua::pushivec_stack(L, offset); - lua::pushivec_stack(L, size); - lua::pushinteger(L, seed); - lua::pushinteger(L, bpd); - if (lua::call_nothrow(L, 6, biomeParameters)) { + pushenv(L, *env); + if (getfield(L, "generate_biome_parameters")) { + pushivec_stack(L, offset); + pushivec_stack(L, size); + pushinteger(L, seed); + pushinteger(L, bpd); + if (call_nothrow(L, 6, biomeParameters)) { for (int i = biomeParameters-1; i >= 0; i--) { maps.push_back( - lua::touserdata(L, -1-i)->getHeightmap()); + touserdata(L, -1-i)->getHeightmap()); } - lua::pop(L, 1+biomeParameters); + pop(L, 1+biomeParameters); return maps; } } - lua::pop(L); + pop(L); for (uint i = 0; i < biomeParameters; i++) { maps.push_back(std::make_shared(size.x, size.y)); } @@ -92,46 +89,46 @@ public: ) override { std::vector placements; - lua::stackguard _(L); - lua::pushenv(L, *env); - if (lua::getfield(L, "place_structures")) { - lua::pushivec_stack(L, offset); - lua::pushivec_stack(L, size); - lua::pushinteger(L, seed); - lua::newuserdata(L, heightmap); - lua::pushinteger(L, chunkHeight); - if (lua::call_nothrow(L, 7, 1)) { - int len = lua::objlen(L, -1); + stackguard _(L); + pushenv(L, *env); + if (getfield(L, "place_structures")) { + pushivec_stack(L, offset); + pushivec_stack(L, size); + pushinteger(L, seed); + newuserdata(L, heightmap); + pushinteger(L, chunkHeight); + if (call_nothrow(L, 7, 1)) { + int len = objlen(L, -1); for (int i = 1; i <= len; i++) { - lua::rawgeti(L, i); + rawgeti(L, i); - lua::rawgeti(L, 1); + rawgeti(L, 1); int structIndex = 0; - if (lua::isstring(L, -1)) { + if (isstring(L, -1)) { const auto& found = def.structuresIndices.find( - lua::require_string(L, -1) + require_string(L, -1) ); if (found != def.structuresIndices.end()) { structIndex = found->second; } } else { - structIndex = lua::tointeger(L, -1); + structIndex = tointeger(L, -1); } - lua::pop(L); + pop(L); - lua::rawgeti(L, 2); - glm::ivec3 pos = lua::tovec3(L, -1); - lua::pop(L); + rawgeti(L, 2); + glm::ivec3 pos = tovec3(L, -1); + pop(L); - lua::rawgeti(L, 3); - int rotation = lua::tointeger(L, -1) & 0b11; - lua::pop(L); + rawgeti(L, 3); + int rotation = tointeger(L, -1) & 0b11; + pop(L); - lua::pop(L); + pop(L); placements.emplace_back(structIndex, pos, rotation); } - lua::pop(L); + pop(L); } } return placements; @@ -141,28 +138,26 @@ public: std::unique_ptr scripting::load_generator( const GeneratorDef& def, const fs::path& file, const std::string& dirPath ) { - auto L = lua::create_state(lua::StateType::GENERATOR); - auto env = lua::create_environment(L); - lua::stackguard _(L); + auto L = create_state(StateType::GENERATOR); + auto env = create_environment(L); + stackguard _(L); - lua::pushenv(L, *env); - lua::pushstring(L, dirPath); - lua::setfield(L, "__DIR__"); - lua::pushstring(L, dirPath + "/script.lua"); - lua::setfield(L, "__FILE__"); + pushenv(L, *env); + pushstring(L, dirPath); + setfield(L, "__DIR__"); + pushstring(L, dirPath + "/script.lua"); + setfield(L, "__FILE__"); - lua::pop(L); + pop(L); if (fs::exists(file)) { std::string src = files::read_string(file); logger.info() << "script (generator) " << file.u8string(); - lua::pop(L, lua::execute(L, *env, src, file.u8string())); + pop(L, execute(L, *env, src, file.u8string())); } else { // Use default (empty) script - lua::pop(L, lua::execute(L, *env, "", "")); + pop(L, execute(L, *env, "", "")); } - return std::make_unique( - L, def, std::move(env) - ); + return std::make_unique(L, def, std::move(env)); }