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");