diff options
Diffstat (limited to '')
-rw-r--r-- | src/script/lua_api/l_env.cpp | 78 | ||||
-rw-r--r-- | src/script/lua_api/l_env.h | 3 | ||||
-rw-r--r-- | src/script/lua_api/l_object.cpp | 10 | ||||
-rw-r--r-- | src/script/lua_api/l_server.cpp | 2 | ||||
-rw-r--r-- | src/script/lua_api/l_util.cpp | 2 |
5 files changed, 84 insertions, 11 deletions
diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index b26c89e7d..a7e5abecd 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -46,6 +46,10 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "client/client.h" #endif +#include <fcntl.h> +#include <sys/syscall.h> +#include <unistd.h> + const EnumString ModApiEnvMod::es_ClearObjectsMode[] = { {CLEAR_OBJECTS_MODE_FULL, "full"}, @@ -258,8 +262,9 @@ void LuaEmergeAreaCallback(v3s16 blockpos, EmergeAction action, void *param) assert(state->refcount > 0); // state must be protected by envlock - Server *server = state->script->getServer(); - MutexAutoLock envlock(server->m_env_mutex); +// Server *server = state->script->getServer(); +// MutexAutoLock envlock(server->m_env_mutex); + // already locked now, don't deadlock state->refcount--; @@ -728,6 +733,73 @@ int ModApiEnvMod::l_get_player_by_name(lua_State *L) return 1; } +inline uint64_t microtime(void) { + struct timeval tv; + struct timezone tz = {0}; + + gettimeofday(&tv, &tz); + return (tv.tv_sec * 1000000) + tv.tv_usec; +} + +int ModApiEnvMod::l_read_all(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TSTRING); + const char *path = lua_tostring(L, 1); + CHECK_SECURE_PATH(L, path, false); + + int fd = open(path, O_RDONLY); + if (fd < 0) + return 0; + + size_t len = lseek(fd, 0, SEEK_END); + char data[len]; + len = pread(fd, data, len, 0); + close(fd); + + lua_pushlstring(L, data, len); + + return 1; +} + +int ModApiEnvMod::l_read_sections(lua_State *L) +{ + luaL_checktype(L, 2, LUA_TSTRING); + luaL_checktype(L, 1, LUA_TTABLE); + + const char *path = lua_tostring(L, 2); + CHECK_SECURE_PATH(L, path, false); + + s32 len = lua_objlen(L, 1); + if (len%2 == 1) + return 0; + + int fd = open(path, O_RDONLY); + if (fd < 0) + return 0; + + size_t filesize = lseek(fd, 0, SEEK_END); + char data[len/2][filesize]; + size_t sectionlens[len/2]; + + int retvals = 0; + for (s32 i = 1; i <= len; i++) { + lua_rawgeti(L, 1, i); + off_t offset = lua_tonumber(L, -1); + lua_pop(L, 1); + i++; + lua_rawgeti(L, 1, i); + sectionlens[(i/2)-1] = pread(fd, data[(i/2)-1], lua_tonumber(L, -1), offset); + lua_pop(L, 1); + retvals++; + } + close(fd); + + for (s32 i = 0; i < len/2; i++) + lua_pushlstring(L, data[i], sectionlens[i]); + + return retvals; +} + // get_objects_inside_radius(pos, radius) int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L) { @@ -1495,6 +1567,8 @@ void ModApiEnvMod::Initialize(lua_State *L, int top) API_FCT(forceload_free_block); API_FCT(compare_block_status); API_FCT(get_translated_string); + API_FCT(read_all); + API_FCT(read_sections); } void ModApiEnvMod::InitializeClient(lua_State *L, int top) diff --git a/src/script/lua_api/l_env.h b/src/script/lua_api/l_env.h index a7d406d2a..f5e482d2d 100644 --- a/src/script/lua_api/l_env.h +++ b/src/script/lua_api/l_env.h @@ -201,6 +201,9 @@ private: // Get a string translated server side static int l_get_translated_string(lua_State * L); + static int l_read_all(lua_State *L); + static int l_read_sections(lua_State *L); + /* Helpers */ static void collectNodeIds(lua_State *L, int idx, diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index 6bd07a4c1..dc65def23 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -730,8 +730,7 @@ int ObjectRef::l_set_nametag_attributes(lua_State *L) } lua_pop(L, 1); - std::string nametag = getstringfield_default(L, 2, "text", ""); - prop->nametag = nametag; + prop->nametag = getstringfield_default(L, 2, "text", prop->nametag); prop->validate(); sao->notifyObjectPropertiesModified(); @@ -1366,7 +1365,7 @@ int ObjectRef::l_get_player_control(lua_State *L) lua_newtable(L); if (player == nullptr) return 1; - + const PlayerControl &control = player->getPlayerControl(); lua_pushboolean(L, control.direction_keys & (1 << 0)); lua_setfield(L, -2, "up"); @@ -2082,11 +2081,10 @@ int ObjectRef::l_set_stars(lua_State *L) star_params.scale = getfloatfield_default(L, 2, "scale", star_params.scale); + star_params.day_opacity = getfloatfield_default(L, 2, + "day_opacity", star_params.day_opacity); } - star_params.day_opacity = getfloatfield_default(L, 2, - "day_opacity", star_params.day_opacity); - getServer(L)->setStars(player, star_params); return 0; } diff --git a/src/script/lua_api/l_server.cpp b/src/script/lua_api/l_server.cpp index a5daae346..251037a9c 100644 --- a/src/script/lua_api/l_server.cpp +++ b/src/script/lua_api/l_server.cpp @@ -240,7 +240,6 @@ int ModApiServer::l_get_player_information(lua_State *L) lua_pushstring(L, info.lang_code.c_str()); lua_settable(L, table); -#ifndef NDEBUG lua_pushstring(L,"serialization_version"); lua_pushnumber(L, info.ser_vers); lua_settable(L, table); @@ -264,7 +263,6 @@ int ModApiServer::l_get_player_information(lua_State *L) lua_pushstring(L,"state"); lua_pushstring(L, ClientInterface::state2Name(info.state).c_str()); lua_settable(L, table); -#endif return 1; } diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index f602aed99..bf12c9863 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -165,7 +165,7 @@ int ModApiUtil::l_get_tool_wear_after_use(lua_State *L) NO_MAP_LOCK_REQUIRED; u32 uses = readParam<int>(L, 1); u16 initial_wear = readParam<int>(L, 2, 0); - u16 wear = calculateResultWear(uses, initial_wear); + u32 wear = calculateResultWear(uses, initial_wear); lua_pushnumber(L, wear); return 1; } |