From 7973a9c32b6f598717fc03e6d5fd71ec37f8a2ca Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 11 Jun 2024 01:52:28 +0300 Subject: [PATCH 1/6] fix: lua coroutines support --- src/logic/scripting/lua/LuaState.cpp | 374 ++++--------------- src/logic/scripting/lua/LuaState.hpp | 70 +--- src/logic/scripting/lua/libconsole.cpp | 21 +- src/logic/scripting/lua/libcore.cpp | 56 +-- src/logic/scripting/lua/libfile.cpp | 68 ++-- src/logic/scripting/lua/libgui.cpp | 302 +++++++-------- src/logic/scripting/lua/libinput.cpp | 13 +- src/logic/scripting/lua/libitem.cpp | 16 +- src/logic/scripting/lua/libjson.cpp | 7 +- src/logic/scripting/lua/libtoml.cpp | 9 +- src/logic/scripting/lua/lua_util.cpp | 117 ++++++ src/logic/scripting/lua/lua_util.hpp | 100 ++++- src/logic/scripting/scripting.cpp | 184 ++++----- src/logic/scripting/scripting_functional.cpp | 89 ++--- src/logic/scripting/scripting_hud.cpp | 21 +- 15 files changed, 707 insertions(+), 740 deletions(-) create mode 100644 src/logic/scripting/lua/lua_util.cpp diff --git a/src/logic/scripting/lua/LuaState.cpp b/src/logic/scripting/lua/LuaState.cpp index 524e3049..0d70a378 100644 --- a/src/logic/scripting/lua/LuaState.cpp +++ b/src/logic/scripting/lua/LuaState.cpp @@ -21,11 +21,11 @@ namespace scripting { luaerror::luaerror(const std::string& message) : std::runtime_error(message) { } -void LuaState::removeLibFuncs(const char* libname, const char* funcs[]) { - if (getglobal(libname)) { +void LuaState::removeLibFuncs(lua_State* L, const char* libname, const char* funcs[]) { + if (getglobal(L, libname)) { for (uint i = 0; funcs[i]; i++) { - pushnil(); - setfield(funcs[i], -2); + lua_pushnil(L); + setfield(L, funcs[i], -2); } } } @@ -34,10 +34,11 @@ LuaState::LuaState() { logger.info() << LUA_VERSION; logger.info() << LUAJIT_VERSION; - L = luaL_newstate(); + auto L = luaL_newstate(); if (L == nullptr) { throw luaerror("could not to initialize Lua"); } + mainThread = L; // Allowed standard libraries luaopen_base(L); luaopen_math(L); @@ -57,15 +58,15 @@ LuaState::LuaState() { "tmpname", nullptr }; - removeLibFuncs("os", removed_os); + removeLibFuncs(L, "os", removed_os); - createLibs(); + createLibs(L); - lua_pushvalue(L, LUA_GLOBALSINDEX); - setglobal(envName(0)); + pushglobals(L); + setglobal(L, envName(0)); lua_createtable(L, 0, 0); - setglobal(LAMBDAS_TABLE); + setglobal(L, LAMBDAS_TABLE); } std::string LuaState::envName(int env) { @@ -73,42 +74,15 @@ std::string LuaState::envName(int env) { } LuaState::~LuaState() { - lua_close(L); + lua_close(mainThread); } -void LuaState::logError(const std::string& text) { - logger.error() << text; -} - -void LuaState::addfunc(const std::string& name, lua_CFunction func) { +void LuaState::addfunc(lua_State* L, const std::string& name, lua_CFunction func) { lua_pushcfunction(L, func); lua_setglobal(L, name.c_str()); } -bool LuaState::getglobal(const std::string& name) { - lua_getglobal(L, name.c_str()); - if (lua_isnil(L, lua_gettop(L))) { - lua_pop(L, lua_gettop(L)); - return false; - } - return true; -} - -bool LuaState::hasglobal(const std::string& name) { - lua_getglobal(L, name.c_str()); - if (lua_isnil(L, lua_gettop(L))) { - lua_pop(L, lua_gettop(L)); - return false; - } - lua_pop(L, lua_gettop(L)); - return true; -} - -void LuaState::setglobal(const std::string& name) { - lua_setglobal(L, name.c_str()); -} - -bool LuaState::rename(const std::string& from, const std::string& to) { +bool LuaState::rename(lua_State* L, const std::string& from, const std::string& to) { const char* src = from.c_str(); lua_getglobal(L, src); if (lua_isnil(L, lua_gettop(L))) { @@ -123,262 +97,65 @@ bool LuaState::rename(const std::string& from, const std::string& to) { return true; } -void LuaState::remove(const std::string& name) { +void LuaState::remove(lua_State* L, const std::string& name) { lua_pushnil(L); lua_setglobal(L, name.c_str()); } -void LuaState::createLibs() { - openlib("audio", audiolib); - openlib("block", blocklib); - openlib("console", consolelib); - openlib("core", corelib); - openlib("file", filelib); - openlib("gui", guilib); - openlib("input", inputlib); - openlib("inventory", inventorylib); - openlib("item", itemlib); - openlib("json", jsonlib); - openlib("pack", packlib); - openlib("player", playerlib); - openlib("time", timelib); - openlib("toml", tomllib); - openlib("world", worldlib); +void LuaState::createLibs(lua_State* L) { + openlib(L, "audio", audiolib); + 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, "item", itemlib); + openlib(L, "json", jsonlib); + openlib(L, "pack", packlib); + openlib(L, "player", playerlib); + openlib(L, "time", timelib); + openlib(L, "toml", tomllib); + openlib(L, "world", worldlib); - addfunc("print", lua_wrap_errors); + addfunc(L, "print", lua_wrap_errors); } -void LuaState::loadbuffer(int env, const std::string& src, const std::string& file) { +void LuaState::loadbuffer(lua_State* L, int env, const std::string& src, const std::string& file) { if (luaL_loadbuffer(L, src.c_str(), src.length(), file.c_str())) { throw luaerror(lua_tostring(L, -1)); } - if (env && getglobal(envName(env))) { + if (env && getglobal(L, envName(env))) { lua_setfenv(L, -2); } } -int LuaState::call(int argc, int nresults) { - if (lua_pcall(L, argc, nresults, 0)) { - throw luaerror(lua_tostring(L, -1)); - } - return 1; -} - -int LuaState::callNoThrow(int argc) { - if (lua_pcall(L, argc, LUA_MULTRET, 0)) { - logError(lua_tostring(L, -1)); - return 0; - } - return 1; -} - -int LuaState::eval(int env, const std::string& src, const std::string& file) { +int LuaState::eval(lua_State* L, int env, const std::string& src, const std::string& file) { auto srcText = "return "+src; - loadbuffer(env, srcText, file); - return call(0); + loadbuffer(L, env, srcText, file); + return call(L, 0); } -int LuaState::execute(int env, const std::string& src, const std::string& file) { - loadbuffer(env, src, file); - return callNoThrow(0); +int LuaState::execute(lua_State* L, int env, const std::string& src, const std::string& file) { + loadbuffer(L, env, src, file); + return callNoThrow(L, 0); } -int LuaState::gettop() const { - return lua_gettop(L); -} - -int LuaState::pushinteger(lua_Integer x) { - lua_pushinteger(L, x); - return 1; -} - -int LuaState::pushnumber(lua_Number x) { - lua_pushnumber(L, x); - return 1; -} - -int LuaState::pushboolean(bool x) { - lua_pushboolean(L, x); - return 1; -} - -int LuaState::pushivec3(lua_Integer x, lua_Integer y, lua_Integer z) { - lua::pushivec3(L, x, y, z); - return 3; -} - -int LuaState::pushstring(const std::string& str) { - lua_pushstring(L, str.c_str()); - return 1; -} - -int LuaState::pushenv(int env) { - if (getglobal(envName(env))) { +int LuaState::pushenv(lua_State* L, int env) { + if (getglobal(L, envName(env))) { return 1; } return 0; } -int LuaState::pushvalue(int idx) { - lua_pushvalue(L, idx); - return 1; -} - -int LuaState::pushvalue(const dynamic::Value& value) { - using namespace dynamic; - - if (auto* flag = std::get_if(&value)) { - pushboolean(*flag); - } else if (auto* num = std::get_if(&value)) { - pushinteger(*num); - } else if (auto* num = std::get_if(&value)) { - pushnumber(*num); - } else if (auto* str = std::get_if(&value)) { - pushstring(str->c_str()); - } else if (auto listptr = std::get_if(&value)) { - auto list = *listptr; - lua_createtable(L, list->size(), 0); - for (size_t i = 0; i < list->size(); i++) { - pushvalue(list->get(i)); - lua_rawseti(L, -2, i+1); - } - } else if (auto mapptr = std::get_if(&value)) { - auto map = *mapptr; - lua_createtable(L, 0, map->size()); - for (auto& entry : map->values) { - pushvalue(entry.second); - lua_setfield(L, -2, entry.first.c_str()); - } - } else { - pushnil(); - } - return 1; -} - -int LuaState::pushglobals() { - lua_pushvalue(L, LUA_GLOBALSINDEX); - return 1; -} - -int LuaState::pushcfunction(lua_CFunction function) { - lua_pushcfunction(L, function); - return 1; -} - -void LuaState::pop(int n) { - lua_pop(L, n); -} - -int LuaState::pushnil() { - lua_pushnil(L); - return 1; -} - -bool LuaState::getfield(const std::string& name, int idx) { - lua_getfield(L, idx, name.c_str()); - if (lua_isnil(L, -1)) { - lua_pop(L, -1); - return false; - } - return true; -} - -void LuaState::setfield(const std::string& name, int idx) { - lua_setfield(L, idx, name.c_str()); -} - -bool LuaState::toboolean(int idx) { - return lua_toboolean(L, idx); -} - -lua_Integer LuaState::tointeger(int idx) { - return lua_tointeger(L, idx); -} - -lua_Number LuaState::tonumber(int idx) { - return lua_tonumber(L, idx); -} - -glm::vec2 LuaState::tovec2(int idx) { - return lua::tovec2(L, idx); -} - -glm::vec4 LuaState::tocolor(int idx) { - return lua::tocolor(L, idx); -} - -const char* LuaState::tostring(int idx) { - return lua_tostring(L, idx); -} - -dynamic::Value LuaState::tovalue(int idx) { - using namespace dynamic; - auto type = lua_type(L, idx); - switch (type) { - case LUA_TNIL: - case LUA_TNONE: - return dynamic::NONE; - case LUA_TBOOLEAN: - return lua_toboolean(L, idx) == 1; - case LUA_TNUMBER: { - auto number = lua_tonumber(L, idx); - auto integer = lua_tointeger(L, idx); - if (number == (lua_Number)integer) { - return integer; - } else { - return number; - } - } - case LUA_TSTRING: - return std::string(lua_tostring(L, idx)); - case LUA_TTABLE: { - int len = lua_objlen(L, idx); - if (len) { - // array - auto list = create_list(); - for (int i = 1; i <= len; i++) { - lua_rawgeti(L, idx, i); - list->put(tovalue(-1)); - lua_pop(L, 1); - } - return list; - } else { - // table - auto map = create_map(); - lua_pushvalue(L, idx); - lua_pushnil(L); - while (lua_next(L, -2)) { - lua_pushvalue(L, -2); - auto key = lua_tostring(L, -1); - map->put(key, tovalue(-2)); - lua_pop(L, 2); - } - lua_pop(L, 1); - return map; - } - } - default: - throw std::runtime_error( - "lua type "+std::string(luaL_typename(L, type))+" is not supported" - ); - } -} - -bool LuaState::isstring(int idx) { - return lua_isstring(L, idx); -} - -bool LuaState::isfunction(int idx) { - return lua_isfunction(L, idx); -} - -void LuaState::openlib(const std::string& name, const luaL_Reg* libfuncs) { +void LuaState::openlib(lua_State* L, const std::string& name, const luaL_Reg* libfuncs) { lua_newtable(L); luaL_setfuncs(L, libfuncs, 0); lua_setglobal(L, name.c_str()); } -std::shared_ptr LuaState::createLambdaHandler() { +std::shared_ptr LuaState::createLambdaHandler(lua_State* L) { auto ptr = reinterpret_cast(lua_topointer(L, -1)); auto name = util::mangleid(ptr); lua_getglobal(L, LAMBDAS_TABLE.c_str()); @@ -395,40 +172,33 @@ std::shared_ptr LuaState::createLambdaHandler() { }); } -runnable LuaState::createRunnable() { - auto funcptr = createLambdaHandler(); +runnable LuaState::createRunnable(lua_State* L) { + auto funcptr = createLambdaHandler(L); return [=]() { lua_getglobal(L, LAMBDAS_TABLE.c_str()); lua_getfield(L, -1, funcptr->c_str()); - callNoThrow(0); + callNoThrow(L, 0); }; } -scripting::common_func LuaState::createLambda() { - auto funcptr = createLambdaHandler(); +scripting::common_func LuaState::createLambda(lua_State* L) { + auto funcptr = createLambdaHandler(L); return [=](const std::vector& args) { lua_getglobal(L, LAMBDAS_TABLE.c_str()); lua_getfield(L, -1, funcptr->c_str()); for (const auto& arg : args) { - pushvalue(arg); + pushvalue(L, arg); } - if (call(args.size(), 1)) { - auto result = tovalue(-1); - pop(1); + if (call(L, args.size(), 1)) { + auto result = tovalue(L, -1); + lua_pop(L, 1); return result; } return dynamic::Value(dynamic::NONE); }; } -const char* LuaState::requireString(int idx) { - if (!lua_isstring(L, idx)) { - throw luaerror("string expected at "+std::to_string(idx)); - } - return lua_tostring(L, idx); -} - -int LuaState::createEnvironment(int parent) { +int LuaState::createEnvironment(lua_State* L, int parent) { int id = nextEnvironment++; // local env = {} @@ -439,7 +209,7 @@ int LuaState::createEnvironment(int parent) { if (parent == 0) { lua_pushvalue(L, LUA_GLOBALSINDEX); } else { - if (pushenv(parent) == 0) { + if (pushenv(L, parent) == 0) { lua_pushvalue(L, LUA_GLOBALSINDEX); } } @@ -447,43 +217,43 @@ int LuaState::createEnvironment(int parent) { lua_setmetatable(L, -2); // envname = env - setglobal(envName(id)); + setglobal(L, envName(id)); return id; } -void LuaState::removeEnvironment(int id) { +void LuaState::removeEnvironment(lua_State* L, int id) { if (id == 0) { return; } lua_pushnil(L); - setglobal(envName(id)); + setglobal(L, envName(id)); } -bool LuaState::emit_event(const std::string &name, std::function args) { - getglobal("events"); - getfield("emit"); - pushstring(name); - callNoThrow(args(this) + 1); - bool result = toboolean(-1); - pop(2); +bool LuaState::emitEvent(lua_State* L, const std::string &name, std::function args) { + getglobal(L, "events"); + getfield(L, "emit"); + lua::pushstring(L, name); + callNoThrow(L, args(L) + 1); + bool result = lua_toboolean(L, -1); + lua_pop(L, 2); return result; } -void LuaState::dumpStack() { - int top = gettop(); +void LuaState::dumpStack(lua_State* L) { + int top = lua_gettop(L); for (int i = 1; i <= top; i++) { std::cout << std::setw(3) << i << std::setw(20) << luaL_typename(L, i) << std::setw(30); switch (lua_type(L, i)) { case LUA_TNUMBER: - std::cout << tonumber(i); + std::cout << lua_tonumber(L, i); break; case LUA_TSTRING: - std::cout << tostring(i); + std::cout << lua_tostring(L, i); break; case LUA_TBOOLEAN: - std::cout << (toboolean(i) ? "true" : "false"); + std::cout << (lua_toboolean(L, i) ? "true" : "false"); break; case LUA_TNIL: std::cout << "nil"; @@ -496,6 +266,6 @@ void LuaState::dumpStack() { } } -lua_State* LuaState::getLua() const { - return L; +lua_State* LuaState::getMainThread() const { + return mainThread; } diff --git a/src/logic/scripting/lua/LuaState.hpp b/src/logic/scripting/lua/LuaState.hpp index 74c0257d..f8a925e5 100644 --- a/src/logic/scripting/lua/LuaState.hpp +++ b/src/logic/scripting/lua/LuaState.hpp @@ -17,65 +17,35 @@ namespace lua { }; class LuaState { - lua_State* L; + lua_State* mainThread; + int nextEnvironment = 1; - void logError(const std::string& text); - void removeLibFuncs(const char* libname, const char* funcs[]); - void createLibs(); + void removeLibFuncs(lua_State*, const char* libname, const char* funcs[]); + void createLibs(lua_State* L); - std::shared_ptr createLambdaHandler(); + std::shared_ptr createLambdaHandler(lua_State*); public: LuaState(); ~LuaState(); static std::string envName(int env); - void loadbuffer(int env, const std::string& src, const std::string& file); - int gettop() const; - int pushivec3(lua_Integer x, lua_Integer y, lua_Integer z); - int pushinteger(lua_Integer x); - int pushnumber(lua_Number x); - int pushboolean(bool x); - int pushstring(const std::string& str); - int pushenv(int env); - int pushvalue(int idx); - int pushvalue(const dynamic::Value& value); - int pushnil(); - int pushglobals(); - int pushcfunction(lua_CFunction function); - void pop(int n=1); - bool getfield(const std::string& name, int idx = -1); - void setfield(const std::string& name, int idx = -2); - bool toboolean(int idx); - lua_Integer tointeger(int idx); - lua_Number tonumber(int idx); - glm::vec2 tovec2(int idx); - glm::vec4 tocolor(int idx); - dynamic::Value tovalue(int idx); - const char* tostring(int idx); - bool isstring(int idx); - bool isfunction(int idx); - int call(int argc, int nresults=-1); - int callNoThrow(int argc); - int execute(int env, const std::string& src, const std::string& file=""); - int eval(int env, const std::string& src, const std::string& file=""); - void openlib(const std::string& name, const luaL_Reg* libfuncs); - void addfunc(const std::string& name, lua_CFunction func); - bool getglobal(const std::string& name); - void setglobal(const std::string& name); - bool hasglobal(const std::string& name); - bool rename(const std::string& from, const std::string& to); - void remove(const std::string& name);; - runnable createRunnable(); - scripting::common_func createLambda(); + void loadbuffer(lua_State*, int env, const std::string& src, const std::string& file); + int pushenv(lua_State*, int env); + int execute(lua_State*, int env, const std::string& src, const std::string& file=""); + int eval(lua_State*, int env, const std::string& src, const std::string& file=""); + void openlib(lua_State*, const std::string& name, const luaL_Reg* libfuncs); + void addfunc(lua_State*, const std::string& name, lua_CFunction func); + bool rename(lua_State*, const std::string& from, const std::string& to); + void remove(lua_State*, const std::string& name);; + runnable createRunnable(lua_State*); + scripting::common_func createLambda(lua_State*); - const char* requireString(int idx); - - int createEnvironment(int parent); - void removeEnvironment(int id); - bool emit_event(const std::string& name, std::function args=[](auto*){return 0;}); - void dumpStack(); - lua_State* getLua() const; + int createEnvironment(lua_State*, int parent); + void removeEnvironment(lua_State*, int id); + bool emitEvent(lua_State*, const std::string& name, std::function args=[](auto*){return 0;}); + void dumpStack(lua_State*); + lua_State* getMainThread() const; }; } diff --git a/src/logic/scripting/lua/libconsole.cpp b/src/logic/scripting/lua/libconsole.cpp index 88238f65..0a27bcde 100644 --- a/src/logic/scripting/lua/libconsole.cpp +++ b/src/logic/scripting/lua/libconsole.cpp @@ -1,5 +1,6 @@ #include "api_lua.hpp" #include "lua_commons.hpp" +#include "lua_util.hpp" #include "LuaState.hpp" #include "../scripting.hpp" @@ -17,10 +18,10 @@ static int l_add_command(lua_State* L) { if (!lua_isfunction(L, 3)) { throw std::runtime_error("invalid callback"); } - auto scheme = state->requireString(1); - auto description = state->requireString(2); + auto scheme = lua::require_string(L, 1); + auto description = lua::require_string(L, 2); lua_pushvalue(L, 3); - auto func = state->createLambda(); + auto func = state->createLambda(L); try { engine->getCommandsInterpreter()->getRepository()->add( scheme, description, [func](auto, auto args, auto kwargs) { @@ -33,16 +34,16 @@ static int l_add_command(lua_State* L) { return 0; } -static int l_execute(lua_State*) { - auto prompt = state->requireString(1); +static int l_execute(lua_State* L) { + auto prompt = lua::require_string(L, 1); auto result = engine->getCommandsInterpreter()->execute(prompt); - state->pushvalue(result); + lua::pushvalue(L, result); return 1; } -static int l_set(lua_State*) { - auto name = state->requireString(1); - auto value = state->tovalue(2); +static int l_set(lua_State* L) { + auto name = lua::require_string(L, 1); + auto value = lua::tovalue(L, 2); (*engine->getCommandsInterpreter())[name] = value; return 0; } @@ -62,7 +63,7 @@ static int l_get_commands_list(lua_State* L) { } static int l_get_command_info(lua_State* L) { - auto name = state->requireString(1); + auto name = lua::require_string(L, 1); auto interpreter = engine->getCommandsInterpreter(); auto repo = interpreter->getRepository(); auto command = repo->get(name); diff --git a/src/logic/scripting/lua/libcore.cpp b/src/logic/scripting/lua/libcore.cpp index 068c8824..48003a7d 100644 --- a/src/logic/scripting/lua/libcore.cpp +++ b/src/logic/scripting/lua/libcore.cpp @@ -1,4 +1,5 @@ #include "lua_commons.hpp" +#include "lua_util.hpp" #include "api_lua.hpp" #include "LuaState.hpp" @@ -21,48 +22,49 @@ namespace scripting { extern lua::LuaState* state; } +using namespace scripting; static int l_new_world(lua_State* L) { - auto name = lua_tostring(L, 1); - auto seed = lua_tostring(L, 2); - auto generator = lua_tostring(L, 3); - auto controller = scripting::engine->getController(); + auto name = lua::require_string(L, 1); + auto seed = lua::require_string(L, 2); + auto generator = lua::require_string(L, 3); + auto controller = engine->getController(); controller->createWorld(name, seed, generator); return 0; } static int l_open_world(lua_State* L) { - auto name = lua_tostring(L, 1); + auto name = lua::require_string(L, 1); - auto controller = scripting::engine->getController(); + auto controller = engine->getController(); controller->openWorld(name, false); return 0; } static int l_reopen_world(lua_State*) { - auto controller = scripting::engine->getController(); - controller->reopenWorld(scripting::level->getWorld()); + auto controller = engine->getController(); + controller->reopenWorld(level->getWorld()); return 0; } static int l_close_world(lua_State* L) { - if (scripting::controller == nullptr) { + if (controller == nullptr) { throw std::runtime_error("no world open"); } bool save_world = lua_toboolean(L, 1); if (save_world) { - scripting::controller->saveWorld(); + controller->saveWorld(); } // destroy LevelScreen and run quit callbacks - scripting::engine->setScreen(nullptr); + engine->setScreen(nullptr); // create and go to menu screen - scripting::engine->setScreen(std::make_shared(scripting::engine)); + engine->setScreen(std::make_shared(engine)); return 0; } static int l_delete_world(lua_State* L) { - auto name = lua_tostring(L, 1); - auto controller = scripting::engine->getController(); + auto name = lua::require_string(L, 1); + auto controller = engine->getController(); controller->deleteWorld(name); return 0; } @@ -95,35 +97,35 @@ static int l_reconfig_packs(lua_State* L) { remPacks.emplace_back(lua_tostring(L, -1)); lua_pop(L, 1); } - auto controller = scripting::engine->getController(); - controller->reconfigPacks(scripting::controller, addPacks, remPacks); + auto engine_controller = engine->getController(); + engine_controller->reconfigPacks(controller, addPacks, remPacks); return 0; } static int l_get_setting(lua_State* L) { - auto name = lua_tostring(L, 1); - const auto value = scripting::engine->getSettingsHandler().getValue(name); - scripting::state->pushvalue(value); + auto name = lua::require_string(L, 1); + const auto value = engine->getSettingsHandler().getValue(name); + lua::pushvalue(L, value); return 1; } static int l_set_setting(lua_State* L) { - auto name = lua_tostring(L, 1); - const auto value = scripting::state->tovalue(2); - scripting::engine->getSettingsHandler().setValue(name, value); + auto name = lua::require_string(L, 1); + const auto value = lua::tovalue(L, 2); + engine->getSettingsHandler().setValue(name, value); return 0; } static int l_str_setting(lua_State* L) { - auto name = lua_tostring(L, 1); - const auto string = scripting::engine->getSettingsHandler().toString(name); - scripting::state->pushstring(string); + auto name = lua::require_string(L, 1); + const auto string = engine->getSettingsHandler().toString(name); + lua::pushstring(L, string); return 1; } static int l_get_setting_info(lua_State* L) { - auto name = lua_tostring(L, 1); - auto setting = scripting::engine->getSettingsHandler().getSetting(name); + auto name = lua::require_string(L, 1); + auto setting = engine->getSettingsHandler().getSetting(name); lua_createtable(L, 0, 1); if (auto number = dynamic_cast(setting)) { lua_pushnumber(L, number->getMin()); diff --git a/src/logic/scripting/lua/libfile.cpp b/src/logic/scripting/lua/libfile.cpp index ecea4c23..85336add 100644 --- a/src/logic/scripting/lua/libfile.cpp +++ b/src/logic/scripting/lua/libfile.cpp @@ -1,4 +1,5 @@ #include "lua_commons.hpp" +#include "lua_util.hpp" #include "api_lua.hpp" #include "LuaState.hpp" #include "../scripting.hpp" @@ -31,7 +32,7 @@ static fs::path resolve_path_soft(const std::string& path) { } static int l_file_find(lua_State* L) { - std::string path = state->requireString(1); + auto path = lua::require_string(L, 1); try { lua_pushstring(L, engine->getResPaths()->findRaw(path).c_str()); return 1; @@ -41,13 +42,13 @@ static int l_file_find(lua_State* L) { } static int l_file_resolve(lua_State* L) { - fs::path path = resolve_path(state->requireString(1)); + fs::path path = resolve_path(lua::require_string(L, 1)); lua_pushstring(L, path.u8string().c_str()); return 1; } static int l_file_read(lua_State* L) { - fs::path path = resolve_path(state->requireString(1)); + fs::path path = resolve_path(lua::require_string(L, 1)); if (fs::is_regular_file(path)) { lua_pushstring(L, files::read_string(path).c_str()); return 1; @@ -55,50 +56,50 @@ static int l_file_read(lua_State* L) { throw std::runtime_error("file does not exists "+util::quote(path.u8string())); } -static int l_file_write(lua_State*) { - fs::path path = resolve_path(state->requireString(1)); - auto text = state->requireString(2); +static int l_file_write(lua_State* L) { + fs::path path = resolve_path(lua::require_string(L, 1)); + std::string text = lua::require_string(L, 2); files::write_string(path, text); return 1; } -static int l_file_remove(lua_State*) { - std::string rawpath = state->requireString(1); +static int l_file_remove(lua_State* L) { + std::string rawpath = lua::require_string(L, 1); fs::path path = resolve_path(rawpath); auto entryPoint = rawpath.substr(0, rawpath.find(':')); if (entryPoint != "world") { throw std::runtime_error("access denied"); } - return state->pushboolean(fs::remove(path)); + return lua::pushboolean(L, fs::remove(path)); } -static int l_file_remove_tree(lua_State*) { - std::string rawpath = state->requireString(1); +static int l_file_remove_tree(lua_State* L) { + std::string rawpath = lua::require_string(L, 1); fs::path path = resolve_path(rawpath); auto entryPoint = rawpath.substr(0, rawpath.find(':')); if (entryPoint != "world") { throw std::runtime_error("access denied"); } - return state->pushinteger(fs::remove_all(path)); + return lua::pushinteger(L, fs::remove_all(path)); } -static int l_file_exists(lua_State*) { - fs::path path = resolve_path_soft(state->requireString(1)); - return state->pushboolean(fs::exists(path)); +static int l_file_exists(lua_State* L) { + fs::path path = resolve_path_soft(lua::require_string(L, 1)); + return lua::pushboolean(L, fs::exists(path)); } -static int l_file_isfile(lua_State*) { - fs::path path = resolve_path_soft(state->requireString(1)); - return state->pushboolean(fs::is_regular_file(path)); +static int l_file_isfile(lua_State* L) { + fs::path path = resolve_path_soft(lua::require_string(L, 1)); + return lua::pushboolean(L, fs::is_regular_file(path)); } -static int l_file_isdir(lua_State*) { - fs::path path = resolve_path_soft(state->requireString(1)); - return state->pushboolean(fs::is_directory(path)); +static int l_file_isdir(lua_State* L) { + fs::path path = resolve_path_soft(lua::require_string(L, 1)); + return lua::pushboolean(L, fs::is_directory(path)); } static int l_file_length(lua_State* L) { - fs::path path = resolve_path(state->requireString(1)); + fs::path path = resolve_path(lua::require_string(L, 1)); if (fs::exists(path)){ lua_pushinteger(L, fs::file_size(path)); } else { @@ -108,19 +109,17 @@ static int l_file_length(lua_State* L) { } static int l_file_mkdir(lua_State* L) { - fs::path path = resolve_path(state->requireString(1)); - lua_pushboolean(L, fs::create_directory(path)); - return 1; + fs::path path = resolve_path(lua::require_string(L, 1)); + return lua::pushboolean(L, fs::create_directory(path)); } static int l_file_mkdirs(lua_State* L) { - fs::path path = resolve_path(state->requireString(1)); - lua_pushboolean(L, fs::create_directories(path)); - return 1; + fs::path path = resolve_path(lua::require_string(L, 1)); + return lua::pushboolean(L, fs::create_directories(path)); } static int l_file_read_bytes(lua_State* L) { - fs::path path = resolve_path(state->requireString(1)); + fs::path path = resolve_path(lua::require_string(L, 1)); if (fs::is_regular_file(path)) { size_t length = static_cast(fs::file_size(path)); @@ -162,7 +161,7 @@ static int l_file_write_bytes(lua_State* L) { throw std::runtime_error("string expected"); } - fs::path path = resolve_path(state->requireString(pathIndex)); + fs::path path = resolve_path(lua::require_string(L, pathIndex)); std::vector bytes; @@ -171,8 +170,7 @@ static int l_file_write_bytes(lua_State* L) { if(result != 1) { return result; } else { - lua_pushboolean(L, files::write_bytes(path, bytes.data(), bytes.size())); - return 1; + return lua::pushboolean(L, files::write_bytes(path, bytes.data(), bytes.size())); } } @@ -187,7 +185,7 @@ static int l_file_list_all_res(lua_State* L, const std::string& path) { } static int l_file_list(lua_State* L) { - std::string dirname = state->requireString(1); + std::string dirname = lua::require_string(L, 1); if (dirname.find(':') == std::string::npos) { return l_file_list_all_res(L, dirname); } @@ -208,7 +206,7 @@ static int l_file_list(lua_State* L) { } static int l_file_gzip_compress(lua_State* L) { - fs::path path = resolve_path(state->requireString(1)); + fs::path path = resolve_path(lua::require_string(L, 1)); if (fs::is_regular_file(path)) { size_t length = static_cast(fs::file_size(path)); @@ -221,7 +219,7 @@ static int l_file_gzip_compress(lua_State* L) { } static int l_file_gzip_decompress(lua_State* L) { - fs::path path = resolve_path(state->requireString(1)); + fs::path path = resolve_path(lua::require_string(L, 1)); if (fs::is_regular_file(path)) { size_t length = static_cast(fs::file_size(path)); diff --git a/src/logic/scripting/lua/libgui.cpp b/src/logic/scripting/lua/libgui.cpp index 13e9102a..e89b1046 100644 --- a/src/logic/scripting/lua/libgui.cpp +++ b/src/logic/scripting/lua/libgui.cpp @@ -50,8 +50,8 @@ static DocumentNode getDocumentNode(lua_State*, const std::string& name, const s static DocumentNode getDocumentNode(lua_State* L, int idx=1) { lua_getfield(L, idx, "docname"); lua_getfield(L, idx, "name"); - auto docname = state->requireString(-2); - auto name = state->requireString(-1); + auto docname = lua::require_string(L, -2); + auto name = lua::require_string(L, -1); auto node = getDocumentNode(L, docname, name); lua_pop(L, 2); return node; @@ -74,7 +74,7 @@ static int l_menu_reset(lua_State* L) { static int l_textbox_paste(lua_State* L) { auto node = getDocumentNode(L); auto box = dynamic_cast(node.node.get()); - auto text = state->requireString(2); + auto text = lua::require_string(L, 2); box->paste(util::str2wstr_utf8(text)); return 0; } @@ -82,7 +82,7 @@ static int l_textbox_paste(lua_State* L) { static int l_container_add(lua_State* L) { auto docnode = getDocumentNode(L); auto node = dynamic_cast(docnode.node.get()); - auto xmlsrc = state->requireString(2); + auto xmlsrc = lua::require_string(L, 2); try { auto subnode = guiutil::create(xmlsrc, docnode.document->getEnvironment()); node->add(subnode); @@ -115,10 +115,10 @@ static int l_container_clear(lua_State* L) { static int l_container_set_interval(lua_State* L) { auto node = getDocumentNode(L, 1); - auto interval = state->tointeger(2) / 1000.0f; + auto interval = lua_tointeger(L, 2) / 1000.0f; if (auto container = std::dynamic_pointer_cast(node.node)) { - state->pushvalue(3); - auto runnable = state->createRunnable(); + lua_pushvalue(L, 3); + auto runnable = state->createRunnable(L); container->listenInterval(interval, runnable); } return 0; @@ -131,212 +131,212 @@ static int l_move_into(lua_State* L) { return 0; } -static int p_get_inventory(UINode* node) { +static int p_get_inventory(UINode* node, lua_State* L) { if (auto inventory = dynamic_cast(node)) { auto inv = inventory->getInventory(); - return state->pushinteger(inv ? inv->getId() : 0); + return lua::pushinteger(L, inv ? inv->getId() : 0); } return 0; } -static int p_get_reset(UINode* node) { +static int p_get_reset(UINode* node, lua_State* L) { if (dynamic_cast(node)) { - return state->pushcfunction(l_menu_reset); + return lua::pushcfunction(L, l_menu_reset); } return 0; } -static int p_get_back(UINode* node) { +static int p_get_back(UINode* node, lua_State* L) { if (dynamic_cast(node)) { - return state->pushcfunction(l_menu_back); + return lua::pushcfunction(L, l_menu_back); } return 0; } -static int p_get_paste(UINode* node) { +static int p_get_paste(UINode* node, lua_State* L) { if (dynamic_cast(node)) { - return state->pushcfunction(l_textbox_paste); + return lua::pushcfunction(L, l_textbox_paste); } return 0; } -static int p_get_page(UINode* node) { +static int p_get_page(UINode* node, lua_State* L) { if (auto menu = dynamic_cast(node)) { - return state->pushstring(menu->getCurrent().name); + return lua::pushstring(L, menu->getCurrent().name); } return 0; } -static int p_is_checked(UINode* node) { +static int p_is_checked(UINode* node, lua_State* L) { if (auto box = dynamic_cast(node)) { - return state->pushboolean(box->isChecked()); + return lua::pushboolean(L, box->isChecked()); } else if (auto box = dynamic_cast(node)) { - return state->pushboolean(box->isChecked()); + return lua::pushboolean(L, box->isChecked()); } return 0; } -static int p_get_value(UINode* node) { +static int p_get_value(UINode* node, lua_State* L) { if (auto bar = dynamic_cast(node)) { - return state->pushnumber(bar->getValue()); + return lua::pushnumber(L, bar->getValue()); } return 0; } -static int p_get_min(UINode* node) { +static int p_get_min(UINode* node, lua_State* L) { if (auto bar = dynamic_cast(node)) { - return state->pushnumber(bar->getMin()); + return lua::pushnumber(L, bar->getMin()); } return 0; } -static int p_get_max(UINode* node) { +static int p_get_max(UINode* node, lua_State* L) { if (auto bar = dynamic_cast(node)) { - return state->pushnumber(bar->getMax()); + return lua::pushnumber(L, bar->getMax()); } return 0; } -static int p_get_step(UINode* node) { +static int p_get_step(UINode* node, lua_State* L) { if (auto bar = dynamic_cast(node)) { - return state->pushnumber(bar->getStep()); + return lua::pushnumber(L, bar->getStep()); } return 0; } -static int p_get_track_width(UINode* node) { +static int p_get_track_width(UINode* node, lua_State* L) { if (auto bar = dynamic_cast(node)) { - return state->pushnumber(bar->getTrackWidth()); + return lua::pushnumber(L, bar->getTrackWidth()); } return 0; } -static int p_get_track_color(UINode* node) { +static int p_get_track_color(UINode* node, lua_State* L) { if (auto bar = dynamic_cast(node)) { - return lua::pushcolor_arr(state->getLua(), bar->getTrackColor()); + return lua::pushcolor_arr(L, bar->getTrackColor()); } return 0; } -static int p_is_valid(UINode* node) { +static int p_is_valid(UINode* node, lua_State* L) { if (auto box = dynamic_cast(node)) { - return state->pushboolean(box->validate()); + return lua::pushboolean(L, box->validate()); } return 0; } -static int p_get_caret(UINode* node) { +static int p_get_caret(UINode* node, lua_State* L) { if (auto box = dynamic_cast(node)) { - return state->pushinteger(static_cast(box->getCaret())); + return lua::pushinteger(L, static_cast(box->getCaret())); } return 0; } -static int p_get_placeholder(UINode* node) { +static int p_get_placeholder(UINode* node, lua_State* L) { if (auto box = dynamic_cast(node)) { - return state->pushstring(util::wstr2str_utf8(box->getPlaceholder())); + return lua::pushwstring(L, box->getPlaceholder()); } return 0; } -static int p_get_text(UINode* node) { +static int p_get_text(UINode* node, lua_State* L) { if (auto button = dynamic_cast(node)) { - return state->pushstring(util::wstr2str_utf8(button->getText())); + return lua::pushwstring(L, button->getText()); } else if (auto label = dynamic_cast(node)) { - return state->pushstring(util::wstr2str_utf8(label->getText())); + return lua::pushwstring(L, label->getText()); } else if (auto box = dynamic_cast(node)) { - return state->pushstring(util::wstr2str_utf8(box->getText())); + return lua::pushwstring(L, box->getText()); } return 0; } -static int p_get_editable(UINode* node) { +static int p_get_editable(UINode* node, lua_State* L) { if (auto box = dynamic_cast(node)) { - return state->pushboolean(box->isEditable()); + return lua::pushboolean(L, box->isEditable()); } return 0; } -static int p_get_src(UINode* node) { +static int p_get_src(UINode* node, lua_State* L) { if (auto image = dynamic_cast(node)) { - return state->pushstring(image->getTexture()); + return lua::pushstring(L, image->getTexture()); } return 0; } -static int p_get_add(UINode* node) { +static int p_get_add(UINode* node, lua_State* L) { if (dynamic_cast(node)) { - return state->pushcfunction(lua_wrap_errors); + return lua::pushcfunction(L, lua_wrap_errors); } return 0; } -static int p_get_destruct(UINode*) { - return state->pushcfunction(lua_wrap_errors); +static int p_get_destruct(UINode*, lua_State* L) { + return lua::pushcfunction(L, lua_wrap_errors); } -static int p_get_clear(UINode* node) { +static int p_get_clear(UINode* node, lua_State* L) { if (dynamic_cast(node)) { - return state->pushcfunction(lua_wrap_errors); + return lua::pushcfunction(L, lua_wrap_errors); } return 0; } -static int p_set_interval(UINode* node) { +static int p_set_interval(UINode* node, lua_State* L) { if (dynamic_cast(node)) { - return state->pushcfunction(lua_wrap_errors); + return lua::pushcfunction(L, lua_wrap_errors); } return 0; } -static int p_get_color(UINode* node) { - return lua::pushcolor_arr(state->getLua(), node->getColor()); +static int p_get_color(UINode* node, lua_State* L) { + return lua::pushcolor_arr(L, node->getColor()); } -static int p_get_hover_color(UINode* node) { - return lua::pushcolor_arr(state->getLua(), node->getHoverColor()); +static int p_get_hover_color(UINode* node, lua_State* L) { + return lua::pushcolor_arr(L, node->getHoverColor()); } -static int p_get_pressed_color(UINode* node) { - return lua::pushcolor_arr(state->getLua(), node->getPressedColor()); +static int p_get_pressed_color(UINode* node, lua_State* L) { + return lua::pushcolor_arr(L, node->getPressedColor()); } -static int p_get_tooltip(UINode* node) { - return state->pushstring(util::wstr2str_utf8(node->getTooltip())); +static int p_get_tooltip(UINode* node, lua_State* L) { + return lua::pushwstring(L, node->getTooltip()); } -static int p_get_tooltip_delay(UINode* node) { - return state->pushnumber(node->getTooltipDelay()); +static int p_get_tooltip_delay(UINode* node, lua_State* L) { + return lua::pushnumber(L, node->getTooltipDelay()); } -static int p_get_pos(UINode* node) { - return lua::pushvec2_arr(state->getLua(), node->getPos()); +static int p_get_pos(UINode* node, lua_State* L) { + return lua::pushvec2_arr(L, node->getPos()); } -static int p_get_wpos(UINode* node) { - return lua::pushvec2_arr(state->getLua(), node->calcPos()); +static int p_get_wpos(UINode* node, lua_State* L) { + return lua::pushvec2_arr(L, node->calcPos()); } -static int p_get_size(UINode* node) { - return lua::pushvec2_arr(state->getLua(), node->getSize()); +static int p_get_size(UINode* node, lua_State* L) { + return lua::pushvec2_arr(L, node->getSize()); } -static int p_is_interactive(UINode* node) { - return state->pushboolean(node->isInteractive()); +static int p_is_interactive(UINode* node, lua_State* L) { + return lua::pushboolean(L, node->isInteractive()); } -static int p_is_visible(UINode* node) { - return state->pushboolean(node->isVisible()); +static int p_is_visible(UINode* node, lua_State* L) { + return lua::pushboolean(L, node->isVisible()); } -static int p_is_enabled(UINode* node) { - return state->pushboolean(node->isEnabled()); +static int p_is_enabled(UINode* node, lua_State* L) { + return lua::pushboolean(L, node->isEnabled()); } -static int p_move_into(UINode*) { - return state->pushcfunction(l_move_into); +static int p_move_into(UINode*, lua_State* L) { + return lua::pushcfunction(L, l_move_into); } -static int p_get_focused(UINode* node) { - return state->pushboolean(node->isFocused()); +static int p_get_focused(UINode* node, lua_State* L) { + return lua::pushboolean(L, node->isFocused()); } static int l_gui_getattr(lua_State* L) { - auto docname = state->requireString(1); - auto element = state->requireString(2); - auto attr = state->requireString(3); + auto docname = lua::require_string(L, 1); + auto element = lua::require_string(L, 2); + auto attr = lua::require_string(L, 3); auto docnode = getDocumentNode(L, docname, element); auto node = docnode.node; - static const std::unordered_map> getters { + static const std::unordered_map> getters { {"color", p_get_color}, {"hoverColor", p_get_hover_color}, {"pressedColor", p_get_pressed_color}, @@ -375,118 +375,118 @@ static int l_gui_getattr(lua_State* L) { }; auto func = getters.find(attr); if (func != getters.end()) { - return func->second(node.get()); + return func->second(node.get(), L); } return 0; } -static void p_set_color(UINode* node, int idx) { - node->setColor(state->tocolor(idx)); +static void p_set_color(UINode* node, lua_State* L, int idx) { + node->setColor(lua::tocolor(L, idx)); } -static void p_set_hover_color(UINode* node, int idx) { - node->setHoverColor(state->tocolor(idx)); +static void p_set_hover_color(UINode* node, lua_State* L, int idx) { + node->setHoverColor(lua::tocolor(L, idx)); } -static void p_set_pressed_color(UINode* node, int idx) { - node->setPressedColor(state->tocolor(idx)); +static void p_set_pressed_color(UINode* node, lua_State* L, int idx) { + node->setPressedColor(lua::tocolor(L, idx)); } -static void p_set_tooltip(UINode* node, int idx) { - node->setTooltip(util::str2wstr_utf8(state->requireString(idx))); +static void p_set_tooltip(UINode* node, lua_State* L, int idx) { + node->setTooltip(lua::require_wstring(L, idx)); } -static void p_set_tooltip_delay(UINode* node, int idx) { - node->setTooltipDelay(state->tonumber(idx)); +static void p_set_tooltip_delay(UINode* node, lua_State* L, int idx) { + node->setTooltipDelay(lua_tonumber(L, idx)); } -static void p_set_pos(UINode* node, int idx) { - node->setPos(state->tovec2(idx)); +static void p_set_pos(UINode* node, lua_State* L, int idx) { + node->setPos(lua::tovec2(L, idx)); } -static void p_set_wpos(UINode* node, int idx) { - node->setPos(state->tovec2(idx)-node->calcPos()); +static void p_set_wpos(UINode* node, lua_State* L, int idx) { + node->setPos(lua::tovec2(L, idx)-node->calcPos()); } -static void p_set_size(UINode* node, int idx) { - node->setSize(state->tovec2(idx)); +static void p_set_size(UINode* node, lua_State* L, int idx) { + node->setSize(lua::tovec2(L, idx)); } -static void p_set_interactive(UINode* node, int idx) { - node->setInteractive(state->toboolean(idx)); +static void p_set_interactive(UINode* node, lua_State* L, int idx) { + node->setInteractive(lua_toboolean(L, idx)); } -static void p_set_visible(UINode* node, int idx) { - node->setVisible(state->toboolean(idx)); +static void p_set_visible(UINode* node, lua_State* L, int idx) { + node->setVisible(lua_toboolean(L, idx)); } -static void p_set_enabled(UINode* node, int idx) { - node->setEnabled(state->toboolean(idx)); +static void p_set_enabled(UINode* node, lua_State* L, int idx) { + node->setEnabled(lua_toboolean(L, idx)); } -static void p_set_placeholder(UINode* node, int idx) { +static void p_set_placeholder(UINode* node, lua_State* L, int idx) { if (auto box = dynamic_cast(node)) { - box->setPlaceholder(util::str2wstr_utf8(state->requireString(idx))); + box->setPlaceholder(lua::require_wstring(L, idx)); } } -static void p_set_text(UINode* node, int idx) { +static void p_set_text(UINode* node, lua_State* L, int idx) { if (auto label = dynamic_cast(node)) { - label->setText(util::str2wstr_utf8(state->requireString(idx))); + label->setText(lua::require_wstring(L, idx)); } else if (auto button = dynamic_cast(node)) { - button->setText(util::str2wstr_utf8(state->requireString(idx))); + button->setText(lua::require_wstring(L, idx)); } else if (auto box = dynamic_cast(node)) { - box->setText(util::str2wstr_utf8(state->requireString(idx))); + box->setText(lua::require_wstring(L, idx)); } } -static void p_set_caret(UINode* node, int idx) { +static void p_set_caret(UINode* node, lua_State* L, int idx) { if (auto box = dynamic_cast(node)) { - box->setCaret(static_cast(state->tointeger(idx))); + box->setCaret(static_cast(lua_tointeger(L, idx))); } } -static void p_set_editable(UINode* node, int idx) { +static void p_set_editable(UINode* node, lua_State* L, int idx) { if (auto box = dynamic_cast(node)) { - box->setEditable(state->toboolean(idx)); + box->setEditable(lua_toboolean(L, idx)); } } -static void p_set_src(UINode* node, int idx) { +static void p_set_src(UINode* node, lua_State* L, int idx) { if (auto image = dynamic_cast(node)) { - image->setTexture(state->requireString(idx)); + image->setTexture(lua::require_string(L, idx)); } } -static void p_set_value(UINode* node, int idx) { +static void p_set_value(UINode* node, lua_State* L, int idx) { if (auto bar = dynamic_cast(node)) { - bar->setValue(state->tonumber(idx)); + bar->setValue(lua_tonumber(L, idx)); } } -static void p_set_min(UINode* node, int idx) { +static void p_set_min(UINode* node, lua_State* L, int idx) { if (auto bar = dynamic_cast(node)) { - bar->setMin(state->tonumber(idx)); + bar->setMin(lua_tonumber(L, idx)); } } -static void p_set_max(UINode* node, int idx) { +static void p_set_max(UINode* node, lua_State* L, int idx) { if (auto bar = dynamic_cast(node)) { - bar->setMax(state->tonumber(idx)); + bar->setMax(lua_tonumber(L, idx)); } } -static void p_set_step(UINode* node, int idx) { +static void p_set_step(UINode* node, lua_State* L, int idx) { if (auto bar = dynamic_cast(node)) { - bar->setStep(state->tonumber(idx)); + bar->setStep(lua_tonumber(L, idx)); } } -static void p_set_track_width(UINode* node, int idx) { +static void p_set_track_width(UINode* node, lua_State* L, int idx) { if (auto bar = dynamic_cast(node)) { - bar->setTrackWidth(state->tointeger(idx)); + bar->setTrackWidth(lua_tointeger(L, idx)); } } -static void p_set_track_color(UINode* node, int idx) { +static void p_set_track_color(UINode* node, lua_State* L, int idx) { if (auto bar = dynamic_cast(node)) { - bar->setTrackColor(state->tocolor(idx)); + bar->setTrackColor(lua::tocolor(L, idx)); } } -static void p_set_checked(UINode* node, int idx) { +static void p_set_checked(UINode* node, lua_State* L, int idx) { if (auto box = dynamic_cast(node)) { - box->setChecked(state->toboolean(idx)); + box->setChecked(lua_toboolean(L, idx)); } else if (auto box = dynamic_cast(node)) { - box->setChecked(state->toboolean(idx)); + box->setChecked(lua_toboolean(L, idx)); } } -static void p_set_page(UINode* node, int idx) { +static void p_set_page(UINode* node, lua_State* L, int idx) { if (auto menu = dynamic_cast(node)) { - menu->setPage(state->requireString(idx)); + menu->setPage(lua::require_string(L, idx)); } } -static void p_set_inventory(UINode* node, int idx) { +static void p_set_inventory(UINode* node, lua_State* L, int idx) { if (auto view = dynamic_cast(node)) { - auto inventory = level->inventories->get(state->tointeger(idx)); + auto inventory = level->inventories->get(lua_tointeger(L, idx)); if (inventory == nullptr) { view->unbind(); } else { @@ -494,8 +494,8 @@ static void p_set_inventory(UINode* node, int idx) { } } } -static void p_set_focused(const std::shared_ptr &node, int idx) { - if (state->toboolean(idx) && !node->isFocused()) { +static void p_set_focused(const std::shared_ptr &node, lua_State* L, int idx) { + if (lua_toboolean(L, idx) && !node->isFocused()) { scripting::engine->getGUI()->setFocus(node); } else if (node->isFocused()){ node->defocus(); @@ -503,14 +503,14 @@ static void p_set_focused(const std::shared_ptr &node, int idx) { } static int l_gui_setattr(lua_State* L) { - auto docname = state->requireString(1); - auto element = state->requireString(2); - auto attr = state->requireString(3); + auto docname = lua::require_string(L, 1); + auto element = lua::require_string(L, 2); + auto attr = lua::require_string(L, 3); auto docnode = getDocumentNode(L, docname, element); auto node = docnode.node; - static const std::unordered_map> setters { + static const std::unordered_map> setters { {"color", p_set_color}, {"hoverColor", p_set_hover_color}, {"pressedColor", p_set_pressed_color}, @@ -539,20 +539,20 @@ static int l_gui_setattr(lua_State* L) { }; auto func = setters.find(attr); if (func != setters.end()) { - func->second(node.get(), 4); + func->second(node.get(), L, 4); } - static const std::unordered_map,int)>> setters2 { + static const std::unordered_map,lua_State*,int)>> setters2 { {"focused", p_set_focused}, }; auto func2 = setters2.find(attr); if (func2 != setters2.end()) { - func2->second(node, 4); + func2->second(node, L, 4); } return 0; } static int l_gui_get_env(lua_State* L) { - auto name = state->requireString(1); + auto name = lua::require_string(L, 1); auto doc = scripting::engine->getAssets()->getLayout(name); if (doc == nullptr) { throw std::runtime_error("document '"+std::string(name)+"' not found"); @@ -562,9 +562,9 @@ static int l_gui_get_env(lua_State* L) { } static int l_gui_str(lua_State* L) { - auto text = util::str2wstr_utf8(state->requireString(1)); + auto text = lua::require_wstring(L, 1); if (!lua_isnoneornil(L, 2)) { - auto context = util::str2wstr_utf8(state->requireString(2)); + auto context = lua::require_wstring(L, 2); lua_pushstring(L, util::wstr2str_utf8(langs::get(text, context)).c_str()); } else { lua_pushstring(L, util::wstr2str_utf8(langs::get(text)).c_str()); @@ -573,7 +573,7 @@ static int l_gui_str(lua_State* L) { } static int l_gui_reindex(lua_State* L) { - auto name = state->requireString(1); + auto name = lua::require_string(L, 1); auto doc = scripting::engine->getAssets()->getLayout(name); if (doc == nullptr) { throw std::runtime_error("document '"+std::string(name)+"' not found"); diff --git a/src/logic/scripting/lua/libinput.cpp b/src/logic/scripting/lua/libinput.cpp index 309cc26b..5e9413ea 100644 --- a/src/logic/scripting/lua/libinput.cpp +++ b/src/logic/scripting/lua/libinput.cpp @@ -16,29 +16,28 @@ namespace scripting { extern lua::LuaState* state; extern Hud* hud; } - using namespace scripting; static int l_keycode(lua_State* L) { - const char* name = state->requireString(1); + auto name = lua::require_string(L, 1); lua_pushinteger(L, static_cast(input_util::keycode_from(name))); return 1; } static int l_mousecode(lua_State* L) { - const char* name = state->requireString(1); + auto name = lua::require_string(L, 1); lua_pushinteger(L, static_cast(input_util::mousecode_from(name))); return 1; } -static int l_add_callback(lua_State*) { - auto bindname = state->requireString(1); +static int l_add_callback(lua_State* L) { + auto bindname = lua::require_string(L, 1); const auto& bind = Events::bindings.find(bindname); if (bind == Events::bindings.end()) { throw std::runtime_error("unknown binding "+util::quote(bindname)); } - state->pushvalue(2); - runnable actual_callback = state->createRunnable(); + lua_pushvalue(L, 2); + runnable actual_callback = state->createRunnable(L); runnable callback = [=]() { if (!scripting::engine->getGUI()->isFocusCaught()) { actual_callback(); diff --git a/src/logic/scripting/lua/libitem.cpp b/src/logic/scripting/lua/libitem.cpp index c8d6c11a..16d8d877 100644 --- a/src/logic/scripting/lua/libitem.cpp +++ b/src/logic/scripting/lua/libitem.cpp @@ -1,4 +1,5 @@ #include "lua_commons.hpp" +#include "lua_util.hpp" #include "api_lua.hpp" #include "LuaState.hpp" #include "../scripting.hpp" @@ -8,12 +9,11 @@ namespace scripting { extern lua::LuaState* state; } - using namespace scripting; static int l_item_name(lua_State* L) { - auto indices = scripting::content->getIndices(); - lua_Number id = lua_tointeger(L, 1); + auto indices = content->getIndices(); + auto id = lua_tointeger(L, 1); if (static_cast(id) >= indices->countItemDefs()) { return 0; } @@ -23,15 +23,15 @@ static int l_item_name(lua_State* L) { } static int l_item_index(lua_State* L) { - auto name = scripting::state->requireString(1); - lua_pushinteger(L, scripting::content->requireItem(name).rt.id); + auto name = lua::require_string(L, 1); + lua_pushinteger(L, content->requireItem(name).rt.id); return 1; } static int l_item_stack_size(lua_State* L) { - auto indices = scripting::content->getIndices(); - lua_Integer id = lua_tointeger(L, 1); - if (id < 0 || size_t(id) >= indices->countItemDefs()) { + auto indices = content->getIndices(); + auto id = lua_tointeger(L, 1); + if (static_cast(id) >= indices->countItemDefs()) { return 0; } auto def = indices->getItemDef(id); diff --git a/src/logic/scripting/lua/libjson.cpp b/src/logic/scripting/lua/libjson.cpp index 7d3be119..9cb316b4 100644 --- a/src/logic/scripting/lua/libjson.cpp +++ b/src/logic/scripting/lua/libjson.cpp @@ -1,4 +1,5 @@ #include "api_lua.hpp" +#include "lua_util.hpp" #include "lua_commons.hpp" #include "LuaState.hpp" @@ -10,7 +11,7 @@ namespace scripting { } static int l_json_stringify(lua_State* L) { - auto value = scripting::state->tovalue(1); + auto value = lua::tovalue(L, 1); if (auto mapptr = std::get_if(&value)) { bool nice = lua_toboolean(L, 2); @@ -23,9 +24,9 @@ static int l_json_stringify(lua_State* L) { } static int l_json_parse(lua_State* L) { - auto string = scripting::state->requireString(1); + auto string = lua::require_string(L, 1); auto element = json::parse("", string); - scripting::state->pushvalue(element); + lua::pushvalue(L, element); return 1; } diff --git a/src/logic/scripting/lua/libtoml.cpp b/src/logic/scripting/lua/libtoml.cpp index 86411d42..29348eeb 100644 --- a/src/logic/scripting/lua/libtoml.cpp +++ b/src/logic/scripting/lua/libtoml.cpp @@ -1,4 +1,5 @@ #include "api_lua.hpp" +#include "lua_util.hpp" #include "lua_commons.hpp" #include "LuaState.hpp" @@ -11,7 +12,7 @@ namespace scripting { using namespace scripting; static int l_toml_stringify(lua_State* L) { - auto value = state->tovalue(1); + auto value = lua::tovalue(L, 1); if (auto mapptr = std::get_if(&value)) { auto string = toml::stringify(**mapptr); @@ -22,11 +23,11 @@ static int l_toml_stringify(lua_State* L) { } } -static int l_toml_parse(lua_State*) { - auto string = state->requireString(1); +static int l_toml_parse(lua_State* L) { + auto string = lua::require_string(L, 1); auto element = toml::parse("", string); auto value = std::make_unique(element); - state->pushvalue(*value); + lua::pushvalue(L, *value); return 1; } diff --git a/src/logic/scripting/lua/lua_util.cpp b/src/logic/scripting/lua/lua_util.cpp new file mode 100644 index 00000000..24cee222 --- /dev/null +++ b/src/logic/scripting/lua/lua_util.cpp @@ -0,0 +1,117 @@ +#include "lua_util.hpp" + +#include "../../../debug/Logger.hpp" +#include "../../../util/stringutil.hpp" + +static debug::Logger logger("lua"); + +int lua::pushvalue(lua_State* L, const dynamic::Value& value) { + using namespace dynamic; + + if (auto* flag = std::get_if(&value)) { + lua_pushboolean(L, *flag); + } else if (auto* num = std::get_if(&value)) { + lua_pushinteger(L, *num); + } else if (auto* num = std::get_if(&value)) { + lua_pushnumber(L, *num); + } else if (auto* str = std::get_if(&value)) { + lua_pushstring(L, str->c_str()); + } else if (auto listptr = std::get_if(&value)) { + auto list = *listptr; + lua_createtable(L, list->size(), 0); + for (size_t i = 0; i < list->size(); i++) { + pushvalue(L, list->get(i)); + lua_rawseti(L, -2, i+1); + } + } else if (auto mapptr = std::get_if(&value)) { + auto map = *mapptr; + lua_createtable(L, 0, map->size()); + for (auto& entry : map->values) { + pushvalue(L, entry.second); + lua_setfield(L, -2, entry.first.c_str()); + } + } else { + lua_pushnil(L); + } + return 1; +} + +std::wstring lua::require_wstring(lua_State* L, int idx) { + return util::str2wstr_utf8(lua::require_string(L, idx)); +} + +int lua::pushwstring(lua_State* L, const std::wstring& str) { + return lua::pushstring(L, util::wstr2str_utf8(str)); +} + +dynamic::Value lua::tovalue(lua_State* L, int idx) { + using namespace dynamic; + auto type = lua_type(L, idx); + switch (type) { + case LUA_TNIL: + case LUA_TNONE: + return dynamic::NONE; + case LUA_TBOOLEAN: + return lua_toboolean(L, idx) == 1; + case LUA_TNUMBER: { + auto number = lua_tonumber(L, idx); + auto integer = lua_tointeger(L, idx); + if (number == (lua_Number)integer) { + return integer; + } else { + return number; + } + } + case LUA_TSTRING: + return std::string(lua_tostring(L, idx)); + case LUA_TTABLE: { + int len = lua_objlen(L, idx); + if (len) { + // array + auto list = create_list(); + for (int i = 1; i <= len; i++) { + lua_rawgeti(L, idx, i); + list->put(tovalue(L, -1)); + lua_pop(L, 1); + } + return list; + } else { + // table + auto map = create_map(); + lua_pushvalue(L, idx); + lua_pushnil(L); + while (lua_next(L, -2)) { + lua_pushvalue(L, -2); + auto key = lua_tostring(L, -1); + map->put(key, tovalue(L, -2)); + lua_pop(L, 2); + } + lua_pop(L, 1); + return map; + } + } + default: + throw std::runtime_error( + "lua type "+std::string(luaL_typename(L, type))+" is not supported" + ); + } +} + +void lua::logError(const std::string& text) { + logger.error() << text; +} + +int lua::call(lua_State* L, int argc, int nresults) { + if (lua_pcall(L, argc, nresults, 0)) { + throw luaerror(lua_tostring(L, -1)); + } + return 1; +} + +int lua::callNoThrow(lua_State* L, int argc) { + if (lua_pcall(L, argc, LUA_MULTRET, 0)) { + logError(lua_tostring(L, -1)); + return 0; + } + return 1; +} diff --git a/src/logic/scripting/lua/lua_util.hpp b/src/logic/scripting/lua/lua_util.hpp index 799bd3d4..e9833c79 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -14,6 +14,23 @@ #include namespace lua { + // function wrappers with number of pushed values as return value + + inline int pushnil(lua_State* L) { + lua_pushnil(L); + return 1; + } + + inline int pushinteger(lua_State* L, lua_Integer x) { + lua_pushinteger(L, x); + return 1; + } + + inline int pushnumber(lua_State* L, lua_Number x) { + lua_pushnumber(L, x); + return 1; + } + inline int pushivec3(lua_State* L, lua_Integer x, lua_Integer y, lua_Integer z) { lua_pushinteger(L, x); lua_pushinteger(L, y); @@ -84,7 +101,6 @@ namespace lua { lua_rawseti(L, -2, 4); return 1; } - inline int pushcolor_arr(lua_State* L, glm::vec4 vec) { lua_createtable(L, 4, 0); lua_getglobal(L, "color_mt"); @@ -100,6 +116,33 @@ namespace lua { lua_rawseti(L, -2, 4); return 1; } + inline int pushcfunction(lua_State* L, lua_CFunction func) { + lua_pushcfunction(L, func); + return 1; + } + inline int pushstring(lua_State* L, const std::string& str) { + lua_pushstring(L, str.c_str()); + return 1; + } + + int pushwstring(lua_State* L, const std::wstring& str); + + inline int pushboolean(lua_State* L, bool value) { + lua_pushboolean(L, value); + return 1; + } + inline int pushglobals(lua_State* L) { + lua_pushvalue(L, LUA_GLOBALSINDEX); + return 1; + } + inline int pushvalue(lua_State* L, int idx) { + lua_pushvalue(L, idx); + return 1; + } + + inline bool toboolean(lua_State* L, int idx) { + return lua_toboolean(L, idx); + } inline glm::vec2 tovec2(lua_State* L, int idx) { lua_pushvalue(L, idx); @@ -130,6 +173,61 @@ namespace lua { lua_pop(L, 1); return glm::vec4(r/255, g/255, b/255, a/255); } + + inline const char* require_string(lua_State* L, int idx) { + if (!lua_isstring(L, idx)) { + throw luaerror("string expected at "+std::to_string(idx)); + } + return lua_tostring(L, idx); + } + + std::wstring require_wstring(lua_State*, int idx); + int pushvalue(lua_State*, const dynamic::Value& value); + dynamic::Value tovalue(lua_State*, int idx); + + inline void pop(lua_State* L, int n=1) { + lua_pop(L, n); + } + + inline bool getfield(lua_State* L, const std::string& name, int idx=-1) { + lua_getfield(L, idx, name.c_str()); + if (lua_isnil(L, -1)) { + lua_pop(L, -1); + return false; + } + return true; + } + + inline void setfield(lua_State* L, const std::string& name, int idx=-2) { + lua_setfield(L, idx, name.c_str()); + } + + inline bool getglobal(lua_State* L, const std::string& name) { + lua_getglobal(L, name.c_str()); + if (lua_isnil(L, lua_gettop(L))) { + lua_pop(L, lua_gettop(L)); + return false; + } + return true; + } + + inline bool hasglobal(lua_State* L, const std::string& name) { + lua_getglobal(L, name.c_str()); + if (lua_isnil(L, lua_gettop(L))) { + lua_pop(L, lua_gettop(L)); + return false; + } + lua_pop(L, lua_gettop(L)); + return true; + } + + inline void setglobal(lua_State* L, const std::string& name) { + lua_setglobal(L, name.c_str()); + } + + void logError(const std::string& text); + int call(lua_State*, int argc, int nresults=-1); + int callNoThrow(lua_State*, int argc); } #endif // LOGIC_SCRIPTING_LUA_UTIL_HPP_ diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index 493b6f91..18185ac7 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -17,6 +17,7 @@ #include "../../voxels/Block.hpp" #include "../../world/Level.hpp" #include "lua/LuaState.hpp" +#include "lua/lua_util.hpp" #include #include @@ -37,12 +38,12 @@ const ContentIndices* scripting::indices = nullptr; BlocksController* scripting::blocks = nullptr; LevelController* scripting::controller = nullptr; -void load_script(const fs::path& name) { +static void load_script(const fs::path& name) { auto paths = scripting::engine->getPaths(); fs::path file = paths->getResources()/fs::path("scripts")/name; std::string src = files::read_string(file); - state->execute(0, src, file.u8string()); + state->execute(state->getMainThread(), 0, src, file.u8string()); } void scripting::initialize(Engine* engine) { @@ -59,46 +60,49 @@ scriptenv scripting::get_root_environment() { } scriptenv scripting::create_pack_environment(const ContentPack& pack) { - int id = state->createEnvironment(0); - state->pushenv(id); - state->pushvalue(-1); - state->setfield("PACK_ENV"); - state->pushstring(pack.id); - state->setfield("PACK_ID"); - state->pop(); + auto L = state->getMainThread(); + int id = state->createEnvironment(L, 0); + state->pushenv(L, id); + lua::pushvalue(L, -1); + lua::setfield(L, "PACK_ENV"); + lua::pushstring(L, pack.id); + lua::setfield(L, "PACK_ID"); + lua::pop(L); return std::shared_ptr(new int(id), [=](int* id) { - state->removeEnvironment(*id); + state->removeEnvironment(L, *id); delete id; }); } scriptenv scripting::create_doc_environment(const scriptenv& parent, const std::string& name) { - int id = state->createEnvironment(*parent); - state->pushenv(id); - state->pushvalue(-1); - state->setfield("DOC_ENV"); - state->pushstring(name.c_str()); - state->setfield("DOC_NAME"); + auto L = state->getMainThread(); + int id = state->createEnvironment(L, *parent); + state->pushenv(L, id); + lua_pushvalue(L, -1); + lua::setfield(L, "DOC_ENV"); + lua::pushstring(L, name); + lua::setfield(L, "DOC_NAME"); - if (state->getglobal("Document")) { - if (state->getfield("new")) { - state->pushstring(name.c_str()); - if (state->callNoThrow(1)) { - state->setfield("document", -3); + if (lua::getglobal(L, "Document")) { + if (lua::getfield(L, "new")) { + lua::pushstring(L, name); + if (lua::callNoThrow(L, 1)) { + lua::setfield(L, "document", -3); } } - state->pop(); + lua::pop(L); } - state->pop(); + lua::pop(L); return std::shared_ptr(new int(id), [=](int* id) { - state->removeEnvironment(*id); + state->removeEnvironment(L, *id); delete id; }); } void scripting::process_post_runnables() { - if (state->getglobal("__process_post_runnables")) { - state->callNoThrow(0); + auto L = state->getMainThread(); + if (lua::getglobal(L, "__process_post_runnables")) { + lua::callNoThrow(L, 0); } } @@ -110,38 +114,42 @@ void scripting::on_world_load(LevelController* controller) { scripting::controller = controller; load_script("world.lua"); + auto L = state->getMainThread(); for (auto& pack : scripting::engine->getContentPacks()) { - state->emit_event(pack.id + ".worldopen"); + state->emitEvent(L, pack.id + ".worldopen"); } } void scripting::on_world_tick() { + auto L = state->getMainThread(); for (auto& pack : scripting::engine->getContentPacks()) { - state->emit_event(pack.id + ".worldtick"); + state->emitEvent(L, pack.id + ".worldtick"); } } void scripting::on_world_save() { + auto L = state->getMainThread(); for (auto& pack : scripting::engine->getContentPacks()) { - state->emit_event(pack.id + ".worldsave"); + state->emitEvent(L, pack.id + ".worldsave"); } } void scripting::on_world_quit() { + auto L = state->getMainThread(); for (auto& pack : scripting::engine->getContentPacks()) { - state->emit_event(pack.id + ".worldquit"); + state->emitEvent(L, pack.id + ".worldquit"); } - state->getglobal("pack"); + lua::getglobal(L, "pack"); for (auto& pack : scripting::engine->getContentPacks()) { - state->getfield("unload"); - state->pushstring(pack.id); - state->callNoThrow(1); + lua::getfield(L, "unload"); + lua::pushstring(L, pack.id); + lua::callNoThrow(L, 1); } - state->pop(); + lua::pop(L); - if (state->getglobal("__scripts_cleanup")) { - state->callNoThrow(0); + if (lua::getglobal(L, "__scripts_cleanup")) { + lua::callNoThrow(L, 0); } scripting::level = nullptr; scripting::content = nullptr; @@ -152,77 +160,73 @@ void scripting::on_world_quit() { void scripting::on_blocks_tick(const Block* block, int tps) { std::string name = block->name + ".blockstick"; - state->emit_event(name, [tps] (lua::LuaState* state) { - state->pushinteger(tps); - return 1; + state->emitEvent(state->getMainThread(), name, [tps] (lua_State* 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"; - state->emit_event(name, [x, y, z] (lua::LuaState* state) { - state->pushivec3(x, y, z); - return 3; + state->emitEvent(state->getMainThread(), name, [x, y, z] (lua_State* L) { + return lua::pushivec3(L, x, y, z); }); } void scripting::random_update_block(const Block* block, int x, int y, int z) { std::string name = block->name + ".randupdate"; - state->emit_event(name, [x, y, z] (lua::LuaState* state) { - state->pushivec3(x, y, z); - return 3; + state->emitEvent(state->getMainThread(), name, [x, y, z] (lua_State* L) { + return lua::pushivec3(L, x, y, z); }); } void scripting::on_block_placed(Player* player, const Block* block, int x, int y, int z) { std::string name = block->name + ".placed"; - state->emit_event(name, [x, y, z, player] (lua::LuaState* state) { - state->pushivec3(x, y, z); - state->pushinteger(player->getId()); - return 4; + state->emitEvent(state->getMainThread(), name, [x, y, z, player] (lua_State* L) { + lua::pushivec3(L, x, y, z); + lua::pushinteger(L, player->getId()); + return 4; }); } void scripting::on_block_broken(Player* player, const Block* block, int x, int y, int z) { std::string name = block->name + ".broken"; - state->emit_event(name, [x, y, z, player] (lua::LuaState* state) { - state->pushivec3(x, y, z); - state->pushinteger(player->getId()); + state->emitEvent(state->getMainThread(), name, [x, y, z, player] (lua_State* L) { + lua::pushivec3(L, x, y, z); + lua::pushinteger(L, player->getId()); return 4; }); } bool scripting::on_block_interact(Player* player, const Block* block, glm::ivec3 pos) { std::string name = block->name + ".interact"; - return state->emit_event(name, [pos, player] (lua::LuaState* state) { - state->pushivec3(pos.x, pos.y, pos.z); - state->pushinteger(player->getId()); + return state->emitEvent(state->getMainThread(), name, [pos, player] (lua_State* L) { + lua::pushivec3(L, pos.x, pos.y, pos.z); + lua::pushinteger(L, player->getId()); return 4; }); } bool scripting::on_item_use(Player* player, const ItemDef* item) { std::string name = item->name + ".use"; - return state->emit_event(name, [player] (lua::LuaState* state) { - state->pushinteger(player->getId()); - return 1; + return state->emitEvent(state->getMainThread(), name, [player] (lua_State* L) { + return lua::pushinteger(L, player->getId()); }); } bool scripting::on_item_use_on_block(Player* player, const ItemDef* item, int x, int y, int z) { std::string name = item->name + ".useon"; - return state->emit_event(name, [x, y, z, player] (lua::LuaState* state) { - state->pushivec3(x, y, z); - state->pushinteger(player->getId()); + return state->emitEvent(state->getMainThread(), name, [x, y, z, player] (lua_State* L) { + lua::pushivec3(L, x, y, z); + lua::pushinteger(L, player->getId()); return 4; }); } bool scripting::on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z) { std::string name = item->name + ".blockbreakby"; - return state->emit_event(name, [x, y, z, player] (lua::LuaState* state) { - state->pushivec3(x, y, z); - state->pushinteger(player->getId()); + return state->emitEvent(state->getMainThread(), name, [x, y, z, player] (lua_State* L) { + lua::pushivec3(L, x, y, z); + lua::pushinteger(L, player->getId()); return 4; }); } @@ -233,9 +237,9 @@ void scripting::on_ui_open( ) { auto argsptr = std::make_shared>(std::move(args)); std::string name = layout->getId() + ".open"; - state->emit_event(name, [=] (lua::LuaState* state) { + state->emitEvent(state->getMainThread(), name, [=] (lua_State* L) { for (const auto& value : *argsptr) { - state->pushvalue(value); + lua::pushvalue(L, value); } return argsptr->size(); }); @@ -243,37 +247,37 @@ void scripting::on_ui_open( void scripting::on_ui_progress(UiDocument* layout, int workDone, int workTotal) { std::string name = layout->getId() + ".progress"; - state->emit_event(name, [=] (lua::LuaState* state) { - state->pushinteger(workDone); - state->pushinteger(workTotal); + state->emitEvent(state->getMainThread(), name, [=] (lua_State* L) { + lua::pushinteger(L, workDone); + lua::pushinteger(L, workTotal); return 2; }); } void scripting::on_ui_close(UiDocument* layout, Inventory* inventory) { std::string name = layout->getId() + ".close"; - state->emit_event(name, [inventory] (lua::LuaState* state) { - state->pushinteger(inventory == nullptr ? 0 : inventory->getId()); - return 1; + state->emitEvent(state->getMainThread(), name, [inventory] (lua_State* L) { + return lua::pushinteger(L, inventory ? inventory->getId() : 0); }); } bool scripting::register_event(int env, const std::string& name, const std::string& id) { - if (state->pushenv(env) == 0) { - state->pushglobals(); + auto L = state->getMainThread(); + if (state->pushenv(L, env) == 0) { + lua::pushglobals(L); } - if (state->getfield(name)) { - state->pop(); - state->getglobal("events"); - state->getfield("on"); - state->pushstring(id); - state->getfield(name, -4); - state->callNoThrow(2); - state->pop(); + if (lua::getfield(L, name)) { + lua::pop(L); + lua::getglobal(L, "events"); + lua::getfield(L, "on"); + lua::pushstring(L, id); + lua::getfield(L, name, -4); + lua::callNoThrow(L, 2); + lua::pop(L); // remove previous name - state->pushnil(); - state->setfield(name); + lua::pushnil(L); + lua::setfield(L, name); return true; } return false; @@ -283,7 +287,7 @@ void scripting::load_block_script(const scriptenv& senv, const std::string& pref int env = *senv; std::string src = files::read_string(file); logger.info() << "script (block) " << file.u8string(); - state->execute(env, src, file.u8string()); + state->execute(state->getMainThread(), env, src, file.u8string()); funcsset.init = register_event(env, "init", prefix+".init"); funcsset.update = register_event(env, "on_update", prefix+".update"); funcsset.randupdate = register_event(env, "on_random_update", prefix+".randupdate"); @@ -297,7 +301,7 @@ void scripting::load_item_script(const scriptenv& senv, const std::string& prefi int env = *senv; std::string src = files::read_string(file); logger.info() << "script (item) " << file.u8string(); - state->execute(env, src, file.u8string()); + state->execute(state->getMainThread(), env, src, file.u8string()); funcsset.init = register_event(env, "init", prefix+".init"); funcsset.on_use = register_event(env, "on_use", prefix+".use"); @@ -311,8 +315,7 @@ void scripting::load_world_script(const scriptenv& senv, const std::string& pref std::string src = files::read_string(file); logger.info() << "loading world script for " << prefix; - state->loadbuffer(env, src, file.u8string()); - state->callNoThrow(0); + state->execute(state->getMainThread(), env, src, file.u8string()); register_event(env, "init", prefix+".init"); register_event(env, "on_world_open", prefix+".worldopen"); @@ -327,8 +330,7 @@ void scripting::load_layout_script(const scriptenv& senv, const std::string& pre std::string src = files::read_string(file); logger.info() << "loading script " << file.u8string(); - state->loadbuffer(env, src, file.u8string()); - state->callNoThrow(0); + state->execute(state->getMainThread(), env, src, file.u8string()); script.onopen = register_event(env, "on_open", prefix+".open"); script.onprogress = register_event(env, "on_progress", prefix+".progress"); script.onclose = register_event(env, "on_close", prefix+".close"); diff --git a/src/logic/scripting/scripting_functional.cpp b/src/logic/scripting/scripting_functional.cpp index 644b2b95..c381dc35 100644 --- a/src/logic/scripting/scripting_functional.cpp +++ b/src/logic/scripting/scripting_functional.cpp @@ -1,5 +1,6 @@ #include "scripting_functional.hpp" +#include "lua/lua_util.hpp" #include "lua/LuaState.hpp" #include "../../debug/Logger.hpp" #include "../../util/stringutil.hpp" @@ -17,26 +18,30 @@ runnable scripting::create_runnable( const std::string& src, const std::string& file ) { + auto L = state->getMainThread(); try { - state->loadbuffer(*env, src, file); - return state->createRunnable(); + state->loadbuffer(L, *env, src, file); + return state->createRunnable(L); } catch (const lua::luaerror& err) { logger.error() << err.what(); return [](){}; } } -static bool processCallback( +static lua_State* processCallback( const scriptenv& env, const std::string& src, const std::string& file ) { + auto L = state->getMainThread(); try { - return state->eval(*env, src, file) != 0; + if (state->eval(L, *env, src, file) != 0) { + return L; + } } catch (lua::luaerror& err) { logger.error() << err.what(); - return false; } + return nullptr; } wstringconsumer scripting::create_wstring_consumer( @@ -45,9 +50,9 @@ wstringconsumer scripting::create_wstring_consumer( const std::string& file ) { return [=](const std::wstring& x){ - if (processCallback(env, src, file)) { - state->pushstring(util::wstr2str_utf8(x)); - state->callNoThrow(1); + if (auto L = processCallback(env, src, file)) { + lua::pushwstring(L, x); + lua::callNoThrow(L, 1); } }; } @@ -58,12 +63,12 @@ wstringsupplier scripting::create_wstring_supplier( const std::string& file ) { return [=](){ - if (processCallback(env, src, file)) { - if (state->isfunction(-1)) { - state->callNoThrow(0); + if (auto L = processCallback(env, src, file)) { + if (lua_isfunction(L, -1)) { + lua::callNoThrow(L, 0); } - auto str = state->tostring(-1); state->pop(); - return util::str2wstr_utf8(str); + auto str = lua::require_wstring(L, -1); lua_pop(L, 1); + return str; } return std::wstring(L""); }; @@ -75,10 +80,10 @@ wstringchecker scripting::create_wstring_validator( const std::string& file ) { return [=](const std::wstring& x){ - if (processCallback(env, src, file)) { - state->pushstring(util::wstr2str_utf8(x)); - if (state->callNoThrow(1)) - return state->toboolean(-1); + if (auto L = processCallback(env, src, file)) { + lua::pushwstring(L, x); + if (lua::callNoThrow(L, 1)) + return lua::toboolean(L, -1); } return false; }; @@ -90,9 +95,9 @@ boolconsumer scripting::create_bool_consumer( const std::string& file ) { return [=](bool x){ - if (processCallback(env, src, file)) { - state->pushboolean(x); - state->callNoThrow(1); + if (auto L = processCallback(env, src, file)) { + lua::pushboolean(L, x); + lua::callNoThrow(L, 1); } }; } @@ -103,11 +108,11 @@ boolsupplier scripting::create_bool_supplier( const std::string& file ) { return [=](){ - if (processCallback(env, src, file)) { - if (state->isfunction(-1)) { - state->callNoThrow(0); + if (auto L = processCallback(env, src, file)) { + if (lua_isfunction(L, -1)) { + lua::callNoThrow(L, 0); } - bool x = state->toboolean(-1); state->pop(); + bool x = lua::toboolean(L,-1); lua_pop(L, 1); return x; } return false; @@ -120,9 +125,9 @@ doubleconsumer scripting::create_number_consumer( const std::string& file ) { return [=](double x){ - if (processCallback(env, src, file)) { - state->pushnumber(x); - state->callNoThrow(1); + if (auto L = processCallback(env, src, file)) { + lua::pushnumber(L, x); + lua::callNoThrow(L, 1); } }; } @@ -133,12 +138,12 @@ doublesupplier scripting::create_number_supplier( const std::string& file ) { return [=](){ - if (processCallback(env, src, file)) { - if (state->isfunction(-1)) { - state->callNoThrow(0); + if (auto L = processCallback(env, src, file)) { + if (lua_isfunction(L, -1)) { + lua::callNoThrow(L, 0); } - auto x = state->tonumber(-1); - state->pop(); + auto x = lua_tonumber(L, -1); + lua::pop(L); return x; } return 0.0; @@ -150,12 +155,12 @@ int_array_consumer scripting::create_int_array_consumer( const std::string& src, const std::string& file ) { - return [=](const int arr[], size_t len){ - if (processCallback(env, src, file)) { + return [=](const int arr[], size_t len) { + if (auto L = processCallback(env, src, file)) { for (uint i = 0; i < len; i++) { - state->pushinteger(arr[i]); + lua::pushinteger(L, arr[i]); } - state->callNoThrow(len); + lua::callNoThrow(L, len); } }; } @@ -165,13 +170,13 @@ vec2supplier scripting::create_vec2_supplier( const std::string& src, const std::string& file ) { - return [=](){ - if (processCallback(env, src, file)) { - if (state->isfunction(-1)) { - state->callNoThrow(0); + return [=]() { + if (auto L = processCallback(env, src, file)) { + if (lua_isfunction(L, -1)) { + lua::callNoThrow(L, 0); } - auto y = state->tonumber(-1); state->pop(); - auto x = state->tonumber(-1); state->pop(); + auto y = lua_tonumber(L, -1); lua::pop(L); + auto x = lua_tonumber(L, -1); lua::pop(L); return glm::vec2(x, y); } return glm::vec2(0, 0); diff --git a/src/logic/scripting/scripting_hud.cpp b/src/logic/scripting/scripting_hud.cpp index 2c78973b..c1853dad 100644 --- a/src/logic/scripting/scripting_hud.cpp +++ b/src/logic/scripting/scripting_hud.cpp @@ -1,6 +1,7 @@ #include "scripting_hud.hpp" #include "scripting.hpp" +#include "lua/lua_util.hpp" #include "lua/api_lua.hpp" #include "lua/LuaState.hpp" @@ -13,6 +14,7 @@ namespace scripting { extern lua::LuaState* state; } +using namespace scripting; static debug::Logger logger("scripting-hud"); @@ -20,21 +22,21 @@ Hud* scripting::hud = nullptr; void scripting::on_frontend_init(Hud* hud) { scripting::hud = hud; - scripting::state->openlib("hud", hudlib); + state->openlib(state->getMainThread(), "hud", hudlib); for (auto& pack : scripting::engine->getContentPacks()) { - state->emit_event(pack.id + ".hudopen", [&] (lua::LuaState* state) { - state->pushinteger(hud->getPlayer()->getId()); - return 1; + state->emitEvent(state->getMainThread(), pack.id + ".hudopen", + [&] (lua_State* L) { + return lua::pushinteger(L, hud->getPlayer()->getId()); }); } } void scripting::on_frontend_close() { for (auto& pack : scripting::engine->getContentPacks()) { - state->emit_event(pack.id + ".hudclose", [&] (lua::LuaState* state) { - state->pushinteger(hud->getPlayer()->getId()); - return 1; + state->emitEvent(state->getMainThread(), pack.id + ".hudclose", + [&] (lua_State* L) { + return lua::pushinteger(L, hud->getPlayer()->getId()); }); } scripting::hud = nullptr; @@ -45,8 +47,9 @@ void scripting::load_hud_script(const scriptenv& senv, const std::string& packid std::string src = files::read_string(file); logger.info() << "loading script " << file.u8string(); - state->loadbuffer(env, src, file.u8string()); - state->callNoThrow(0); + auto L = state->getMainThread(); + state->loadbuffer(L, env, src, file.u8string()); + lua::callNoThrow(L, 0); register_event(env, "init", packid+".init"); register_event(env, "on_hud_open", packid+".hudopen"); From 0647bc6f904971214c81e252d19d141734966996 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 11 Jun 2024 03:37:35 +0300 Subject: [PATCH 2/6] refactor: 'lua' namespace expansion --- src/logic/scripting/lua/LuaState.cpp | 170 +------------------ src/logic/scripting/lua/LuaState.hpp | 21 --- src/logic/scripting/lua/api_lua.hpp | 2 +- src/logic/scripting/lua/libaudio.cpp | 51 +++--- src/logic/scripting/lua/libblock.cpp | 48 +++--- src/logic/scripting/lua/libconsole.cpp | 22 +-- src/logic/scripting/lua/libcore.cpp | 53 +++--- src/logic/scripting/lua/libfile.cpp | 44 ++--- src/logic/scripting/lua/libgui.cpp | 89 +++++----- src/logic/scripting/lua/libhud.cpp | 23 ++- src/logic/scripting/lua/libinput.cpp | 17 +- src/logic/scripting/lua/libinventory.cpp | 113 ++++++------ src/logic/scripting/lua/libitem.cpp | 32 ++-- src/logic/scripting/lua/libjson.cpp | 19 +-- src/logic/scripting/lua/libpack.cpp | 75 ++++---- src/logic/scripting/lua/libplayer.cpp | 102 +++++------ src/logic/scripting/lua/libtime.cpp | 13 +- src/logic/scripting/lua/libtoml.cpp | 16 +- src/logic/scripting/lua/libworld.cpp | 47 +++-- src/logic/scripting/lua/lua_commons.cpp | 9 + src/logic/scripting/lua/lua_commons.hpp | 29 ++-- src/logic/scripting/lua/lua_util.cpp | 120 ++++++++++++- src/logic/scripting/lua/lua_util.hpp | 149 ++++++++++++---- src/logic/scripting/scripting.cpp | 37 ++-- src/logic/scripting/scripting_functional.cpp | 27 ++- src/logic/scripting/scripting_hud.cpp | 8 +- 26 files changed, 613 insertions(+), 723 deletions(-) create mode 100644 src/logic/scripting/lua/lua_commons.cpp diff --git a/src/logic/scripting/lua/LuaState.cpp b/src/logic/scripting/lua/LuaState.cpp index 0d70a378..3321fe7e 100644 --- a/src/logic/scripting/lua/LuaState.cpp +++ b/src/logic/scripting/lua/LuaState.cpp @@ -8,16 +8,10 @@ #include #include -inline std::string LAMBDAS_TABLE = "$L"; - static debug::Logger logger("lua-state"); using namespace lua; -namespace scripting { - extern LuaState* state; -} - luaerror::luaerror(const std::string& message) : std::runtime_error(message) { } @@ -63,16 +57,12 @@ LuaState::LuaState() { createLibs(L); pushglobals(L); - setglobal(L, envName(0)); + setglobal(L, env_name(0)); lua_createtable(L, 0, 0); setglobal(L, LAMBDAS_TABLE); } -std::string LuaState::envName(int env) { - return "_ENV"+util::mangleid(env); -} - LuaState::~LuaState() { lua_close(mainThread); } @@ -82,26 +72,6 @@ void LuaState::addfunc(lua_State* L, const std::string& name, lua_CFunction func lua_setglobal(L, name.c_str()); } -bool LuaState::rename(lua_State* L, const std::string& from, const std::string& to) { - const char* src = from.c_str(); - lua_getglobal(L, src); - if (lua_isnil(L, lua_gettop(L))) { - lua_pop(L, lua_gettop(L)); - return false; - } - lua_setglobal(L, to.c_str()); - - // remove previous - lua_pushnil(L); - lua_setglobal(L, src); - return true; -} - -void LuaState::remove(lua_State* L, const std::string& name) { - lua_pushnil(L); - lua_setglobal(L, name.c_str()); -} - void LuaState::createLibs(lua_State* L) { openlib(L, "audio", audiolib); openlib(L, "block", blocklib); @@ -119,34 +89,7 @@ void LuaState::createLibs(lua_State* L) { openlib(L, "toml", tomllib); openlib(L, "world", worldlib); - addfunc(L, "print", lua_wrap_errors); -} - -void LuaState::loadbuffer(lua_State* L, int env, const std::string& src, const std::string& file) { - if (luaL_loadbuffer(L, src.c_str(), src.length(), file.c_str())) { - throw luaerror(lua_tostring(L, -1)); - } - if (env && getglobal(L, envName(env))) { - lua_setfenv(L, -2); - } -} - -int LuaState::eval(lua_State* L, int env, const std::string& src, const std::string& file) { - auto srcText = "return "+src; - loadbuffer(L, env, srcText, file); - return call(L, 0); -} - -int LuaState::execute(lua_State* L, int env, const std::string& src, const std::string& file) { - loadbuffer(L, env, src, file); - return callNoThrow(L, 0); -} - -int LuaState::pushenv(lua_State* L, int env) { - if (getglobal(L, envName(env))) { - return 1; - } - return 0; + addfunc(L, "print", lua::wrap); } void LuaState::openlib(lua_State* L, const std::string& name, const luaL_Reg* libfuncs) { @@ -155,117 +98,16 @@ void LuaState::openlib(lua_State* L, const std::string& name, const luaL_Reg* li lua_setglobal(L, name.c_str()); } -std::shared_ptr LuaState::createLambdaHandler(lua_State* L) { - auto ptr = reinterpret_cast(lua_topointer(L, -1)); - auto name = util::mangleid(ptr); - lua_getglobal(L, LAMBDAS_TABLE.c_str()); - lua_pushvalue(L, -2); - lua_setfield(L, -2, name.c_str()); - lua_pop(L, 2); - - return std::shared_ptr(new std::string(name), [=](auto* name) { - lua_getglobal(L, LAMBDAS_TABLE.c_str()); - lua_pushnil(L); - lua_setfield(L, -2, name->c_str()); - lua_pop(L, 1); - delete name; - }); -} - -runnable LuaState::createRunnable(lua_State* L) { - auto funcptr = createLambdaHandler(L); - return [=]() { - lua_getglobal(L, LAMBDAS_TABLE.c_str()); - lua_getfield(L, -1, funcptr->c_str()); - callNoThrow(L, 0); - }; -} - -scripting::common_func LuaState::createLambda(lua_State* L) { - auto funcptr = createLambdaHandler(L); - return [=](const std::vector& args) { - lua_getglobal(L, LAMBDAS_TABLE.c_str()); - lua_getfield(L, -1, funcptr->c_str()); - for (const auto& arg : args) { - pushvalue(L, arg); - } - if (call(L, args.size(), 1)) { - auto result = tovalue(L, -1); - lua_pop(L, 1); - return result; - } - return dynamic::Value(dynamic::NONE); - }; -} - -int LuaState::createEnvironment(lua_State* L, int parent) { - int id = nextEnvironment++; - - // local env = {} - lua_createtable(L, 0, 1); - - // setmetatable(env, {__index=_G}) - lua_createtable(L, 0, 1); - if (parent == 0) { - lua_pushvalue(L, LUA_GLOBALSINDEX); - } else { - if (pushenv(L, parent) == 0) { - lua_pushvalue(L, LUA_GLOBALSINDEX); - } - } - lua_setfield(L, -2, "__index"); - lua_setmetatable(L, -2); - - // envname = env - setglobal(L, envName(id)); - return id; -} - - -void LuaState::removeEnvironment(lua_State* L, int id) { - if (id == 0) { - return; - } - lua_pushnil(L); - setglobal(L, envName(id)); -} - bool LuaState::emitEvent(lua_State* L, const std::string &name, std::function args) { getglobal(L, "events"); getfield(L, "emit"); - lua::pushstring(L, name); - callNoThrow(L, args(L) + 1); - bool result = lua_toboolean(L, -1); - lua_pop(L, 2); + pushstring(L, name); + call_nothrow(L, args(L) + 1); + bool result = toboolean(L, -1); + pop(L, 2); return result; } - -void LuaState::dumpStack(lua_State* L) { - int top = lua_gettop(L); - for (int i = 1; i <= top; i++) { - std::cout << std::setw(3) << i << std::setw(20) << luaL_typename(L, i) << std::setw(30); - switch (lua_type(L, i)) { - case LUA_TNUMBER: - std::cout << lua_tonumber(L, i); - break; - case LUA_TSTRING: - std::cout << lua_tostring(L, i); - break; - case LUA_TBOOLEAN: - std::cout << (lua_toboolean(L, i) ? "true" : "false"); - break; - case LUA_TNIL: - std::cout << "nil"; - break; - default: - std::cout << lua_topointer(L, i); - break; - } - std::cout << std::endl; - } -} - lua_State* LuaState::getMainThread() const { return mainThread; } diff --git a/src/logic/scripting/lua/LuaState.hpp b/src/logic/scripting/lua/LuaState.hpp index f8a925e5..2d724373 100644 --- a/src/logic/scripting/lua/LuaState.hpp +++ b/src/logic/scripting/lua/LuaState.hpp @@ -11,40 +11,19 @@ #include namespace lua { - class luaerror : public std::runtime_error { - public: - luaerror(const std::string& message); - }; - class LuaState { lua_State* mainThread; - int nextEnvironment = 1; - void removeLibFuncs(lua_State*, const char* libname, const char* funcs[]); void createLibs(lua_State* L); - - std::shared_ptr createLambdaHandler(lua_State*); public: LuaState(); ~LuaState(); - static std::string envName(int env); - void loadbuffer(lua_State*, int env, const std::string& src, const std::string& file); - int pushenv(lua_State*, int env); - int execute(lua_State*, int env, const std::string& src, const std::string& file=""); - int eval(lua_State*, int env, const std::string& src, const std::string& file=""); void openlib(lua_State*, const std::string& name, const luaL_Reg* libfuncs); void addfunc(lua_State*, const std::string& name, lua_CFunction func); - bool rename(lua_State*, const std::string& from, const std::string& to); - void remove(lua_State*, const std::string& name);; - runnable createRunnable(lua_State*); - scripting::common_func createLambda(lua_State*); - int createEnvironment(lua_State*, int parent); - void removeEnvironment(lua_State*, int id); bool emitEvent(lua_State*, const std::string& name, std::function args=[](auto*){return 0;}); - void dumpStack(lua_State*); lua_State* getMainThread() const; }; } diff --git a/src/logic/scripting/lua/api_lua.hpp b/src/logic/scripting/lua/api_lua.hpp index 69526057..c1ff9ebf 100644 --- a/src/logic/scripting/lua/api_lua.hpp +++ b/src/logic/scripting/lua/api_lua.hpp @@ -1,7 +1,7 @@ #ifndef LOGIC_SCRIPTING_API_LUA_HPP_ #define LOGIC_SCRIPTING_API_LUA_HPP_ -#include "lua_commons.hpp" +#include "lua_util.hpp" #include diff --git a/src/logic/scripting/lua/libaudio.cpp b/src/logic/scripting/lua/libaudio.cpp index 833b00ae..9db10d3a 100644 --- a/src/logic/scripting/lua/libaudio.cpp +++ b/src/logic/scripting/lua/libaudio.cpp @@ -1,10 +1,7 @@ #include "api_lua.hpp" -#include "lua_commons.hpp" -#include "lua_util.hpp" #include "../../../audio/audio.hpp" #include "../../../engine.hpp" -#include "../scripting.hpp" inline const char* DEFAULT_CHANNEL = "regular"; @@ -386,29 +383,29 @@ static int l_audio_count_streams(lua_State* L) { } const luaL_Reg audiolib [] = { - {"play_sound", lua_wrap_errors}, - {"play_sound_2d", lua_wrap_errors}, - {"play_stream", lua_wrap_errors}, - {"play_stream_2d", lua_wrap_errors}, - {"stop", lua_wrap_errors}, - {"pause", lua_wrap_errors}, - {"resume", lua_wrap_errors}, - {"set_loop", lua_wrap_errors}, - {"set_volume", lua_wrap_errors}, - {"set_pitch", lua_wrap_errors}, - {"set_time", lua_wrap_errors}, - {"set_position", lua_wrap_errors}, - {"set_velocity", lua_wrap_errors}, - {"is_playing", lua_wrap_errors}, - {"is_paused", lua_wrap_errors}, - {"is_loop", lua_wrap_errors}, - {"get_volume", lua_wrap_errors}, - {"get_pitch", lua_wrap_errors}, - {"get_time", lua_wrap_errors}, - {"get_duration", lua_wrap_errors}, - {"get_position", lua_wrap_errors}, - {"get_velocity", lua_wrap_errors}, - {"count_speakers", lua_wrap_errors}, - {"count_streams", lua_wrap_errors}, + {"play_sound", lua::wrap}, + {"play_sound_2d", lua::wrap}, + {"play_stream", lua::wrap}, + {"play_stream_2d", lua::wrap}, + {"stop", lua::wrap}, + {"pause", lua::wrap}, + {"resume", lua::wrap}, + {"set_loop", lua::wrap}, + {"set_volume", lua::wrap}, + {"set_pitch", lua::wrap}, + {"set_time", lua::wrap}, + {"set_position", lua::wrap}, + {"set_velocity", lua::wrap}, + {"is_playing", lua::wrap}, + {"is_paused", lua::wrap}, + {"is_loop", lua::wrap}, + {"get_volume", lua::wrap}, + {"get_pitch", lua::wrap}, + {"get_time", lua::wrap}, + {"get_duration", lua::wrap}, + {"get_position", lua::wrap}, + {"get_velocity", lua::wrap}, + {"count_speakers", lua::wrap}, + {"count_streams", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libblock.cpp b/src/logic/scripting/lua/libblock.cpp index 8bdafb22..a09aa592 100644 --- a/src/logic/scripting/lua/libblock.cpp +++ b/src/logic/scripting/lua/libblock.cpp @@ -1,8 +1,4 @@ -#include "lua_commons.hpp" - #include "api_lua.hpp" -#include "lua_util.hpp" -#include "../scripting.hpp" #include "../../../world/Level.hpp" #include "../../../voxels/Chunks.hpp" @@ -279,27 +275,27 @@ static int l_caption(lua_State* L) { } const luaL_Reg blocklib [] = { - {"index", lua_wrap_errors}, - {"name", lua_wrap_errors}, - {"material", lua_wrap_errors}, - {"caption", lua_wrap_errors}, - {"defs_count", lua_wrap_errors}, - {"is_solid_at", lua_wrap_errors}, - {"is_replaceable_at", lua_wrap_errors}, - {"set", lua_wrap_errors}, - {"get", lua_wrap_errors}, - {"get_X", lua_wrap_errors}, - {"get_Y", lua_wrap_errors}, - {"get_Z", lua_wrap_errors}, - {"get_states", lua_wrap_errors}, - {"set_states", lua_wrap_errors}, - {"get_rotation", lua_wrap_errors}, - {"set_rotation", lua_wrap_errors}, - {"get_user_bits", lua_wrap_errors}, - {"set_user_bits", lua_wrap_errors}, - {"is_extended", lua_wrap_errors}, - {"get_size", lua_wrap_errors}, - {"is_segment", lua_wrap_errors}, - {"seek_origin", lua_wrap_errors}, + {"index", lua::wrap}, + {"name", lua::wrap}, + {"material", lua::wrap}, + {"caption", lua::wrap}, + {"defs_count", lua::wrap}, + {"is_solid_at", lua::wrap}, + {"is_replaceable_at", lua::wrap}, + {"set", lua::wrap}, + {"get", lua::wrap}, + {"get_X", lua::wrap}, + {"get_Y", lua::wrap}, + {"get_Z", lua::wrap}, + {"get_states", lua::wrap}, + {"set_states", lua::wrap}, + {"get_rotation", lua::wrap}, + {"set_rotation", lua::wrap}, + {"get_user_bits", lua::wrap}, + {"set_user_bits", lua::wrap}, + {"is_extended", lua::wrap}, + {"get_size", lua::wrap}, + {"is_segment", lua::wrap}, + {"seek_origin", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libconsole.cpp b/src/logic/scripting/lua/libconsole.cpp index 0a27bcde..f7eb6cb6 100644 --- a/src/logic/scripting/lua/libconsole.cpp +++ b/src/logic/scripting/lua/libconsole.cpp @@ -1,17 +1,9 @@ #include "api_lua.hpp" -#include "lua_commons.hpp" -#include "lua_util.hpp" -#include "LuaState.hpp" -#include "../scripting.hpp" #include "../../CommandsInterpreter.hpp" #include "../../../engine.hpp" #include "../../../coders/commons.hpp" -namespace scripting { - extern lua::LuaState* state; -} - using namespace scripting; static int l_add_command(lua_State* L) { @@ -20,8 +12,8 @@ static int l_add_command(lua_State* L) { } auto scheme = lua::require_string(L, 1); auto description = lua::require_string(L, 2); - lua_pushvalue(L, 3); - auto func = state->createLambda(L); + lua::pushvalue(L, 3); + auto func = lua::create_lambda(L); try { engine->getCommandsInterpreter()->getRepository()->add( scheme, description, [func](auto, auto args, auto kwargs) { @@ -115,10 +107,10 @@ static int l_get_command_info(lua_State* L) { } const luaL_Reg consolelib [] = { - {"add_command", lua_wrap_errors}, - {"execute", lua_wrap_errors}, - {"set", lua_wrap_errors}, - {"get_commands_list", lua_wrap_errors}, - {"get_command_info", lua_wrap_errors}, + {"add_command", lua::wrap}, + {"execute", lua::wrap}, + {"set", lua::wrap}, + {"get_commands_list", lua::wrap}, + {"get_command_info", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libcore.cpp b/src/logic/scripting/lua/libcore.cpp index 48003a7d..eb98cca3 100644 --- a/src/logic/scripting/lua/libcore.cpp +++ b/src/logic/scripting/lua/libcore.cpp @@ -1,7 +1,4 @@ -#include "lua_commons.hpp" -#include "lua_util.hpp" #include "api_lua.hpp" -#include "LuaState.hpp" #include "../../../engine.hpp" #include "../../../files/settings_io.hpp" @@ -14,14 +11,10 @@ #include "../../../window/Events.hpp" #include "../../../window/Window.hpp" #include "../../../world/WorldGenerators.hpp" -#include "../scripting.hpp" #include #include -namespace scripting { - extern lua::LuaState* state; -} using namespace scripting; static int l_new_world(lua_State* L) { @@ -128,17 +121,17 @@ static int l_get_setting_info(lua_State* L) { auto setting = engine->getSettingsHandler().getSetting(name); lua_createtable(L, 0, 1); if (auto number = dynamic_cast(setting)) { - lua_pushnumber(L, number->getMin()); - lua_setfield(L, -2, "min"); - lua_pushnumber(L, number->getMax()); - lua_setfield(L, -2, "max"); + lua::pushnumber(L, number->getMin()); + lua::setfield(L, "min"); + lua::pushnumber(L, number->getMax()); + lua::setfield(L, "max"); return 1; } if (auto integer = dynamic_cast(setting)) { - lua_pushinteger(L, integer->getMin()); - lua_setfield(L, -2, "min"); - lua_pushinteger(L, integer->getMax()); - lua_setfield(L, -2, "max"); + lua::pushinteger(L, integer->getMin()); + lua::setfield(L, "min"); + lua::pushinteger(L, integer->getMax()); + lua::setfield(L, "max"); return 1; } lua_pop(L, 1); @@ -151,7 +144,7 @@ static int l_quit(lua_State*) { } static int l_get_default_generator(lua_State* L) { - lua_pushstring(L, WorldGenerators::getDefaultGeneratorID().c_str()); + lua::pushstring(L, WorldGenerators::getDefaultGeneratorID().c_str()); return 1; } @@ -161,7 +154,7 @@ static int l_get_generators(lua_State* L) { int i = 0; for (auto& id : generators) { - lua_pushstring(L, id.c_str()); + lua::pushstring(L, id.c_str()); lua_rawseti(L, -2, i + 1); i++; } @@ -169,18 +162,18 @@ static int l_get_generators(lua_State* L) { } const luaL_Reg corelib [] = { - {"new_world", lua_wrap_errors}, - {"open_world", lua_wrap_errors}, - {"reopen_world", lua_wrap_errors}, - {"close_world", lua_wrap_errors}, - {"delete_world", lua_wrap_errors}, - {"reconfig_packs", lua_wrap_errors}, - {"get_setting", lua_wrap_errors}, - {"set_setting", lua_wrap_errors}, - {"str_setting", lua_wrap_errors}, - {"get_setting_info", lua_wrap_errors}, - {"quit", lua_wrap_errors}, - {"get_default_generator", lua_wrap_errors}, - {"get_generators", lua_wrap_errors}, + {"new_world", lua::wrap}, + {"open_world", lua::wrap}, + {"reopen_world", lua::wrap}, + {"close_world", lua::wrap}, + {"delete_world", lua::wrap}, + {"reconfig_packs", lua::wrap}, + {"get_setting", lua::wrap}, + {"set_setting", lua::wrap}, + {"str_setting", lua::wrap}, + {"get_setting_info", lua::wrap}, + {"quit", lua::wrap}, + {"get_default_generator", lua::wrap}, + {"get_generators", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libfile.cpp b/src/logic/scripting/lua/libfile.cpp index 85336add..b9477c4b 100644 --- a/src/logic/scripting/lua/libfile.cpp +++ b/src/logic/scripting/lua/libfile.cpp @@ -1,8 +1,5 @@ -#include "lua_commons.hpp" -#include "lua_util.hpp" #include "api_lua.hpp" -#include "LuaState.hpp" -#include "../scripting.hpp" + #include "../../../engine.hpp" #include "../../../coders/gzip.hpp" #include "../../../files/files.hpp" @@ -13,11 +10,6 @@ #include namespace fs = std::filesystem; - -namespace scripting { - extern lua::LuaState* state; -} - using namespace scripting; static fs::path resolve_path(const std::string& path) { @@ -232,22 +224,22 @@ static int l_file_gzip_decompress(lua_State* L) { } const luaL_Reg filelib [] = { - {"exists", lua_wrap_errors}, - {"find", lua_wrap_errors}, - {"isdir", lua_wrap_errors}, - {"isfile", lua_wrap_errors}, - {"length", lua_wrap_errors}, - {"list", lua_wrap_errors}, - {"mkdir", lua_wrap_errors}, - {"mkdirs", lua_wrap_errors}, - {"read_bytes", lua_wrap_errors}, - {"read", lua_wrap_errors}, - {"remove", lua_wrap_errors}, - {"remove_tree", lua_wrap_errors}, - {"resolve", lua_wrap_errors}, - {"write_bytes", lua_wrap_errors}, - {"write", lua_wrap_errors}, - {"gzip_compress", lua_wrap_errors}, - {"gzip_decompress", lua_wrap_errors}, + {"exists", lua::wrap}, + {"find", lua::wrap}, + {"isdir", lua::wrap}, + {"isfile", lua::wrap}, + {"length", lua::wrap}, + {"list", lua::wrap}, + {"mkdir", lua::wrap}, + {"mkdirs", lua::wrap}, + {"read_bytes", lua::wrap}, + {"read", lua::wrap}, + {"remove", lua::wrap}, + {"remove_tree", lua::wrap}, + {"resolve", lua::wrap}, + {"write_bytes", lua::wrap}, + {"write", lua::wrap}, + {"gzip_compress", lua::wrap}, + {"gzip_decompress", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libgui.cpp b/src/logic/scripting/lua/libgui.cpp index e89b1046..c61611bd 100644 --- a/src/logic/scripting/lua/libgui.cpp +++ b/src/logic/scripting/lua/libgui.cpp @@ -1,9 +1,5 @@ -#include "lua_commons.hpp" #include "api_lua.hpp" -#include "lua_util.hpp" -#include "LuaState.hpp" -#include "../scripting.hpp" #include "../../../engine.hpp" #include "../../../assets/Assets.hpp" #include "../../../items/Inventories.hpp" @@ -23,18 +19,13 @@ #include "../../../world/Level.hpp" using namespace gui; - -namespace scripting { - extern lua::LuaState* state; -} +using namespace scripting; struct DocumentNode { UiDocument* document; std::shared_ptr node; }; -using namespace scripting; - static DocumentNode getDocumentNode(lua_State*, const std::string& name, const std::string& nodeName) { auto doc = engine->getAssets()->getLayout(name); if (doc == nullptr) { @@ -115,10 +106,10 @@ static int l_container_clear(lua_State* L) { static int l_container_set_interval(lua_State* L) { auto node = getDocumentNode(L, 1); - auto interval = lua_tointeger(L, 2) / 1000.0f; + auto interval = lua::tointeger(L, 2) / 1000.0f; if (auto container = std::dynamic_pointer_cast(node.node)) { - lua_pushvalue(L, 3); - auto runnable = state->createRunnable(L); + lua::pushvalue(L, 3); + auto runnable = lua::create_runnable(L); container->listenInterval(interval, runnable); } return 0; @@ -266,25 +257,25 @@ static int p_get_src(UINode* node, lua_State* L) { static int p_get_add(UINode* node, lua_State* L) { if (dynamic_cast(node)) { - return lua::pushcfunction(L, lua_wrap_errors); + return lua::pushcfunction(L, lua::wrap); } return 0; } static int p_get_destruct(UINode*, lua_State* L) { - return lua::pushcfunction(L, lua_wrap_errors); + return lua::pushcfunction(L, lua::wrap); } static int p_get_clear(UINode* node, lua_State* L) { if (dynamic_cast(node)) { - return lua::pushcfunction(L, lua_wrap_errors); + return lua::pushcfunction(L, lua::wrap); } return 0; } static int p_set_interval(UINode* node, lua_State* L) { if (dynamic_cast(node)) { - return lua::pushcfunction(L, lua_wrap_errors); + return lua::pushcfunction(L, lua::wrap); } return 0; } @@ -393,7 +384,7 @@ static void p_set_tooltip(UINode* node, lua_State* L, int idx) { node->setTooltip(lua::require_wstring(L, idx)); } static void p_set_tooltip_delay(UINode* node, lua_State* L, int idx) { - node->setTooltipDelay(lua_tonumber(L, idx)); + node->setTooltipDelay(lua::tonumber(L, idx)); } static void p_set_pos(UINode* node, lua_State* L, int idx) { node->setPos(lua::tovec2(L, idx)); @@ -405,13 +396,13 @@ static void p_set_size(UINode* node, lua_State* L, int idx) { node->setSize(lua::tovec2(L, idx)); } static void p_set_interactive(UINode* node, lua_State* L, int idx) { - node->setInteractive(lua_toboolean(L, idx)); + node->setInteractive(lua::toboolean(L, idx)); } static void p_set_visible(UINode* node, lua_State* L, int idx) { - node->setVisible(lua_toboolean(L, idx)); + node->setVisible(lua::toboolean(L, idx)); } static void p_set_enabled(UINode* node, lua_State* L, int idx) { - node->setEnabled(lua_toboolean(L, idx)); + node->setEnabled(lua::toboolean(L, idx)); } static void p_set_placeholder(UINode* node, lua_State* L, int idx) { if (auto box = dynamic_cast(node)) { @@ -429,12 +420,12 @@ static void p_set_text(UINode* node, lua_State* L, int idx) { } static void p_set_caret(UINode* node, lua_State* L, int idx) { if (auto box = dynamic_cast(node)) { - box->setCaret(static_cast(lua_tointeger(L, idx))); + box->setCaret(static_cast(lua::tointeger(L, idx))); } } static void p_set_editable(UINode* node, lua_State* L, int idx) { if (auto box = dynamic_cast(node)) { - box->setEditable(lua_toboolean(L, idx)); + box->setEditable(lua::toboolean(L, idx)); } } static void p_set_src(UINode* node, lua_State* L, int idx) { @@ -444,27 +435,27 @@ static void p_set_src(UINode* node, lua_State* L, int idx) { } static void p_set_value(UINode* node, lua_State* L, int idx) { if (auto bar = dynamic_cast(node)) { - bar->setValue(lua_tonumber(L, idx)); + bar->setValue(lua::tonumber(L, idx)); } } static void p_set_min(UINode* node, lua_State* L, int idx) { if (auto bar = dynamic_cast(node)) { - bar->setMin(lua_tonumber(L, idx)); + bar->setMin(lua::tonumber(L, idx)); } } static void p_set_max(UINode* node, lua_State* L, int idx) { if (auto bar = dynamic_cast(node)) { - bar->setMax(lua_tonumber(L, idx)); + bar->setMax(lua::tonumber(L, idx)); } } static void p_set_step(UINode* node, lua_State* L, int idx) { if (auto bar = dynamic_cast(node)) { - bar->setStep(lua_tonumber(L, idx)); + bar->setStep(lua::tonumber(L, idx)); } } static void p_set_track_width(UINode* node, lua_State* L, int idx) { if (auto bar = dynamic_cast(node)) { - bar->setTrackWidth(lua_tointeger(L, idx)); + bar->setTrackWidth(lua::tointeger(L, idx)); } } static void p_set_track_color(UINode* node, lua_State* L, int idx) { @@ -474,9 +465,9 @@ static void p_set_track_color(UINode* node, lua_State* L, int idx) { } static void p_set_checked(UINode* node, lua_State* L, int idx) { if (auto box = dynamic_cast(node)) { - box->setChecked(lua_toboolean(L, idx)); + box->setChecked(lua::toboolean(L, idx)); } else if (auto box = dynamic_cast(node)) { - box->setChecked(lua_toboolean(L, idx)); + box->setChecked(lua::toboolean(L, idx)); } } static void p_set_page(UINode* node, lua_State* L, int idx) { @@ -486,7 +477,7 @@ static void p_set_page(UINode* node, lua_State* L, int idx) { } static void p_set_inventory(UINode* node, lua_State* L, int idx) { if (auto view = dynamic_cast(node)) { - auto inventory = level->inventories->get(lua_tointeger(L, idx)); + auto inventory = level->inventories->get(lua::tointeger(L, idx)); if (inventory == nullptr) { view->unbind(); } else { @@ -495,8 +486,8 @@ static void p_set_inventory(UINode* node, lua_State* L, int idx) { } } static void p_set_focused(const std::shared_ptr &node, lua_State* L, int idx) { - if (lua_toboolean(L, idx) && !node->isFocused()) { - scripting::engine->getGUI()->setFocus(node); + if (lua::toboolean(L, idx) && !node->isFocused()) { + engine->getGUI()->setFocus(node); } else if (node->isFocused()){ node->defocus(); } @@ -553,11 +544,11 @@ static int l_gui_setattr(lua_State* L) { static int l_gui_get_env(lua_State* L) { auto name = lua::require_string(L, 1); - auto doc = scripting::engine->getAssets()->getLayout(name); + auto doc = engine->getAssets()->getLayout(name); if (doc == nullptr) { throw std::runtime_error("document '"+std::string(name)+"' not found"); } - lua_getglobal(L, lua::LuaState::envName(*doc->getEnvironment()).c_str()); + lua::getglobal(L, lua::env_name(*doc->getEnvironment())); return 1; } @@ -565,16 +556,16 @@ static int l_gui_str(lua_State* L) { auto text = lua::require_wstring(L, 1); if (!lua_isnoneornil(L, 2)) { auto context = lua::require_wstring(L, 2); - lua_pushstring(L, util::wstr2str_utf8(langs::get(text, context)).c_str()); + lua::pushwstring(L, langs::get(text, context)); } else { - lua_pushstring(L, util::wstr2str_utf8(langs::get(text)).c_str()); + lua::pushwstring(L, langs::get(text)); } return 1; } static int l_gui_reindex(lua_State* L) { auto name = lua::require_string(L, 1); - auto doc = scripting::engine->getAssets()->getLayout(name); + auto doc = engine->getAssets()->getLayout(name); if (doc == nullptr) { throw std::runtime_error("document '"+std::string(name)+"' not found"); } @@ -588,24 +579,24 @@ static int l_gui_get_locales_info(lua_State* L) { lua_createtable(L, 0, locales.size()); for (auto& entry : locales) { lua_createtable(L, 0, 1); - lua_pushstring(L, entry.second.name.c_str()); - lua_setfield(L, -2, "name"); - lua_setfield(L, -2, entry.first.c_str()); + lua::pushstring(L, entry.second.name); + lua::setfield(L, "name"); + lua::setfield(L, entry.first); } return 1; } static int l_gui_getviewport(lua_State* L) { - return lua::pushvec2_arr(L, scripting::engine->getGUI()->getContainer()->getSize()); + return lua::pushvec2_arr(L, engine->getGUI()->getContainer()->getSize()); } const luaL_Reg guilib [] = { - {"get_viewport", lua_wrap_errors}, - {"getattr", lua_wrap_errors}, - {"setattr", lua_wrap_errors}, - {"get_env", lua_wrap_errors}, - {"str", lua_wrap_errors}, - {"get_locales_info", lua_wrap_errors}, - {"__reindex", lua_wrap_errors}, + {"get_viewport", lua::wrap}, + {"getattr", lua::wrap}, + {"setattr", lua::wrap}, + {"get_env", lua::wrap}, + {"str", lua::wrap}, + {"get_locales_info", lua::wrap}, + {"__reindex", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libhud.cpp b/src/logic/scripting/lua/libhud.cpp index 93d3505b..98a692e5 100644 --- a/src/logic/scripting/lua/libhud.cpp +++ b/src/logic/scripting/lua/libhud.cpp @@ -1,6 +1,4 @@ -#include "lua_commons.hpp" #include "api_lua.hpp" -#include "LuaState.hpp" #include "../../../assets/Assets.hpp" #include "../../../content/Content.hpp" @@ -16,7 +14,6 @@ #include "../../../voxels/Chunks.hpp" #include "../../../voxels/voxel.hpp" #include "../../../world/Level.hpp" -#include "../scripting.hpp" #include #include @@ -129,15 +126,15 @@ static int l_hud_get_player(lua_State* L) { } const luaL_Reg hudlib [] = { - {"open_inventory", lua_wrap_errors}, - {"close_inventory", lua_wrap_errors}, - {"open_block", lua_wrap_errors}, - {"open_permanent", lua_wrap_errors}, - {"show_overlay", lua_wrap_errors}, - {"get_block_inventory", lua_wrap_errors}, - {"close", lua_wrap_errors}, - {"pause", lua_wrap_errors}, - {"resume", lua_wrap_errors}, - {"get_player", lua_wrap_errors}, + {"open_inventory", lua::wrap}, + {"close_inventory", lua::wrap}, + {"open_block", lua::wrap}, + {"open_permanent", lua::wrap}, + {"show_overlay", lua::wrap}, + {"get_block_inventory", lua::wrap}, + {"close", lua::wrap}, + {"pause", lua::wrap}, + {"resume", lua::wrap}, + {"get_player", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libinput.cpp b/src/logic/scripting/lua/libinput.cpp index 5e9413ea..33cb0898 100644 --- a/src/logic/scripting/lua/libinput.cpp +++ b/src/logic/scripting/lua/libinput.cpp @@ -1,8 +1,4 @@ #include "api_lua.hpp" -#include "lua_util.hpp" -#include "lua_commons.hpp" -#include "LuaState.hpp" -#include "../scripting.hpp" #include "../../../window/input.hpp" #include "../../../window/Events.hpp" @@ -13,7 +9,6 @@ #include "../../../engine.hpp" namespace scripting { - extern lua::LuaState* state; extern Hud* hud; } using namespace scripting; @@ -37,7 +32,7 @@ static int l_add_callback(lua_State* L) { throw std::runtime_error("unknown binding "+util::quote(bindname)); } lua_pushvalue(L, 2); - runnable actual_callback = state->createRunnable(L); + runnable actual_callback = lua::create_runnable(L); runnable callback = [=]() { if (!scripting::engine->getGUI()->isFocusCaught()) { actual_callback(); @@ -69,11 +64,11 @@ static int l_get_bindings(lua_State* L) { } const luaL_Reg inputlib [] = { - {"keycode", lua_wrap_errors}, - {"mousecode", lua_wrap_errors}, - {"add_callback", lua_wrap_errors}, - {"get_mouse_pos", lua_wrap_errors}, - {"get_bindings", lua_wrap_errors}, + {"keycode", lua::wrap}, + {"mousecode", lua::wrap}, + {"add_callback", lua::wrap}, + {"get_mouse_pos", lua::wrap}, + {"get_bindings", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libinventory.cpp b/src/logic/scripting/lua/libinventory.cpp index f6d164e1..362e2449 100644 --- a/src/logic/scripting/lua/libinventory.cpp +++ b/src/logic/scripting/lua/libinventory.cpp @@ -1,23 +1,21 @@ -#include "lua_commons.hpp" - #include "api_lua.hpp" -#include "lua_util.hpp" -#include "../scripting.hpp" #include "../../../content/Content.hpp" #include "../../../world/Level.hpp" #include "../../../items/ItemStack.hpp" #include "../../../items/Inventories.hpp" #include "../../../logic/BlocksController.hpp" +using namespace scripting; + static void validate_itemid(itemid_t id) { - if (id >= scripting::indices->countItemDefs()) { + if (id >= indices->countItemDefs()) { throw std::runtime_error("invalid item id"); } } static std::shared_ptr get_inventory(int64_t id) { - auto inv = scripting::level->inventories->get(id); + auto inv = level->inventories->get(id); if (inv == nullptr) { throw std::runtime_error("inventory not found: "+std::to_string(id)); } @@ -25,7 +23,7 @@ static std::shared_ptr get_inventory(int64_t id) { } static std::shared_ptr get_inventory(int64_t id, int arg) { - auto inv = scripting::level->inventories->get(id); + auto inv = level->inventories->get(id); if (inv == nullptr) { throw std::runtime_error("inventory not found: "+std::to_string(id)+ " argument "+std::to_string(arg)); @@ -40,21 +38,21 @@ static void validate_slotid(int slotid, Inventory* inv) { } static int l_inventory_get(lua_State* L) { - lua_Integer invid = lua_tointeger(L, 1); - lua_Integer slotid = lua_tointeger(L, 2); + auto invid = lua::tointeger(L, 1); + auto slotid = lua::tointeger(L, 2); auto inv = get_inventory(invid); validate_slotid(slotid, inv.get()); const ItemStack& item = inv->getSlot(slotid); - lua_pushinteger(L, item.getItemId()); - lua_pushinteger(L, item.getCount()); + lua::pushinteger(L, item.getItemId()); + lua::pushinteger(L, item.getCount()); return 2; } static int l_inventory_set(lua_State* L) { - lua_Integer invid = lua_tointeger(L, 1); - lua_Integer slotid = lua_tointeger(L, 2); - lua_Integer itemid = lua_tointeger(L, 3); - lua_Integer count = lua_tointeger(L, 4); + auto invid = lua::tointeger(L, 1); + auto slotid = lua::tointeger(L, 2); + auto itemid = lua::tointeger(L, 3); + auto count = lua::tointeger(L, 4); validate_itemid(itemid); auto inv = get_inventory(invid); @@ -66,90 +64,85 @@ static int l_inventory_set(lua_State* L) { } static int l_inventory_size(lua_State* L) { - lua_Integer invid = lua_tointeger(L, 1); + auto invid = lua::tointeger(L, 1); auto inv = get_inventory(invid); - lua_pushinteger(L, inv->size()); - return 1; + return lua::pushinteger(L, inv->size()); } static int l_inventory_add(lua_State* L) { - lua_Integer invid = lua_tointeger(L, 1); - lua_Integer itemid = lua_tointeger(L, 2); - lua_Integer count = lua_tointeger(L, 3); + auto invid = lua::tointeger(L, 1); + auto itemid = lua::tointeger(L, 2); + auto count = lua::tointeger(L, 3); validate_itemid(itemid); auto inv = get_inventory(invid); ItemStack item(itemid, count); - inv->move(item, scripting::indices); - lua_pushinteger(L, item.getCount()); - return 1; + inv->move(item, indices); + return lua::pushinteger(L, item.getCount()); } static int l_inventory_get_block(lua_State* L) { - lua_Integer x = lua_tointeger(L, 1); - lua_Integer y = lua_tointeger(L, 2); - lua_Integer z = lua_tointeger(L, 3); - int64_t id = scripting::blocks->createBlockInventory(x, y, z); - lua_pushinteger(L, id); - return 1; + auto x = lua::tointeger(L, 1); + auto y = lua::tointeger(L, 2); + auto z = lua::tointeger(L, 3); + int64_t id = blocks->createBlockInventory(x, y, z); + return lua::pushinteger(L, id); } static int l_inventory_bind_block(lua_State* L) { - lua_Integer id = lua_tointeger(L, 1); - lua_Integer x = lua_tointeger(L, 2); - lua_Integer y = lua_tointeger(L, 3); - lua_Integer z = lua_tointeger(L, 4); - scripting::blocks->bindInventory(id, x, y, z); + auto id = lua::tointeger(L, 1); + auto x = lua::tointeger(L, 2); + auto y = lua::tointeger(L, 3); + auto z = lua::tointeger(L, 4); + blocks->bindInventory(id, x, y, z); return 0; } static int l_inventory_unbind_block(lua_State* L) { - lua_Integer x = lua_tointeger(L, 1); - lua_Integer y = lua_tointeger(L, 2); - lua_Integer z = lua_tointeger(L, 3); - scripting::blocks->unbindInventory(x, y, z); + auto x = lua::tointeger(L, 1); + auto y = lua::tointeger(L, 2); + auto z = lua::tointeger(L, 3); + blocks->unbindInventory(x, y, z); return 0; } static int l_inventory_clone(lua_State* L) { - lua_Integer id = lua_tointeger(L, 1); - auto clone = scripting::level->inventories->clone(id); + auto id = lua::tointeger(L, 1); + auto clone = level->inventories->clone(id); if (clone == nullptr) { - lua_pushinteger(L, 0); - return 1; + return lua::pushinteger(L, 0); } - lua_pushinteger(L, clone->getId()); - return 1; + return lua::pushinteger(L, clone->getId()); } static int l_inventory_move(lua_State* L) { - lua_Integer invAid = lua_tointeger(L, 1); - lua_Integer slotAid = lua_tointeger(L, 2); + auto invAid = lua::tointeger(L, 1); + auto slotAid = lua::tointeger(L, 2); auto invA = get_inventory(invAid, 1); validate_slotid(slotAid, invA.get()); - lua_Integer invBid = lua_tointeger(L, 3); - lua_Integer slotBid = lua_isnil(L, 4) ? -1 : lua_tointeger(L, 4); + auto invBid = lua::tointeger(L, 3); + auto slotBid = lua_isnil(L, 4) ? -1 : lua::tointeger(L, 4); auto invB = get_inventory(invBid, 3); auto& slot = invA->getSlot(slotAid); if (slotBid == -1) { - invB->move(slot, scripting::content->getIndices()); + invB->move(slot, content->getIndices()); } else { - invB->move(slot, scripting::content->getIndices(), slotBid, slotBid+1); + invB->move(slot, content->getIndices(), slotBid, slotBid+1); } return 0; } const luaL_Reg inventorylib [] = { - {"get", lua_wrap_errors}, - {"set", lua_wrap_errors}, - {"size", lua_wrap_errors}, - {"add", lua_wrap_errors}, - {"move", lua_wrap_errors}, - {"get_block", lua_wrap_errors}, - {"bind_block", lua_wrap_errors}, - {"unbind_block", lua_wrap_errors}, - {"clone", lua_wrap_errors}, + {"get", lua::wrap}, + {"set", lua::wrap}, + {"size", lua::wrap}, + {"add", lua::wrap}, + {"move", lua::wrap}, + {"get_block", lua::wrap}, + {"bind_block", lua::wrap}, + {"unbind_block", lua::wrap}, + {"clone", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libitem.cpp b/src/logic/scripting/lua/libitem.cpp index 16d8d877..162a9c09 100644 --- a/src/logic/scripting/lua/libitem.cpp +++ b/src/logic/scripting/lua/libitem.cpp @@ -1,53 +1,43 @@ -#include "lua_commons.hpp" -#include "lua_util.hpp" #include "api_lua.hpp" -#include "LuaState.hpp" -#include "../scripting.hpp" + #include "../../../content/Content.hpp" #include "../../../items/ItemDef.hpp" -namespace scripting { - extern lua::LuaState* state; -} using namespace scripting; static int l_item_name(lua_State* L) { auto indices = content->getIndices(); - auto id = lua_tointeger(L, 1); + auto id = lua::tointeger(L, 1); if (static_cast(id) >= indices->countItemDefs()) { return 0; } auto def = indices->getItemDef(id); - lua_pushstring(L, def->name.c_str()); - return 1; + return lua::pushstring(L, def->name); } static int l_item_index(lua_State* L) { auto name = lua::require_string(L, 1); - lua_pushinteger(L, content->requireItem(name).rt.id); - return 1; + return lua::pushinteger(L, content->requireItem(name).rt.id); } static int l_item_stack_size(lua_State* L) { auto indices = content->getIndices(); - auto id = lua_tointeger(L, 1); + auto id = lua::tointeger(L, 1); if (static_cast(id) >= indices->countItemDefs()) { return 0; } auto def = indices->getItemDef(id); - lua_pushinteger(L, def->stackSize); - return 1; + return lua::pushinteger(L, def->stackSize); } static int l_item_defs_count(lua_State* L) { - lua_pushinteger(L, scripting::indices->countItemDefs()); - return 1; + return lua::pushinteger(L, indices->countItemDefs()); } const luaL_Reg itemlib [] = { - {"index", lua_wrap_errors}, - {"name", lua_wrap_errors}, - {"stack_size", lua_wrap_errors}, - {"defs_count", lua_wrap_errors}, + {"index", lua::wrap}, + {"name", lua::wrap}, + {"stack_size", lua::wrap}, + {"defs_count", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libjson.cpp b/src/logic/scripting/lua/libjson.cpp index 9cb316b4..2a9508e6 100644 --- a/src/logic/scripting/lua/libjson.cpp +++ b/src/logic/scripting/lua/libjson.cpp @@ -1,23 +1,15 @@ #include "api_lua.hpp" -#include "lua_util.hpp" -#include "lua_commons.hpp" -#include "LuaState.hpp" #include "../../../coders/json.hpp" #include "../../../data/dynamic.hpp" -namespace scripting { - extern lua::LuaState* state; -} - static int l_json_stringify(lua_State* L) { auto value = lua::tovalue(L, 1); if (auto mapptr = std::get_if(&value)) { - bool nice = lua_toboolean(L, 2); + bool nice = lua::toboolean(L, 2); auto string = json::stringify(mapptr->get(), nice, " "); - lua_pushstring(L, string.c_str()); - return 1; + return lua::pushstring(L, string.c_str()); } else { throw std::runtime_error("table expected"); } @@ -26,12 +18,11 @@ static int l_json_stringify(lua_State* L) { static int l_json_parse(lua_State* L) { auto string = lua::require_string(L, 1); auto element = json::parse("", string); - lua::pushvalue(L, element); - return 1; + return lua::pushvalue(L, element); } const luaL_Reg jsonlib [] = { - {"tostring", lua_wrap_errors}, - {"parse", lua_wrap_errors}, + {"tostring", lua::wrap}, + {"parse", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libpack.cpp b/src/logic/scripting/lua/libpack.cpp index 683f17e3..931823ac 100644 --- a/src/logic/scripting/lua/libpack.cpp +++ b/src/logic/scripting/lua/libpack.cpp @@ -1,7 +1,5 @@ #include "api_lua.hpp" -#include "lua_commons.hpp" -#include "../scripting.hpp" #include "../../../engine.hpp" #include "../../../assets/AssetsLoader.hpp" #include "../../../files/engine_paths.hpp" @@ -14,29 +12,28 @@ #include #include +using namespace scripting; + static int l_pack_get_folder(lua_State* L) { - std::string packName = lua_tostring(L, 1); + std::string packName = lua::tostring(L, 1); if (packName == "core") { - auto folder = scripting::engine->getPaths()->getResources().u8string()+"/"; - lua_pushstring(L, folder.c_str()); - return 1; + auto folder = engine->getPaths()->getResources().u8string()+"/"; + return lua::pushstring(L, folder); } - for (auto& pack : scripting::engine->getContentPacks()) { + for (auto& pack : engine->getContentPacks()) { if (pack.id == packName) { - lua_pushstring(L, (pack.folder.u8string()+"/").c_str()); - return 1; + return lua::pushstring(L, pack.folder.u8string()+"/"); } } - lua_pushstring(L, ""); - return 1; + return lua::pushstring(L, ""); } /// @brief pack.get_installed() -> array static int l_pack_get_installed(lua_State* L) { - auto& packs = scripting::engine->getContentPacks(); + auto& packs = engine->getContentPacks(); lua_createtable(L, packs.size(), 0); for (size_t i = 0; i < packs.size(); i++) { - lua_pushstring(L, packs[i].id.c_str()); + lua::pushstring(L, packs[i].id); lua_rawseti(L, -2, i + 1); } return 1; @@ -45,13 +42,13 @@ static int l_pack_get_installed(lua_State* L) { /// @brief pack.get_available() -> array static int l_pack_get_available(lua_State* L) { fs::path worldFolder(""); - if (scripting::level) { - worldFolder = scripting::level->getWorld()->wfile->getFolder(); + if (level) { + worldFolder = level->getWorld()->wfile->getFolder(); } - auto manager = scripting::engine->createPacksManager(worldFolder); + auto manager = engine->createPacksManager(worldFolder); manager.scan(); - const auto& installed = scripting::engine->getContentPacks(); + const auto& installed = engine->getContentPacks(); for (auto& pack : installed) { manager.exclude(pack.id); } @@ -59,7 +56,7 @@ static int l_pack_get_available(lua_State* L) { lua_createtable(L, names.size(), 0); for (size_t i = 0; i < names.size(); i++) { - lua_pushstring(L, names[i].c_str()); + lua::pushstring(L, names[i]); lua_rawseti(L, -2, i + 1); } return 1; @@ -68,22 +65,22 @@ static int l_pack_get_available(lua_State* L) { static int l_pack_get_info(lua_State* L, const ContentPack& pack, const Content* content) { lua_createtable(L, 0, 5); - lua_pushstring(L, pack.id.c_str()); - lua_setfield(L, -2, "id"); + lua::pushstring(L, pack.id); + lua::setfield(L, "id"); - lua_pushstring(L, pack.title.c_str()); - lua_setfield(L, -2, "title"); + lua::pushstring(L, pack.title); + lua::setfield(L, "title"); - lua_pushstring(L, pack.creator.c_str()); - lua_setfield(L, -2, "creator"); + lua::pushstring(L, pack.creator); + lua::setfield(L, "creator"); - lua_pushstring(L, pack.description.c_str()); - lua_setfield(L, -2, "description"); + lua::pushstring(L, pack.description); + lua::setfield(L, "description"); - lua_pushstring(L, pack.version.c_str()); - lua_setfield(L, -2, "version"); + lua::pushstring(L, pack.version); + lua::setfield(L, "version"); - auto assets = scripting::engine->getAssets(); + auto assets = engine->getAssets(); std::string icon = pack.id+".icon"; if (!AssetsLoader::loadExternalTexture(assets, icon, { pack.folder/fs::path("icon.png") @@ -91,8 +88,8 @@ static int l_pack_get_info(lua_State* L, const ContentPack& pack, const Content* icon = "gui/no_icon"; } - lua_pushstring(L, icon.c_str()); - lua_setfield(L, -2, "icon"); + lua::pushstring(L, icon); + lua::setfield(L, "icon"); if (!pack.dependencies.empty()) { lua_createtable(L, pack.dependencies.size(), 0); @@ -108,13 +105,13 @@ static int l_pack_get_info(lua_State* L, const ContentPack& pack, const Content* lua_pushfstring(L, "%s%s", prefix.c_str(), dpack.id.c_str()); lua_rawseti(L, -2, i+1); } - lua_setfield(L, -2, "dependencies"); + lua::setfield(L, "dependencies"); } auto runtime = content ? content->getPackRuntime(pack.id) : nullptr; if (runtime) { - lua_pushboolean(L, runtime->getStats().hasSavingContent()); - lua_setfield(L, -2, "has_indices"); + lua::pushboolean(L, runtime->getStats().hasSavingContent()); + lua::setfield(L, "has_indices"); } return 1; } @@ -163,10 +160,10 @@ static int l_pack_get_base_packs(lua_State* L) { } const luaL_Reg packlib [] = { - {"get_folder", lua_wrap_errors}, - {"get_installed", lua_wrap_errors}, - {"get_available", lua_wrap_errors}, - {"get_info", lua_wrap_errors}, - {"get_base_packs", lua_wrap_errors}, + {"get_folder", lua::wrap}, + {"get_installed", lua::wrap}, + {"get_available", lua::wrap}, + {"get_info", lua::wrap}, + {"get_base_packs", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libplayer.cpp b/src/logic/scripting/lua/libplayer.cpp index fde82362..6db20c60 100644 --- a/src/logic/scripting/lua/libplayer.cpp +++ b/src/logic/scripting/lua/libplayer.cpp @@ -1,6 +1,5 @@ -#include "lua_commons.hpp" #include "api_lua.hpp" -#include "../scripting.hpp" + #include "../../../world/Level.hpp" #include "../../../objects/Player.hpp" #include "../../../physics/Hitbox.hpp" @@ -9,20 +8,17 @@ #include +using namespace scripting; + inline std::shared_ptr get_player(lua_State* L, int idx) { - return scripting::level->getObject(lua_tointeger(L, idx)); + return level->getObject(lua::tointeger(L, idx)); } static int l_player_get_pos(lua_State* L) { - auto player = get_player(L, 1); - if (!player) { - return 0; + if (auto player = get_player(L, 1)) { + return lua::pushvec3(L, player->hitbox->position); } - glm::vec3 pos = player->hitbox->position; - lua_pushnumber(L, pos.x); - lua_pushnumber(L, pos.y); - lua_pushnumber(L, pos.z); - return 3; + return 0; } static int l_player_set_pos(lua_State* L) { @@ -30,23 +26,18 @@ static int l_player_set_pos(lua_State* L) { if (!player) { return 0; } - auto x = lua_tonumber(L, 2); - auto y = lua_tonumber(L, 3); - auto z = lua_tonumber(L, 4); + auto x = lua::tonumber(L, 2); + auto y = lua::tonumber(L, 3); + auto z = lua::tonumber(L, 4); player->hitbox->position = glm::vec3(x, y, z); return 0; } static int l_player_get_vel(lua_State* L) { - auto player = get_player(L, 1); - if (!player) { - return 0; + if (auto player = get_player(L, 1)) { + return lua::pushvec3(L, player->hitbox->velocity); } - glm::vec3 vel = player->hitbox->velocity; - lua_pushnumber(L, vel.x); - lua_pushnumber(L, vel.y); - lua_pushnumber(L, vel.z); - return 3; + return 0; } static int l_player_set_vel(lua_State* L) { @@ -54,23 +45,18 @@ static int l_player_set_vel(lua_State* L) { if (!player) { return 0; } - auto x = lua_tonumber(L, 2); - auto y = lua_tonumber(L, 3); - auto z = lua_tonumber(L, 4); + auto x = lua::tonumber(L, 2); + auto y = lua::tonumber(L, 3); + auto z = lua::tonumber(L, 4); player->hitbox->velocity = glm::vec3(x, y, z); return 0; } static int l_player_get_rot(lua_State* L) { - auto player = get_player(L, 1); - if (!player) { - return 0; + if (auto player = get_player(L, 1)) { + return lua::pushvec3(L, player->cam); } - const glm::vec3& rot = player->cam; - lua_pushnumber(L, rot.x); - lua_pushnumber(L, rot.y); - lua_pushnumber(L, rot.z); - return 3; + return 0; } static int l_player_set_rot(lua_State* L) { @@ -80,11 +66,11 @@ static int l_player_set_rot(lua_State* L) { } glm::vec3& cam = player->cam; - lua_Number x = lua_tonumber(L, 2); - lua_Number y = lua_tonumber(L, 3); + lua_Number x = lua::tonumber(L, 2); + lua_Number y = lua::tonumber(L, 3); lua_Number z = cam.z; if (lua_isnumber(L, 4)) { - z = lua_tonumber(L, 4); + z = lua::tonumber(L, 4); } cam.x = x; cam.y = y; @@ -97,37 +83,35 @@ static int l_player_get_inv(lua_State* L) { if (!player) { return 0; } - lua_pushinteger(L, player->getInventory()->getId()); - lua_pushinteger(L, player->getChosenSlot()); + lua::pushinteger(L, player->getInventory()->getId()); + lua::pushinteger(L, player->getChosenSlot()); return 2; } static int l_player_is_flight(lua_State* L) { if (auto player = get_player(L, 1)) { - lua_pushboolean(L, player->isFlight()); - return 1; + return lua::pushboolean(L, player->isFlight()); } return 0; } static int l_player_set_flight(lua_State* L) { if (auto player = get_player(L, 1)) { - player->setFlight(lua_toboolean(L, 2)); + player->setFlight(lua::toboolean(L, 2)); } return 0; } static int l_player_is_noclip(lua_State* L) { if (auto player = get_player(L, 1)) { - lua_pushboolean(L, player->isNoclip()); - return 1; + return lua::pushboolean(L, player->isNoclip()); } return 0; } static int l_player_set_noclip(lua_State* L) { if (auto player = get_player(L, 1)) { - player->setNoclip(lua_toboolean(L, 2)); + player->setNoclip(lua::toboolean(L, 2)); } return 0; } @@ -137,27 +121,23 @@ static int l_player_get_selected_block(lua_State* L) { if (player->selection.vox.id == BLOCK_VOID) { return 0; } - const glm::ivec3 pos = player->selection.position; - lua_pushinteger(L, pos.x); - lua_pushinteger(L, pos.y); - lua_pushinteger(L, pos.z); - return 3; + return lua::pushivec3(L, player->selection.position); } return 0; } const luaL_Reg playerlib [] = { - {"get_pos", lua_wrap_errors}, - {"set_pos", lua_wrap_errors}, - {"get_vel", lua_wrap_errors}, - {"set_vel", lua_wrap_errors}, - {"get_rot", lua_wrap_errors}, - {"set_rot", lua_wrap_errors}, - {"get_inventory", lua_wrap_errors}, - {"is_flight", lua_wrap_errors}, - {"set_flight", lua_wrap_errors}, - {"is_noclip", lua_wrap_errors}, - {"set_noclip", lua_wrap_errors}, - {"get_selected_block", lua_wrap_errors}, + {"get_pos", lua::wrap}, + {"set_pos", lua::wrap}, + {"get_vel", lua::wrap}, + {"set_vel", lua::wrap}, + {"get_rot", lua::wrap}, + {"set_rot", lua::wrap}, + {"get_inventory", lua::wrap}, + {"is_flight", lua::wrap}, + {"set_flight", lua::wrap}, + {"is_noclip", lua::wrap}, + {"set_noclip", lua::wrap}, + {"get_selected_block", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libtime.cpp b/src/logic/scripting/lua/libtime.cpp index b5a19a0a..392a387f 100644 --- a/src/logic/scripting/lua/libtime.cpp +++ b/src/logic/scripting/lua/libtime.cpp @@ -1,21 +1,18 @@ #include "api_lua.hpp" -#include "lua_commons.hpp" -#include "../scripting.hpp" + #include "../../../engine.hpp" #include "../../../window/Window.hpp" static int l_time_uptime(lua_State* L) { - lua_pushnumber(L, Window::time()); - return 1; + return lua::pushnumber(L, Window::time()); } static int l_time_delta(lua_State* L) { - lua_pushnumber(L, scripting::engine->getDelta()); - return 1; + return lua::pushnumber(L, scripting::engine->getDelta()); } const luaL_Reg timelib [] = { - {"uptime", lua_wrap_errors}, - {"delta", lua_wrap_errors}, + {"uptime", lua::wrap}, + {"delta", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libtoml.cpp b/src/logic/scripting/lua/libtoml.cpp index 29348eeb..5d5bb9dd 100644 --- a/src/logic/scripting/lua/libtoml.cpp +++ b/src/logic/scripting/lua/libtoml.cpp @@ -1,14 +1,8 @@ #include "api_lua.hpp" -#include "lua_util.hpp" -#include "lua_commons.hpp" -#include "LuaState.hpp" #include "../../../coders/toml.hpp" #include "../../../data/dynamic.hpp" -namespace scripting { - extern lua::LuaState* state; -} using namespace scripting; static int l_toml_stringify(lua_State* L) { @@ -16,8 +10,7 @@ static int l_toml_stringify(lua_State* L) { if (auto mapptr = std::get_if(&value)) { auto string = toml::stringify(**mapptr); - lua_pushstring(L, string.c_str()); - return 1; + return lua::pushstring(L, string); } else { throw std::runtime_error("table expected"); } @@ -27,12 +20,11 @@ static int l_toml_parse(lua_State* L) { auto string = lua::require_string(L, 1); auto element = toml::parse("", string); auto value = std::make_unique(element); - lua::pushvalue(L, *value); - return 1; + return lua::pushvalue(L, *value); } const luaL_Reg tomllib [] = { - {"tostring", lua_wrap_errors}, - {"parse", lua_wrap_errors}, + {"tostring", lua::wrap}, + {"parse", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libworld.cpp b/src/logic/scripting/lua/libworld.cpp index 51ec7e0e..13eddb7a 100644 --- a/src/logic/scripting/lua/libworld.cpp +++ b/src/logic/scripting/lua/libworld.cpp @@ -1,7 +1,5 @@ -#include "lua_commons.hpp" #include "api_lua.hpp" -#include "../scripting.hpp" #include "../../../assets/Assets.hpp" #include "../../../assets/AssetsLoader.hpp" #include "../../../files/engine_paths.hpp" @@ -12,10 +10,11 @@ #include #include +using namespace scripting; namespace fs = std::filesystem; static int l_world_get_list(lua_State* L) { - auto paths = scripting::engine->getPaths(); + auto paths = engine->getPaths(); auto worlds = paths->scanForWorlds(); lua_createtable(L, worlds.size(), 0); @@ -23,10 +22,10 @@ static int l_world_get_list(lua_State* L) { lua_createtable(L, 0, 1); auto name = worlds[i].filename().u8string(); - lua_pushstring(L, name.c_str()); - lua_setfield(L, -2, "name"); + lua::pushstring(L, name); + lua::setfield(L, "name"); - auto assets = scripting::engine->getAssets(); + auto assets = engine->getAssets(); std::string icon = "world:"+name+".icon"; if (!AssetsLoader::loadExternalTexture(assets, icon, { worlds[i]/fs::path("icon.png"), @@ -34,8 +33,8 @@ static int l_world_get_list(lua_State* L) { })) { icon = "gui/no_world_icon"; } - lua_pushstring(L, icon.c_str()); - lua_setfield(L, -2, "icon"); + lua::pushstring(L, icon); + lua::setfield(L, "icon"); lua_rawseti(L, -2, i + 1); } @@ -43,39 +42,35 @@ static int l_world_get_list(lua_State* L) { } static int l_world_get_total_time(lua_State* L) { - lua_pushnumber(L, scripting::level->getWorld()->totalTime); - return 1; + return lua::pushnumber(L, level->getWorld()->totalTime); } static int l_world_get_day_time(lua_State* L) { - lua_pushnumber(L, scripting::level->getWorld()->daytime); - return 1; + return lua::pushnumber(L, level->getWorld()->daytime); } static int l_world_set_day_time(lua_State* L) { - double value = lua_tonumber(L, 1); - scripting::level->getWorld()->daytime = fmod(value, 1.0); + auto value = lua::tonumber(L, 1); + level->getWorld()->daytime = fmod(value, 1.0); return 0; } static int l_world_get_seed(lua_State* L) { - lua_pushinteger(L, scripting::level->getWorld()->getSeed()); - return 1; + return lua::pushinteger(L, level->getWorld()->getSeed()); } static int l_world_exists(lua_State* L) { - auto name = lua_tostring(L, 1); - auto worldsDir = scripting::engine->getPaths()->getWorldFolder(name); - lua_pushboolean(L, fs::is_directory(worldsDir)); - return 1; + auto name = lua::require_string(L, 1); + auto worldsDir = engine->getPaths()->getWorldFolder(name); + return lua::pushboolean(L, fs::is_directory(worldsDir)); } const luaL_Reg worldlib [] = { - {"get_list", lua_wrap_errors}, - {"get_total_time", lua_wrap_errors}, - {"get_day_time", lua_wrap_errors}, - {"set_day_time", lua_wrap_errors}, - {"get_seed", lua_wrap_errors}, - {"exists", lua_wrap_errors}, + {"get_list", lua::wrap}, + {"get_total_time", lua::wrap}, + {"get_day_time", lua::wrap}, + {"set_day_time", lua::wrap}, + {"get_seed", lua::wrap}, + {"exists", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/lua_commons.cpp b/src/logic/scripting/lua/lua_commons.cpp new file mode 100644 index 00000000..00669d03 --- /dev/null +++ b/src/logic/scripting/lua/lua_commons.cpp @@ -0,0 +1,9 @@ +#include "lua_commons.hpp" + +#include "../../../debug/Logger.hpp" + +static debug::Logger logger("lua"); + +void lua::log_error(const std::string& text) { + logger.error() << text; +} diff --git a/src/logic/scripting/lua/lua_commons.hpp b/src/logic/scripting/lua/lua_commons.hpp index f51b7aac..e1e4dddb 100644 --- a/src/logic/scripting/lua/lua_commons.hpp +++ b/src/logic/scripting/lua/lua_commons.hpp @@ -1,34 +1,31 @@ #ifndef LOGIC_SCRIPTING_LUA_HPP_ #define LOGIC_SCRIPTING_LUA_HPP_ +#include "../../../delegates.hpp" +#include "../scripting.hpp" + #ifdef __linux__ #include #include #else #include #endif +#include + +#include +#include #ifndef LUAJIT_VERSION #error LuaJIT required #endif -#include -#include +namespace lua { + class luaerror : public std::runtime_error { + public: + luaerror(const std::string& message); + }; -template int lua_wrap_errors(lua_State *L) { - int result = 0; - try { - result = func(L); - } - // transform exception with description into lua_error - catch (std::exception &e) { - luaL_error(L, e.what()); - } - // Rethrow any other exception (lua error for example) - catch (...) { - throw; - } - return result; + void log_error(const std::string& text); } #endif // LOGIC_SCRIPTING_LUA_HPP_ diff --git a/src/logic/scripting/lua/lua_util.cpp b/src/logic/scripting/lua/lua_util.cpp index 24cee222..02a4dce3 100644 --- a/src/logic/scripting/lua/lua_util.cpp +++ b/src/logic/scripting/lua/lua_util.cpp @@ -1,9 +1,17 @@ #include "lua_util.hpp" -#include "../../../debug/Logger.hpp" #include "../../../util/stringutil.hpp" -static debug::Logger logger("lua"); +#include +#include + +using namespace lua; + +static int nextEnvironment = 1; + +std::string lua::env_name(int env) { + return "_ENV"+util::mangleid(env); +} int lua::pushvalue(lua_State* L, const dynamic::Value& value) { using namespace dynamic; @@ -97,10 +105,6 @@ dynamic::Value lua::tovalue(lua_State* L, int idx) { } } -void lua::logError(const std::string& text) { - logger.error() << text; -} - int lua::call(lua_State* L, int argc, int nresults) { if (lua_pcall(L, argc, nresults, 0)) { throw luaerror(lua_tostring(L, -1)); @@ -108,10 +112,110 @@ int lua::call(lua_State* L, int argc, int nresults) { return 1; } -int lua::callNoThrow(lua_State* L, int argc) { +int lua::call_nothrow(lua_State* L, int argc) { if (lua_pcall(L, argc, LUA_MULTRET, 0)) { - logError(lua_tostring(L, -1)); + log_error(lua_tostring(L, -1)); return 0; } return 1; } + +void lua::dump_stack(lua_State* L) { + int top = lua_gettop(L); + for (int i = 1; i <= top; i++) { + std::cout << std::setw(3) << i << std::setw(20) << luaL_typename(L, i) << std::setw(30); + switch (lua_type(L, i)) { + case LUA_TNUMBER: + std::cout << lua_tonumber(L, i); + break; + case LUA_TSTRING: + std::cout << lua_tostring(L, i); + break; + case LUA_TBOOLEAN: + std::cout << (lua_toboolean(L, i) ? "true" : "false"); + break; + case LUA_TNIL: + std::cout << "nil"; + break; + default: + std::cout << lua_topointer(L, i); + break; + } + std::cout << std::endl; + } +} + +static std::shared_ptr createLambdaHandler(lua_State* L) { + auto ptr = reinterpret_cast(lua_topointer(L, -1)); + auto name = util::mangleid(ptr); + getglobal(L, LAMBDAS_TABLE); + pushvalue(L, -2); + setfield(L, name); + pop(L, 2); + + return std::shared_ptr(new std::string(name), [=](std::string* name) { + getglobal(L, LAMBDAS_TABLE); + pushnil(L); + setfield(L, *name); + pop(L); + delete name; + }); +} + +runnable lua::create_runnable(lua_State* L) { + auto funcptr = createLambdaHandler(L); + return [=]() { + lua_getglobal(L, LAMBDAS_TABLE.c_str()); + lua_getfield(L, -1, funcptr->c_str()); + call_nothrow(L, 0); + }; +} + +scripting::common_func lua::create_lambda(lua_State* L) { + auto funcptr = createLambdaHandler(L); + return [=](const std::vector& args) { + lua_getglobal(L, LAMBDAS_TABLE.c_str()); + lua_getfield(L, -1, funcptr->c_str()); + for (const auto& arg : args) { + pushvalue(L, arg); + } + if (call(L, args.size(), 1)) { + auto result = tovalue(L, -1); + lua_pop(L, 1); + return result; + } + return dynamic::Value(dynamic::NONE); + }; +} + +int lua::createEnvironment(lua_State* L, int parent) { + int id = nextEnvironment++; + + // local env = {} + lua_createtable(L, 0, 1); + + // setmetatable(env, {__index=_G}) + lua_createtable(L, 0, 1); + if (parent == 0) { + pushglobals(L); + } else { + if (pushenv(L, parent) == 0) { + pushglobals(L); + } + } + setfield(L, "__index"); + lua_setmetatable(L, -2); + + // envname = env + setglobal(L, env_name(id)); + return id; +} + + +void lua::removeEnvironment(lua_State* L, int id) { + if (id == 0) { + return; + } + pushnil(L); + setglobal(L, env_name(id)); +} diff --git a/src/logic/scripting/lua/lua_util.hpp b/src/logic/scripting/lua/lua_util.hpp index e9833c79..dd4d7034 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -1,19 +1,33 @@ #ifndef LOGIC_SCRIPTING_LUA_UTIL_HPP_ #define LOGIC_SCRIPTING_LUA_UTIL_HPP_ -#include "LuaState.hpp" - -#ifdef __linux__ -#include -#include -#else -#include -#endif -#include - -#include +#include "lua_commons.hpp" namespace lua { + inline std::string LAMBDAS_TABLE = "$L"; + + std::string env_name(int env); + + template int wrap(lua_State *L) { + int result = 0; + try { + result = func(L); + } + // transform exception with description into lua_error + catch (std::exception &e) { + luaL_error(L, e.what()); + } + // Rethrow any other exception (lua error for example) + catch (...) { + throw; + } + return result; + } + + inline void pop(lua_State* L, int n=1) { + lua_pop(L, n); + } + // function wrappers with number of pushed values as return value inline int pushnil(lua_State* L) { @@ -144,6 +158,18 @@ namespace lua { return lua_toboolean(L, idx); } + inline lua_Integer tointeger(lua_State* L, int idx) { + return lua_tointeger(L, idx); + } + + inline lua_Number tonumber(lua_State* L, int idx) { + return lua_tonumber(L, idx); + } + + inline const char* tostring(lua_State* L, int idx) { + return lua_tostring(L, idx); + } + inline glm::vec2 tovec2(lua_State* L, int idx) { lua_pushvalue(L, idx); if (!lua_istable(L, idx) || lua_objlen(L, idx) < 2) { @@ -153,42 +179,30 @@ namespace lua { lua_Number x = lua_tonumber(L, -1); lua_pop(L, 1); lua_rawgeti(L, -1, 2); lua_Number y = lua_tonumber(L, -1); lua_pop(L, 1); - lua_pop(L, 1); + pop(L); return glm::vec2(x, y); } inline glm::vec4 tocolor(lua_State* L, int idx) { - lua_pushvalue(L, idx); + pushvalue(L, idx); if (!lua_istable(L, -1) || lua_objlen(L, idx) < 4) { throw std::runtime_error("RGBA array required"); } lua_rawgeti(L, -1, 1); - lua_Number r = lua_tonumber(L, -1); lua_pop(L, 1); + lua_Number r = tonumber(L, -1); pop(L); lua_rawgeti(L, -1, 2); - lua_Number g = lua_tonumber(L, -1); lua_pop(L, 1); + lua_Number g = tonumber(L, -1); pop(L); lua_rawgeti(L, -1, 3); - lua_Number b = lua_tonumber(L, -1); lua_pop(L, 1); + lua_Number b = tonumber(L, -1); pop(L); lua_rawgeti(L, -1, 4); - lua_Number a = lua_tonumber(L, -1); lua_pop(L, 1); - lua_pop(L, 1); + lua_Number a = tonumber(L, -1); pop(L); + pop(L); return glm::vec4(r/255, g/255, b/255, a/255); } - inline const char* require_string(lua_State* L, int idx) { - if (!lua_isstring(L, idx)) { - throw luaerror("string expected at "+std::to_string(idx)); - } - return lua_tostring(L, idx); - } - - std::wstring require_wstring(lua_State*, int idx); int pushvalue(lua_State*, const dynamic::Value& value); dynamic::Value tovalue(lua_State*, int idx); - inline void pop(lua_State* L, int n=1) { - lua_pop(L, n); - } - inline bool getfield(lua_State* L, const std::string& name, int idx=-1) { lua_getfield(L, idx, name.c_str()); if (lua_isnil(L, -1)) { @@ -204,8 +218,8 @@ namespace lua { inline bool getglobal(lua_State* L, const std::string& name) { lua_getglobal(L, name.c_str()); - if (lua_isnil(L, lua_gettop(L))) { - lua_pop(L, lua_gettop(L)); + if (lua_isnil(L, -1)) { + pop(L); return false; } return true; @@ -213,11 +227,11 @@ namespace lua { inline bool hasglobal(lua_State* L, const std::string& name) { lua_getglobal(L, name.c_str()); - if (lua_isnil(L, lua_gettop(L))) { - lua_pop(L, lua_gettop(L)); + if (lua_isnil(L, -1)) { + lua_pop(L, -1); return false; } - lua_pop(L, lua_gettop(L)); + pop(L); return true; } @@ -225,9 +239,70 @@ namespace lua { lua_setglobal(L, name.c_str()); } - void logError(const std::string& text); + inline const char* require_string(lua_State* L, int idx) { + if (!lua_isstring(L, idx)) { + throw luaerror("string expected at "+std::to_string(idx)); + } + return tostring(L, idx); + } + + std::wstring require_wstring(lua_State*, int idx); + + inline bool rename(lua_State* L, const std::string& from, const std::string& to) { + const char* src = from.c_str(); + lua_getglobal(L, src); + if (lua_isnil(L, lua_gettop(L))) { + lua_pop(L, lua_gettop(L)); + return false; + } + lua_setglobal(L, to.c_str()); + + // remove previous + lua_pushnil(L); + lua_setglobal(L, src); + return true; + } + + inline void remove(lua_State* L, const std::string& name) { + lua_pushnil(L); + lua_setglobal(L, name.c_str()); + } + + inline void loadbuffer(lua_State* L, int env, const std::string& src, const std::string& file) { + if (luaL_loadbuffer(L, src.c_str(), src.length(), file.c_str())) { + throw luaerror(lua_tostring(L, -1)); + } + if (env && getglobal(L, env_name(env))) { + lua_setfenv(L, -2); + } + } + int call(lua_State*, int argc, int nresults=-1); - int callNoThrow(lua_State*, int argc); + int call_nothrow(lua_State*, int argc); + + inline int eval(lua_State* L, int env, const std::string& src, const std::string& file="") { + auto srcText = "return "+src; + loadbuffer(L, env, srcText, file); + return call(L, 0); + } + + inline int execute(lua_State* L, int env, const std::string& src, const std::string& file="") { + loadbuffer(L, env, src, file); + return call_nothrow(L, 0); + } + + runnable create_runnable(lua_State*); + scripting::common_func create_lambda(lua_State* ); + + inline int pushenv(lua_State* L, int env) { + if (getglobal(L, env_name(env))) { + return 1; + } + return 0; + } + int createEnvironment(lua_State*, int parent); + void removeEnvironment(lua_State*, int id); + void dump_stack(lua_State*); } #endif // LOGIC_SCRIPTING_LUA_UTIL_HPP_ diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index 18185ac7..0de194db 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -43,7 +43,7 @@ static void load_script(const fs::path& name) { fs::path file = paths->getResources()/fs::path("scripts")/name; std::string src = files::read_string(file); - state->execute(state->getMainThread(), 0, src, file.u8string()); + lua::execute(state->getMainThread(), 0, src, file.u8string()); } void scripting::initialize(Engine* engine) { @@ -61,24 +61,24 @@ scriptenv scripting::get_root_environment() { scriptenv scripting::create_pack_environment(const ContentPack& pack) { auto L = state->getMainThread(); - int id = state->createEnvironment(L, 0); - state->pushenv(L, id); + int id = lua::createEnvironment(L, 0); + lua::pushenv(L, id); lua::pushvalue(L, -1); lua::setfield(L, "PACK_ENV"); lua::pushstring(L, pack.id); lua::setfield(L, "PACK_ID"); lua::pop(L); return std::shared_ptr(new int(id), [=](int* id) { - state->removeEnvironment(L, *id); + lua::removeEnvironment(L, *id); delete id; }); } scriptenv scripting::create_doc_environment(const scriptenv& parent, const std::string& name) { auto L = state->getMainThread(); - int id = state->createEnvironment(L, *parent); - state->pushenv(L, id); - lua_pushvalue(L, -1); + int id = lua::createEnvironment(L, *parent); + lua::pushenv(L, id); + lua::pushvalue(L, -1); lua::setfield(L, "DOC_ENV"); lua::pushstring(L, name); lua::setfield(L, "DOC_NAME"); @@ -86,7 +86,7 @@ scriptenv scripting::create_doc_environment(const scriptenv& parent, const std:: if (lua::getglobal(L, "Document")) { if (lua::getfield(L, "new")) { lua::pushstring(L, name); - if (lua::callNoThrow(L, 1)) { + if (lua::call_nothrow(L, 1)) { lua::setfield(L, "document", -3); } } @@ -94,7 +94,7 @@ scriptenv scripting::create_doc_environment(const scriptenv& parent, const std:: } lua::pop(L); return std::shared_ptr(new int(id), [=](int* id) { - state->removeEnvironment(L, *id); + lua::removeEnvironment(L, *id); delete id; }); } @@ -102,7 +102,7 @@ scriptenv scripting::create_doc_environment(const scriptenv& parent, const std:: void scripting::process_post_runnables() { auto L = state->getMainThread(); if (lua::getglobal(L, "__process_post_runnables")) { - lua::callNoThrow(L, 0); + lua::call_nothrow(L, 0); } } @@ -144,12 +144,12 @@ void scripting::on_world_quit() { for (auto& pack : scripting::engine->getContentPacks()) { lua::getfield(L, "unload"); lua::pushstring(L, pack.id); - lua::callNoThrow(L, 1); + lua::call_nothrow(L, 1); } lua::pop(L); if (lua::getglobal(L, "__scripts_cleanup")) { - lua::callNoThrow(L, 0); + lua::call_nothrow(L, 0); } scripting::level = nullptr; scripting::content = nullptr; @@ -263,7 +263,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 = state->getMainThread(); - if (state->pushenv(L, env) == 0) { + if (lua::pushenv(L, env) == 0) { lua::pushglobals(L); } if (lua::getfield(L, name)) { @@ -272,7 +272,7 @@ bool scripting::register_event(int env, const std::string& name, const std::stri lua::getfield(L, "on"); lua::pushstring(L, id); lua::getfield(L, name, -4); - lua::callNoThrow(L, 2); + lua::call_nothrow(L, 2); lua::pop(L); // remove previous name @@ -287,7 +287,7 @@ void scripting::load_block_script(const scriptenv& senv, const std::string& pref int env = *senv; std::string src = files::read_string(file); logger.info() << "script (block) " << file.u8string(); - state->execute(state->getMainThread(), env, src, file.u8string()); + lua::execute(state->getMainThread(), env, src, file.u8string()); funcsset.init = register_event(env, "init", prefix+".init"); funcsset.update = register_event(env, "on_update", prefix+".update"); funcsset.randupdate = register_event(env, "on_random_update", prefix+".randupdate"); @@ -301,7 +301,7 @@ void scripting::load_item_script(const scriptenv& senv, const std::string& prefi int env = *senv; std::string src = files::read_string(file); logger.info() << "script (item) " << file.u8string(); - state->execute(state->getMainThread(), env, src, file.u8string()); + lua::execute(state->getMainThread(), env, src, file.u8string()); funcsset.init = register_event(env, "init", prefix+".init"); funcsset.on_use = register_event(env, "on_use", prefix+".use"); @@ -314,8 +314,7 @@ void scripting::load_world_script(const scriptenv& senv, const std::string& pref std::string src = files::read_string(file); logger.info() << "loading world script for " << prefix; - - state->execute(state->getMainThread(), env, src, file.u8string()); + lua::execute(state->getMainThread(), env, src, file.u8string()); register_event(env, "init", prefix+".init"); register_event(env, "on_world_open", prefix+".worldopen"); @@ -330,7 +329,7 @@ void scripting::load_layout_script(const scriptenv& senv, const std::string& pre std::string src = files::read_string(file); logger.info() << "loading script " << file.u8string(); - state->execute(state->getMainThread(), env, src, file.u8string()); + lua::execute(state->getMainThread(), env, src, file.u8string()); script.onopen = register_event(env, "on_open", prefix+".open"); script.onprogress = register_event(env, "on_progress", prefix+".progress"); script.onclose = register_event(env, "on_close", prefix+".close"); diff --git a/src/logic/scripting/scripting_functional.cpp b/src/logic/scripting/scripting_functional.cpp index c381dc35..6f9bad9c 100644 --- a/src/logic/scripting/scripting_functional.cpp +++ b/src/logic/scripting/scripting_functional.cpp @@ -8,11 +8,10 @@ namespace scripting { extern lua::LuaState* state; } +using namespace scripting; static debug::Logger logger("scripting_func"); -using namespace scripting; - runnable scripting::create_runnable( const scriptenv& env, const std::string& src, @@ -20,8 +19,8 @@ runnable scripting::create_runnable( ) { auto L = state->getMainThread(); try { - state->loadbuffer(L, *env, src, file); - return state->createRunnable(L); + lua::loadbuffer(L, *env, src, file); + return lua::create_runnable(L); } catch (const lua::luaerror& err) { logger.error() << err.what(); return [](){}; @@ -35,7 +34,7 @@ static lua_State* processCallback( ) { auto L = state->getMainThread(); try { - if (state->eval(L, *env, src, file) != 0) { + if (lua::eval(L, *env, src, file) != 0) { return L; } } catch (lua::luaerror& err) { @@ -52,7 +51,7 @@ wstringconsumer scripting::create_wstring_consumer( return [=](const std::wstring& x){ if (auto L = processCallback(env, src, file)) { lua::pushwstring(L, x); - lua::callNoThrow(L, 1); + lua::call_nothrow(L, 1); } }; } @@ -65,7 +64,7 @@ wstringsupplier scripting::create_wstring_supplier( return [=](){ if (auto L = processCallback(env, src, file)) { if (lua_isfunction(L, -1)) { - lua::callNoThrow(L, 0); + lua::call_nothrow(L, 0); } auto str = lua::require_wstring(L, -1); lua_pop(L, 1); return str; @@ -82,7 +81,7 @@ wstringchecker scripting::create_wstring_validator( return [=](const std::wstring& x){ if (auto L = processCallback(env, src, file)) { lua::pushwstring(L, x); - if (lua::callNoThrow(L, 1)) + if (lua::call_nothrow(L, 1)) return lua::toboolean(L, -1); } return false; @@ -97,7 +96,7 @@ boolconsumer scripting::create_bool_consumer( return [=](bool x){ if (auto L = processCallback(env, src, file)) { lua::pushboolean(L, x); - lua::callNoThrow(L, 1); + lua::call_nothrow(L, 1); } }; } @@ -110,7 +109,7 @@ boolsupplier scripting::create_bool_supplier( return [=](){ if (auto L = processCallback(env, src, file)) { if (lua_isfunction(L, -1)) { - lua::callNoThrow(L, 0); + lua::call_nothrow(L, 0); } bool x = lua::toboolean(L,-1); lua_pop(L, 1); return x; @@ -127,7 +126,7 @@ doubleconsumer scripting::create_number_consumer( return [=](double x){ if (auto L = processCallback(env, src, file)) { lua::pushnumber(L, x); - lua::callNoThrow(L, 1); + lua::call_nothrow(L, 1); } }; } @@ -140,7 +139,7 @@ doublesupplier scripting::create_number_supplier( return [=](){ if (auto L = processCallback(env, src, file)) { if (lua_isfunction(L, -1)) { - lua::callNoThrow(L, 0); + lua::call_nothrow(L, 0); } auto x = lua_tonumber(L, -1); lua::pop(L); @@ -160,7 +159,7 @@ int_array_consumer scripting::create_int_array_consumer( for (uint i = 0; i < len; i++) { lua::pushinteger(L, arr[i]); } - lua::callNoThrow(L, len); + lua::call_nothrow(L, len); } }; } @@ -173,7 +172,7 @@ vec2supplier scripting::create_vec2_supplier( return [=]() { if (auto L = processCallback(env, src, file)) { if (lua_isfunction(L, -1)) { - lua::callNoThrow(L, 0); + lua::call_nothrow(L, 0); } auto y = lua_tonumber(L, -1); lua::pop(L); auto x = lua_tonumber(L, -1); lua::pop(L); diff --git a/src/logic/scripting/scripting_hud.cpp b/src/logic/scripting/scripting_hud.cpp index c1853dad..ebcb0642 100644 --- a/src/logic/scripting/scripting_hud.cpp +++ b/src/logic/scripting/scripting_hud.cpp @@ -24,7 +24,7 @@ void scripting::on_frontend_init(Hud* hud) { scripting::hud = hud; state->openlib(state->getMainThread(), "hud", hudlib); - for (auto& pack : scripting::engine->getContentPacks()) { + for (auto& pack : engine->getContentPacks()) { state->emitEvent(state->getMainThread(), 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_close() { - for (auto& pack : scripting::engine->getContentPacks()) { + for (auto& pack : engine->getContentPacks()) { state->emitEvent(state->getMainThread(), pack.id + ".hudclose", [&] (lua_State* L) { return lua::pushinteger(L, hud->getPlayer()->getId()); @@ -47,9 +47,7 @@ void scripting::load_hud_script(const scriptenv& senv, const std::string& packid std::string src = files::read_string(file); logger.info() << "loading script " << file.u8string(); - auto L = state->getMainThread(); - state->loadbuffer(L, env, src, file.u8string()); - lua::callNoThrow(L, 0); + lua::execute(state->getMainThread(), env, src, file.u8string()); register_event(env, "init", packid+".init"); register_event(env, "on_hud_open", packid+".hudopen"); From 90bc86408b49f385193c01afa6c5c53061a38af6 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 11 Jun 2024 13:18:30 +0300 Subject: [PATCH 3/6] refactor: LuaState replaced with lua_engine --- src/logic/scripting/lua/LuaState.hpp | 31 -- src/logic/scripting/lua/libaudio.cpp | 172 +++++------- src/logic/scripting/lua/libblock.cpp | 211 +++++++------- src/logic/scripting/lua/libconsole.cpp | 52 ++-- src/logic/scripting/lua/libcore.cpp | 45 ++- src/logic/scripting/lua/libfile.cpp | 50 ++-- src/logic/scripting/lua/libgui.cpp | 6 +- src/logic/scripting/lua/libhud.cpp | 69 +++-- src/logic/scripting/lua/libinput.cpp | 14 +- src/logic/scripting/lua/libinventory.cpp | 2 +- src/logic/scripting/lua/libjson.cpp | 2 +- src/logic/scripting/lua/libpack.cpp | 36 +-- src/logic/scripting/lua/libplayer.cpp | 2 +- src/logic/scripting/lua/libworld.cpp | 7 +- .../lua/{LuaState.cpp => lua_engine.cpp} | 108 ++++--- src/logic/scripting/lua/lua_engine.hpp | 21 ++ src/logic/scripting/lua/lua_overrides.cpp | 13 +- src/logic/scripting/lua/lua_util.cpp | 76 ++--- src/logic/scripting/lua/lua_util.hpp | 265 +++++++++++------- src/logic/scripting/scripting.cpp | 74 +++-- src/logic/scripting/scripting_functional.cpp | 48 ++-- src/logic/scripting/scripting_hud.cpp | 14 +- 22 files changed, 643 insertions(+), 675 deletions(-) delete mode 100644 src/logic/scripting/lua/LuaState.hpp rename src/logic/scripting/lua/{LuaState.cpp => lua_engine.cpp} (66%) create mode 100644 src/logic/scripting/lua/lua_engine.hpp diff --git a/src/logic/scripting/lua/LuaState.hpp b/src/logic/scripting/lua/LuaState.hpp deleted file mode 100644 index 2d724373..00000000 --- a/src/logic/scripting/lua/LuaState.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef LOGIC_SCRIPTING_LUA_STATE_HPP_ -#define LOGIC_SCRIPTING_LUA_STATE_HPP_ - -#include "lua_commons.hpp" - -#include "../scripting_functional.hpp" -#include "../../../data/dynamic.hpp" -#include "../../../delegates.hpp" - -#include -#include - -namespace lua { - class LuaState { - lua_State* mainThread; - - void removeLibFuncs(lua_State*, const char* libname, const char* funcs[]); - void createLibs(lua_State* L); - public: - LuaState(); - ~LuaState(); - - void openlib(lua_State*, const std::string& name, const luaL_Reg* libfuncs); - void addfunc(lua_State*, const std::string& name, lua_CFunction func); - - bool emitEvent(lua_State*, const std::string& name, std::function args=[](auto*){return 0;}); - lua_State* getMainThread() const; - }; -} - -#endif // LOGIC_SCRIPTING_LUA_STATE_HPP_ diff --git a/src/logic/scripting/lua/libaudio.cpp b/src/logic/scripting/lua/libaudio.cpp index 9db10d3a..e8fea81b 100644 --- a/src/logic/scripting/lua/libaudio.cpp +++ b/src/logic/scripting/lua/libaudio.cpp @@ -7,8 +7,8 @@ inline const char* DEFAULT_CHANNEL = "regular"; inline int extract_channel_index(lua_State* L, int idx) { const char* channel = DEFAULT_CHANNEL; - if (!lua_isnoneornil(L, idx)) { - channel = lua_tostring(L, idx); + if (!lua::isnoneornil(L, idx)) { + channel = lua::tostring(L, idx); } int index = audio::get_channel_index(channel); if (index == 0) { @@ -92,20 +92,19 @@ inline audio::speakerid_t play_stream( /// channel: string = "regular", /// loop: bool = false) static int l_audio_play_stream(lua_State* L) { - lua_pushinteger(L, static_cast( + return lua::pushinteger(L, static_cast( play_stream( - lua_tostring(L, 1), + lua::tostring(L, 1), false, - lua_tonumber(L, 2), - lua_tonumber(L, 3), - lua_tonumber(L, 4), - lua_tonumber(L, 5), - lua_tonumber(L, 6), - lua_toboolean(L, 8), + lua::tonumber(L, 2), + lua::tonumber(L, 3), + lua::tonumber(L, 4), + lua::tonumber(L, 5), + lua::tonumber(L, 6), + lua::toboolean(L, 8), extract_channel_index(L, 7) ) )); - return 1; } /// @brief audio.play_stream_2d( @@ -115,18 +114,17 @@ static int l_audio_play_stream(lua_State* L) { /// channel: string = "regular", /// loop: bool = false) static int l_audio_play_stream_2d(lua_State* L) { - lua_pushinteger(L, static_cast( + return lua::pushinteger(L, static_cast( play_stream( - lua_tostring(L, 1), + lua::tostring(L, 1), true, 0.0, 0.0, 0.0, - lua_tonumber(L, 2), - lua_tonumber(L, 3), - lua_toboolean(L, 5), + lua::tonumber(L, 2), + lua::tonumber(L, 3), + lua::toboolean(L, 5), extract_channel_index(L, 4) ) )); - return 1; } /// @brief audio.play_sound( @@ -139,20 +137,19 @@ static int l_audio_play_stream_2d(lua_State* L) { /// channel: string = "regular", /// loop: bool = false) static int l_audio_play_sound(lua_State* L) { - lua_pushinteger(L, static_cast( + return lua::pushinteger(L, static_cast( play_sound( - lua_tostring(L, 1), + lua::tostring(L, 1), false, - lua_tonumber(L, 2), - lua_tonumber(L, 3), - lua_tonumber(L, 4), - lua_tonumber(L, 5), - lua_tonumber(L, 6), - lua_toboolean(L, 8), + lua::tonumber(L, 2), + lua::tonumber(L, 3), + lua::tonumber(L, 4), + lua::tonumber(L, 5), + lua::tonumber(L, 6), + lua::toboolean(L, 8), extract_channel_index(L, 7) ) )); - return 1; } /// @brief audio.play_sound_2d( @@ -162,23 +159,22 @@ static int l_audio_play_sound(lua_State* L) { /// channel: string = "regular", /// loop: bool = false) static int l_audio_play_sound_2d(lua_State* L) { - lua_pushinteger(L, static_cast( + return lua::pushinteger(L, static_cast( play_sound( - lua_tostring(L, 1), + lua::tostring(L, 1), true, 0.0, 0.0, 0.0, - lua_tonumber(L, 2), - lua_tonumber(L, 3), - lua_toboolean(L, 5), + lua::tonumber(L, 2), + lua::tonumber(L, 3), + lua::toboolean(L, 5), extract_channel_index(L, 4) ) )); - return 1; } /// @brief audio.stop(speakerid: integer) -> nil static int l_audio_stop(lua_State* L) { - auto speaker = audio::get_speaker(lua_tointeger(L, 1)); + auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { speaker->stop(); } @@ -187,7 +183,7 @@ static int l_audio_stop(lua_State* L) { /// @brief audio.pause(speakerid: integer) -> nil static int l_audio_pause(lua_State* L) { - auto speaker = audio::get_speaker(lua_tointeger(L, 1)); + auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { speaker->pause(); } @@ -196,7 +192,7 @@ static int l_audio_pause(lua_State* L) { /// @brief audio.resume(speakerid: integer) -> nil static int l_audio_resume(lua_State* L) { - auto speaker = audio::get_speaker(lua_tointeger(L, 1)); + auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr && speaker->isPaused()) { speaker->play(); } @@ -205,48 +201,47 @@ static int l_audio_resume(lua_State* L) { /// @brief audio.set_loop(speakerid: integer, value: bool) -> nil static int l_audio_set_loop(lua_State* L) { - auto speaker = audio::get_speaker(lua_tointeger(L, 1)); + auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { - bool value = lua_toboolean(L, 2); - speaker->setLoop(value); + speaker->setLoop(lua::toboolean(L, 2)); } return 0; } /// @brief audio.set_volume(speakerid: integer, value: number) -> nil static int l_audio_set_volume(lua_State* L) { - auto speaker = audio::get_speaker(lua_tointeger(L, 1)); + auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { - speaker->setVolume(static_cast(lua_tonumber(L, 2))); + speaker->setVolume(static_cast(lua::tonumber(L, 2))); } return 0; } /// @brief audio.set_pitch(speakerid: integer, value: number) -> nil static int l_audio_set_pitch(lua_State* L) { - auto speaker = audio::get_speaker(lua_tointeger(L, 1)); + auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { - speaker->setPitch(static_cast(lua_tonumber(L, 2))); + speaker->setPitch(static_cast(lua::tonumber(L, 2))); } return 0; } /// @brief audio.set_time(speakerid: integer, value: number) -> nil static int l_audio_set_time(lua_State* L) { - auto speaker = audio::get_speaker(lua_tointeger(L, 1)); + auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { - speaker->setTime(static_cast(lua_tonumber(L, 2))); + speaker->setTime(static_cast(lua::tonumber(L, 2))); } return 0; } /// @brief audio.set_position(speakerid: integer, x: number, y: number, z: number) -> nil static int l_audio_set_position(lua_State* L) { - auto speaker = audio::get_speaker(lua_tointeger(L, 1)); + auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { - auto x = lua_tonumber(L, 2); - auto y = lua_tonumber(L, 3); - auto z = lua_tonumber(L, 4); + auto x = lua::tonumber(L, 2); + auto y = lua::tonumber(L, 3); + auto z = lua::tonumber(L, 4); speaker->setPosition(glm::vec3( static_cast(x), static_cast(y), @@ -258,11 +253,11 @@ static int l_audio_set_position(lua_State* L) { /// @brief audio.set_velocity(speakerid: integer, x: number, y: number, z: number) -> nil static int l_audio_set_velocity(lua_State* L) { - auto speaker = audio::get_speaker(lua_tointeger(L, 1)); + auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { - auto x = lua_tonumber(L, 2); - auto y = lua_tonumber(L, 3); - auto z = lua_tonumber(L, 4); + auto x = lua::tonumber(L, 2); + auto y = lua::tonumber(L, 3); + auto z = lua::tonumber(L, 4); speaker->setVelocity(glm::vec3( static_cast(x), static_cast(y), @@ -274,112 +269,93 @@ static int l_audio_set_velocity(lua_State* L) { /// @brief audio.is_playing(speakerid: integer) -> bool static int l_audio_is_playing(lua_State* L) { - auto speaker = audio::get_speaker(lua_tointeger(L, 1)); + auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { - lua_pushboolean(L, speaker->isPlaying()); - return 1; + return lua::pushboolean(L, speaker->isPlaying()); } - lua_pushboolean(L, false); - return 1; + return lua::pushboolean(L, false); } /// @brief audio.is_paused(speakerid: integer) -> bool static int l_audio_is_paused(lua_State* L) { - auto speaker = audio::get_speaker(lua_tointeger(L, 1)); + auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { - lua_pushboolean(L, speaker->isPaused()); - return 1; + return lua::pushboolean(L, speaker->isPaused()); } - lua_pushboolean(L, false); - return 1; + return lua::pushboolean(L, false); } /// @brief audio.is_loop(speakerid: integer) -> bool static int l_audio_is_loop(lua_State* L) { - auto speaker = audio::get_speaker(lua_tointeger(L, 1)); + auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { - lua_pushboolean(L, speaker->isLoop()); - return 1; + return lua::pushboolean(L, speaker->isLoop()); } - lua_pushboolean(L, false); - return 1; + return lua::pushboolean(L, false); } /// @brief audio.get_volume(speakerid: integer) -> number static int l_audio_get_volume(lua_State* L) { - auto speaker = audio::get_speaker(lua_tointeger(L, 1)); + auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { - lua_pushnumber(L, speaker->getVolume()); - return 1; + return lua::pushnumber(L, speaker->getVolume()); } - lua_pushnumber(L, 0.0); - return 1; + return lua::pushnumber(L, 0.0); } /// @brief audio.get_pitch(speakerid: integer) -> number static int l_audio_get_pitch(lua_State* L) { - auto speaker = audio::get_speaker(lua_tointeger(L, 1)); + auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { - lua_pushnumber(L, speaker->getPitch()); - return 1; + return lua::pushnumber(L, speaker->getPitch()); } - lua_pushnumber(L, 1.0); - return 1; + return lua::pushnumber(L, 1.0); } /// @brief audio.get_time(speakerid: integer) -> number static int l_audio_get_time(lua_State* L) { - auto speaker = audio::get_speaker(lua_tointeger(L, 1)); + auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { - lua_pushnumber(L, speaker->getTime()); - return 1; + return lua::pushnumber(L, speaker->getTime()); } - lua_pushnumber(L, 0.0); - return 1; + return lua::pushnumber(L, 0.0); } /// @brief audio.get_duration(speakerid: integer) -> number static int l_audio_get_duration(lua_State* L) { - auto speaker = audio::get_speaker(lua_tointeger(L, 1)); + auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { - lua_pushnumber(L, speaker->getDuration()); - return 1; + return lua::pushnumber(L, speaker->getDuration()); } - lua_pushnumber(L, 0.0); - return 1; + return lua::pushnumber(L, 0.0); } /// @brief audio.get_position(speakerid: integer) -> number, number, number static int l_audio_get_position(lua_State* L) { - auto speaker = audio::get_speaker(lua_tointeger(L, 1)); + auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { - lua::pushvec3(L, speaker->getPosition()); - return 1; + return lua::pushvec3(L, speaker->getPosition()); } return 0; } /// @brief audio.get_velocity(speakerid: integer) -> number, number, number static int l_audio_get_velocity(lua_State* L) { - auto speaker = audio::get_speaker(lua_tointeger(L, 1)); + auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { - auto vec = speaker->getVelocity(); - lua::pushvec3(L, vec); - return 1; + return lua::pushvec3(L, speaker->getVelocity()); } return 0; } // @brief audio.count_speakers() -> integer static int l_audio_count_speakers(lua_State* L) { - lua_pushinteger(L, audio::count_speakers()); - return 1; + return lua::pushinteger(L, audio::count_speakers()); } // @brief audio.count_streams() -> integer static int l_audio_count_streams(lua_State* L) { - lua_pushinteger(L, audio::count_streams()); - return 1; + return lua::pushinteger(L, audio::count_streams()); } const luaL_Reg audiolib [] = { diff --git a/src/logic/scripting/lua/libblock.cpp b/src/logic/scripting/lua/libblock.cpp index a09aa592..621222d8 100644 --- a/src/logic/scripting/lua/libblock.cpp +++ b/src/logic/scripting/lua/libblock.cpp @@ -9,9 +9,11 @@ #include "../../../content/Content.hpp" #include "../../../logic/BlocksController.hpp" +using namespace scripting; + static Block* require_block(lua_State* L) { - auto indices = scripting::content->getIndices(); - lua_Integer id = lua_tointeger(L, 1); + auto indices = content->getIndices(); + auto id = lua::tointeger(L, 1); if (static_cast(id) >= indices->countBlockDefs()) { return nullptr; } @@ -20,44 +22,38 @@ static Block* require_block(lua_State* L) { static int l_name(lua_State* L) { if (auto def = require_block(L)) { - lua_pushstring(L, def->name.c_str()); - return 1; + return lua::pushstring(L, def->name); } return 0; } static int l_material(lua_State* L) { if (auto def = require_block(L)) { - lua_pushstring(L, def->material.c_str()); - return 1; + return lua::pushstring(L, def->material); } return 0; } static int l_is_solid_at(lua_State* L) { - lua_Integer x = lua_tointeger(L, 1); - lua_Integer y = lua_tointeger(L, 2); - lua_Integer z = lua_tointeger(L, 3); + auto x = lua::tointeger(L, 1); + auto y = lua::tointeger(L, 2); + auto z = lua::tointeger(L, 3); - lua_pushboolean(L, scripting::level->chunks->isSolidBlock(x, y, z)); - return 1; + return lua::pushboolean(L, level->chunks->isSolidBlock(x, y, z)); } static int l_count(lua_State* L) { - lua_pushinteger(L, scripting::indices->countBlockDefs()); - return 1; + return lua::pushinteger(L, indices->countBlockDefs()); } static int l_index(lua_State* L) { - std::string name = lua_tostring(L, 1); - lua_pushinteger(L, scripting::content->requireBlock(name).rt.id); - return 1; + auto name = lua::require_string(L, 1); + return lua::pushinteger(L, content->requireBlock(name).rt.id); } static int l_is_extended(lua_State* L) { if (auto def = require_block(L)) { - lua_pushboolean(L, def->rt.extended); - return 1; + return lua::pushboolean(L, def->rt.extended); } return 0; } @@ -70,65 +66,61 @@ static int l_get_size(lua_State* L) { } static int l_is_segment(lua_State* L) { - lua_Integer x = lua_tointeger(L, 1); - lua_Integer y = lua_tointeger(L, 2); - lua_Integer z = lua_tointeger(L, 3); - auto vox = scripting::level->chunks->get(x, y, z); - - lua_pushboolean(L, vox->state.segment); - return 1; + auto x = lua::tointeger(L, 1); + auto y = lua::tointeger(L, 2); + auto z = lua::tointeger(L, 3); + auto vox = level->chunks->get(x, y, z); + return lua::pushboolean(L, vox->state.segment); } static int l_seek_origin(lua_State* L) { - lua_Integer x = lua_tointeger(L, 1); - lua_Integer y = lua_tointeger(L, 2); - lua_Integer z = lua_tointeger(L, 3); - auto vox = scripting::level->chunks->get(x, y, z); - auto def = scripting::indices->getBlockDef(vox->id); - - return lua::pushivec3(L, scripting::level->chunks->seekOrigin({x, y, z}, def, vox->state)); + auto x = lua::tointeger(L, 1); + auto y = lua::tointeger(L, 2); + auto z = lua::tointeger(L, 3); + auto vox = level->chunks->get(x, y, z); + auto def = indices->getBlockDef(vox->id); + return lua::pushivec3(L, level->chunks->seekOrigin({x, y, z}, def, vox->state)); } static int l_set(lua_State* L) { - lua_Integer x = lua_tointeger(L, 1); - lua_Integer y = lua_tointeger(L, 2); - lua_Integer z = lua_tointeger(L, 3); - lua_Integer id = lua_tointeger(L, 4); - lua_Integer state = lua_tointeger(L, 5); - bool noupdate = lua_toboolean(L, 6); - if (static_cast(id) >= scripting::indices->countBlockDefs()) { + auto x = lua::tointeger(L, 1); + auto y = lua::tointeger(L, 2); + auto z = lua::tointeger(L, 3); + auto id = lua::tointeger(L, 4); + auto state = lua::tointeger(L, 5); + bool noupdate = lua::toboolean(L, 6); + if (static_cast(id) >= indices->countBlockDefs()) { return 0; } - if (!scripting::level->chunks->get(x, y, z)) { + if (!level->chunks->get(x, y, z)) { return 0; } - scripting::level->chunks->set(x, y, z, id, int2blockstate(state)); - scripting::level->lighting->onBlockSet(x,y,z, id); + level->chunks->set(x, y, z, id, int2blockstate(state)); + level->lighting->onBlockSet(x,y,z, id); if (!noupdate) { - scripting::blocks->updateSides(x, y, z); + blocks->updateSides(x, y, z); } return 0; } static int l_get(lua_State* L) { - lua_Integer x = lua_tointeger(L, 1); - lua_Integer y = lua_tointeger(L, 2); - lua_Integer z = lua_tointeger(L, 3); - voxel* vox = scripting::level->chunks->get(x, y, z); + auto x = lua::tointeger(L, 1); + auto y = lua::tointeger(L, 2); + auto z = lua::tointeger(L, 3); + auto vox = level->chunks->get(x, y, z); int id = vox == nullptr ? -1 : vox->id; - lua_pushinteger(L, id); - return 1; + return lua::pushinteger(L, id); } static int l_get_x(lua_State* L) { - lua_Integer x = lua_tointeger(L, 1); - lua_Integer y = lua_tointeger(L, 2); - lua_Integer z = lua_tointeger(L, 3); - voxel* vox = scripting::level->chunks->get(x, y, z); + auto x = lua::tointeger(L, 1); + auto y = lua::tointeger(L, 2); + auto z = lua::tointeger(L, 3); + auto vox = level->chunks->get(x, y, z); if (vox == nullptr) { return lua::pushivec3(L, 1, 0, 0); } - auto def = scripting::level->content->getIndices()->getBlockDef(vox->id); + auto def = level->content->getIndices()->getBlockDef(vox->id); if (!def->rotatable) { return lua::pushivec3(L, 1, 0, 0); } else { @@ -138,14 +130,14 @@ static int l_get_x(lua_State* L) { } static int l_get_y(lua_State* L) { - lua_Integer x = lua_tointeger(L, 1); - lua_Integer y = lua_tointeger(L, 2); - lua_Integer z = lua_tointeger(L, 3); - voxel* vox = scripting::level->chunks->get(x, y, z); + auto x = lua::tointeger(L, 1); + auto y = lua::tointeger(L, 2); + auto z = lua::tointeger(L, 3); + auto vox = level->chunks->get(x, y, z); if (vox == nullptr) { return lua::pushivec3(L, 0, 1, 0); } - auto def = scripting::level->content->getIndices()->getBlockDef(vox->id); + auto def = level->content->getIndices()->getBlockDef(vox->id); if (!def->rotatable) { return lua::pushivec3(L, 0, 1, 0); } else { @@ -155,14 +147,14 @@ static int l_get_y(lua_State* L) { } static int l_get_z(lua_State* L) { - lua_Integer x = lua_tointeger(L, 1); - lua_Integer y = lua_tointeger(L, 2); - lua_Integer z = lua_tointeger(L, 3); - voxel* vox = scripting::level->chunks->get(x, y, z); + auto x = lua::tointeger(L, 1); + auto y = lua::tointeger(L, 2); + auto z = lua::tointeger(L, 3); + auto vox = level->chunks->get(x, y, z); if (vox == nullptr) { return lua::pushivec3(L, 0, 0, 1); } - auto def = scripting::level->content->getIndices()->getBlockDef(vox->id); + auto def = level->content->getIndices()->getBlockDef(vox->id); if (!def->rotatable) { return lua::pushivec3(L, 0, 0, 1); } else { @@ -172,83 +164,79 @@ static int l_get_z(lua_State* L) { } static int l_get_rotation(lua_State* L) { - lua_Integer x = lua_tointeger(L, 1); - lua_Integer y = lua_tointeger(L, 2); - lua_Integer z = lua_tointeger(L, 3); - voxel* vox = scripting::level->chunks->get(x, y, z); + auto x = lua::tointeger(L, 1); + auto y = lua::tointeger(L, 2); + auto z = lua::tointeger(L, 3); + voxel* vox = level->chunks->get(x, y, z); int rotation = vox == nullptr ? 0 : vox->state.rotation; - lua_pushinteger(L, rotation); - return 1; + return lua::pushinteger(L, rotation); } static int l_set_rotation(lua_State* L) { - lua_Integer x = lua_tointeger(L, 1); - lua_Integer y = lua_tointeger(L, 2); - lua_Integer z = lua_tointeger(L, 3); - lua_Integer value = lua_tointeger(L, 4); - scripting::level->chunks->setRotation(x, y, z, value); + auto x = lua::tointeger(L, 1); + auto y = lua::tointeger(L, 2); + auto z = lua::tointeger(L, 3); + auto value = lua::tointeger(L, 4); + level->chunks->setRotation(x, y, z, value); return 0; } static int l_get_states(lua_State* L) { - lua_Integer x = lua_tointeger(L, 1); - lua_Integer y = lua_tointeger(L, 2); - lua_Integer z = lua_tointeger(L, 3); - voxel* vox = scripting::level->chunks->get(x, y, z); + auto x = lua::tointeger(L, 1); + auto y = lua::tointeger(L, 2); + auto z = lua::tointeger(L, 3); + auto vox = level->chunks->get(x, y, z); int states = vox == nullptr ? 0 : blockstate2int(vox->state); - lua_pushinteger(L, states); - return 1; + return lua::pushinteger(L, states); } static int l_set_states(lua_State* L) { - lua_Integer x = lua_tointeger(L, 1); - lua_Integer y = lua_tointeger(L, 2); - lua_Integer z = lua_tointeger(L, 3); - lua_Integer states = lua_tointeger(L, 4); + auto x = lua::tointeger(L, 1); + auto y = lua::tointeger(L, 2); + auto z = lua::tointeger(L, 3); + auto states = lua::tointeger(L, 4); - Chunk* chunk = scripting::level->chunks->getChunkByVoxel(x, y, z); + auto chunk = level->chunks->getChunkByVoxel(x, y, z); if (chunk == nullptr) { return 0; } - voxel* vox = scripting::level->chunks->get(x, y, z); + auto vox = level->chunks->get(x, y, z); vox->state = int2blockstate(states); chunk->setModifiedAndUnsaved(); return 0; } static int l_get_user_bits(lua_State* L) { - lua_Integer x = lua_tointeger(L, 1); - lua_Integer y = lua_tointeger(L, 2); - lua_Integer z = lua_tointeger(L, 3); - lua_Integer offset = lua_tointeger(L, 4) + VOXEL_USER_BITS_OFFSET; - lua_Integer bits = lua_tointeger(L, 5); + auto x = lua::tointeger(L, 1); + auto y = lua::tointeger(L, 2); + auto z = lua::tointeger(L, 3); + auto offset = lua::tointeger(L, 4) + VOXEL_USER_BITS_OFFSET; + auto bits = lua::tointeger(L, 5); - voxel* vox = scripting::level->chunks->get(x, y, z); + auto vox = level->chunks->get(x, y, z); if (vox == nullptr) { - lua_pushinteger(L, 0); - return 1; + return lua::pushinteger(L, 0); } uint mask = ((1 << bits) - 1) << offset; uint data = (blockstate2int(vox->state) & mask) >> offset; - lua_pushinteger(L, data); - return 1; + return lua::pushinteger(L, data); } static int l_set_user_bits(lua_State* L) { - lua_Integer x = lua_tointeger(L, 1); - lua_Integer y = lua_tointeger(L, 2); - lua_Integer z = lua_tointeger(L, 3); - lua_Integer offset = lua_tointeger(L, 4); - lua_Integer bits = lua_tointeger(L, 5); + auto x = lua::tointeger(L, 1); + auto y = lua::tointeger(L, 2); + auto z = lua::tointeger(L, 3); + auto offset = lua::tointeger(L, 4); + auto bits = lua::tointeger(L, 5); size_t mask = ((1 << bits) - 1) << offset; - lua_Integer value = (lua_tointeger(L, 6) << offset) & mask; + auto value = (lua::tointeger(L, 6) << offset) & mask; - Chunk* chunk = scripting::level->chunks->getChunkByVoxel(x, y, z); + auto chunk = level->chunks->getChunkByVoxel(x, y, z); if (chunk == nullptr) { return 0; } - voxel* vox = scripting::level->chunks->get(x, y, z); + auto vox = level->chunks->get(x, y, z); if (vox == nullptr) { return 0; } @@ -258,18 +246,15 @@ static int l_set_user_bits(lua_State* L) { } static int l_is_replaceable_at(lua_State* L) { - lua_Integer x = lua_tointeger(L, 1); - lua_Integer y = lua_tointeger(L, 2); - lua_Integer z = lua_tointeger(L, 3); - - lua_pushboolean(L, scripting::level->chunks->isReplaceableBlock(x, y, z)); - return 1; + auto x = lua::tointeger(L, 1); + auto y = lua::tointeger(L, 2); + auto z = lua::tointeger(L, 3); + return lua::pushboolean(L, level->chunks->isReplaceableBlock(x, y, z)); } static int l_caption(lua_State* L) { if (auto def = require_block(L)) { - lua_pushstring(L, def->caption.c_str()); - return 1; + return lua::pushstring(L, def->caption); } return 0; } diff --git a/src/logic/scripting/lua/libconsole.cpp b/src/logic/scripting/lua/libconsole.cpp index f7eb6cb6..f7e71cb6 100644 --- a/src/logic/scripting/lua/libconsole.cpp +++ b/src/logic/scripting/lua/libconsole.cpp @@ -7,7 +7,7 @@ using namespace scripting; static int l_add_command(lua_State* L) { - if (!lua_isfunction(L, 3)) { + if (!lua::isfunction(L, 3)) { throw std::runtime_error("invalid callback"); } auto scheme = lua::require_string(L, 1); @@ -45,11 +45,11 @@ static int l_get_commands_list(lua_State* L) { auto repo = interpreter->getRepository(); const auto& commands = repo->getCommands(); - lua_createtable(L, commands.size(), 0); + lua::createtable(L, commands.size(), 0); size_t index = 1; for (const auto& entry : commands) { - lua_pushstring(L, entry.first.c_str()); - lua_rawseti(L, -2, index++); + lua::pushstring(L, entry.first); + lua::rawseti(L, index++); } return 1; } @@ -65,44 +65,44 @@ static int l_get_command_info(lua_State* L) { const auto& args = command->getArgs(); const auto& kwargs = command->getKwArgs(); - lua_createtable(L, 0, 4); + lua::createtable(L, 0, 4); - lua_pushstring(L, name); - lua_setfield(L, -2, "name"); + lua::pushstring(L, name); + lua::setfield(L, "name"); - lua_pushstring(L, command->getDescription().c_str()); - lua_setfield(L, -2, "description"); + lua::pushstring(L, command->getDescription()); + lua::setfield(L, "description"); - lua_createtable(L, args.size(), 0); + lua::createtable(L, args.size(), 0); for (size_t i = 0; i < args.size(); i++) { auto& arg = args.at(i); - lua_createtable(L, 0, 2); + lua::createtable(L, 0, 2); - lua_pushstring(L, arg.name.c_str()); - lua_setfield(L, -2, "name"); + lua::pushstring(L, arg.name); + lua::setfield(L, "name"); - lua_pushstring(L, cmd::argtype_name(arg.type).c_str()); - lua_setfield(L, -2, "type"); + lua::pushstring(L, cmd::argtype_name(arg.type)); + lua::setfield(L, "type"); if (arg.optional) { - lua_pushboolean(L, true); - lua_setfield(L, -2, "optional"); + lua::pushboolean(L, true); + lua::setfield(L, "optional"); } - lua_rawseti(L, -2, i+1); + lua::rawseti(L, i+1); } - lua_setfield(L, -2, "args"); + lua::setfield(L, "args"); - lua_createtable(L, 0, kwargs.size()); + lua::createtable(L, 0, kwargs.size()); for (auto& entry : kwargs) { auto& arg = entry.second; - lua_createtable(L, 0, 1); + lua::createtable(L, 0, 1); - lua_pushstring(L, cmd::argtype_name(arg.type).c_str()); - lua_setfield(L, -2, "type"); - - lua_setfield(L, -2, arg.name.c_str()); + lua::pushstring(L, cmd::argtype_name(arg.type)); + lua::setfield(L, "type"); + + lua::setfield(L, arg.name); } - lua_setfield(L, -2, "kwargs"); + lua::setfield(L, "kwargs"); return 1; } diff --git a/src/logic/scripting/lua/libcore.cpp b/src/logic/scripting/lua/libcore.cpp index eb98cca3..4d0135c8 100644 --- a/src/logic/scripting/lua/libcore.cpp +++ b/src/logic/scripting/lua/libcore.cpp @@ -44,7 +44,7 @@ static int l_close_world(lua_State* L) { if (controller == nullptr) { throw std::runtime_error("no world open"); } - bool save_world = lua_toboolean(L, 1); + bool save_world = lua::toboolean(L, 1); if (save_world) { controller->saveWorld(); } @@ -63,32 +63,32 @@ static int l_delete_world(lua_State* L) { } static int l_reconfig_packs(lua_State* L) { - if (!lua_istable(L, 1)) { + if (!lua::istable(L, 1)) { throw std::runtime_error("strings array expected as the first argument"); } - if (!lua_istable(L, 2)) { + if (!lua::istable(L, 2)) { throw std::runtime_error("strings array expected as the second argument"); } std::vector addPacks; - if (!lua_istable(L, 1)) { + if (!lua::istable(L, 1)) { throw std::runtime_error("an array expected as argument 1"); } - int addLen = lua_objlen(L, 1); + int addLen = lua::objlen(L, 1); for (int i = 0; i < addLen; i++) { - lua_rawgeti(L, 1, i+1); - addPacks.emplace_back(lua_tostring(L, -1)); - lua_pop(L, 1); + lua::rawgeti(L, i+1, 1); + addPacks.emplace_back(lua::tostring(L, -1)); + lua::pop(L); } std::vector remPacks; - if (!lua_istable(L, 2)) { + if (!lua::istable(L, 2)) { throw std::runtime_error("an array expected as argument 2"); } - int remLen = lua_objlen(L, 2); + int remLen = lua::objlen(L, 2); for (int i = 0; i < remLen; i++) { - lua_rawgeti(L, 2, i+1); - remPacks.emplace_back(lua_tostring(L, -1)); - lua_pop(L, 1); + lua::rawgeti(L, i+1, 2); + remPacks.emplace_back(lua::tostring(L, -1)); + lua::pop(L); } auto engine_controller = engine->getController(); engine_controller->reconfigPacks(controller, addPacks, remPacks); @@ -98,8 +98,7 @@ static int l_reconfig_packs(lua_State* L) { static int l_get_setting(lua_State* L) { auto name = lua::require_string(L, 1); const auto value = engine->getSettingsHandler().getValue(name); - lua::pushvalue(L, value); - return 1; + return lua::pushvalue(L, value); } static int l_set_setting(lua_State* L) { @@ -112,14 +111,13 @@ static int l_set_setting(lua_State* L) { static int l_str_setting(lua_State* L) { auto name = lua::require_string(L, 1); const auto string = engine->getSettingsHandler().toString(name); - lua::pushstring(L, string); - return 1; + return lua::pushstring(L, string); } static int l_get_setting_info(lua_State* L) { auto name = lua::require_string(L, 1); auto setting = engine->getSettingsHandler().getSetting(name); - lua_createtable(L, 0, 1); + lua::createtable(L, 0, 1); if (auto number = dynamic_cast(setting)) { lua::pushnumber(L, number->getMin()); lua::setfield(L, "min"); @@ -134,7 +132,7 @@ static int l_get_setting_info(lua_State* L) { lua::setfield(L, "max"); return 1; } - lua_pop(L, 1); + lua::pop(L); throw std::runtime_error("unsupported setting type"); } @@ -144,18 +142,17 @@ static int l_quit(lua_State*) { } static int l_get_default_generator(lua_State* L) { - lua::pushstring(L, WorldGenerators::getDefaultGeneratorID().c_str()); - return 1; + return lua::pushstring(L, WorldGenerators::getDefaultGeneratorID()); } static int l_get_generators(lua_State* L) { const auto& generators = WorldGenerators::getGeneratorsIDs(); - lua_createtable(L, generators.size(), 0); + lua::createtable(L, generators.size(), 0); int i = 0; for (auto& id : generators) { - lua::pushstring(L, id.c_str()); - lua_rawseti(L, -2, i + 1); + lua::pushstring(L, id); + lua::rawseti(L, i + 1); i++; } return 1; diff --git a/src/logic/scripting/lua/libfile.cpp b/src/logic/scripting/lua/libfile.cpp index b9477c4b..97c89bf0 100644 --- a/src/logic/scripting/lua/libfile.cpp +++ b/src/logic/scripting/lua/libfile.cpp @@ -26,8 +26,7 @@ static fs::path resolve_path_soft(const std::string& path) { static int l_file_find(lua_State* L) { auto path = lua::require_string(L, 1); try { - lua_pushstring(L, engine->getResPaths()->findRaw(path).c_str()); - return 1; + return lua::pushstring(L, engine->getResPaths()->findRaw(path)); } catch (const std::runtime_error& err) { return 0; } @@ -35,15 +34,13 @@ static int l_file_find(lua_State* L) { static int l_file_resolve(lua_State* L) { fs::path path = resolve_path(lua::require_string(L, 1)); - lua_pushstring(L, path.u8string().c_str()); - return 1; + return lua::pushstring(L, path.u8string()); } static int l_file_read(lua_State* L) { fs::path path = resolve_path(lua::require_string(L, 1)); if (fs::is_regular_file(path)) { - lua_pushstring(L, files::read_string(path).c_str()); - return 1; + return lua::pushstring(L, files::read_string(path)); } throw std::runtime_error("file does not exists "+util::quote(path.u8string())); } @@ -93,11 +90,10 @@ static int l_file_isdir(lua_State* L) { static int l_file_length(lua_State* L) { fs::path path = resolve_path(lua::require_string(L, 1)); if (fs::exists(path)){ - lua_pushinteger(L, fs::file_size(path)); + return lua::pushinteger(L, fs::file_size(path)); } else { - lua_pushinteger(L, -1); + return lua::pushinteger(L, -1); } - return 1; } static int l_file_mkdir(lua_State* L) { @@ -117,12 +113,12 @@ static int l_file_read_bytes(lua_State* L) { auto bytes = files::read_bytes(path, length); - lua_createtable(L, length, 0); - int newTable = lua_gettop(L); + lua::createtable(L, length, 0); + int newTable = lua::gettop(L); for(size_t i = 0; i < length; i++) { - lua_pushinteger(L, bytes[i]); - lua_rawseti(L, newTable, i+1); + lua::pushinteger(L, bytes[i]); + lua::rawseti(L, i+1, newTable); } return 1; } @@ -130,17 +126,17 @@ static int l_file_read_bytes(lua_State* L) { } static int read_bytes_from_table(lua_State* L, int tableIndex, std::vector& bytes) { - if(!lua_istable(L, tableIndex)) { + if(!lua::istable(L, tableIndex)) { throw std::runtime_error("table expected"); } else { - lua_pushnil(L); + lua::pushnil(L); while(lua_next(L, tableIndex - 1) != 0) { - const int byte = lua_tointeger(L, -1); + const int byte = lua::tointeger(L, -1); if(byte < 0 || byte > 255) { throw std::runtime_error("invalid byte '"+std::to_string(byte)+"'"); } bytes.push_back(byte); - lua_pop(L, 1); + lua::pop(L); } return 1; } @@ -149,7 +145,7 @@ static int read_bytes_from_table(lua_State* L, int tableIndex, std::vectorgetResPaths()->listdirRaw(path); - lua_createtable(L, files.size(), 0); + lua::createtable(L, files.size(), 0); for (size_t i = 0; i < files.size(); i++) { - lua_pushstring(L, files[i].c_str()); - lua_rawseti(L, -2, i+1); + lua::pushstring(L, files[i]); + lua::rawseti(L, i+1); } return 1; } @@ -185,13 +181,13 @@ static int l_file_list(lua_State* L) { if (!fs::is_directory(path)) { throw std::runtime_error(util::quote(path.u8string())+" is not a directory"); } - lua_createtable(L, 0, 0); + lua::createtable(L, 0, 0); size_t index = 1; for (auto& entry : fs::directory_iterator(path)) { auto name = entry.path().filename().u8string(); auto file = dirname + "/" + name; - lua_pushstring(L, file.c_str()); - lua_rawseti(L, -2, index); + lua::pushstring(L, file); + lua::rawseti(L, index); index++; } return 1; @@ -204,8 +200,7 @@ static int l_file_gzip_compress(lua_State* L) { auto compressed_bytes = gzip::compress(files::read_bytes(path, length).get(), length); - lua_pushboolean(L, files::write_bytes(path, compressed_bytes.data(), compressed_bytes.size())); - return 1; + return lua::pushboolean(L, files::write_bytes(path, compressed_bytes.data(), compressed_bytes.size())); } throw std::runtime_error("file does not exist " + util::quote(path.u8string())); } @@ -217,8 +212,7 @@ static int l_file_gzip_decompress(lua_State* L) { auto decompressed_bytes = gzip::decompress(files::read_bytes(path, length).get(), length); - lua_pushboolean(L, files::write_bytes(path, decompressed_bytes.data(), decompressed_bytes.size())); - return 1; + return lua::pushboolean(L, files::write_bytes(path, decompressed_bytes.data(), decompressed_bytes.size())); } throw std::runtime_error("file does not exist " + util::quote(path.u8string())); } diff --git a/src/logic/scripting/lua/libgui.cpp b/src/logic/scripting/lua/libgui.cpp index c61611bd..524c30c1 100644 --- a/src/logic/scripting/lua/libgui.cpp +++ b/src/logic/scripting/lua/libgui.cpp @@ -554,7 +554,7 @@ static int l_gui_get_env(lua_State* L) { static int l_gui_str(lua_State* L) { auto text = lua::require_wstring(L, 1); - if (!lua_isnoneornil(L, 2)) { + if (!lua::isnoneornil(L, 2)) { auto context = lua::require_wstring(L, 2); lua::pushwstring(L, langs::get(text, context)); } else { @@ -576,9 +576,9 @@ static int l_gui_reindex(lua_State* L) { /// @brief gui.get_locales_info() -> table of tables static int l_gui_get_locales_info(lua_State* L) { auto& locales = langs::locales_info; - lua_createtable(L, 0, locales.size()); + lua::createtable(L, 0, locales.size()); for (auto& entry : locales) { - lua_createtable(L, 0, 1); + lua::createtable(L, 0, 1); lua::pushstring(L, entry.second.name); lua::setfield(L, "name"); lua::setfield(L, entry.first); diff --git a/src/logic/scripting/lua/libhud.cpp b/src/logic/scripting/lua/libhud.cpp index 98a692e5..d782fd2d 100644 --- a/src/logic/scripting/lua/libhud.cpp +++ b/src/logic/scripting/lua/libhud.cpp @@ -21,65 +21,66 @@ namespace scripting { extern Hud* hud; } +using namespace scripting; static int l_hud_open_inventory(lua_State*) { - if (!scripting::hud->isInventoryOpen()) { - scripting::hud->openInventory(); + if (!hud->isInventoryOpen()) { + hud->openInventory(); } return 0; } static int l_hud_close_inventory(lua_State*) { - if (scripting::hud->isInventoryOpen()) { - scripting::hud->closeInventory(); + if (hud->isInventoryOpen()) { + hud->closeInventory(); } return 0; } static int l_hud_open_block(lua_State* L) { - auto x = lua_tointeger(L, 1); - auto y = lua_tointeger(L, 2); - auto z = lua_tointeger(L, 3); - bool playerInventory = !lua_toboolean(L, 4); + auto x = lua::tointeger(L, 1); + auto y = lua::tointeger(L, 2); + auto z = lua::tointeger(L, 3); + bool playerInventory = !lua::toboolean(L, 4); - voxel* vox = scripting::level->chunks->get(x, y, z); + auto vox = level->chunks->get(x, y, z); if (vox == nullptr) { throw std::runtime_error("block does not exists at " + std::to_string(x) + " " + std::to_string(y) + " " + std::to_string(z) ); } - auto def = scripting::content->getIndices()->getBlockDef(vox->id); - auto assets = scripting::engine->getAssets(); + auto def = content->getIndices()->getBlockDef(vox->id); + auto assets = engine->getAssets(); auto layout = assets->getLayout(def->uiLayout); if (layout == nullptr) { throw std::runtime_error("block '"+def->name+"' has no ui layout"); } - auto id = scripting::blocks->createBlockInventory(x, y, z); - scripting::hud->openInventory( - glm::ivec3(x, y, z), layout, scripting::level->inventories->get(id), playerInventory + auto id = blocks->createBlockInventory(x, y, z); + hud->openInventory( + glm::ivec3(x, y, z), layout, level->inventories->get(id), playerInventory ); - lua_pushinteger(L, id); - lua_pushstring(L, def->uiLayout.c_str()); + lua::pushinteger(L, id); + lua::pushstring(L, def->uiLayout); return 2; } static int l_hud_show_overlay(lua_State* L) { - const char* name = lua_tostring(L, 1); - bool playerInventory = lua_toboolean(L, 2); + auto name = lua::tostring(L, 1); + bool playerInventory = lua::toboolean(L, 2); - auto assets = scripting::engine->getAssets(); + auto assets = engine->getAssets(); auto layout = assets->getLayout(name); if (layout == nullptr) { throw std::runtime_error("there is no ui layout "+util::quote(name)); } - scripting::hud->showOverlay(layout, playerInventory); + hud->showOverlay(layout, playerInventory); return 0; } -static UiDocument* require_layout(lua_State* L, const char* name) { - auto assets = scripting::engine->getAssets(); +static UiDocument* require_layout(const char* name) { + auto assets = engine->getAssets(); auto layout = assets->getLayout(name); if (layout == nullptr) { throw std::runtime_error("layout '"+std::string(name)+"' is not found"); @@ -88,41 +89,39 @@ static UiDocument* require_layout(lua_State* L, const char* name) { } static int l_hud_open_permanent(lua_State* L) { - auto layout = require_layout(L, lua_tostring(L, 1)); - scripting::hud->openPermanent(layout); + auto layout = require_layout(lua::tostring(L, 1)); + hud->openPermanent(layout); return 0; } static int l_hud_close(lua_State* L) { - auto layout = require_layout(L, lua_tostring(L, 1)); - scripting::hud->remove(layout->getRoot()); + auto layout = require_layout(lua::tostring(L, 1)); + hud->remove(layout->getRoot()); return 0; } static int l_hud_pause(lua_State*) { - scripting::hud->setPause(true); + hud->setPause(true); return 0; } static int l_hud_resume(lua_State*) { - scripting::hud->setPause(false); + hud->setPause(false); return 0; } static int l_hud_get_block_inventory(lua_State* L) { - auto inventory = scripting::hud->getBlockInventory(); + auto inventory = hud->getBlockInventory(); if (inventory == nullptr) { - lua_pushinteger(L, 0); + return lua::pushinteger(L, 0); } else { - lua_pushinteger(L, inventory->getId()); + return lua::pushinteger(L, inventory->getId()); } - return 1; } static int l_hud_get_player(lua_State* L) { - auto player = scripting::hud->getPlayer(); - lua_pushinteger(L, player->getId()); - return 1; + auto player = hud->getPlayer(); + return lua::pushinteger(L, player->getId()); } const luaL_Reg hudlib [] = { diff --git a/src/logic/scripting/lua/libinput.cpp b/src/logic/scripting/lua/libinput.cpp index 33cb0898..d82919da 100644 --- a/src/logic/scripting/lua/libinput.cpp +++ b/src/logic/scripting/lua/libinput.cpp @@ -15,14 +15,12 @@ using namespace scripting; static int l_keycode(lua_State* L) { auto name = lua::require_string(L, 1); - lua_pushinteger(L, static_cast(input_util::keycode_from(name))); - return 1; + return lua::pushinteger(L, static_cast(input_util::keycode_from(name))); } static int l_mousecode(lua_State* L) { auto name = lua::require_string(L, 1); - lua_pushinteger(L, static_cast(input_util::mousecode_from(name))); - return 1; + return lua::pushinteger(L, static_cast(input_util::mousecode_from(name))); } static int l_add_callback(lua_State* L) { @@ -31,7 +29,7 @@ static int l_add_callback(lua_State* L) { if (bind == Events::bindings.end()) { throw std::runtime_error("unknown binding "+util::quote(bindname)); } - lua_pushvalue(L, 2); + lua::pushvalue(L, 2); runnable actual_callback = lua::create_runnable(L); runnable callback = [=]() { if (!scripting::engine->getGUI()->isFocusCaught()) { @@ -52,12 +50,12 @@ static int l_get_mouse_pos(lua_State* L) { static int l_get_bindings(lua_State* L) { auto& bindings = Events::bindings; - lua_createtable(L, bindings.size(), 0); + lua::createtable(L, bindings.size(), 0); int i = 0; for (auto& entry : bindings) { - lua_pushstring(L, entry.first.c_str()); - lua_rawseti(L, -2, i + 1); + lua::pushstring(L, entry.first); + lua::rawseti(L, i + 1); i++; } return 1; diff --git a/src/logic/scripting/lua/libinventory.cpp b/src/logic/scripting/lua/libinventory.cpp index 362e2449..d0d052da 100644 --- a/src/logic/scripting/lua/libinventory.cpp +++ b/src/logic/scripting/lua/libinventory.cpp @@ -122,7 +122,7 @@ static int l_inventory_move(lua_State* L) { validate_slotid(slotAid, invA.get()); auto invBid = lua::tointeger(L, 3); - auto slotBid = lua_isnil(L, 4) ? -1 : lua::tointeger(L, 4); + auto slotBid = lua::isnil(L, 4) ? -1 : lua::tointeger(L, 4); auto invB = get_inventory(invBid, 3); auto& slot = invA->getSlot(slotAid); if (slotBid == -1) { diff --git a/src/logic/scripting/lua/libjson.cpp b/src/logic/scripting/lua/libjson.cpp index 2a9508e6..055a272c 100644 --- a/src/logic/scripting/lua/libjson.cpp +++ b/src/logic/scripting/lua/libjson.cpp @@ -9,7 +9,7 @@ static int l_json_stringify(lua_State* L) { if (auto mapptr = std::get_if(&value)) { bool nice = lua::toboolean(L, 2); auto string = json::stringify(mapptr->get(), nice, " "); - return lua::pushstring(L, string.c_str()); + return lua::pushstring(L, string); } else { throw std::runtime_error("table expected"); } diff --git a/src/logic/scripting/lua/libpack.cpp b/src/logic/scripting/lua/libpack.cpp index 931823ac..b9c16c94 100644 --- a/src/logic/scripting/lua/libpack.cpp +++ b/src/logic/scripting/lua/libpack.cpp @@ -31,10 +31,10 @@ static int l_pack_get_folder(lua_State* L) { /// @brief pack.get_installed() -> array static int l_pack_get_installed(lua_State* L) { auto& packs = engine->getContentPacks(); - lua_createtable(L, packs.size(), 0); + lua::createtable(L, packs.size(), 0); for (size_t i = 0; i < packs.size(); i++) { lua::pushstring(L, packs[i].id); - lua_rawseti(L, -2, i + 1); + lua::rawseti(L, i + 1); } return 1; } @@ -54,16 +54,16 @@ static int l_pack_get_available(lua_State* L) { } auto names = manager.getAllNames(); - lua_createtable(L, names.size(), 0); + lua::createtable(L, names.size(), 0); for (size_t i = 0; i < names.size(); i++) { lua::pushstring(L, names[i]); - lua_rawseti(L, -2, i + 1); + lua::rawseti(L, i + 1); } return 1; } static int l_pack_get_info(lua_State* L, const ContentPack& pack, const Content* content) { - lua_createtable(L, 0, 5); + lua::createtable(L, 0, 5); lua::pushstring(L, pack.id); lua::setfield(L, "id"); @@ -92,7 +92,7 @@ static int l_pack_get_info(lua_State* L, const ContentPack& pack, const Content* lua::setfield(L, "icon"); if (!pack.dependencies.empty()) { - lua_createtable(L, pack.dependencies.size(), 0); + lua::createtable(L, pack.dependencies.size(), 0); for (size_t i = 0; i < pack.dependencies.size(); i++) { auto& dpack = pack.dependencies.at(i); std::string prefix; @@ -102,8 +102,8 @@ static int l_pack_get_info(lua_State* L, const ContentPack& pack, const Content* case DependencyLevel::weak: prefix = "~"; break; default: throw std::runtime_error(""); } - lua_pushfstring(L, "%s%s", prefix.c_str(), dpack.id.c_str()); - lua_rawseti(L, -2, i+1); + lua::pushfstring(L, "%s%s", prefix.c_str(), dpack.id.c_str()); + lua::rawseti(L, i+1); } lua::setfield(L, "dependencies"); } @@ -124,20 +124,20 @@ static int l_pack_get_info(lua_State* L, const ContentPack& pack, const Content* /// [optional] has_indices: bool /// } or nil static int l_pack_get_info(lua_State* L) { - auto packid = lua_tostring(L, 1); + auto packid = lua::tostring(L, 1); - auto content = scripting::engine->getContent(); - auto& packs = scripting::engine->getContentPacks(); + auto content = engine->getContent(); + auto& packs = engine->getContentPacks(); auto found = std::find_if(packs.begin(), packs.end(), [packid](const auto& pack) { return pack.id == packid; }); if (found == packs.end()) { // TODO: optimize fs::path worldFolder(""); - if (scripting::level) { - worldFolder = scripting::level->getWorld()->wfile->getFolder(); + if (level) { + worldFolder = level->getWorld()->wfile->getFolder(); } - auto manager = scripting::engine->createPacksManager(worldFolder); + auto manager = engine->createPacksManager(worldFolder); manager.scan(); auto vec = manager.getAll({packid}); if (!vec.empty()) { @@ -150,11 +150,11 @@ static int l_pack_get_info(lua_State* L) { } static int l_pack_get_base_packs(lua_State* L) { - auto& packs = scripting::engine->getBasePacks(); - lua_createtable(L, packs.size(), 0); + auto& packs = engine->getBasePacks(); + lua::createtable(L, packs.size(), 0); for (size_t i = 0; i < packs.size(); i++) { - lua_pushstring(L, packs[i].c_str()); - lua_rawseti(L, -2, i + 1); + lua::pushstring(L, packs[i]); + lua::rawseti(L, i + 1); } return 1; } diff --git a/src/logic/scripting/lua/libplayer.cpp b/src/logic/scripting/lua/libplayer.cpp index 6db20c60..59e6226a 100644 --- a/src/logic/scripting/lua/libplayer.cpp +++ b/src/logic/scripting/lua/libplayer.cpp @@ -69,7 +69,7 @@ static int l_player_set_rot(lua_State* L) { lua_Number x = lua::tonumber(L, 2); lua_Number y = lua::tonumber(L, 3); lua_Number z = cam.z; - if (lua_isnumber(L, 4)) { + if (lua::isnumber(L, 4)) { z = lua::tonumber(L, 4); } cam.x = x; diff --git a/src/logic/scripting/lua/libworld.cpp b/src/logic/scripting/lua/libworld.cpp index 13eddb7a..75b6914a 100644 --- a/src/logic/scripting/lua/libworld.cpp +++ b/src/logic/scripting/lua/libworld.cpp @@ -17,9 +17,9 @@ static int l_world_get_list(lua_State* L) { auto paths = engine->getPaths(); auto worlds = paths->scanForWorlds(); - lua_createtable(L, worlds.size(), 0); + lua::createtable(L, worlds.size(), 0); for (size_t i = 0; i < worlds.size(); i++) { - lua_createtable(L, 0, 1); + lua::createtable(L, 0, 1); auto name = worlds[i].filename().u8string(); lua::pushstring(L, name); @@ -35,8 +35,7 @@ static int l_world_get_list(lua_State* L) { } lua::pushstring(L, icon); lua::setfield(L, "icon"); - - lua_rawseti(L, -2, i + 1); + lua::rawseti(L, i + 1); } return 1; } diff --git a/src/logic/scripting/lua/LuaState.cpp b/src/logic/scripting/lua/lua_engine.cpp similarity index 66% rename from src/logic/scripting/lua/LuaState.cpp rename to src/logic/scripting/lua/lua_engine.cpp index 3321fe7e..df6d8a8c 100644 --- a/src/logic/scripting/lua/LuaState.cpp +++ b/src/logic/scripting/lua/lua_engine.cpp @@ -1,6 +1,5 @@ -#include "LuaState.hpp" +#include "lua_engine.hpp" -#include "lua_util.hpp" #include "api_lua.hpp" #include "../../../debug/Logger.hpp" #include "../../../util/stringutil.hpp" @@ -9,70 +8,23 @@ #include static debug::Logger logger("lua-state"); +static lua_State* main_thread = nullptr; using namespace lua; luaerror::luaerror(const std::string& message) : std::runtime_error(message) { } -void LuaState::removeLibFuncs(lua_State* L, const char* libname, const char* funcs[]) { +static void remove_lib_funcs(lua_State* L, const char* libname, const char* funcs[]) { if (getglobal(L, libname)) { for (uint i = 0; funcs[i]; i++) { - lua_pushnil(L); + pushnil(L); setfield(L, funcs[i], -2); } } } -LuaState::LuaState() { - logger.info() << LUA_VERSION; - logger.info() << LUAJIT_VERSION; - - auto L = luaL_newstate(); - if (L == nullptr) { - throw luaerror("could not to initialize Lua"); - } - mainThread = L; - // Allowed standard libraries - luaopen_base(L); - luaopen_math(L); - luaopen_string(L); - luaopen_table(L); - luaopen_debug(L); - luaopen_jit(L); - luaopen_bit(L); - - luaopen_os(L); - const char* removed_os[] { - "execute", - "exit", - "remove", - "rename", - "setlocale", - "tmpname", - nullptr - }; - removeLibFuncs(L, "os", removed_os); - - createLibs(L); - - pushglobals(L); - setglobal(L, env_name(0)); - - lua_createtable(L, 0, 0); - setglobal(L, LAMBDAS_TABLE); -} - -LuaState::~LuaState() { - lua_close(mainThread); -} - -void LuaState::addfunc(lua_State* L, const std::string& name, lua_CFunction func) { - lua_pushcfunction(L, func); - lua_setglobal(L, name.c_str()); -} - -void LuaState::createLibs(lua_State* L) { +static void create_libs(lua_State* L) { openlib(L, "audio", audiolib); openlib(L, "block", blocklib); openlib(L, "console", consolelib); @@ -92,13 +44,49 @@ void LuaState::createLibs(lua_State* L) { addfunc(L, "print", lua::wrap); } -void LuaState::openlib(lua_State* L, const std::string& name, const luaL_Reg* libfuncs) { - lua_newtable(L); - luaL_setfuncs(L, libfuncs, 0); - lua_setglobal(L, name.c_str()); +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; + // Allowed standard libraries + luaopen_base(L); + luaopen_math(L); + luaopen_string(L); + luaopen_table(L); + luaopen_debug(L); + luaopen_jit(L); + luaopen_bit(L); + + luaopen_os(L); + const char* removed_os[] { + "execute", + "exit", + "remove", + "rename", + "setlocale", + "tmpname", + nullptr + }; + remove_lib_funcs(L, "os", removed_os); + create_libs(L); + + pushglobals(L); + setglobal(L, env_name(0)); + + createtable(L, 0, 0); + setglobal(L, LAMBDAS_TABLE); } -bool LuaState::emitEvent(lua_State* L, const std::string &name, std::function args) { +void lua::finalize() { + lua_close(main_thread); +} + +bool lua::emit_event(lua_State* L, const std::string &name, std::function args) { getglobal(L, "events"); getfield(L, "emit"); pushstring(L, name); @@ -108,6 +96,6 @@ bool LuaState::emitEvent(lua_State* L, const std::string &name, std::function +#include + +namespace lua { + void initialize(); + void finalize(); + + bool emit_event(lua_State*, const std::string& name, std::function args=[](auto*){return 0;}); + lua_State* get_main_thread(); +} + +#endif // LOGIC_SCRIPTING_LUA_STATE_HPP_ diff --git a/src/logic/scripting/lua/lua_overrides.cpp b/src/logic/scripting/lua/lua_overrides.cpp index 5d68967b..9d52d993 100644 --- a/src/logic/scripting/lua/lua_overrides.cpp +++ b/src/logic/scripting/lua/lua_overrides.cpp @@ -1,24 +1,23 @@ #include "api_lua.hpp" -#include "lua_commons.hpp" #include /// @brief Modified version of luaB_print from lbaselib.c int l_print(lua_State* L) { int n = lua_gettop(L); /* number of arguments */ - lua_getglobal(L, "tostring"); + lua::getglobal(L, "tostring"); for (int i=1; i<=n; i++) { - lua_pushvalue(L, -1); /* function to be called */ - lua_pushvalue(L, i); /* value to print */ - lua_call(L, 1, 1); - const char* s = lua_tostring(L, -1); /* get result */ + lua::pushvalue(L, -1); /* function to be called */ + lua::pushvalue(L, i); /* value to print */ + lua::call(L, 1, 1); + const char* s = lua::tostring(L, -1); /* get result */ if (s == NULL) return luaL_error(L, LUA_QL("tostring") " must return a string to " LUA_QL("print")); if (i > 1) std::cout << "\t"; std::cout << s; - lua_pop(L, 1); /* pop result */ + lua::pop(L); /* pop result */ } std::cout << std::endl; return 0; diff --git a/src/logic/scripting/lua/lua_util.cpp b/src/logic/scripting/lua/lua_util.cpp index 02a4dce3..724282bb 100644 --- a/src/logic/scripting/lua/lua_util.cpp +++ b/src/logic/scripting/lua/lua_util.cpp @@ -17,29 +17,29 @@ int lua::pushvalue(lua_State* L, const dynamic::Value& value) { using namespace dynamic; if (auto* flag = std::get_if(&value)) { - lua_pushboolean(L, *flag); + pushboolean(L, *flag); } else if (auto* num = std::get_if(&value)) { - lua_pushinteger(L, *num); + pushinteger(L, *num); } else if (auto* num = std::get_if(&value)) { - lua_pushnumber(L, *num); + pushnumber(L, *num); } else if (auto* str = std::get_if(&value)) { - lua_pushstring(L, str->c_str()); + pushstring(L, *str); } else if (auto listptr = std::get_if(&value)) { auto list = *listptr; - lua_createtable(L, list->size(), 0); + createtable(L, list->size(), 0); for (size_t i = 0; i < list->size(); i++) { pushvalue(L, list->get(i)); - lua_rawseti(L, -2, i+1); + rawseti(L, i+1); } } else if (auto mapptr = std::get_if(&value)) { auto map = *mapptr; - lua_createtable(L, 0, map->size()); + createtable(L, 0, map->size()); for (auto& entry : map->values) { pushvalue(L, entry.second); - lua_setfield(L, -2, entry.first.c_str()); + setfield(L, entry.first); } } else { - lua_pushnil(L); + pushnil(L); } return 1; } @@ -54,47 +54,47 @@ int lua::pushwstring(lua_State* L, const std::wstring& str) { dynamic::Value lua::tovalue(lua_State* L, int idx) { using namespace dynamic; - auto type = lua_type(L, idx); + auto type = lua::type(L, idx); switch (type) { case LUA_TNIL: case LUA_TNONE: return dynamic::NONE; case LUA_TBOOLEAN: - return lua_toboolean(L, idx) == 1; + return toboolean(L, idx) == 1; case LUA_TNUMBER: { - auto number = lua_tonumber(L, idx); - auto integer = lua_tointeger(L, idx); - if (number == (lua_Number)integer) { + auto number = tonumber(L, idx); + auto integer = tointeger(L, idx); + if (number == static_cast(integer)) { return integer; } else { return number; } } case LUA_TSTRING: - return std::string(lua_tostring(L, idx)); + return std::string(tostring(L, idx)); case LUA_TTABLE: { - int len = lua_objlen(L, idx); + int len = lua::objlen(L, idx); if (len) { // array auto list = create_list(); for (int i = 1; i <= len; i++) { - lua_rawgeti(L, idx, i); + rawgeti(L, i, idx); list->put(tovalue(L, -1)); - lua_pop(L, 1); + pop(L); } return list; } else { // table auto map = create_map(); - lua_pushvalue(L, idx); - lua_pushnil(L); + pushvalue(L, idx); + pushnil(L); while (lua_next(L, -2)) { - lua_pushvalue(L, -2); - auto key = lua_tostring(L, -1); + pushvalue(L, -2); + auto key = tostring(L, -1); map->put(key, tovalue(L, -2)); - lua_pop(L, 2); + pop(L, 2); } - lua_pop(L, 1); + pop(L); return map; } } @@ -107,38 +107,38 @@ dynamic::Value lua::tovalue(lua_State* L, int idx) { int lua::call(lua_State* L, int argc, int nresults) { if (lua_pcall(L, argc, nresults, 0)) { - throw luaerror(lua_tostring(L, -1)); + throw luaerror(tostring(L, -1)); } return 1; } int lua::call_nothrow(lua_State* L, int argc) { if (lua_pcall(L, argc, LUA_MULTRET, 0)) { - log_error(lua_tostring(L, -1)); + log_error(tostring(L, -1)); return 0; } return 1; } void lua::dump_stack(lua_State* L) { - int top = lua_gettop(L); + int top = gettop(L); for (int i = 1; i <= top; i++) { std::cout << std::setw(3) << i << std::setw(20) << luaL_typename(L, i) << std::setw(30); - switch (lua_type(L, i)) { + switch (lua::type(L, i)) { case LUA_TNUMBER: - std::cout << lua_tonumber(L, i); + std::cout << tonumber(L, i); break; case LUA_TSTRING: - std::cout << lua_tostring(L, i); + std::cout << tostring(L, i); break; case LUA_TBOOLEAN: - std::cout << (lua_toboolean(L, i) ? "true" : "false"); + std::cout << (toboolean(L, i) ? "true" : "false"); break; case LUA_TNIL: std::cout << "nil"; break; default: - std::cout << lua_topointer(L, i); + std::cout << topointer(L, i); break; } std::cout << std::endl; @@ -146,7 +146,7 @@ void lua::dump_stack(lua_State* L) { } static std::shared_ptr createLambdaHandler(lua_State* L) { - auto ptr = reinterpret_cast(lua_topointer(L, -1)); + auto ptr = reinterpret_cast(topointer(L, -1)); auto name = util::mangleid(ptr); getglobal(L, LAMBDAS_TABLE); pushvalue(L, -2); @@ -174,14 +174,14 @@ runnable lua::create_runnable(lua_State* L) { scripting::common_func lua::create_lambda(lua_State* L) { auto funcptr = createLambdaHandler(L); return [=](const std::vector& args) { - lua_getglobal(L, LAMBDAS_TABLE.c_str()); - lua_getfield(L, -1, funcptr->c_str()); + getglobal(L, LAMBDAS_TABLE); + getfield(L, *funcptr); for (const auto& arg : args) { pushvalue(L, arg); } if (call(L, args.size(), 1)) { auto result = tovalue(L, -1); - lua_pop(L, 1); + pop(L); return result; } return dynamic::Value(dynamic::NONE); @@ -192,10 +192,10 @@ int lua::createEnvironment(lua_State* L, int parent) { int id = nextEnvironment++; // local env = {} - lua_createtable(L, 0, 1); + createtable(L, 0, 1); // setmetatable(env, {__index=_G}) - lua_createtable(L, 0, 1); + createtable(L, 0, 1); if (parent == 0) { pushglobals(L); } else { diff --git a/src/logic/scripting/lua/lua_util.hpp b/src/logic/scripting/lua/lua_util.hpp index dd4d7034..13a49372 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -27,6 +27,53 @@ namespace lua { inline void pop(lua_State* L, int n=1) { lua_pop(L, n); } + inline int gettop(lua_State* L) { + return lua_gettop(L); + } + inline size_t objlen(lua_State* L, int idx) { + return lua_objlen(L, idx); + } + inline int type(lua_State* L, int idx) { + return lua_type(L, idx); + } + inline const char* type_name(lua_State* L, int idx) { + return lua_typename(L, idx); + } + inline int rawgeti(lua_State* L, int n, int idx=-1) { + lua_rawgeti(L, idx, n); + return 1; + } + inline void rawseti(lua_State* L, int n, int idx=-2) { + lua_rawseti(L, idx, n); + } + + inline int createtable(lua_State* L, int narr, int nrec) { + lua_createtable(L, narr, nrec); + return 1; + } + + inline bool isnil(lua_State* L, int idx) { + return lua_isnil(L, idx); + } + + inline bool getglobal(lua_State* L, const std::string& name) { + lua_getglobal(L, name.c_str()); + if (isnil(L, -1)) { + pop(L); + return false; + } + return true; + } + + inline bool hasglobal(lua_State* L, const std::string& name) { + lua_getglobal(L, name.c_str()); + if (isnil(L, -1)) { + pop(L); + return false; + } + pop(L); + return true; + } // function wrappers with number of pushed values as return value @@ -46,88 +93,88 @@ namespace lua { } inline int pushivec3(lua_State* L, lua_Integer x, lua_Integer y, lua_Integer z) { - lua_pushinteger(L, x); - lua_pushinteger(L, y); - lua_pushinteger(L, z); + pushinteger(L, x); + pushinteger(L, y); + pushinteger(L, z); return 3; } inline int pushivec3(lua_State* L, glm::ivec3 vec) { - lua_pushinteger(L, vec.x); - lua_pushinteger(L, vec.y); - lua_pushinteger(L, vec.z); + pushinteger(L, vec.x); + pushinteger(L, vec.y); + pushinteger(L, vec.z); return 3; } inline int pushvec3(lua_State* L, glm::vec3 vec) { - lua_pushnumber(L, vec.x); - lua_pushnumber(L, vec.y); - lua_pushnumber(L, vec.z); + pushnumber(L, vec.x); + pushnumber(L, vec.y); + pushnumber(L, vec.z); return 3; } inline int pushvec4(lua_State* L, glm::vec4 vec) { - lua_pushnumber(L, vec.x); - lua_pushnumber(L, vec.y); - lua_pushnumber(L, vec.z); - lua_pushnumber(L, vec.w); + pushnumber(L, vec.x); + pushnumber(L, vec.y); + pushnumber(L, vec.z); + pushnumber(L, vec.w); return 4; } inline int pushvec2_arr(lua_State* L, glm::vec2 vec) { - lua_createtable(L, 2, 0); - lua_getglobal(L, "vec2_mt"); + createtable(L, 2, 0); + getglobal(L, "vec2_mt"); lua_setmetatable(L, -2); - lua_pushnumber(L, vec.x); - lua_rawseti(L, -2, 1); - lua_pushnumber(L, vec.y); - lua_rawseti(L, -2, 2); + pushnumber(L, vec.x); + rawseti(L, 1); + pushnumber(L, vec.y); + rawseti(L, 2); return 1; } inline int pushvec3_arr(lua_State* L, glm::vec3 vec) { - lua_createtable(L, 3, 0); - lua_getglobal(L, "vec3_mt"); + createtable(L, 3, 0); + getglobal(L, "vec3_mt"); lua_setmetatable(L, -2); - lua_pushnumber(L, vec.x); - lua_rawseti(L, -2, 1); - lua_pushnumber(L, vec.y); - lua_rawseti(L, -2, 2); - lua_pushnumber(L, vec.z); - lua_rawseti(L, -2, 3); + pushnumber(L, vec.x); + rawseti(L, 1); + pushnumber(L, vec.y); + rawseti(L, 2); + pushnumber(L, vec.z); + rawseti(L, 3); return 1; } inline int pushvec4_arr(lua_State* L, glm::vec4 vec) { - lua_createtable(L, 4, 0); - lua_getglobal(L, "vec4_mt"); + createtable(L, 4, 0); + getglobal(L, "vec4_mt"); lua_setmetatable(L, -2); - lua_pushnumber(L, vec.x); - lua_rawseti(L, -2, 1); - lua_pushnumber(L, vec.y); - lua_rawseti(L, -2, 2); - lua_pushnumber(L, vec.z); - lua_rawseti(L, -2, 3); - lua_pushnumber(L, vec.w); - lua_rawseti(L, -2, 4); + pushnumber(L, vec.x); + rawseti(L, 1); + pushnumber(L, vec.y); + rawseti(L, 2); + pushnumber(L, vec.z); + rawseti(L, 3); + pushnumber(L, vec.w); + rawseti(L, 4); return 1; } inline int pushcolor_arr(lua_State* L, glm::vec4 vec) { - lua_createtable(L, 4, 0); - lua_getglobal(L, "color_mt"); + createtable(L, 4, 0); + getglobal(L, "color_mt"); lua_setmetatable(L, -2); - lua_pushinteger(L, vec.x*255); - lua_rawseti(L, -2, 1); - lua_pushinteger(L, vec.y*255); - lua_rawseti(L, -2, 2); - lua_pushinteger(L, vec.z*255); - lua_rawseti(L, -2, 3); - lua_pushinteger(L, vec.w*255); - lua_rawseti(L, -2, 4); + pushinteger(L, vec.x*255); + rawseti(L, 1); + pushinteger(L, vec.y*255); + rawseti(L, 2); + pushinteger(L, vec.z*255); + rawseti(L, 3); + pushinteger(L, vec.w*255); + rawseti(L, 4); return 1; } inline int pushcfunction(lua_State* L, lua_CFunction func) { @@ -139,63 +186,84 @@ namespace lua { return 1; } + template + inline int pushfstring(lua_State *L, const char * fmt, Args... args) { + lua_pushfstring(L, fmt, args...); + return 1; + } + int pushwstring(lua_State* L, const std::wstring& str); inline int pushboolean(lua_State* L, bool value) { lua_pushboolean(L, value); return 1; } - inline int pushglobals(lua_State* L) { - lua_pushvalue(L, LUA_GLOBALSINDEX); - return 1; - } inline int pushvalue(lua_State* L, int idx) { lua_pushvalue(L, idx); return 1; } - + inline int pushglobals(lua_State* L) { + return pushvalue(L, LUA_GLOBALSINDEX); + } + inline bool isnoneornil(lua_State* L, int idx) { + return lua_isnoneornil(L, idx); + } + inline bool isboolean(lua_State* L, int idx) { + return lua_isboolean(L, idx); + } + inline bool isnumber(lua_State* L, int idx) { + return lua_isnumber(L, idx); + } + inline bool isstring(lua_State* L, int idx) { + return lua_isstring(L, idx); + } + inline bool istable(lua_State* L, int idx) { + return lua_istable(L, idx); + } + inline bool isfunction(lua_State* L, int idx) { + return lua_isfunction(L, idx); + } inline bool toboolean(lua_State* L, int idx) { return lua_toboolean(L, idx); } - inline lua_Integer tointeger(lua_State* L, int idx) { return lua_tointeger(L, idx); } - inline lua_Number tonumber(lua_State* L, int idx) { return lua_tonumber(L, idx); } - inline const char* tostring(lua_State* L, int idx) { return lua_tostring(L, idx); } - + inline const void* topointer(lua_State* L, int idx) { + return lua_topointer(L, idx); + } inline glm::vec2 tovec2(lua_State* L, int idx) { - lua_pushvalue(L, idx); - if (!lua_istable(L, idx) || lua_objlen(L, idx) < 2) { + pushvalue(L, idx); + if (!istable(L, idx) || objlen(L, idx) < 2) { throw std::runtime_error("value must be an array of two numbers"); } - lua_rawgeti(L, -1, 1); - lua_Number x = lua_tonumber(L, -1); lua_pop(L, 1); - lua_rawgeti(L, -1, 2); - lua_Number y = lua_tonumber(L, -1); lua_pop(L, 1); + rawgeti(L, 1); + auto x = tonumber(L, -1); pop(L); + rawgeti(L, 2); + auto y = tonumber(L, -1); pop(L); pop(L); return glm::vec2(x, y); } inline glm::vec4 tocolor(lua_State* L, int idx) { pushvalue(L, idx); - if (!lua_istable(L, -1) || lua_objlen(L, idx) < 4) { + if (!istable(L, -1) || objlen(L, idx) < 4) { throw std::runtime_error("RGBA array required"); } - lua_rawgeti(L, -1, 1); - lua_Number r = tonumber(L, -1); pop(L); - lua_rawgeti(L, -1, 2); - lua_Number g = tonumber(L, -1); pop(L); - lua_rawgeti(L, -1, 3); - lua_Number b = tonumber(L, -1); pop(L); - lua_rawgeti(L, -1, 4); - lua_Number a = tonumber(L, -1); pop(L); + rawgeti(L, 1); + auto r = tonumber(L, -1); pop(L); + rawgeti(L, 2); + auto g = tonumber(L, -1); pop(L); + rawgeti(L, 3); + auto b = tonumber(L, -1); pop(L); + rawgeti(L, 4); + auto a = tonumber(L, -1); pop(L); pop(L); return glm::vec4(r/255, g/255, b/255, a/255); } @@ -205,8 +273,8 @@ namespace lua { inline bool getfield(lua_State* L, const std::string& name, int idx=-1) { lua_getfield(L, idx, name.c_str()); - if (lua_isnil(L, -1)) { - lua_pop(L, -1); + if (isnil(L, -1)) { + pop(L); return false; } return true; @@ -216,31 +284,12 @@ namespace lua { lua_setfield(L, idx, name.c_str()); } - inline bool getglobal(lua_State* L, const std::string& name) { - lua_getglobal(L, name.c_str()); - if (lua_isnil(L, -1)) { - pop(L); - return false; - } - return true; - } - - inline bool hasglobal(lua_State* L, const std::string& name) { - lua_getglobal(L, name.c_str()); - if (lua_isnil(L, -1)) { - lua_pop(L, -1); - return false; - } - pop(L); - return true; - } - inline void setglobal(lua_State* L, const std::string& name) { lua_setglobal(L, name.c_str()); } inline const char* require_string(lua_State* L, int idx) { - if (!lua_isstring(L, idx)) { + if (!isstring(L, idx)) { throw luaerror("string expected at "+std::to_string(idx)); } return tostring(L, idx); @@ -249,28 +298,27 @@ namespace lua { std::wstring require_wstring(lua_State*, int idx); inline bool rename(lua_State* L, const std::string& from, const std::string& to) { - const char* src = from.c_str(); - lua_getglobal(L, src); - if (lua_isnil(L, lua_gettop(L))) { - lua_pop(L, lua_gettop(L)); + getglobal(L, from); + if (lua_isnil(L, -1)) { + pop(L, 1); return false; } - lua_setglobal(L, to.c_str()); + setglobal(L, to); // remove previous - lua_pushnil(L); - lua_setglobal(L, src); + pushnil(L); + setglobal(L, from); return true; } inline void remove(lua_State* L, const std::string& name) { - lua_pushnil(L); - lua_setglobal(L, name.c_str()); + pushnil(L); + setglobal(L, name); } inline void loadbuffer(lua_State* L, int env, const std::string& src, const std::string& file) { if (luaL_loadbuffer(L, src.c_str(), src.length(), file.c_str())) { - throw luaerror(lua_tostring(L, -1)); + throw luaerror(tostring(L, -1)); } if (env && getglobal(L, env_name(env))) { lua_setfenv(L, -2); @@ -303,6 +351,17 @@ namespace lua { int createEnvironment(lua_State*, int parent); void removeEnvironment(lua_State*, int id); void dump_stack(lua_State*); + + inline void addfunc(lua_State* L, const std::string& name, lua_CFunction func) { + pushcfunction(L, func); + setglobal(L, name); + } + + inline void openlib(lua_State* L, const std::string& name, const luaL_Reg* libfuncs) { + createtable(L, 0, 0); + luaL_setfuncs(L, libfuncs, 0); + setglobal(L, name); + } } #endif // LOGIC_SCRIPTING_LUA_UTIL_HPP_ diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index 0de194db..9c8b6eef 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -1,5 +1,7 @@ #include "scripting.hpp" +#include "lua/lua_engine.hpp" + #include "../../content/ContentPack.hpp" #include "../../debug/Logger.hpp" #include "../../engine.hpp" @@ -16,22 +18,15 @@ #include "../../util/timeutil.hpp" #include "../../voxels/Block.hpp" #include "../../world/Level.hpp" -#include "lua/LuaState.hpp" -#include "lua/lua_util.hpp" #include #include using namespace scripting; -namespace scripting { - extern lua::LuaState* state; -} - static debug::Logger logger("scripting"); Engine* scripting::engine = nullptr; -lua::LuaState* scripting::state = nullptr; Level* scripting::level = nullptr; const Content* scripting::content = nullptr; const ContentIndices* scripting::indices = nullptr; @@ -43,13 +38,12 @@ static void load_script(const fs::path& name) { fs::path file = paths->getResources()/fs::path("scripts")/name; std::string src = files::read_string(file); - lua::execute(state->getMainThread(), 0, src, file.u8string()); + lua::execute(lua::get_main_thread(), 0, src, file.u8string()); } void scripting::initialize(Engine* engine) { scripting::engine = engine; - - state = new lua::LuaState(); + lua::initialize(); load_script(fs::path("stdlib.lua")); load_script(fs::path("stdcmd.lua")); @@ -60,7 +54,7 @@ scriptenv scripting::get_root_environment() { } scriptenv scripting::create_pack_environment(const ContentPack& pack) { - auto L = state->getMainThread(); + auto L = lua::get_main_thread(); int id = lua::createEnvironment(L, 0); lua::pushenv(L, id); lua::pushvalue(L, -1); @@ -75,7 +69,7 @@ scriptenv scripting::create_pack_environment(const ContentPack& pack) { } scriptenv scripting::create_doc_environment(const scriptenv& parent, const std::string& name) { - auto L = state->getMainThread(); + auto L = lua::get_main_thread(); int id = lua::createEnvironment(L, *parent); lua::pushenv(L, id); lua::pushvalue(L, -1); @@ -100,7 +94,7 @@ scriptenv scripting::create_doc_environment(const scriptenv& parent, const std:: } void scripting::process_post_runnables() { - auto L = state->getMainThread(); + auto L = lua::get_main_thread(); if (lua::getglobal(L, "__process_post_runnables")) { lua::call_nothrow(L, 0); } @@ -114,30 +108,30 @@ void scripting::on_world_load(LevelController* controller) { scripting::controller = controller; load_script("world.lua"); - auto L = state->getMainThread(); + auto L = lua::get_main_thread(); for (auto& pack : scripting::engine->getContentPacks()) { - state->emitEvent(L, pack.id + ".worldopen"); + lua::emit_event(L, pack.id + ".worldopen"); } } void scripting::on_world_tick() { - auto L = state->getMainThread(); + auto L = lua::get_main_thread(); for (auto& pack : scripting::engine->getContentPacks()) { - state->emitEvent(L, pack.id + ".worldtick"); + lua::emit_event(L, pack.id + ".worldtick"); } } void scripting::on_world_save() { - auto L = state->getMainThread(); + auto L = lua::get_main_thread(); for (auto& pack : scripting::engine->getContentPacks()) { - state->emitEvent(L, pack.id + ".worldsave"); + lua::emit_event(L, pack.id + ".worldsave"); } } void scripting::on_world_quit() { - auto L = state->getMainThread(); + auto L = lua::get_main_thread(); for (auto& pack : scripting::engine->getContentPacks()) { - state->emitEvent(L, pack.id + ".worldquit"); + lua::emit_event(L, pack.id + ".worldquit"); } lua::getglobal(L, "pack"); @@ -160,28 +154,28 @@ void scripting::on_world_quit() { void scripting::on_blocks_tick(const Block* block, int tps) { std::string name = block->name + ".blockstick"; - state->emitEvent(state->getMainThread(), name, [tps] (lua_State* L) { + lua::emit_event(lua::get_main_thread(), name, [tps] (lua_State* 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"; - state->emitEvent(state->getMainThread(), name, [x, y, z] (lua_State* L) { + lua::emit_event(lua::get_main_thread(), name, [x, y, z] (lua_State* L) { return lua::pushivec3(L, x, y, z); }); } void scripting::random_update_block(const Block* block, int x, int y, int z) { std::string name = block->name + ".randupdate"; - state->emitEvent(state->getMainThread(), name, [x, y, z] (lua_State* L) { + lua::emit_event(lua::get_main_thread(), name, [x, y, z] (lua_State* L) { return lua::pushivec3(L, x, y, z); }); } void scripting::on_block_placed(Player* player, const Block* block, int x, int y, int z) { std::string name = block->name + ".placed"; - state->emitEvent(state->getMainThread(), name, [x, y, z, player] (lua_State* L) { + lua::emit_event(lua::get_main_thread(), name, [x, y, z, player] (lua_State* L) { lua::pushivec3(L, x, y, z); lua::pushinteger(L, player->getId()); return 4; @@ -190,7 +184,7 @@ void scripting::on_block_placed(Player* player, const Block* block, int x, int y void scripting::on_block_broken(Player* player, const Block* block, int x, int y, int z) { std::string name = block->name + ".broken"; - state->emitEvent(state->getMainThread(), name, [x, y, z, player] (lua_State* L) { + lua::emit_event(lua::get_main_thread(), name, [x, y, z, player] (lua_State* L) { lua::pushivec3(L, x, y, z); lua::pushinteger(L, player->getId()); return 4; @@ -199,7 +193,7 @@ void scripting::on_block_broken(Player* player, const Block* block, int x, int y bool scripting::on_block_interact(Player* player, const Block* block, glm::ivec3 pos) { std::string name = block->name + ".interact"; - return state->emitEvent(state->getMainThread(), name, [pos, player] (lua_State* L) { + return lua::emit_event(lua::get_main_thread(), name, [pos, player] (lua_State* L) { lua::pushivec3(L, pos.x, pos.y, pos.z); lua::pushinteger(L, player->getId()); return 4; @@ -208,14 +202,14 @@ bool scripting::on_block_interact(Player* player, const Block* block, glm::ivec3 bool scripting::on_item_use(Player* player, const ItemDef* item) { std::string name = item->name + ".use"; - return state->emitEvent(state->getMainThread(), name, [player] (lua_State* L) { + return lua::emit_event(lua::get_main_thread(), name, [player] (lua_State* L) { return lua::pushinteger(L, player->getId()); }); } bool scripting::on_item_use_on_block(Player* player, const ItemDef* item, int x, int y, int z) { std::string name = item->name + ".useon"; - return state->emitEvent(state->getMainThread(), name, [x, y, z, player] (lua_State* L) { + return lua::emit_event(lua::get_main_thread(), name, [x, y, z, player] (lua_State* L) { lua::pushivec3(L, x, y, z); lua::pushinteger(L, player->getId()); return 4; @@ -224,7 +218,7 @@ bool scripting::on_item_use_on_block(Player* player, const ItemDef* item, int x, bool scripting::on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z) { std::string name = item->name + ".blockbreakby"; - return state->emitEvent(state->getMainThread(), name, [x, y, z, player] (lua_State* L) { + return lua::emit_event(lua::get_main_thread(), name, [x, y, z, player] (lua_State* L) { lua::pushivec3(L, x, y, z); lua::pushinteger(L, player->getId()); return 4; @@ -237,7 +231,7 @@ void scripting::on_ui_open( ) { auto argsptr = std::make_shared>(std::move(args)); std::string name = layout->getId() + ".open"; - state->emitEvent(state->getMainThread(), name, [=] (lua_State* L) { + lua::emit_event(lua::get_main_thread(), name, [=] (lua_State* L) { for (const auto& value : *argsptr) { lua::pushvalue(L, value); } @@ -247,7 +241,7 @@ void scripting::on_ui_open( void scripting::on_ui_progress(UiDocument* layout, int workDone, int workTotal) { std::string name = layout->getId() + ".progress"; - state->emitEvent(state->getMainThread(), name, [=] (lua_State* L) { + lua::emit_event(lua::get_main_thread(), name, [=] (lua_State* L) { lua::pushinteger(L, workDone); lua::pushinteger(L, workTotal); return 2; @@ -256,13 +250,13 @@ void scripting::on_ui_progress(UiDocument* layout, int workDone, int workTotal) void scripting::on_ui_close(UiDocument* layout, Inventory* inventory) { std::string name = layout->getId() + ".close"; - state->emitEvent(state->getMainThread(), name, [inventory] (lua_State* L) { + lua::emit_event(lua::get_main_thread(), name, [inventory] (lua_State* L) { return lua::pushinteger(L, inventory ? inventory->getId() : 0); }); } bool scripting::register_event(int env, const std::string& name, const std::string& id) { - auto L = state->getMainThread(); + auto L = lua::get_main_thread(); if (lua::pushenv(L, env) == 0) { lua::pushglobals(L); } @@ -287,7 +281,7 @@ void scripting::load_block_script(const scriptenv& senv, const std::string& pref int env = *senv; std::string src = files::read_string(file); logger.info() << "script (block) " << file.u8string(); - lua::execute(state->getMainThread(), env, src, file.u8string()); + lua::execute(lua::get_main_thread(), env, src, file.u8string()); funcsset.init = register_event(env, "init", prefix+".init"); funcsset.update = register_event(env, "on_update", prefix+".update"); funcsset.randupdate = register_event(env, "on_random_update", prefix+".randupdate"); @@ -301,7 +295,7 @@ void scripting::load_item_script(const scriptenv& senv, const std::string& prefi int env = *senv; std::string src = files::read_string(file); logger.info() << "script (item) " << file.u8string(); - lua::execute(state->getMainThread(), env, src, file.u8string()); + lua::execute(lua::get_main_thread(), env, src, file.u8string()); funcsset.init = register_event(env, "init", prefix+".init"); funcsset.on_use = register_event(env, "on_use", prefix+".use"); @@ -314,7 +308,7 @@ void scripting::load_world_script(const scriptenv& senv, const std::string& pref std::string src = files::read_string(file); logger.info() << "loading world script for " << prefix; - lua::execute(state->getMainThread(), env, src, file.u8string()); + lua::execute(lua::get_main_thread(), env, src, file.u8string()); register_event(env, "init", prefix+".init"); register_event(env, "on_world_open", prefix+".worldopen"); @@ -329,16 +323,14 @@ void scripting::load_layout_script(const scriptenv& senv, const std::string& pre std::string src = files::read_string(file); logger.info() << "loading script " << file.u8string(); - lua::execute(state->getMainThread(), env, src, file.u8string()); + lua::execute(lua::get_main_thread(), env, src, file.u8string()); script.onopen = register_event(env, "on_open", prefix+".open"); script.onprogress = register_event(env, "on_progress", prefix+".progress"); script.onclose = register_event(env, "on_close", prefix+".close"); } void scripting::close() { - delete state; - - state = nullptr; + lua::finalize(); content = nullptr; indices = nullptr; level = nullptr; diff --git a/src/logic/scripting/scripting_functional.cpp b/src/logic/scripting/scripting_functional.cpp index 6f9bad9c..adc7370d 100644 --- a/src/logic/scripting/scripting_functional.cpp +++ b/src/logic/scripting/scripting_functional.cpp @@ -1,13 +1,9 @@ #include "scripting_functional.hpp" -#include "lua/lua_util.hpp" -#include "lua/LuaState.hpp" +#include "lua/lua_engine.hpp" #include "../../debug/Logger.hpp" #include "../../util/stringutil.hpp" -namespace scripting { - extern lua::LuaState* state; -} using namespace scripting; static debug::Logger logger("scripting_func"); @@ -17,7 +13,7 @@ runnable scripting::create_runnable( const std::string& src, const std::string& file ) { - auto L = state->getMainThread(); + auto L = lua::get_main_thread(); try { lua::loadbuffer(L, *env, src, file); return lua::create_runnable(L); @@ -27,12 +23,12 @@ runnable scripting::create_runnable( } } -static lua_State* processCallback( +static lua_State* process_callback( const scriptenv& env, const std::string& src, const std::string& file ) { - auto L = state->getMainThread(); + auto L = lua::get_main_thread(); try { if (lua::eval(L, *env, src, file) != 0) { return L; @@ -49,7 +45,7 @@ wstringconsumer scripting::create_wstring_consumer( const std::string& file ) { return [=](const std::wstring& x){ - if (auto L = processCallback(env, src, file)) { + if (auto L = process_callback(env, src, file)) { lua::pushwstring(L, x); lua::call_nothrow(L, 1); } @@ -62,11 +58,11 @@ wstringsupplier scripting::create_wstring_supplier( const std::string& file ) { return [=](){ - if (auto L = processCallback(env, src, file)) { - if (lua_isfunction(L, -1)) { + if (auto L = process_callback(env, src, file)) { + if (lua::isfunction(L, -1)) { lua::call_nothrow(L, 0); } - auto str = lua::require_wstring(L, -1); lua_pop(L, 1); + auto str = lua::require_wstring(L, -1); lua::pop(L); return str; } return std::wstring(L""); @@ -79,7 +75,7 @@ wstringchecker scripting::create_wstring_validator( const std::string& file ) { return [=](const std::wstring& x){ - if (auto L = processCallback(env, src, file)) { + if (auto L = process_callback(env, src, file)) { lua::pushwstring(L, x); if (lua::call_nothrow(L, 1)) return lua::toboolean(L, -1); @@ -94,7 +90,7 @@ boolconsumer scripting::create_bool_consumer( const std::string& file ) { return [=](bool x){ - if (auto L = processCallback(env, src, file)) { + if (auto L = process_callback(env, src, file)) { lua::pushboolean(L, x); lua::call_nothrow(L, 1); } @@ -107,11 +103,11 @@ boolsupplier scripting::create_bool_supplier( const std::string& file ) { return [=](){ - if (auto L = processCallback(env, src, file)) { - if (lua_isfunction(L, -1)) { + if (auto L = process_callback(env, src, file)) { + if (lua::isfunction(L, -1)) { lua::call_nothrow(L, 0); } - bool x = lua::toboolean(L,-1); lua_pop(L, 1); + bool x = lua::toboolean(L,-1); lua::pop(L); return x; } return false; @@ -124,7 +120,7 @@ doubleconsumer scripting::create_number_consumer( const std::string& file ) { return [=](double x){ - if (auto L = processCallback(env, src, file)) { + if (auto L = process_callback(env, src, file)) { lua::pushnumber(L, x); lua::call_nothrow(L, 1); } @@ -137,11 +133,11 @@ doublesupplier scripting::create_number_supplier( const std::string& file ) { return [=](){ - if (auto L = processCallback(env, src, file)) { - if (lua_isfunction(L, -1)) { + if (auto L = process_callback(env, src, file)) { + if (lua::isfunction(L, -1)) { lua::call_nothrow(L, 0); } - auto x = lua_tonumber(L, -1); + auto x = lua::tonumber(L, -1); lua::pop(L); return x; } @@ -155,7 +151,7 @@ int_array_consumer scripting::create_int_array_consumer( const std::string& file ) { return [=](const int arr[], size_t len) { - if (auto L = processCallback(env, src, file)) { + if (auto L = process_callback(env, src, file)) { for (uint i = 0; i < len; i++) { lua::pushinteger(L, arr[i]); } @@ -170,12 +166,12 @@ vec2supplier scripting::create_vec2_supplier( const std::string& file ) { return [=]() { - if (auto L = processCallback(env, src, file)) { - if (lua_isfunction(L, -1)) { + if (auto L = process_callback(env, src, file)) { + if (lua::isfunction(L, -1)) { lua::call_nothrow(L, 0); } - auto y = lua_tonumber(L, -1); lua::pop(L); - auto x = lua_tonumber(L, -1); lua::pop(L); + auto y = lua::tonumber(L, -1); lua::pop(L); + auto x = lua::tonumber(L, -1); lua::pop(L); return glm::vec2(x, y); } return glm::vec2(0, 0); diff --git a/src/logic/scripting/scripting_hud.cpp b/src/logic/scripting/scripting_hud.cpp index ebcb0642..b1318b69 100644 --- a/src/logic/scripting/scripting_hud.cpp +++ b/src/logic/scripting/scripting_hud.cpp @@ -1,9 +1,8 @@ #include "scripting_hud.hpp" #include "scripting.hpp" -#include "lua/lua_util.hpp" #include "lua/api_lua.hpp" -#include "lua/LuaState.hpp" +#include "lua/lua_engine.hpp" #include "../../debug/Logger.hpp" #include "../../frontend/hud.hpp" @@ -11,9 +10,6 @@ #include "../../files/files.hpp" #include "../../engine.hpp" -namespace scripting { - extern lua::LuaState* state; -} using namespace scripting; static debug::Logger logger("scripting-hud"); @@ -22,10 +18,10 @@ Hud* scripting::hud = nullptr; void scripting::on_frontend_init(Hud* hud) { scripting::hud = hud; - state->openlib(state->getMainThread(), "hud", hudlib); + lua::openlib(lua::get_main_thread(), "hud", hudlib); for (auto& pack : engine->getContentPacks()) { - state->emitEvent(state->getMainThread(), pack.id + ".hudopen", + lua::emit_event(lua::get_main_thread(), pack.id + ".hudopen", [&] (lua_State* L) { return lua::pushinteger(L, hud->getPlayer()->getId()); }); @@ -34,7 +30,7 @@ void scripting::on_frontend_init(Hud* hud) { void scripting::on_frontend_close() { for (auto& pack : engine->getContentPacks()) { - state->emitEvent(state->getMainThread(), pack.id + ".hudclose", + lua::emit_event(lua::get_main_thread(), pack.id + ".hudclose", [&] (lua_State* L) { return lua::pushinteger(L, hud->getPlayer()->getId()); }); @@ -47,7 +43,7 @@ void scripting::load_hud_script(const scriptenv& senv, const std::string& packid std::string src = files::read_string(file); logger.info() << "loading script " << file.u8string(); - lua::execute(state->getMainThread(), env, src, file.u8string()); + lua::execute(lua::get_main_thread(), env, src, file.u8string()); register_event(env, "init", packid+".init"); register_event(env, "on_hud_open", packid+".hudopen"); From 913e5983b127b3defc6eae52d15b28f2143d1bcb Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 11 Jun 2024 13:51:11 +0300 Subject: [PATCH 4/6] refactor: complete 'lua' namespace use --- src/logic/scripting/lua/api_lua.hpp | 2 +- src/logic/scripting/lua/libaudio.cpp | 78 ++++----- src/logic/scripting/lua/libblock.cpp | 46 ++--- src/logic/scripting/lua/libconsole.cpp | 10 +- src/logic/scripting/lua/libcore.cpp | 26 +-- src/logic/scripting/lua/libfile.cpp | 40 ++--- src/logic/scripting/lua/libgui.cpp | 168 +++++++++---------- src/logic/scripting/lua/libhud.cpp | 20 +-- src/logic/scripting/lua/libinput.cpp | 10 +- src/logic/scripting/lua/libinventory.cpp | 18 +- src/logic/scripting/lua/libitem.cpp | 8 +- src/logic/scripting/lua/libjson.cpp | 4 +- src/logic/scripting/lua/libpack.cpp | 12 +- src/logic/scripting/lua/libplayer.cpp | 32 ++-- src/logic/scripting/lua/libtime.cpp | 4 +- src/logic/scripting/lua/libtoml.cpp | 4 +- src/logic/scripting/lua/libworld.cpp | 12 +- src/logic/scripting/lua/lua_commons.hpp | 4 + src/logic/scripting/lua/lua_engine.cpp | 10 +- src/logic/scripting/lua/lua_engine.hpp | 4 +- src/logic/scripting/lua/lua_overrides.cpp | 4 +- src/logic/scripting/lua/lua_util.cpp | 40 ++--- src/logic/scripting/lua/lua_util.hpp | 143 ++++++++-------- src/logic/scripting/scripting.cpp | 24 +-- src/logic/scripting/scripting_functional.cpp | 2 +- src/logic/scripting/scripting_hud.cpp | 4 +- 26 files changed, 370 insertions(+), 359 deletions(-) diff --git a/src/logic/scripting/lua/api_lua.hpp b/src/logic/scripting/lua/api_lua.hpp index c1ff9ebf..81c20644 100644 --- a/src/logic/scripting/lua/api_lua.hpp +++ b/src/logic/scripting/lua/api_lua.hpp @@ -24,6 +24,6 @@ extern const luaL_Reg tomllib []; extern const luaL_Reg worldlib []; // Lua Overrides -extern int l_print(lua_State* L); +extern int l_print(lua::State* L); #endif // LOGIC_SCRIPTING_API_LUA_HPP_ diff --git a/src/logic/scripting/lua/libaudio.cpp b/src/logic/scripting/lua/libaudio.cpp index e8fea81b..bec44719 100644 --- a/src/logic/scripting/lua/libaudio.cpp +++ b/src/logic/scripting/lua/libaudio.cpp @@ -5,7 +5,7 @@ inline const char* DEFAULT_CHANNEL = "regular"; -inline int extract_channel_index(lua_State* L, int idx) { +inline int extract_channel_index(lua::State* L, int idx) { const char* channel = DEFAULT_CHANNEL; if (!lua::isnoneornil(L, idx)) { channel = lua::tostring(L, idx); @@ -20,11 +20,11 @@ inline int extract_channel_index(lua_State* L, int idx) { inline audio::speakerid_t play_sound( const char* name, bool relative, - lua_Number x, - lua_Number y, - lua_Number z, - lua_Number volume, - lua_Number pitch, + lua::Number x, + lua::Number y, + lua::Number z, + lua::Number volume, + lua::Number pitch, bool loop, int channel ) { @@ -55,11 +55,11 @@ inline audio::speakerid_t play_sound( inline audio::speakerid_t play_stream( const char* filename, bool relative, - lua_Number x, - lua_Number y, - lua_Number z, - lua_Number volume, - lua_Number pitch, + lua::Number x, + lua::Number y, + lua::Number z, + lua::Number volume, + lua::Number pitch, bool loop, int channel ) { @@ -91,8 +91,8 @@ inline audio::speakerid_t play_stream( /// pitch: number, /// channel: string = "regular", /// loop: bool = false) -static int l_audio_play_stream(lua_State* L) { - return lua::pushinteger(L, static_cast( +static int l_audio_play_stream(lua::State* L) { + return lua::pushinteger(L, static_cast( play_stream( lua::tostring(L, 1), false, @@ -113,8 +113,8 @@ static int l_audio_play_stream(lua_State* L) { /// pitch: number, /// channel: string = "regular", /// loop: bool = false) -static int l_audio_play_stream_2d(lua_State* L) { - return lua::pushinteger(L, static_cast( +static int l_audio_play_stream_2d(lua::State* L) { + return lua::pushinteger(L, static_cast( play_stream( lua::tostring(L, 1), true, @@ -136,8 +136,8 @@ static int l_audio_play_stream_2d(lua_State* L) { /// pitch: number, /// channel: string = "regular", /// loop: bool = false) -static int l_audio_play_sound(lua_State* L) { - return lua::pushinteger(L, static_cast( +static int l_audio_play_sound(lua::State* L) { + return lua::pushinteger(L, static_cast( play_sound( lua::tostring(L, 1), false, @@ -158,8 +158,8 @@ static int l_audio_play_sound(lua_State* L) { /// pitch: number, /// channel: string = "regular", /// loop: bool = false) -static int l_audio_play_sound_2d(lua_State* L) { - return lua::pushinteger(L, static_cast( +static int l_audio_play_sound_2d(lua::State* L) { + return lua::pushinteger(L, static_cast( play_sound( lua::tostring(L, 1), true, @@ -173,7 +173,7 @@ static int l_audio_play_sound_2d(lua_State* L) { } /// @brief audio.stop(speakerid: integer) -> nil -static int l_audio_stop(lua_State* L) { +static int l_audio_stop(lua::State* L) { auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { speaker->stop(); @@ -182,7 +182,7 @@ static int l_audio_stop(lua_State* L) { } /// @brief audio.pause(speakerid: integer) -> nil -static int l_audio_pause(lua_State* L) { +static int l_audio_pause(lua::State* L) { auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { speaker->pause(); @@ -191,7 +191,7 @@ static int l_audio_pause(lua_State* L) { } /// @brief audio.resume(speakerid: integer) -> nil -static int l_audio_resume(lua_State* L) { +static int l_audio_resume(lua::State* L) { auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr && speaker->isPaused()) { speaker->play(); @@ -200,7 +200,7 @@ static int l_audio_resume(lua_State* L) { } /// @brief audio.set_loop(speakerid: integer, value: bool) -> nil -static int l_audio_set_loop(lua_State* L) { +static int l_audio_set_loop(lua::State* L) { auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { speaker->setLoop(lua::toboolean(L, 2)); @@ -209,7 +209,7 @@ static int l_audio_set_loop(lua_State* L) { } /// @brief audio.set_volume(speakerid: integer, value: number) -> nil -static int l_audio_set_volume(lua_State* L) { +static int l_audio_set_volume(lua::State* L) { auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { speaker->setVolume(static_cast(lua::tonumber(L, 2))); @@ -218,7 +218,7 @@ static int l_audio_set_volume(lua_State* L) { } /// @brief audio.set_pitch(speakerid: integer, value: number) -> nil -static int l_audio_set_pitch(lua_State* L) { +static int l_audio_set_pitch(lua::State* L) { auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { speaker->setPitch(static_cast(lua::tonumber(L, 2))); @@ -227,7 +227,7 @@ static int l_audio_set_pitch(lua_State* L) { } /// @brief audio.set_time(speakerid: integer, value: number) -> nil -static int l_audio_set_time(lua_State* L) { +static int l_audio_set_time(lua::State* L) { auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { speaker->setTime(static_cast(lua::tonumber(L, 2))); @@ -236,7 +236,7 @@ static int l_audio_set_time(lua_State* L) { } /// @brief audio.set_position(speakerid: integer, x: number, y: number, z: number) -> nil -static int l_audio_set_position(lua_State* L) { +static int l_audio_set_position(lua::State* L) { auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { auto x = lua::tonumber(L, 2); @@ -252,7 +252,7 @@ static int l_audio_set_position(lua_State* L) { } /// @brief audio.set_velocity(speakerid: integer, x: number, y: number, z: number) -> nil -static int l_audio_set_velocity(lua_State* L) { +static int l_audio_set_velocity(lua::State* L) { auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { auto x = lua::tonumber(L, 2); @@ -268,7 +268,7 @@ static int l_audio_set_velocity(lua_State* L) { } /// @brief audio.is_playing(speakerid: integer) -> bool -static int l_audio_is_playing(lua_State* L) { +static int l_audio_is_playing(lua::State* L) { auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { return lua::pushboolean(L, speaker->isPlaying()); @@ -277,7 +277,7 @@ static int l_audio_is_playing(lua_State* L) { } /// @brief audio.is_paused(speakerid: integer) -> bool -static int l_audio_is_paused(lua_State* L) { +static int l_audio_is_paused(lua::State* L) { auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { return lua::pushboolean(L, speaker->isPaused()); @@ -286,7 +286,7 @@ static int l_audio_is_paused(lua_State* L) { } /// @brief audio.is_loop(speakerid: integer) -> bool -static int l_audio_is_loop(lua_State* L) { +static int l_audio_is_loop(lua::State* L) { auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { return lua::pushboolean(L, speaker->isLoop()); @@ -295,7 +295,7 @@ static int l_audio_is_loop(lua_State* L) { } /// @brief audio.get_volume(speakerid: integer) -> number -static int l_audio_get_volume(lua_State* L) { +static int l_audio_get_volume(lua::State* L) { auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { return lua::pushnumber(L, speaker->getVolume()); @@ -304,7 +304,7 @@ static int l_audio_get_volume(lua_State* L) { } /// @brief audio.get_pitch(speakerid: integer) -> number -static int l_audio_get_pitch(lua_State* L) { +static int l_audio_get_pitch(lua::State* L) { auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { return lua::pushnumber(L, speaker->getPitch()); @@ -313,7 +313,7 @@ static int l_audio_get_pitch(lua_State* L) { } /// @brief audio.get_time(speakerid: integer) -> number -static int l_audio_get_time(lua_State* L) { +static int l_audio_get_time(lua::State* L) { auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { return lua::pushnumber(L, speaker->getTime()); @@ -322,7 +322,7 @@ static int l_audio_get_time(lua_State* L) { } /// @brief audio.get_duration(speakerid: integer) -> number -static int l_audio_get_duration(lua_State* L) { +static int l_audio_get_duration(lua::State* L) { auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { return lua::pushnumber(L, speaker->getDuration()); @@ -331,7 +331,7 @@ static int l_audio_get_duration(lua_State* L) { } /// @brief audio.get_position(speakerid: integer) -> number, number, number -static int l_audio_get_position(lua_State* L) { +static int l_audio_get_position(lua::State* L) { auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { return lua::pushvec3(L, speaker->getPosition()); @@ -340,7 +340,7 @@ static int l_audio_get_position(lua_State* L) { } /// @brief audio.get_velocity(speakerid: integer) -> number, number, number -static int l_audio_get_velocity(lua_State* L) { +static int l_audio_get_velocity(lua::State* L) { auto speaker = audio::get_speaker(lua::tointeger(L, 1)); if (speaker != nullptr) { return lua::pushvec3(L, speaker->getVelocity()); @@ -349,12 +349,12 @@ static int l_audio_get_velocity(lua_State* L) { } // @brief audio.count_speakers() -> integer -static int l_audio_count_speakers(lua_State* L) { +static int l_audio_count_speakers(lua::State* L) { return lua::pushinteger(L, audio::count_speakers()); } // @brief audio.count_streams() -> integer -static int l_audio_count_streams(lua_State* L) { +static int l_audio_count_streams(lua::State* L) { return lua::pushinteger(L, audio::count_streams()); } diff --git a/src/logic/scripting/lua/libblock.cpp b/src/logic/scripting/lua/libblock.cpp index 621222d8..f992b32e 100644 --- a/src/logic/scripting/lua/libblock.cpp +++ b/src/logic/scripting/lua/libblock.cpp @@ -11,7 +11,7 @@ using namespace scripting; -static Block* require_block(lua_State* L) { +static Block* require_block(lua::State* L) { auto indices = content->getIndices(); auto id = lua::tointeger(L, 1); if (static_cast(id) >= indices->countBlockDefs()) { @@ -20,21 +20,21 @@ static Block* require_block(lua_State* L) { return indices->getBlockDef(id); } -static int l_name(lua_State* L) { +static int l_name(lua::State* L) { if (auto def = require_block(L)) { return lua::pushstring(L, def->name); } return 0; } -static int l_material(lua_State* L) { +static int l_material(lua::State* L) { if (auto def = require_block(L)) { return lua::pushstring(L, def->material); } return 0; } -static int l_is_solid_at(lua_State* L) { +static int l_is_solid_at(lua::State* L) { auto x = lua::tointeger(L, 1); auto y = lua::tointeger(L, 2); auto z = lua::tointeger(L, 3); @@ -42,30 +42,30 @@ static int l_is_solid_at(lua_State* L) { return lua::pushboolean(L, level->chunks->isSolidBlock(x, y, z)); } -static int l_count(lua_State* L) { +static int l_count(lua::State* L) { return lua::pushinteger(L, indices->countBlockDefs()); } -static int l_index(lua_State* L) { +static int l_index(lua::State* L) { auto name = lua::require_string(L, 1); return lua::pushinteger(L, content->requireBlock(name).rt.id); } -static int l_is_extended(lua_State* L) { +static int l_is_extended(lua::State* L) { if (auto def = require_block(L)) { return lua::pushboolean(L, def->rt.extended); } return 0; } -static int l_get_size(lua_State* L) { +static int l_get_size(lua::State* L) { if (auto def = require_block(L)) { return lua::pushivec3(L, def->size.x, def->size.y, def->size.z); } return 0; } -static int l_is_segment(lua_State* L) { +static int l_is_segment(lua::State* L) { auto x = lua::tointeger(L, 1); auto y = lua::tointeger(L, 2); auto z = lua::tointeger(L, 3); @@ -73,7 +73,7 @@ static int l_is_segment(lua_State* L) { return lua::pushboolean(L, vox->state.segment); } -static int l_seek_origin(lua_State* L) { +static int l_seek_origin(lua::State* L) { auto x = lua::tointeger(L, 1); auto y = lua::tointeger(L, 2); auto z = lua::tointeger(L, 3); @@ -82,7 +82,7 @@ static int l_seek_origin(lua_State* L) { return lua::pushivec3(L, level->chunks->seekOrigin({x, y, z}, def, vox->state)); } -static int l_set(lua_State* L) { +static int l_set(lua::State* L) { auto x = lua::tointeger(L, 1); auto y = lua::tointeger(L, 2); auto z = lua::tointeger(L, 3); @@ -103,7 +103,7 @@ static int l_set(lua_State* L) { return 0; } -static int l_get(lua_State* L) { +static int l_get(lua::State* L) { auto x = lua::tointeger(L, 1); auto y = lua::tointeger(L, 2); auto z = lua::tointeger(L, 3); @@ -112,7 +112,7 @@ static int l_get(lua_State* L) { return lua::pushinteger(L, id); } -static int l_get_x(lua_State* L) { +static int l_get_x(lua::State* L) { auto x = lua::tointeger(L, 1); auto y = lua::tointeger(L, 2); auto z = lua::tointeger(L, 3); @@ -129,7 +129,7 @@ static int l_get_x(lua_State* L) { } } -static int l_get_y(lua_State* L) { +static int l_get_y(lua::State* L) { auto x = lua::tointeger(L, 1); auto y = lua::tointeger(L, 2); auto z = lua::tointeger(L, 3); @@ -146,7 +146,7 @@ static int l_get_y(lua_State* L) { } } -static int l_get_z(lua_State* L) { +static int l_get_z(lua::State* L) { auto x = lua::tointeger(L, 1); auto y = lua::tointeger(L, 2); auto z = lua::tointeger(L, 3); @@ -163,7 +163,7 @@ static int l_get_z(lua_State* L) { } } -static int l_get_rotation(lua_State* L) { +static int l_get_rotation(lua::State* L) { auto x = lua::tointeger(L, 1); auto y = lua::tointeger(L, 2); auto z = lua::tointeger(L, 3); @@ -172,7 +172,7 @@ static int l_get_rotation(lua_State* L) { return lua::pushinteger(L, rotation); } -static int l_set_rotation(lua_State* L) { +static int l_set_rotation(lua::State* L) { auto x = lua::tointeger(L, 1); auto y = lua::tointeger(L, 2); auto z = lua::tointeger(L, 3); @@ -181,7 +181,7 @@ static int l_set_rotation(lua_State* L) { return 0; } -static int l_get_states(lua_State* L) { +static int l_get_states(lua::State* L) { auto x = lua::tointeger(L, 1); auto y = lua::tointeger(L, 2); auto z = lua::tointeger(L, 3); @@ -190,7 +190,7 @@ static int l_get_states(lua_State* L) { return lua::pushinteger(L, states); } -static int l_set_states(lua_State* L) { +static int l_set_states(lua::State* L) { auto x = lua::tointeger(L, 1); auto y = lua::tointeger(L, 2); auto z = lua::tointeger(L, 3); @@ -206,7 +206,7 @@ static int l_set_states(lua_State* L) { return 0; } -static int l_get_user_bits(lua_State* L) { +static int l_get_user_bits(lua::State* L) { auto x = lua::tointeger(L, 1); auto y = lua::tointeger(L, 2); auto z = lua::tointeger(L, 3); @@ -222,7 +222,7 @@ static int l_get_user_bits(lua_State* L) { return lua::pushinteger(L, data); } -static int l_set_user_bits(lua_State* L) { +static int l_set_user_bits(lua::State* L) { auto x = lua::tointeger(L, 1); auto y = lua::tointeger(L, 2); auto z = lua::tointeger(L, 3); @@ -245,14 +245,14 @@ static int l_set_user_bits(lua_State* L) { return 0; } -static int l_is_replaceable_at(lua_State* L) { +static int l_is_replaceable_at(lua::State* L) { auto x = lua::tointeger(L, 1); auto y = lua::tointeger(L, 2); auto z = lua::tointeger(L, 3); return lua::pushboolean(L, level->chunks->isReplaceableBlock(x, y, z)); } -static int l_caption(lua_State* L) { +static int l_caption(lua::State* L) { if (auto def = require_block(L)) { return lua::pushstring(L, def->caption); } diff --git a/src/logic/scripting/lua/libconsole.cpp b/src/logic/scripting/lua/libconsole.cpp index f7e71cb6..7c465a48 100644 --- a/src/logic/scripting/lua/libconsole.cpp +++ b/src/logic/scripting/lua/libconsole.cpp @@ -6,7 +6,7 @@ using namespace scripting; -static int l_add_command(lua_State* L) { +static int l_add_command(lua::State* L) { if (!lua::isfunction(L, 3)) { throw std::runtime_error("invalid callback"); } @@ -26,21 +26,21 @@ static int l_add_command(lua_State* L) { return 0; } -static int l_execute(lua_State* L) { +static int l_execute(lua::State* L) { auto prompt = lua::require_string(L, 1); auto result = engine->getCommandsInterpreter()->execute(prompt); lua::pushvalue(L, result); return 1; } -static int l_set(lua_State* L) { +static int l_set(lua::State* L) { auto name = lua::require_string(L, 1); auto value = lua::tovalue(L, 2); (*engine->getCommandsInterpreter())[name] = value; return 0; } -static int l_get_commands_list(lua_State* L) { +static int l_get_commands_list(lua::State* L) { auto interpreter = engine->getCommandsInterpreter(); auto repo = interpreter->getRepository(); const auto& commands = repo->getCommands(); @@ -54,7 +54,7 @@ static int l_get_commands_list(lua_State* L) { return 1; } -static int l_get_command_info(lua_State* L) { +static int l_get_command_info(lua::State* L) { auto name = lua::require_string(L, 1); auto interpreter = engine->getCommandsInterpreter(); auto repo = interpreter->getRepository(); diff --git a/src/logic/scripting/lua/libcore.cpp b/src/logic/scripting/lua/libcore.cpp index 4d0135c8..61b8340d 100644 --- a/src/logic/scripting/lua/libcore.cpp +++ b/src/logic/scripting/lua/libcore.cpp @@ -17,7 +17,7 @@ using namespace scripting; -static int l_new_world(lua_State* L) { +static int l_new_world(lua::State* L) { auto name = lua::require_string(L, 1); auto seed = lua::require_string(L, 2); auto generator = lua::require_string(L, 3); @@ -26,7 +26,7 @@ static int l_new_world(lua_State* L) { return 0; } -static int l_open_world(lua_State* L) { +static int l_open_world(lua::State* L) { auto name = lua::require_string(L, 1); auto controller = engine->getController(); @@ -34,13 +34,13 @@ static int l_open_world(lua_State* L) { return 0; } -static int l_reopen_world(lua_State*) { +static int l_reopen_world(lua::State*) { auto controller = engine->getController(); controller->reopenWorld(level->getWorld()); return 0; } -static int l_close_world(lua_State* L) { +static int l_close_world(lua::State* L) { if (controller == nullptr) { throw std::runtime_error("no world open"); } @@ -55,14 +55,14 @@ static int l_close_world(lua_State* L) { return 0; } -static int l_delete_world(lua_State* L) { +static int l_delete_world(lua::State* L) { auto name = lua::require_string(L, 1); auto controller = engine->getController(); controller->deleteWorld(name); return 0; } -static int l_reconfig_packs(lua_State* L) { +static int l_reconfig_packs(lua::State* L) { if (!lua::istable(L, 1)) { throw std::runtime_error("strings array expected as the first argument"); } @@ -95,26 +95,26 @@ static int l_reconfig_packs(lua_State* L) { return 0; } -static int l_get_setting(lua_State* L) { +static int l_get_setting(lua::State* L) { auto name = lua::require_string(L, 1); const auto value = engine->getSettingsHandler().getValue(name); return lua::pushvalue(L, value); } -static int l_set_setting(lua_State* L) { +static int l_set_setting(lua::State* L) { auto name = lua::require_string(L, 1); const auto value = lua::tovalue(L, 2); engine->getSettingsHandler().setValue(name, value); return 0; } -static int l_str_setting(lua_State* L) { +static int l_str_setting(lua::State* L) { auto name = lua::require_string(L, 1); const auto string = engine->getSettingsHandler().toString(name); return lua::pushstring(L, string); } -static int l_get_setting_info(lua_State* L) { +static int l_get_setting_info(lua::State* L) { auto name = lua::require_string(L, 1); auto setting = engine->getSettingsHandler().getSetting(name); lua::createtable(L, 0, 1); @@ -136,16 +136,16 @@ static int l_get_setting_info(lua_State* L) { throw std::runtime_error("unsupported setting type"); } -static int l_quit(lua_State*) { +static int l_quit(lua::State*) { Window::setShouldClose(true); return 0; } -static int l_get_default_generator(lua_State* L) { +static int l_get_default_generator(lua::State* L) { return lua::pushstring(L, WorldGenerators::getDefaultGeneratorID()); } -static int l_get_generators(lua_State* L) { +static int l_get_generators(lua::State* L) { const auto& generators = WorldGenerators::getGeneratorsIDs(); lua::createtable(L, generators.size(), 0); diff --git a/src/logic/scripting/lua/libfile.cpp b/src/logic/scripting/lua/libfile.cpp index 97c89bf0..4573e706 100644 --- a/src/logic/scripting/lua/libfile.cpp +++ b/src/logic/scripting/lua/libfile.cpp @@ -23,7 +23,7 @@ static fs::path resolve_path_soft(const std::string& path) { return engine->getPaths()->resolve(path, false); } -static int l_file_find(lua_State* L) { +static int l_file_find(lua::State* L) { auto path = lua::require_string(L, 1); try { return lua::pushstring(L, engine->getResPaths()->findRaw(path)); @@ -32,12 +32,12 @@ static int l_file_find(lua_State* L) { } } -static int l_file_resolve(lua_State* L) { +static int l_file_resolve(lua::State* L) { fs::path path = resolve_path(lua::require_string(L, 1)); return lua::pushstring(L, path.u8string()); } -static int l_file_read(lua_State* L) { +static int l_file_read(lua::State* L) { fs::path path = resolve_path(lua::require_string(L, 1)); if (fs::is_regular_file(path)) { return lua::pushstring(L, files::read_string(path)); @@ -45,14 +45,14 @@ static int l_file_read(lua_State* L) { throw std::runtime_error("file does not exists "+util::quote(path.u8string())); } -static int l_file_write(lua_State* L) { +static int l_file_write(lua::State* L) { fs::path path = resolve_path(lua::require_string(L, 1)); std::string text = lua::require_string(L, 2); files::write_string(path, text); return 1; } -static int l_file_remove(lua_State* L) { +static int l_file_remove(lua::State* L) { std::string rawpath = lua::require_string(L, 1); fs::path path = resolve_path(rawpath); auto entryPoint = rawpath.substr(0, rawpath.find(':')); @@ -62,7 +62,7 @@ static int l_file_remove(lua_State* L) { return lua::pushboolean(L, fs::remove(path)); } -static int l_file_remove_tree(lua_State* L) { +static int l_file_remove_tree(lua::State* L) { std::string rawpath = lua::require_string(L, 1); fs::path path = resolve_path(rawpath); auto entryPoint = rawpath.substr(0, rawpath.find(':')); @@ -72,22 +72,22 @@ static int l_file_remove_tree(lua_State* L) { return lua::pushinteger(L, fs::remove_all(path)); } -static int l_file_exists(lua_State* L) { +static int l_file_exists(lua::State* L) { fs::path path = resolve_path_soft(lua::require_string(L, 1)); return lua::pushboolean(L, fs::exists(path)); } -static int l_file_isfile(lua_State* L) { +static int l_file_isfile(lua::State* L) { fs::path path = resolve_path_soft(lua::require_string(L, 1)); return lua::pushboolean(L, fs::is_regular_file(path)); } -static int l_file_isdir(lua_State* L) { +static int l_file_isdir(lua::State* L) { fs::path path = resolve_path_soft(lua::require_string(L, 1)); return lua::pushboolean(L, fs::is_directory(path)); } -static int l_file_length(lua_State* L) { +static int l_file_length(lua::State* L) { fs::path path = resolve_path(lua::require_string(L, 1)); if (fs::exists(path)){ return lua::pushinteger(L, fs::file_size(path)); @@ -96,17 +96,17 @@ static int l_file_length(lua_State* L) { } } -static int l_file_mkdir(lua_State* L) { +static int l_file_mkdir(lua::State* L) { fs::path path = resolve_path(lua::require_string(L, 1)); return lua::pushboolean(L, fs::create_directory(path)); } -static int l_file_mkdirs(lua_State* L) { +static int l_file_mkdirs(lua::State* L) { fs::path path = resolve_path(lua::require_string(L, 1)); return lua::pushboolean(L, fs::create_directories(path)); } -static int l_file_read_bytes(lua_State* L) { +static int l_file_read_bytes(lua::State* L) { fs::path path = resolve_path(lua::require_string(L, 1)); if (fs::is_regular_file(path)) { size_t length = static_cast(fs::file_size(path)); @@ -125,12 +125,12 @@ static int l_file_read_bytes(lua_State* L) { throw std::runtime_error("file does not exists "+util::quote(path.u8string())); } -static int read_bytes_from_table(lua_State* L, int tableIndex, std::vector& bytes) { +static int read_bytes_from_table(lua::State* L, int tableIndex, std::vector& bytes) { if(!lua::istable(L, tableIndex)) { throw std::runtime_error("table expected"); } else { lua::pushnil(L); - while(lua_next(L, tableIndex - 1) != 0) { + while(lua::next(L, tableIndex - 1) != 0) { const int byte = lua::tointeger(L, -1); if(byte < 0 || byte > 255) { throw std::runtime_error("invalid byte '"+std::to_string(byte)+"'"); @@ -142,7 +142,7 @@ static int read_bytes_from_table(lua_State* L, int tableIndex, std::vectorgetResPaths()->listdirRaw(path); lua::createtable(L, files.size(), 0); for (size_t i = 0; i < files.size(); i++) { @@ -172,7 +172,7 @@ static int l_file_list_all_res(lua_State* L, const std::string& path) { return 1; } -static int l_file_list(lua_State* L) { +static int l_file_list(lua::State* L) { std::string dirname = lua::require_string(L, 1); if (dirname.find(':') == std::string::npos) { return l_file_list_all_res(L, dirname); @@ -193,7 +193,7 @@ static int l_file_list(lua_State* L) { return 1; } -static int l_file_gzip_compress(lua_State* L) { +static int l_file_gzip_compress(lua::State* L) { fs::path path = resolve_path(lua::require_string(L, 1)); if (fs::is_regular_file(path)) { size_t length = static_cast(fs::file_size(path)); @@ -205,7 +205,7 @@ static int l_file_gzip_compress(lua_State* L) { throw std::runtime_error("file does not exist " + util::quote(path.u8string())); } -static int l_file_gzip_decompress(lua_State* L) { +static int l_file_gzip_decompress(lua::State* L) { fs::path path = resolve_path(lua::require_string(L, 1)); if (fs::is_regular_file(path)) { size_t length = static_cast(fs::file_size(path)); diff --git a/src/logic/scripting/lua/libgui.cpp b/src/logic/scripting/lua/libgui.cpp index 524c30c1..ae8c6fbe 100644 --- a/src/logic/scripting/lua/libgui.cpp +++ b/src/logic/scripting/lua/libgui.cpp @@ -26,7 +26,7 @@ struct DocumentNode { std::shared_ptr node; }; -static DocumentNode getDocumentNode(lua_State*, const std::string& name, const std::string& nodeName) { +static DocumentNode getDocumentNode(lua::State*, const std::string& name, const std::string& nodeName) { auto doc = engine->getAssets()->getLayout(name); if (doc == nullptr) { throw std::runtime_error("document '"+name+"' not found"); @@ -38,31 +38,31 @@ static DocumentNode getDocumentNode(lua_State*, const std::string& name, const s return {doc, node}; } -static DocumentNode getDocumentNode(lua_State* L, int idx=1) { - lua_getfield(L, idx, "docname"); - lua_getfield(L, idx, "name"); +static DocumentNode getDocumentNode(lua::State* L, int idx=1) { + lua::getfield(L, "docname", idx); + lua::getfield(L, "name", idx); auto docname = lua::require_string(L, -2); auto name = lua::require_string(L, -1); auto node = getDocumentNode(L, docname, name); - lua_pop(L, 2); + lua::pop(L, 2); return node; } -static int l_menu_back(lua_State* L) { +static int l_menu_back(lua::State* L) { auto node = getDocumentNode(L); auto menu = dynamic_cast(node.node.get()); menu->back(); return 0; } -static int l_menu_reset(lua_State* L) { +static int l_menu_reset(lua::State* L) { auto node = getDocumentNode(L); auto menu = dynamic_cast(node.node.get()); menu->reset(); return 0; } -static int l_textbox_paste(lua_State* L) { +static int l_textbox_paste(lua::State* L) { auto node = getDocumentNode(L); auto box = dynamic_cast(node.node.get()); auto text = lua::require_string(L, 2); @@ -70,7 +70,7 @@ static int l_textbox_paste(lua_State* L) { return 0; } -static int l_container_add(lua_State* L) { +static int l_container_add(lua::State* L) { auto docnode = getDocumentNode(L); auto node = dynamic_cast(docnode.node.get()); auto xmlsrc = lua::require_string(L, 2); @@ -84,7 +84,7 @@ static int l_container_add(lua_State* L) { return 0; } -static int l_node_destruct(lua_State* L) { +static int l_node_destruct(lua::State* L) { auto docnode = getDocumentNode(L); auto node = std::dynamic_pointer_cast(docnode.node); engine->getGUI()->postRunnable([node]() { @@ -96,7 +96,7 @@ static int l_node_destruct(lua_State* L) { return 0; } -static int l_container_clear(lua_State* L) { +static int l_container_clear(lua::State* L) { auto node = getDocumentNode(L, 1); if (auto container = std::dynamic_pointer_cast(node.node)) { container->clear(); @@ -104,7 +104,7 @@ static int l_container_clear(lua_State* L) { return 0; } -static int l_container_set_interval(lua_State* L) { +static int l_container_set_interval(lua::State* L) { auto node = getDocumentNode(L, 1); auto interval = lua::tointeger(L, 2) / 1000.0f; if (auto container = std::dynamic_pointer_cast(node.node)) { @@ -115,14 +115,14 @@ static int l_container_set_interval(lua_State* L) { return 0; } -static int l_move_into(lua_State* L) { +static int l_move_into(lua::State* L) { auto node = getDocumentNode(L, 1); auto dest = getDocumentNode(L, 2); UINode::moveInto(node.node, std::dynamic_pointer_cast(dest.node)); return 0; } -static int p_get_inventory(UINode* node, lua_State* L) { +static int p_get_inventory(UINode* node, lua::State* L) { if (auto inventory = dynamic_cast(node)) { auto inv = inventory->getInventory(); return lua::pushinteger(L, inv ? inv->getId() : 0); @@ -130,35 +130,35 @@ static int p_get_inventory(UINode* node, lua_State* L) { return 0; } -static int p_get_reset(UINode* node, lua_State* L) { +static int p_get_reset(UINode* node, lua::State* L) { if (dynamic_cast(node)) { return lua::pushcfunction(L, l_menu_reset); } return 0; } -static int p_get_back(UINode* node, lua_State* L) { +static int p_get_back(UINode* node, lua::State* L) { if (dynamic_cast(node)) { return lua::pushcfunction(L, l_menu_back); } return 0; } -static int p_get_paste(UINode* node, lua_State* L) { +static int p_get_paste(UINode* node, lua::State* L) { if (dynamic_cast(node)) { return lua::pushcfunction(L, l_textbox_paste); } return 0; } -static int p_get_page(UINode* node, lua_State* L) { +static int p_get_page(UINode* node, lua::State* L) { if (auto menu = dynamic_cast(node)) { return lua::pushstring(L, menu->getCurrent().name); } return 0; } -static int p_is_checked(UINode* node, lua_State* L) { +static int p_is_checked(UINode* node, lua::State* L) { if (auto box = dynamic_cast(node)) { return lua::pushboolean(L, box->isChecked()); } else if (auto box = dynamic_cast(node)) { @@ -167,70 +167,70 @@ static int p_is_checked(UINode* node, lua_State* L) { return 0; } -static int p_get_value(UINode* node, lua_State* L) { +static int p_get_value(UINode* node, lua::State* L) { if (auto bar = dynamic_cast(node)) { return lua::pushnumber(L, bar->getValue()); } return 0; } -static int p_get_min(UINode* node, lua_State* L) { +static int p_get_min(UINode* node, lua::State* L) { if (auto bar = dynamic_cast(node)) { return lua::pushnumber(L, bar->getMin()); } return 0; } -static int p_get_max(UINode* node, lua_State* L) { +static int p_get_max(UINode* node, lua::State* L) { if (auto bar = dynamic_cast(node)) { return lua::pushnumber(L, bar->getMax()); } return 0; } -static int p_get_step(UINode* node, lua_State* L) { +static int p_get_step(UINode* node, lua::State* L) { if (auto bar = dynamic_cast(node)) { return lua::pushnumber(L, bar->getStep()); } return 0; } -static int p_get_track_width(UINode* node, lua_State* L) { +static int p_get_track_width(UINode* node, lua::State* L) { if (auto bar = dynamic_cast(node)) { return lua::pushnumber(L, bar->getTrackWidth()); } return 0; } -static int p_get_track_color(UINode* node, lua_State* L) { +static int p_get_track_color(UINode* node, lua::State* L) { if (auto bar = dynamic_cast(node)) { return lua::pushcolor_arr(L, bar->getTrackColor()); } return 0; } -static int p_is_valid(UINode* node, lua_State* L) { +static int p_is_valid(UINode* node, lua::State* L) { if (auto box = dynamic_cast(node)) { return lua::pushboolean(L, box->validate()); } return 0; } -static int p_get_caret(UINode* node, lua_State* L) { +static int p_get_caret(UINode* node, lua::State* L) { if (auto box = dynamic_cast(node)) { return lua::pushinteger(L, static_cast(box->getCaret())); } return 0; } -static int p_get_placeholder(UINode* node, lua_State* L) { +static int p_get_placeholder(UINode* node, lua::State* L) { if (auto box = dynamic_cast(node)) { return lua::pushwstring(L, box->getPlaceholder()); } return 0; } -static int p_get_text(UINode* node, lua_State* L) { +static int p_get_text(UINode* node, lua::State* L) { if (auto button = dynamic_cast(node)) { return lua::pushwstring(L, button->getText()); } else if (auto label = dynamic_cast(node)) { @@ -241,93 +241,93 @@ static int p_get_text(UINode* node, lua_State* L) { return 0; } -static int p_get_editable(UINode* node, lua_State* L) { +static int p_get_editable(UINode* node, lua::State* L) { if (auto box = dynamic_cast(node)) { return lua::pushboolean(L, box->isEditable()); } return 0; } -static int p_get_src(UINode* node, lua_State* L) { +static int p_get_src(UINode* node, lua::State* L) { if (auto image = dynamic_cast(node)) { return lua::pushstring(L, image->getTexture()); } return 0; } -static int p_get_add(UINode* node, lua_State* L) { +static int p_get_add(UINode* node, lua::State* L) { if (dynamic_cast(node)) { return lua::pushcfunction(L, lua::wrap); } return 0; } -static int p_get_destruct(UINode*, lua_State* L) { +static int p_get_destruct(UINode*, lua::State* L) { return lua::pushcfunction(L, lua::wrap); } -static int p_get_clear(UINode* node, lua_State* L) { +static int p_get_clear(UINode* node, lua::State* L) { if (dynamic_cast(node)) { return lua::pushcfunction(L, lua::wrap); } return 0; } -static int p_set_interval(UINode* node, lua_State* L) { +static int p_set_interval(UINode* node, lua::State* L) { if (dynamic_cast(node)) { return lua::pushcfunction(L, lua::wrap); } return 0; } -static int p_get_color(UINode* node, lua_State* L) { +static int p_get_color(UINode* node, lua::State* L) { return lua::pushcolor_arr(L, node->getColor()); } -static int p_get_hover_color(UINode* node, lua_State* L) { +static int p_get_hover_color(UINode* node, lua::State* L) { return lua::pushcolor_arr(L, node->getHoverColor()); } -static int p_get_pressed_color(UINode* node, lua_State* L) { +static int p_get_pressed_color(UINode* node, lua::State* L) { return lua::pushcolor_arr(L, node->getPressedColor()); } -static int p_get_tooltip(UINode* node, lua_State* L) { +static int p_get_tooltip(UINode* node, lua::State* L) { return lua::pushwstring(L, node->getTooltip()); } -static int p_get_tooltip_delay(UINode* node, lua_State* L) { +static int p_get_tooltip_delay(UINode* node, lua::State* L) { return lua::pushnumber(L, node->getTooltipDelay()); } -static int p_get_pos(UINode* node, lua_State* L) { +static int p_get_pos(UINode* node, lua::State* L) { return lua::pushvec2_arr(L, node->getPos()); } -static int p_get_wpos(UINode* node, lua_State* L) { +static int p_get_wpos(UINode* node, lua::State* L) { return lua::pushvec2_arr(L, node->calcPos()); } -static int p_get_size(UINode* node, lua_State* L) { +static int p_get_size(UINode* node, lua::State* L) { return lua::pushvec2_arr(L, node->getSize()); } -static int p_is_interactive(UINode* node, lua_State* L) { +static int p_is_interactive(UINode* node, lua::State* L) { return lua::pushboolean(L, node->isInteractive()); } -static int p_is_visible(UINode* node, lua_State* L) { +static int p_is_visible(UINode* node, lua::State* L) { return lua::pushboolean(L, node->isVisible()); } -static int p_is_enabled(UINode* node, lua_State* L) { +static int p_is_enabled(UINode* node, lua::State* L) { return lua::pushboolean(L, node->isEnabled()); } -static int p_move_into(UINode*, lua_State* L) { +static int p_move_into(UINode*, lua::State* L) { return lua::pushcfunction(L, l_move_into); } -static int p_get_focused(UINode* node, lua_State* L) { +static int p_get_focused(UINode* node, lua::State* L) { return lua::pushboolean(L, node->isFocused()); } -static int l_gui_getattr(lua_State* L) { +static int l_gui_getattr(lua::State* L) { auto docname = lua::require_string(L, 1); auto element = lua::require_string(L, 2); auto attr = lua::require_string(L, 3); auto docnode = getDocumentNode(L, docname, element); auto node = docnode.node; - static const std::unordered_map> getters { + static const std::unordered_map> getters { {"color", p_get_color}, {"hoverColor", p_get_hover_color}, {"pressedColor", p_get_pressed_color}, @@ -371,45 +371,45 @@ static int l_gui_getattr(lua_State* L) { return 0; } -static void p_set_color(UINode* node, lua_State* L, int idx) { +static void p_set_color(UINode* node, lua::State* L, int idx) { node->setColor(lua::tocolor(L, idx)); } -static void p_set_hover_color(UINode* node, lua_State* L, int idx) { +static void p_set_hover_color(UINode* node, lua::State* L, int idx) { node->setHoverColor(lua::tocolor(L, idx)); } -static void p_set_pressed_color(UINode* node, lua_State* L, int idx) { +static void p_set_pressed_color(UINode* node, lua::State* L, int idx) { node->setPressedColor(lua::tocolor(L, idx)); } -static void p_set_tooltip(UINode* node, lua_State* L, int idx) { +static void p_set_tooltip(UINode* node, lua::State* L, int idx) { node->setTooltip(lua::require_wstring(L, idx)); } -static void p_set_tooltip_delay(UINode* node, lua_State* L, int idx) { +static void p_set_tooltip_delay(UINode* node, lua::State* L, int idx) { node->setTooltipDelay(lua::tonumber(L, idx)); } -static void p_set_pos(UINode* node, lua_State* L, int idx) { +static void p_set_pos(UINode* node, lua::State* L, int idx) { node->setPos(lua::tovec2(L, idx)); } -static void p_set_wpos(UINode* node, lua_State* L, int idx) { +static void p_set_wpos(UINode* node, lua::State* L, int idx) { node->setPos(lua::tovec2(L, idx)-node->calcPos()); } -static void p_set_size(UINode* node, lua_State* L, int idx) { +static void p_set_size(UINode* node, lua::State* L, int idx) { node->setSize(lua::tovec2(L, idx)); } -static void p_set_interactive(UINode* node, lua_State* L, int idx) { +static void p_set_interactive(UINode* node, lua::State* L, int idx) { node->setInteractive(lua::toboolean(L, idx)); } -static void p_set_visible(UINode* node, lua_State* L, int idx) { +static void p_set_visible(UINode* node, lua::State* L, int idx) { node->setVisible(lua::toboolean(L, idx)); } -static void p_set_enabled(UINode* node, lua_State* L, int idx) { +static void p_set_enabled(UINode* node, lua::State* L, int idx) { node->setEnabled(lua::toboolean(L, idx)); } -static void p_set_placeholder(UINode* node, lua_State* L, int idx) { +static void p_set_placeholder(UINode* node, lua::State* L, int idx) { if (auto box = dynamic_cast(node)) { box->setPlaceholder(lua::require_wstring(L, idx)); } } -static void p_set_text(UINode* node, lua_State* L, int idx) { +static void p_set_text(UINode* node, lua::State* L, int idx) { if (auto label = dynamic_cast(node)) { label->setText(lua::require_wstring(L, idx)); } else if (auto button = dynamic_cast(node)) { @@ -418,64 +418,64 @@ static void p_set_text(UINode* node, lua_State* L, int idx) { box->setText(lua::require_wstring(L, idx)); } } -static void p_set_caret(UINode* node, lua_State* L, int idx) { +static void p_set_caret(UINode* node, lua::State* L, int idx) { if (auto box = dynamic_cast(node)) { box->setCaret(static_cast(lua::tointeger(L, idx))); } } -static void p_set_editable(UINode* node, lua_State* L, int idx) { +static void p_set_editable(UINode* node, lua::State* L, int idx) { if (auto box = dynamic_cast(node)) { box->setEditable(lua::toboolean(L, idx)); } } -static void p_set_src(UINode* node, lua_State* L, int idx) { +static void p_set_src(UINode* node, lua::State* L, int idx) { if (auto image = dynamic_cast(node)) { image->setTexture(lua::require_string(L, idx)); } } -static void p_set_value(UINode* node, lua_State* L, int idx) { +static void p_set_value(UINode* node, lua::State* L, int idx) { if (auto bar = dynamic_cast(node)) { bar->setValue(lua::tonumber(L, idx)); } } -static void p_set_min(UINode* node, lua_State* L, int idx) { +static void p_set_min(UINode* node, lua::State* L, int idx) { if (auto bar = dynamic_cast(node)) { bar->setMin(lua::tonumber(L, idx)); } } -static void p_set_max(UINode* node, lua_State* L, int idx) { +static void p_set_max(UINode* node, lua::State* L, int idx) { if (auto bar = dynamic_cast(node)) { bar->setMax(lua::tonumber(L, idx)); } } -static void p_set_step(UINode* node, lua_State* L, int idx) { +static void p_set_step(UINode* node, lua::State* L, int idx) { if (auto bar = dynamic_cast(node)) { bar->setStep(lua::tonumber(L, idx)); } } -static void p_set_track_width(UINode* node, lua_State* L, int idx) { +static void p_set_track_width(UINode* node, lua::State* L, int idx) { if (auto bar = dynamic_cast(node)) { bar->setTrackWidth(lua::tointeger(L, idx)); } } -static void p_set_track_color(UINode* node, lua_State* L, int idx) { +static void p_set_track_color(UINode* node, lua::State* L, int idx) { if (auto bar = dynamic_cast(node)) { bar->setTrackColor(lua::tocolor(L, idx)); } } -static void p_set_checked(UINode* node, lua_State* L, int idx) { +static void p_set_checked(UINode* node, lua::State* L, int idx) { if (auto box = dynamic_cast(node)) { box->setChecked(lua::toboolean(L, idx)); } else if (auto box = dynamic_cast(node)) { box->setChecked(lua::toboolean(L, idx)); } } -static void p_set_page(UINode* node, lua_State* L, int idx) { +static void p_set_page(UINode* node, lua::State* L, int idx) { if (auto menu = dynamic_cast(node)) { menu->setPage(lua::require_string(L, idx)); } } -static void p_set_inventory(UINode* node, lua_State* L, int idx) { +static void p_set_inventory(UINode* node, lua::State* L, int idx) { if (auto view = dynamic_cast(node)) { auto inventory = level->inventories->get(lua::tointeger(L, idx)); if (inventory == nullptr) { @@ -485,7 +485,7 @@ static void p_set_inventory(UINode* node, lua_State* L, int idx) { } } } -static void p_set_focused(const std::shared_ptr &node, lua_State* L, int idx) { +static void p_set_focused(const std::shared_ptr &node, lua::State* L, int idx) { if (lua::toboolean(L, idx) && !node->isFocused()) { engine->getGUI()->setFocus(node); } else if (node->isFocused()){ @@ -493,7 +493,7 @@ static void p_set_focused(const std::shared_ptr &node, lua_State* L, int } } -static int l_gui_setattr(lua_State* L) { +static int l_gui_setattr(lua::State* L) { auto docname = lua::require_string(L, 1); auto element = lua::require_string(L, 2); auto attr = lua::require_string(L, 3); @@ -501,7 +501,7 @@ static int l_gui_setattr(lua_State* L) { auto docnode = getDocumentNode(L, docname, element); auto node = docnode.node; - static const std::unordered_map> setters { + static const std::unordered_map> setters { {"color", p_set_color}, {"hoverColor", p_set_hover_color}, {"pressedColor", p_set_pressed_color}, @@ -532,7 +532,7 @@ static int l_gui_setattr(lua_State* L) { if (func != setters.end()) { func->second(node.get(), L, 4); } - static const std::unordered_map,lua_State*,int)>> setters2 { + static const std::unordered_map,lua::State*,int)>> setters2 { {"focused", p_set_focused}, }; auto func2 = setters2.find(attr); @@ -542,7 +542,7 @@ static int l_gui_setattr(lua_State* L) { return 0; } -static int l_gui_get_env(lua_State* L) { +static int l_gui_get_env(lua::State* L) { auto name = lua::require_string(L, 1); auto doc = engine->getAssets()->getLayout(name); if (doc == nullptr) { @@ -552,7 +552,7 @@ static int l_gui_get_env(lua_State* L) { return 1; } -static int l_gui_str(lua_State* L) { +static int l_gui_str(lua::State* L) { auto text = lua::require_wstring(L, 1); if (!lua::isnoneornil(L, 2)) { auto context = lua::require_wstring(L, 2); @@ -563,7 +563,7 @@ static int l_gui_str(lua_State* L) { return 1; } -static int l_gui_reindex(lua_State* L) { +static int l_gui_reindex(lua::State* L) { auto name = lua::require_string(L, 1); auto doc = engine->getAssets()->getLayout(name); if (doc == nullptr) { @@ -574,7 +574,7 @@ static int l_gui_reindex(lua_State* L) { } /// @brief gui.get_locales_info() -> table of tables -static int l_gui_get_locales_info(lua_State* L) { +static int l_gui_get_locales_info(lua::State* L) { auto& locales = langs::locales_info; lua::createtable(L, 0, locales.size()); for (auto& entry : locales) { @@ -586,7 +586,7 @@ static int l_gui_get_locales_info(lua_State* L) { return 1; } -static int l_gui_getviewport(lua_State* L) { +static int l_gui_getviewport(lua::State* L) { return lua::pushvec2_arr(L, engine->getGUI()->getContainer()->getSize()); } diff --git a/src/logic/scripting/lua/libhud.cpp b/src/logic/scripting/lua/libhud.cpp index d782fd2d..f2bd5535 100644 --- a/src/logic/scripting/lua/libhud.cpp +++ b/src/logic/scripting/lua/libhud.cpp @@ -23,21 +23,21 @@ namespace scripting { } using namespace scripting; -static int l_hud_open_inventory(lua_State*) { +static int l_hud_open_inventory(lua::State*) { if (!hud->isInventoryOpen()) { hud->openInventory(); } return 0; } -static int l_hud_close_inventory(lua_State*) { +static int l_hud_close_inventory(lua::State*) { if (hud->isInventoryOpen()) { hud->closeInventory(); } return 0; } -static int l_hud_open_block(lua_State* L) { +static int l_hud_open_block(lua::State* L) { auto x = lua::tointeger(L, 1); auto y = lua::tointeger(L, 2); auto z = lua::tointeger(L, 3); @@ -66,7 +66,7 @@ static int l_hud_open_block(lua_State* L) { return 2; } -static int l_hud_show_overlay(lua_State* L) { +static int l_hud_show_overlay(lua::State* L) { auto name = lua::tostring(L, 1); bool playerInventory = lua::toboolean(L, 2); @@ -88,29 +88,29 @@ static UiDocument* require_layout(const char* name) { return layout; } -static int l_hud_open_permanent(lua_State* L) { +static int l_hud_open_permanent(lua::State* L) { auto layout = require_layout(lua::tostring(L, 1)); hud->openPermanent(layout); return 0; } -static int l_hud_close(lua_State* L) { +static int l_hud_close(lua::State* L) { auto layout = require_layout(lua::tostring(L, 1)); hud->remove(layout->getRoot()); return 0; } -static int l_hud_pause(lua_State*) { +static int l_hud_pause(lua::State*) { hud->setPause(true); return 0; } -static int l_hud_resume(lua_State*) { +static int l_hud_resume(lua::State*) { hud->setPause(false); return 0; } -static int l_hud_get_block_inventory(lua_State* L) { +static int l_hud_get_block_inventory(lua::State* L) { auto inventory = hud->getBlockInventory(); if (inventory == nullptr) { return lua::pushinteger(L, 0); @@ -119,7 +119,7 @@ static int l_hud_get_block_inventory(lua_State* L) { } } -static int l_hud_get_player(lua_State* L) { +static int l_hud_get_player(lua::State* L) { auto player = hud->getPlayer(); return lua::pushinteger(L, player->getId()); } diff --git a/src/logic/scripting/lua/libinput.cpp b/src/logic/scripting/lua/libinput.cpp index d82919da..8f1d5820 100644 --- a/src/logic/scripting/lua/libinput.cpp +++ b/src/logic/scripting/lua/libinput.cpp @@ -13,17 +13,17 @@ namespace scripting { } using namespace scripting; -static int l_keycode(lua_State* L) { +static int l_keycode(lua::State* L) { auto name = lua::require_string(L, 1); return lua::pushinteger(L, static_cast(input_util::keycode_from(name))); } -static int l_mousecode(lua_State* L) { +static int l_mousecode(lua::State* L) { auto name = lua::require_string(L, 1); return lua::pushinteger(L, static_cast(input_util::mousecode_from(name))); } -static int l_add_callback(lua_State* L) { +static int l_add_callback(lua::State* L) { auto bindname = lua::require_string(L, 1); const auto& bind = Events::bindings.find(bindname); if (bind == Events::bindings.end()) { @@ -44,11 +44,11 @@ static int l_add_callback(lua_State* L) { return 0; } -static int l_get_mouse_pos(lua_State* L) { +static int l_get_mouse_pos(lua::State* L) { return lua::pushvec2_arr(L, Events::cursor); } -static int l_get_bindings(lua_State* L) { +static int l_get_bindings(lua::State* L) { auto& bindings = Events::bindings; lua::createtable(L, bindings.size(), 0); diff --git a/src/logic/scripting/lua/libinventory.cpp b/src/logic/scripting/lua/libinventory.cpp index d0d052da..549593a8 100644 --- a/src/logic/scripting/lua/libinventory.cpp +++ b/src/logic/scripting/lua/libinventory.cpp @@ -37,7 +37,7 @@ static void validate_slotid(int slotid, Inventory* inv) { } } -static int l_inventory_get(lua_State* L) { +static int l_inventory_get(lua::State* L) { auto invid = lua::tointeger(L, 1); auto slotid = lua::tointeger(L, 2); auto inv = get_inventory(invid); @@ -48,7 +48,7 @@ static int l_inventory_get(lua_State* L) { return 2; } -static int l_inventory_set(lua_State* L) { +static int l_inventory_set(lua::State* L) { auto invid = lua::tointeger(L, 1); auto slotid = lua::tointeger(L, 2); auto itemid = lua::tointeger(L, 3); @@ -63,13 +63,13 @@ static int l_inventory_set(lua_State* L) { return 0; } -static int l_inventory_size(lua_State* L) { +static int l_inventory_size(lua::State* L) { auto invid = lua::tointeger(L, 1); auto inv = get_inventory(invid); return lua::pushinteger(L, inv->size()); } -static int l_inventory_add(lua_State* L) { +static int l_inventory_add(lua::State* L) { auto invid = lua::tointeger(L, 1); auto itemid = lua::tointeger(L, 2); auto count = lua::tointeger(L, 3); @@ -81,7 +81,7 @@ static int l_inventory_add(lua_State* L) { return lua::pushinteger(L, item.getCount()); } -static int l_inventory_get_block(lua_State* L) { +static int l_inventory_get_block(lua::State* L) { auto x = lua::tointeger(L, 1); auto y = lua::tointeger(L, 2); auto z = lua::tointeger(L, 3); @@ -89,7 +89,7 @@ static int l_inventory_get_block(lua_State* L) { return lua::pushinteger(L, id); } -static int l_inventory_bind_block(lua_State* L) { +static int l_inventory_bind_block(lua::State* L) { auto id = lua::tointeger(L, 1); auto x = lua::tointeger(L, 2); auto y = lua::tointeger(L, 3); @@ -98,7 +98,7 @@ static int l_inventory_bind_block(lua_State* L) { return 0; } -static int l_inventory_unbind_block(lua_State* L) { +static int l_inventory_unbind_block(lua::State* L) { auto x = lua::tointeger(L, 1); auto y = lua::tointeger(L, 2); auto z = lua::tointeger(L, 3); @@ -106,7 +106,7 @@ static int l_inventory_unbind_block(lua_State* L) { return 0; } -static int l_inventory_clone(lua_State* L) { +static int l_inventory_clone(lua::State* L) { auto id = lua::tointeger(L, 1); auto clone = level->inventories->clone(id); if (clone == nullptr) { @@ -115,7 +115,7 @@ static int l_inventory_clone(lua_State* L) { return lua::pushinteger(L, clone->getId()); } -static int l_inventory_move(lua_State* L) { +static int l_inventory_move(lua::State* L) { auto invAid = lua::tointeger(L, 1); auto slotAid = lua::tointeger(L, 2); auto invA = get_inventory(invAid, 1); diff --git a/src/logic/scripting/lua/libitem.cpp b/src/logic/scripting/lua/libitem.cpp index 162a9c09..25466217 100644 --- a/src/logic/scripting/lua/libitem.cpp +++ b/src/logic/scripting/lua/libitem.cpp @@ -5,7 +5,7 @@ using namespace scripting; -static int l_item_name(lua_State* L) { +static int l_item_name(lua::State* L) { auto indices = content->getIndices(); auto id = lua::tointeger(L, 1); if (static_cast(id) >= indices->countItemDefs()) { @@ -15,12 +15,12 @@ static int l_item_name(lua_State* L) { return lua::pushstring(L, def->name); } -static int l_item_index(lua_State* L) { +static int l_item_index(lua::State* L) { auto name = lua::require_string(L, 1); return lua::pushinteger(L, content->requireItem(name).rt.id); } -static int l_item_stack_size(lua_State* L) { +static int l_item_stack_size(lua::State* L) { auto indices = content->getIndices(); auto id = lua::tointeger(L, 1); if (static_cast(id) >= indices->countItemDefs()) { @@ -30,7 +30,7 @@ static int l_item_stack_size(lua_State* L) { return lua::pushinteger(L, def->stackSize); } -static int l_item_defs_count(lua_State* L) { +static int l_item_defs_count(lua::State* L) { return lua::pushinteger(L, indices->countItemDefs()); } diff --git a/src/logic/scripting/lua/libjson.cpp b/src/logic/scripting/lua/libjson.cpp index 055a272c..0a68fd89 100644 --- a/src/logic/scripting/lua/libjson.cpp +++ b/src/logic/scripting/lua/libjson.cpp @@ -3,7 +3,7 @@ #include "../../../coders/json.hpp" #include "../../../data/dynamic.hpp" -static int l_json_stringify(lua_State* L) { +static int l_json_stringify(lua::State* L) { auto value = lua::tovalue(L, 1); if (auto mapptr = std::get_if(&value)) { @@ -15,7 +15,7 @@ static int l_json_stringify(lua_State* L) { } } -static int l_json_parse(lua_State* L) { +static int l_json_parse(lua::State* L) { auto string = lua::require_string(L, 1); auto element = json::parse("", string); return lua::pushvalue(L, element); diff --git a/src/logic/scripting/lua/libpack.cpp b/src/logic/scripting/lua/libpack.cpp index b9c16c94..256c6032 100644 --- a/src/logic/scripting/lua/libpack.cpp +++ b/src/logic/scripting/lua/libpack.cpp @@ -14,7 +14,7 @@ using namespace scripting; -static int l_pack_get_folder(lua_State* L) { +static int l_pack_get_folder(lua::State* L) { std::string packName = lua::tostring(L, 1); if (packName == "core") { auto folder = engine->getPaths()->getResources().u8string()+"/"; @@ -29,7 +29,7 @@ static int l_pack_get_folder(lua_State* L) { } /// @brief pack.get_installed() -> array -static int l_pack_get_installed(lua_State* L) { +static int l_pack_get_installed(lua::State* L) { auto& packs = engine->getContentPacks(); lua::createtable(L, packs.size(), 0); for (size_t i = 0; i < packs.size(); i++) { @@ -40,7 +40,7 @@ static int l_pack_get_installed(lua_State* L) { } /// @brief pack.get_available() -> array -static int l_pack_get_available(lua_State* L) { +static int l_pack_get_available(lua::State* L) { fs::path worldFolder(""); if (level) { worldFolder = level->getWorld()->wfile->getFolder(); @@ -62,7 +62,7 @@ static int l_pack_get_available(lua_State* L) { return 1; } -static int l_pack_get_info(lua_State* L, const ContentPack& pack, const Content* content) { +static int l_pack_get_info(lua::State* L, const ContentPack& pack, const Content* content) { lua::createtable(L, 0, 5); lua::pushstring(L, pack.id); @@ -123,7 +123,7 @@ static int l_pack_get_info(lua_State* L, const ContentPack& pack, const Content* /// version: str, /// [optional] has_indices: bool /// } or nil -static int l_pack_get_info(lua_State* L) { +static int l_pack_get_info(lua::State* L) { auto packid = lua::tostring(L, 1); auto content = engine->getContent(); @@ -149,7 +149,7 @@ static int l_pack_get_info(lua_State* L) { return l_pack_get_info(L, pack, content); } -static int l_pack_get_base_packs(lua_State* L) { +static int l_pack_get_base_packs(lua::State* L) { auto& packs = engine->getBasePacks(); lua::createtable(L, packs.size(), 0); for (size_t i = 0; i < packs.size(); i++) { diff --git a/src/logic/scripting/lua/libplayer.cpp b/src/logic/scripting/lua/libplayer.cpp index 59e6226a..b9315de7 100644 --- a/src/logic/scripting/lua/libplayer.cpp +++ b/src/logic/scripting/lua/libplayer.cpp @@ -10,18 +10,18 @@ using namespace scripting; -inline std::shared_ptr get_player(lua_State* L, int idx) { +inline std::shared_ptr get_player(lua::State* L, int idx) { return level->getObject(lua::tointeger(L, idx)); } -static int l_player_get_pos(lua_State* L) { +static int l_player_get_pos(lua::State* L) { if (auto player = get_player(L, 1)) { return lua::pushvec3(L, player->hitbox->position); } return 0; } -static int l_player_set_pos(lua_State* L) { +static int l_player_set_pos(lua::State* L) { auto player = get_player(L, 1); if (!player) { return 0; @@ -33,14 +33,14 @@ static int l_player_set_pos(lua_State* L) { return 0; } -static int l_player_get_vel(lua_State* L) { +static int l_player_get_vel(lua::State* L) { if (auto player = get_player(L, 1)) { return lua::pushvec3(L, player->hitbox->velocity); } return 0; } -static int l_player_set_vel(lua_State* L) { +static int l_player_set_vel(lua::State* L) { auto player = get_player(L, 1); if (!player) { return 0; @@ -52,23 +52,23 @@ static int l_player_set_vel(lua_State* L) { return 0; } -static int l_player_get_rot(lua_State* L) { +static int l_player_get_rot(lua::State* L) { if (auto player = get_player(L, 1)) { return lua::pushvec3(L, player->cam); } return 0; } -static int l_player_set_rot(lua_State* L) { +static int l_player_set_rot(lua::State* L) { auto player = get_player(L, 1); if (!player) { return 0; } glm::vec3& cam = player->cam; - lua_Number x = lua::tonumber(L, 2); - lua_Number y = lua::tonumber(L, 3); - lua_Number z = cam.z; + auto x = lua::tonumber(L, 2); + auto y = lua::tonumber(L, 3); + auto z = cam.z; if (lua::isnumber(L, 4)) { z = lua::tonumber(L, 4); } @@ -78,7 +78,7 @@ static int l_player_set_rot(lua_State* L) { return 0; } -static int l_player_get_inv(lua_State* L) { +static int l_player_get_inv(lua::State* L) { auto player = get_player(L, 1); if (!player) { return 0; @@ -88,35 +88,35 @@ static int l_player_get_inv(lua_State* L) { return 2; } -static int l_player_is_flight(lua_State* L) { +static int l_player_is_flight(lua::State* L) { if (auto player = get_player(L, 1)) { return lua::pushboolean(L, player->isFlight()); } return 0; } -static int l_player_set_flight(lua_State* L) { +static int l_player_set_flight(lua::State* L) { if (auto player = get_player(L, 1)) { player->setFlight(lua::toboolean(L, 2)); } return 0; } -static int l_player_is_noclip(lua_State* L) { +static int l_player_is_noclip(lua::State* L) { if (auto player = get_player(L, 1)) { return lua::pushboolean(L, player->isNoclip()); } return 0; } -static int l_player_set_noclip(lua_State* L) { +static int l_player_set_noclip(lua::State* L) { if (auto player = get_player(L, 1)) { player->setNoclip(lua::toboolean(L, 2)); } return 0; } -static int l_player_get_selected_block(lua_State* L) { +static int l_player_get_selected_block(lua::State* L) { if (auto player = get_player(L, 1)) { if (player->selection.vox.id == BLOCK_VOID) { return 0; diff --git a/src/logic/scripting/lua/libtime.cpp b/src/logic/scripting/lua/libtime.cpp index 392a387f..c1f770e0 100644 --- a/src/logic/scripting/lua/libtime.cpp +++ b/src/logic/scripting/lua/libtime.cpp @@ -3,11 +3,11 @@ #include "../../../engine.hpp" #include "../../../window/Window.hpp" -static int l_time_uptime(lua_State* L) { +static int l_time_uptime(lua::State* L) { return lua::pushnumber(L, Window::time()); } -static int l_time_delta(lua_State* L) { +static int l_time_delta(lua::State* L) { return lua::pushnumber(L, scripting::engine->getDelta()); } diff --git a/src/logic/scripting/lua/libtoml.cpp b/src/logic/scripting/lua/libtoml.cpp index 5d5bb9dd..7fbe2302 100644 --- a/src/logic/scripting/lua/libtoml.cpp +++ b/src/logic/scripting/lua/libtoml.cpp @@ -5,7 +5,7 @@ using namespace scripting; -static int l_toml_stringify(lua_State* L) { +static int l_toml_stringify(lua::State* L) { auto value = lua::tovalue(L, 1); if (auto mapptr = std::get_if(&value)) { @@ -16,7 +16,7 @@ static int l_toml_stringify(lua_State* L) { } } -static int l_toml_parse(lua_State* L) { +static int l_toml_parse(lua::State* L) { auto string = lua::require_string(L, 1); auto element = toml::parse("", string); auto value = std::make_unique(element); diff --git a/src/logic/scripting/lua/libworld.cpp b/src/logic/scripting/lua/libworld.cpp index 75b6914a..75110ca0 100644 --- a/src/logic/scripting/lua/libworld.cpp +++ b/src/logic/scripting/lua/libworld.cpp @@ -13,7 +13,7 @@ using namespace scripting; namespace fs = std::filesystem; -static int l_world_get_list(lua_State* L) { +static int l_world_get_list(lua::State* L) { auto paths = engine->getPaths(); auto worlds = paths->scanForWorlds(); @@ -40,25 +40,25 @@ static int l_world_get_list(lua_State* L) { return 1; } -static int l_world_get_total_time(lua_State* L) { +static int l_world_get_total_time(lua::State* L) { return lua::pushnumber(L, level->getWorld()->totalTime); } -static int l_world_get_day_time(lua_State* L) { +static int l_world_get_day_time(lua::State* L) { return lua::pushnumber(L, level->getWorld()->daytime); } -static int l_world_set_day_time(lua_State* L) { +static int l_world_set_day_time(lua::State* L) { auto value = lua::tonumber(L, 1); level->getWorld()->daytime = fmod(value, 1.0); return 0; } -static int l_world_get_seed(lua_State* L) { +static int l_world_get_seed(lua::State* L) { return lua::pushinteger(L, level->getWorld()->getSeed()); } -static int l_world_exists(lua_State* L) { +static int l_world_exists(lua::State* L) { auto name = lua::require_string(L, 1); auto worldsDir = engine->getPaths()->getWorldFolder(name); return lua::pushboolean(L, fs::is_directory(worldsDir)); diff --git a/src/logic/scripting/lua/lua_commons.hpp b/src/logic/scripting/lua/lua_commons.hpp index e1e4dddb..9b8478c8 100644 --- a/src/logic/scripting/lua/lua_commons.hpp +++ b/src/logic/scripting/lua/lua_commons.hpp @@ -26,6 +26,10 @@ namespace lua { }; void log_error(const std::string& text); + + using State = lua_State; + using Number = lua_Number; + using Integer = lua_Integer; } #endif // LOGIC_SCRIPTING_LUA_HPP_ diff --git a/src/logic/scripting/lua/lua_engine.cpp b/src/logic/scripting/lua/lua_engine.cpp index df6d8a8c..bc4d4423 100644 --- a/src/logic/scripting/lua/lua_engine.cpp +++ b/src/logic/scripting/lua/lua_engine.cpp @@ -8,14 +8,14 @@ #include static debug::Logger logger("lua-state"); -static lua_State* main_thread = nullptr; +static lua::State* main_thread = nullptr; using namespace lua; luaerror::luaerror(const std::string& message) : std::runtime_error(message) { } -static void remove_lib_funcs(lua_State* L, const char* libname, const char* funcs[]) { +static void remove_lib_funcs(lua::State* L, const char* libname, const char* funcs[]) { if (getglobal(L, libname)) { for (uint i = 0; funcs[i]; i++) { pushnil(L); @@ -24,7 +24,7 @@ static void remove_lib_funcs(lua_State* L, const char* libname, const char* func } } -static void create_libs(lua_State* L) { +static void create_libs(lua::State* L) { openlib(L, "audio", audiolib); openlib(L, "block", blocklib); openlib(L, "console", consolelib); @@ -86,7 +86,7 @@ void lua::finalize() { lua_close(main_thread); } -bool lua::emit_event(lua_State* L, const std::string &name, std::function args) { +bool lua::emit_event(lua::State* L, const std::string &name, std::function args) { getglobal(L, "events"); getfield(L, "emit"); pushstring(L, name); @@ -96,6 +96,6 @@ bool lua::emit_event(lua_State* L, const std::string &name, std::function args=[](auto*){return 0;}); - lua_State* get_main_thread(); + bool emit_event(lua::State*, const std::string& name, std::function args=[](auto*){return 0;}); + lua::State* get_main_thread(); } #endif // LOGIC_SCRIPTING_LUA_STATE_HPP_ diff --git a/src/logic/scripting/lua/lua_overrides.cpp b/src/logic/scripting/lua/lua_overrides.cpp index 9d52d993..4e72882c 100644 --- a/src/logic/scripting/lua/lua_overrides.cpp +++ b/src/logic/scripting/lua/lua_overrides.cpp @@ -3,8 +3,8 @@ #include /// @brief Modified version of luaB_print from lbaselib.c -int l_print(lua_State* L) { - int n = lua_gettop(L); /* number of arguments */ +int l_print(lua::State* L) { + int n = lua::gettop(L); /* number of arguments */ lua::getglobal(L, "tostring"); for (int i=1; i<=n; i++) { lua::pushvalue(L, -1); /* function to be called */ diff --git a/src/logic/scripting/lua/lua_util.cpp b/src/logic/scripting/lua/lua_util.cpp index 724282bb..e2e9a929 100644 --- a/src/logic/scripting/lua/lua_util.cpp +++ b/src/logic/scripting/lua/lua_util.cpp @@ -13,7 +13,7 @@ std::string lua::env_name(int env) { return "_ENV"+util::mangleid(env); } -int lua::pushvalue(lua_State* L, const dynamic::Value& value) { +int lua::pushvalue(State* L, const dynamic::Value& value) { using namespace dynamic; if (auto* flag = std::get_if(&value)) { @@ -44,15 +44,15 @@ int lua::pushvalue(lua_State* L, const dynamic::Value& value) { return 1; } -std::wstring lua::require_wstring(lua_State* L, int idx) { - return util::str2wstr_utf8(lua::require_string(L, idx)); +std::wstring lua::require_wstring(State* L, int idx) { + return util::str2wstr_utf8(require_string(L, idx)); } -int lua::pushwstring(lua_State* L, const std::wstring& str) { - return lua::pushstring(L, util::wstr2str_utf8(str)); +int lua::pushwstring(State* L, const std::wstring& str) { + return pushstring(L, util::wstr2str_utf8(str)); } -dynamic::Value lua::tovalue(lua_State* L, int idx) { +dynamic::Value lua::tovalue(State* L, int idx) { using namespace dynamic; auto type = lua::type(L, idx); switch (type) { @@ -64,7 +64,7 @@ dynamic::Value lua::tovalue(lua_State* L, int idx) { case LUA_TNUMBER: { auto number = tonumber(L, idx); auto integer = tointeger(L, idx); - if (number == static_cast(integer)) { + if (number == static_cast(integer)) { return integer; } else { return number; @@ -73,7 +73,7 @@ dynamic::Value lua::tovalue(lua_State* L, int idx) { case LUA_TSTRING: return std::string(tostring(L, idx)); case LUA_TTABLE: { - int len = lua::objlen(L, idx); + int len = objlen(L, idx); if (len) { // array auto list = create_list(); @@ -88,7 +88,7 @@ dynamic::Value lua::tovalue(lua_State* L, int idx) { auto map = create_map(); pushvalue(L, idx); pushnil(L); - while (lua_next(L, -2)) { + while (next(L, -2)) { pushvalue(L, -2); auto key = tostring(L, -1); map->put(key, tovalue(L, -2)); @@ -105,14 +105,14 @@ dynamic::Value lua::tovalue(lua_State* L, int idx) { } } -int lua::call(lua_State* L, int argc, int nresults) { +int lua::call(State* L, int argc, int nresults) { if (lua_pcall(L, argc, nresults, 0)) { throw luaerror(tostring(L, -1)); } return 1; } -int lua::call_nothrow(lua_State* L, int argc) { +int lua::call_nothrow(State* L, int argc) { if (lua_pcall(L, argc, LUA_MULTRET, 0)) { log_error(tostring(L, -1)); return 0; @@ -120,7 +120,7 @@ int lua::call_nothrow(lua_State* L, int argc) { return 1; } -void lua::dump_stack(lua_State* L) { +void lua::dump_stack(State* L) { int top = gettop(L); for (int i = 1; i <= top; i++) { std::cout << std::setw(3) << i << std::setw(20) << luaL_typename(L, i) << std::setw(30); @@ -145,7 +145,7 @@ void lua::dump_stack(lua_State* L) { } } -static std::shared_ptr createLambdaHandler(lua_State* L) { +static std::shared_ptr createLambdaHandler(State* L) { auto ptr = reinterpret_cast(topointer(L, -1)); auto name = util::mangleid(ptr); getglobal(L, LAMBDAS_TABLE); @@ -162,16 +162,16 @@ static std::shared_ptr createLambdaHandler(lua_State* L) { }); } -runnable lua::create_runnable(lua_State* L) { +runnable lua::create_runnable(State* L) { auto funcptr = createLambdaHandler(L); return [=]() { - lua_getglobal(L, LAMBDAS_TABLE.c_str()); - lua_getfield(L, -1, funcptr->c_str()); + getglobal(L, LAMBDAS_TABLE); + getfield(L, *funcptr); call_nothrow(L, 0); }; } -scripting::common_func lua::create_lambda(lua_State* L) { +scripting::common_func lua::create_lambda(State* L) { auto funcptr = createLambdaHandler(L); return [=](const std::vector& args) { getglobal(L, LAMBDAS_TABLE); @@ -188,7 +188,7 @@ scripting::common_func lua::create_lambda(lua_State* L) { }; } -int lua::createEnvironment(lua_State* L, int parent) { +int lua::createEnvironment(State* L, int parent) { int id = nextEnvironment++; // local env = {} @@ -204,7 +204,7 @@ int lua::createEnvironment(lua_State* L, int parent) { } } setfield(L, "__index"); - lua_setmetatable(L, -2); + setmetatable(L); // envname = env setglobal(L, env_name(id)); @@ -212,7 +212,7 @@ int lua::createEnvironment(lua_State* L, int parent) { } -void lua::removeEnvironment(lua_State* L, int id) { +void lua::removeEnvironment(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 13a49372..3c705eb3 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -24,39 +24,42 @@ namespace lua { return result; } - inline void pop(lua_State* L, int n=1) { + inline void pop(lua::State* L, int n=1) { lua_pop(L, n); } - inline int gettop(lua_State* L) { + inline int gettop(lua::State* L) { return lua_gettop(L); } - inline size_t objlen(lua_State* L, int idx) { + inline size_t objlen(lua::State* L, int idx) { return lua_objlen(L, idx); } - inline int type(lua_State* L, int idx) { + inline int next(lua::State* L, int idx) { + return lua_next(L, idx); + } + inline int type(lua::State* L, int idx) { return lua_type(L, idx); } - inline const char* type_name(lua_State* L, int idx) { + inline const char* type_name(lua::State* L, int idx) { return lua_typename(L, idx); } - inline int rawgeti(lua_State* L, int n, int idx=-1) { + inline int rawgeti(lua::State* L, int n, int idx=-1) { lua_rawgeti(L, idx, n); return 1; } - inline void rawseti(lua_State* L, int n, int idx=-2) { + inline void rawseti(lua::State* L, int n, int idx=-2) { lua_rawseti(L, idx, n); } - inline int createtable(lua_State* L, int narr, int nrec) { + inline int createtable(lua::State* L, int narr, int nrec) { lua_createtable(L, narr, nrec); return 1; } - inline bool isnil(lua_State* L, int idx) { + inline bool isnil(lua::State* L, int idx) { return lua_isnil(L, idx); } - inline bool getglobal(lua_State* L, const std::string& name) { + inline bool getglobal(lua::State* L, const std::string& name) { lua_getglobal(L, name.c_str()); if (isnil(L, -1)) { pop(L); @@ -65,7 +68,7 @@ namespace lua { return true; } - inline bool hasglobal(lua_State* L, const std::string& name) { + inline bool hasglobal(lua::State* L, const std::string& name) { lua_getglobal(L, name.c_str()); if (isnil(L, -1)) { pop(L); @@ -77,43 +80,43 @@ namespace lua { // function wrappers with number of pushed values as return value - inline int pushnil(lua_State* L) { + inline int pushnil(lua::State* L) { lua_pushnil(L); return 1; } - inline int pushinteger(lua_State* L, lua_Integer x) { + inline int pushinteger(lua::State* L, lua::Integer x) { lua_pushinteger(L, x); return 1; } - inline int pushnumber(lua_State* L, lua_Number x) { + inline int pushnumber(lua::State* L, lua::Number x) { lua_pushnumber(L, x); return 1; } - inline int pushivec3(lua_State* L, lua_Integer x, lua_Integer y, lua_Integer z) { + inline int pushivec3(lua::State* L, lua::Integer x, lua::Integer y, lua::Integer z) { pushinteger(L, x); pushinteger(L, y); pushinteger(L, z); return 3; } - inline int pushivec3(lua_State* L, glm::ivec3 vec) { + inline int pushivec3(lua::State* L, glm::ivec3 vec) { pushinteger(L, vec.x); pushinteger(L, vec.y); pushinteger(L, vec.z); return 3; } - inline int pushvec3(lua_State* L, glm::vec3 vec) { + inline int pushvec3(lua::State* L, glm::vec3 vec) { pushnumber(L, vec.x); pushnumber(L, vec.y); pushnumber(L, vec.z); return 3; } - inline int pushvec4(lua_State* L, glm::vec4 vec) { + inline int pushvec4(lua::State* L, glm::vec4 vec) { pushnumber(L, vec.x); pushnumber(L, vec.y); pushnumber(L, vec.z); @@ -121,10 +124,14 @@ namespace lua { return 4; } - inline int pushvec2_arr(lua_State* L, glm::vec2 vec) { + inline void setmetatable(lua::State* L, int idx=-2) { + lua_setmetatable(L, idx); + } + + inline int pushvec2_arr(lua::State* L, glm::vec2 vec) { createtable(L, 2, 0); getglobal(L, "vec2_mt"); - lua_setmetatable(L, -2); + setmetatable(L); pushnumber(L, vec.x); rawseti(L, 1); @@ -133,10 +140,10 @@ namespace lua { return 1; } - inline int pushvec3_arr(lua_State* L, glm::vec3 vec) { + inline int pushvec3_arr(lua::State* L, glm::vec3 vec) { createtable(L, 3, 0); getglobal(L, "vec3_mt"); - lua_setmetatable(L, -2); + setmetatable(L); pushnumber(L, vec.x); rawseti(L, 1); @@ -147,10 +154,10 @@ namespace lua { return 1; } - inline int pushvec4_arr(lua_State* L, glm::vec4 vec) { + inline int pushvec4_arr(lua::State* L, glm::vec4 vec) { createtable(L, 4, 0); getglobal(L, "vec4_mt"); - lua_setmetatable(L, -2); + setmetatable(L); pushnumber(L, vec.x); rawseti(L, 1); @@ -162,10 +169,10 @@ namespace lua { rawseti(L, 4); return 1; } - inline int pushcolor_arr(lua_State* L, glm::vec4 vec) { + inline int pushcolor_arr(lua::State* L, glm::vec4 vec) { createtable(L, 4, 0); getglobal(L, "color_mt"); - lua_setmetatable(L, -2); + setmetatable(L); pushinteger(L, vec.x*255); rawseti(L, 1); @@ -177,11 +184,11 @@ namespace lua { rawseti(L, 4); return 1; } - inline int pushcfunction(lua_State* L, lua_CFunction func) { + inline int pushcfunction(lua::State* L, lua_CFunction func) { lua_pushcfunction(L, func); return 1; } - inline int pushstring(lua_State* L, const std::string& str) { + inline int pushstring(lua::State* L, const std::string& str) { lua_pushstring(L, str.c_str()); return 1; } @@ -192,53 +199,53 @@ namespace lua { return 1; } - int pushwstring(lua_State* L, const std::wstring& str); + int pushwstring(lua::State* L, const std::wstring& str); - inline int pushboolean(lua_State* L, bool value) { + inline int pushboolean(lua::State* L, bool value) { lua_pushboolean(L, value); return 1; } - inline int pushvalue(lua_State* L, int idx) { + inline int pushvalue(lua::State* L, int idx) { lua_pushvalue(L, idx); return 1; } - inline int pushglobals(lua_State* L) { + inline int pushglobals(lua::State* L) { return pushvalue(L, LUA_GLOBALSINDEX); } - inline bool isnoneornil(lua_State* L, int idx) { + inline bool isnoneornil(lua::State* L, int idx) { return lua_isnoneornil(L, idx); } - inline bool isboolean(lua_State* L, int idx) { + inline bool isboolean(lua::State* L, int idx) { return lua_isboolean(L, idx); } - inline bool isnumber(lua_State* L, int idx) { + inline bool isnumber(lua::State* L, int idx) { return lua_isnumber(L, idx); } - inline bool isstring(lua_State* L, int idx) { + inline bool isstring(lua::State* L, int idx) { return lua_isstring(L, idx); } - inline bool istable(lua_State* L, int idx) { + inline bool istable(lua::State* L, int idx) { return lua_istable(L, idx); } - inline bool isfunction(lua_State* L, int idx) { + inline bool isfunction(lua::State* L, int idx) { return lua_isfunction(L, idx); } - inline bool toboolean(lua_State* L, int idx) { + inline bool toboolean(lua::State* L, int idx) { return lua_toboolean(L, idx); } - inline lua_Integer tointeger(lua_State* L, int idx) { + inline lua::Integer tointeger(lua::State* L, int idx) { return lua_tointeger(L, idx); } - inline lua_Number tonumber(lua_State* L, int idx) { + inline lua::Number tonumber(lua::State* L, int idx) { return lua_tonumber(L, idx); } - inline const char* tostring(lua_State* L, int idx) { + inline const char* tostring(lua::State* L, int idx) { return lua_tostring(L, idx); } - inline const void* topointer(lua_State* L, int idx) { + inline const void* topointer(lua::State* L, int idx) { return lua_topointer(L, idx); } - inline glm::vec2 tovec2(lua_State* L, int idx) { + inline glm::vec2 tovec2(lua::State* L, int idx) { pushvalue(L, idx); if (!istable(L, idx) || objlen(L, idx) < 2) { throw std::runtime_error("value must be an array of two numbers"); @@ -251,7 +258,7 @@ namespace lua { return glm::vec2(x, y); } - inline glm::vec4 tocolor(lua_State* L, int idx) { + inline glm::vec4 tocolor(lua::State* L, int idx) { pushvalue(L, idx); if (!istable(L, -1) || objlen(L, idx) < 4) { throw std::runtime_error("RGBA array required"); @@ -268,10 +275,10 @@ namespace lua { return glm::vec4(r/255, g/255, b/255, a/255); } - int pushvalue(lua_State*, const dynamic::Value& value); - dynamic::Value tovalue(lua_State*, int idx); + int pushvalue(lua::State*, const dynamic::Value& value); + dynamic::Value tovalue(lua::State*, int idx); - inline bool getfield(lua_State* L, const std::string& name, int idx=-1) { + inline bool getfield(lua::State* L, const std::string& name, int idx=-1) { lua_getfield(L, idx, name.c_str()); if (isnil(L, -1)) { pop(L); @@ -280,26 +287,26 @@ namespace lua { return true; } - inline void setfield(lua_State* L, const std::string& name, int idx=-2) { + inline void setfield(lua::State* L, const std::string& name, int idx=-2) { lua_setfield(L, idx, name.c_str()); } - inline void setglobal(lua_State* L, const std::string& name) { + inline void setglobal(lua::State* L, const std::string& name) { lua_setglobal(L, name.c_str()); } - inline const char* require_string(lua_State* L, int idx) { + inline const char* require_string(lua::State* L, int idx) { if (!isstring(L, idx)) { throw luaerror("string expected at "+std::to_string(idx)); } return tostring(L, idx); } - std::wstring require_wstring(lua_State*, int idx); + std::wstring require_wstring(lua::State*, int idx); - inline bool rename(lua_State* L, const std::string& from, const std::string& to) { + inline bool rename(lua::State* L, const std::string& from, const std::string& to) { getglobal(L, from); - if (lua_isnil(L, -1)) { + if (isnil(L, -1)) { pop(L, 1); return false; } @@ -311,12 +318,12 @@ namespace lua { return true; } - inline void remove(lua_State* L, const std::string& name) { + inline void remove(lua::State* L, const std::string& name) { pushnil(L); setglobal(L, name); } - inline void loadbuffer(lua_State* L, int env, const std::string& src, const std::string& file) { + inline void loadbuffer(lua::State* L, int env, const std::string& src, const std::string& file) { if (luaL_loadbuffer(L, src.c_str(), src.length(), file.c_str())) { throw luaerror(tostring(L, -1)); } @@ -325,39 +332,39 @@ namespace lua { } } - int call(lua_State*, int argc, int nresults=-1); - int call_nothrow(lua_State*, int argc); + int call(lua::State*, int argc, int nresults=-1); + int call_nothrow(lua::State*, int argc); - inline int eval(lua_State* L, int env, const std::string& src, const std::string& file="") { + inline int eval(lua::State* L, int env, const std::string& src, const std::string& file="") { auto srcText = "return "+src; loadbuffer(L, env, srcText, file); return call(L, 0); } - inline int execute(lua_State* L, int env, const std::string& src, const std::string& file="") { + inline int execute(lua::State* L, int env, const std::string& src, const std::string& file="") { loadbuffer(L, env, src, file); return call_nothrow(L, 0); } - runnable create_runnable(lua_State*); - scripting::common_func create_lambda(lua_State* ); + runnable create_runnable(lua::State*); + scripting::common_func create_lambda(lua::State* ); - inline int pushenv(lua_State* L, int env) { + inline int pushenv(lua::State* L, int env) { if (getglobal(L, env_name(env))) { return 1; } return 0; } - int createEnvironment(lua_State*, int parent); - void removeEnvironment(lua_State*, int id); - void dump_stack(lua_State*); + int createEnvironment(lua::State*, int parent); + void removeEnvironment(lua::State*, int id); + void dump_stack(lua::State*); - inline void addfunc(lua_State* L, const std::string& name, lua_CFunction func) { + inline void addfunc(lua::State* L, const std::string& name, lua_CFunction func) { pushcfunction(L, func); setglobal(L, name); } - inline void openlib(lua_State* L, const std::string& name, const luaL_Reg* libfuncs) { + inline void openlib(lua::State* L, const std::string& name, const luaL_Reg* libfuncs) { createtable(L, 0, 0); luaL_setfuncs(L, libfuncs, 0); setglobal(L, name); diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index 9c8b6eef..6fc0f32e 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -154,28 +154,28 @@ 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] (lua_State* L) { + lua::emit_event(lua::get_main_thread(), 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] (lua_State* L) { + lua::emit_event(lua::get_main_thread(), name, [x, y, z] (auto L) { return lua::pushivec3(L, 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] (lua_State* L) { + lua::emit_event(lua::get_main_thread(), name, [x, y, z] (auto L) { return lua::pushivec3(L, x, y, z); }); } 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] (lua_State* L) { + lua::emit_event(lua::get_main_thread(), name, [x, y, z, player] (auto L) { lua::pushivec3(L, x, y, z); lua::pushinteger(L, player->getId()); return 4; @@ -184,7 +184,7 @@ void scripting::on_block_placed(Player* player, const Block* block, int x, int y void scripting::on_block_broken(Player* player, const Block* block, int x, int y, int z) { std::string name = block->name + ".broken"; - lua::emit_event(lua::get_main_thread(), name, [x, y, z, player] (lua_State* L) { + lua::emit_event(lua::get_main_thread(), name, [x, y, z, player] (auto L) { lua::pushivec3(L, x, y, z); lua::pushinteger(L, player->getId()); return 4; @@ -193,7 +193,7 @@ void scripting::on_block_broken(Player* player, const Block* block, int x, int y 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] (lua_State* L) { + return lua::emit_event(lua::get_main_thread(), name, [pos, player] (auto L) { lua::pushivec3(L, pos.x, pos.y, pos.z); lua::pushinteger(L, player->getId()); return 4; @@ -202,14 +202,14 @@ bool scripting::on_block_interact(Player* player, const Block* block, glm::ivec3 bool scripting::on_item_use(Player* player, const ItemDef* item) { std::string name = item->name + ".use"; - return lua::emit_event(lua::get_main_thread(), name, [player] (lua_State* L) { + return lua::emit_event(lua::get_main_thread(), name, [player] (lua::State* L) { return lua::pushinteger(L, player->getId()); }); } bool scripting::on_item_use_on_block(Player* player, const ItemDef* item, int x, int y, int z) { std::string name = item->name + ".useon"; - return lua::emit_event(lua::get_main_thread(), name, [x, y, z, player] (lua_State* L) { + return lua::emit_event(lua::get_main_thread(), name, [x, y, z, player] (auto L) { lua::pushivec3(L, x, y, z); lua::pushinteger(L, player->getId()); return 4; @@ -218,7 +218,7 @@ bool scripting::on_item_use_on_block(Player* player, const ItemDef* item, int x, bool scripting::on_item_break_block(Player* player, const ItemDef* item, int x, int y, int z) { std::string name = item->name + ".blockbreakby"; - return lua::emit_event(lua::get_main_thread(), name, [x, y, z, player] (lua_State* L) { + return lua::emit_event(lua::get_main_thread(), name, [x, y, z, player] (auto L) { lua::pushivec3(L, x, y, z); lua::pushinteger(L, player->getId()); return 4; @@ -231,7 +231,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, [=] (lua_State* L) { + lua::emit_event(lua::get_main_thread(), name, [=] (auto L) { for (const auto& value : *argsptr) { lua::pushvalue(L, value); } @@ -241,7 +241,7 @@ void scripting::on_ui_open( 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, [=] (lua_State* L) { + lua::emit_event(lua::get_main_thread(), name, [=] (auto L) { lua::pushinteger(L, workDone); lua::pushinteger(L, workTotal); return 2; @@ -250,7 +250,7 @@ void scripting::on_ui_progress(UiDocument* layout, int workDone, int workTotal) void scripting::on_ui_close(UiDocument* layout, Inventory* inventory) { std::string name = layout->getId() + ".close"; - lua::emit_event(lua::get_main_thread(), name, [inventory] (lua_State* L) { + lua::emit_event(lua::get_main_thread(), name, [inventory] (auto L) { return lua::pushinteger(L, inventory ? inventory->getId() : 0); }); } diff --git a/src/logic/scripting/scripting_functional.cpp b/src/logic/scripting/scripting_functional.cpp index adc7370d..b758f336 100644 --- a/src/logic/scripting/scripting_functional.cpp +++ b/src/logic/scripting/scripting_functional.cpp @@ -23,7 +23,7 @@ runnable scripting::create_runnable( } } -static lua_State* process_callback( +static lua::State* process_callback( const scriptenv& env, const std::string& src, const std::string& file diff --git a/src/logic/scripting/scripting_hud.cpp b/src/logic/scripting/scripting_hud.cpp index b1318b69..0174f9ac 100644 --- a/src/logic/scripting/scripting_hud.cpp +++ b/src/logic/scripting/scripting_hud.cpp @@ -22,7 +22,7 @@ void scripting::on_frontend_init(Hud* hud) { for (auto& pack : engine->getContentPacks()) { lua::emit_event(lua::get_main_thread(), pack.id + ".hudopen", - [&] (lua_State* L) { + [&] (lua::State* L) { return lua::pushinteger(L, hud->getPlayer()->getId()); }); } @@ -31,7 +31,7 @@ void scripting::on_frontend_init(Hud* hud) { void scripting::on_frontend_close() { for (auto& pack : engine->getContentPacks()) { lua::emit_event(lua::get_main_thread(), pack.id + ".hudclose", - [&] (lua_State* L) { + [&] (lua::State* L) { return lua::pushinteger(L, hud->getPlayer()->getId()); }); } From 0eab8ad8f2bab79ed38d484406b9c3d33f5d2651 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 11 Jun 2024 14:01:18 +0300 Subject: [PATCH 5/6] fix: world reload fatal error fix --- src/graphics/ui/elements/Container.cpp | 4 ++++ src/graphics/ui/elements/Container.hpp | 1 + 2 files changed, 5 insertions(+) diff --git a/src/graphics/ui/elements/Container.cpp b/src/graphics/ui/elements/Container.cpp index b258f5c7..6ca2b853 100644 --- a/src/graphics/ui/elements/Container.cpp +++ b/src/graphics/ui/elements/Container.cpp @@ -13,6 +13,10 @@ Container::Container(glm::vec2 size) : UINode(size) { setColor(glm::vec4()); } +Container::~Container() { + Container::clear(); +} + std::shared_ptr Container::getAt(glm::vec2 pos, std::shared_ptr self) { if (!isInteractive() || !isEnabled()) { return nullptr; diff --git a/src/graphics/ui/elements/Container.hpp b/src/graphics/ui/elements/Container.hpp index 6bb6a026..606c9179 100644 --- a/src/graphics/ui/elements/Container.hpp +++ b/src/graphics/ui/elements/Container.hpp @@ -17,6 +17,7 @@ namespace gui { bool scrollable = true; public: Container(glm::vec2 size); + virtual ~Container(); virtual void act(float delta) override; virtual void drawBackground(const DrawContext* pctx, Assets* assets); From 35ef40edfb8f73df2290f5279bd56976ffd008dd Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 11 Jun 2024 14:04:45 +0300 Subject: [PATCH 6/6] fix: compiler warning --- src/voxels/Chunks.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index 413cab85..42e3d53f 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -234,9 +234,6 @@ bool Chunks::checkReplaceability(const Block* def, blockstate state, glm::ivec3 for (int sy = 0; sy < size.y; sy++) { for (int sz = 0; sz < size.z; sz++) { for (int sx = 0; sx < size.x; sx++) { - blockstate segState = state; - segState.segment = segment_to_int(sx, sy, sz); - auto pos = origin; pos += rotation.axisX * sx; pos += rotation.axisY * sy;