diff options
Diffstat (limited to '')
-rw-r--r-- | src/script/cpp_api/s_player.cpp | 58 | ||||
-rw-r--r-- | src/script/cpp_api/s_player.h | 9 | ||||
-rw-r--r-- | src/script/cpp_api/s_security.cpp | 20 | ||||
-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 |
8 files changed, 158 insertions, 24 deletions
diff --git a/src/script/cpp_api/s_player.cpp b/src/script/cpp_api/s_player.cpp index 22b24f363..2f366605b 100644 --- a/src/script/cpp_api/s_player.cpp +++ b/src/script/cpp_api/s_player.cpp @@ -27,6 +27,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "lua_api/l_item.h" #include "util/string.h" +#include "remoteplayer.h" + void ScriptApiPlayer::on_newplayer(ServerActiveObject *player) { SCRIPTAPI_PRECHECKHEADER @@ -78,7 +80,7 @@ bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player, } void ScriptApiPlayer::on_rightclickplayer(ServerActiveObject *player, - ServerActiveObject *clicker) + ServerActiveObject *clicker) { SCRIPTAPI_PRECHECKHEADER // Get core.registered_on_rightclickplayers @@ -91,7 +93,7 @@ void ScriptApiPlayer::on_rightclickplayer(ServerActiveObject *player, } s32 ScriptApiPlayer::on_player_hpchange(ServerActiveObject *player, - s32 hp_change, const PlayerHPChangeReason &reason) + s32 hp_change, const PlayerHPChangeReason &reason) { SCRIPTAPI_PRECHECKHEADER @@ -380,3 +382,55 @@ void ScriptApiPlayer::player_inventory_OnTake( pushPutTakeArguments("take", ma.from_inv, ma.from_list, ma.from_i, stack, player); runCallbacks(4, RUN_CALLBACKS_MODE_FIRST); } + +void ScriptApiPlayer::on_player_change_wield(ServerActiveObject *player, u16 item) +{ + SCRIPTAPI_PRECHECKHEADER + + // Get core.registered_on_leaveplayers + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_player_change_wield"); + // Call callbacks + objectrefGetOrCreate(L, player); + lua_pushnumber(L, item); + runCallbacks(2, RUN_CALLBACKS_MODE_FIRST); +} + +void ScriptApiPlayer::on_player_change_keys(ServerActiveObject *player, PlayerControl &control) +{ + SCRIPTAPI_PRECHECKHEADER + + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_player_change_keys"); + objectrefGetOrCreate(L, player); + + lua_newtable(L); + + lua_pushboolean(L, control.direction_keys & (1 << 0)); + lua_setfield(L, -2, "up"); + lua_pushboolean(L, control.direction_keys & (1 << 1)); + lua_setfield(L, -2, "down"); + lua_pushboolean(L, control.direction_keys & (1 << 2)); + lua_setfield(L, -2, "left"); + lua_pushboolean(L, control.direction_keys & (1 << 3)); + lua_setfield(L, -2, "right"); + lua_pushboolean(L, control.jump); + lua_setfield(L, -2, "jump"); + lua_pushboolean(L, control.aux1); + lua_setfield(L, -2, "aux1"); + lua_pushboolean(L, control.sneak); + lua_setfield(L, -2, "sneak"); + lua_pushboolean(L, control.dig); + lua_setfield(L, -2, "dig"); + lua_pushboolean(L, control.place); + lua_setfield(L, -2, "place"); + // Legacy fields to ensure mod compatibility + lua_pushboolean(L, control.dig); + lua_setfield(L, -2, "LMB"); + lua_pushboolean(L, control.place); + lua_setfield(L, -2, "RMB"); + lua_pushboolean(L, control.zoom); + lua_setfield(L, -2, "zoom"); + + runCallbacks(2, RUN_CALLBACKS_MODE_FIRST); +} diff --git a/src/script/cpp_api/s_player.h b/src/script/cpp_api/s_player.h index e866aee46..8f3834c59 100644 --- a/src/script/cpp_api/s_player.h +++ b/src/script/cpp_api/s_player.h @@ -23,6 +23,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "irr_v3d.h" #include "util/string.h" +#include "remoteplayer.h" + struct MoveAction; struct InventoryLocation; struct ItemStack; @@ -79,7 +81,12 @@ public: void player_inventory_OnTake( const MoveAction &ma, const ItemStack &stack, ServerActiveObject *player); -private: + + void on_player_change_wield(ServerActiveObject *player, u16 item); + + void on_player_change_keys(ServerActiveObject *player, PlayerControl &control); + +//private: void pushPutTakeArguments( const char *method, const InventoryLocation &loc, const std::string &listname, int index, const ItemStack &stack, diff --git a/src/script/cpp_api/s_security.cpp b/src/script/cpp_api/s_security.cpp index 316b19926..2222dec3f 100644 --- a/src/script/cpp_api/s_security.cpp +++ b/src/script/cpp_api/s_security.cpp @@ -30,7 +30,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include <algorithm> #include <iostream> - #define SECURE_API(lib, name) \ lua_pushcfunction(L, sl_##lib##_##name); \ lua_setfield(L, -2, #name); @@ -572,16 +571,18 @@ bool ScriptApiSecurity::checkPath(lua_State *L, const char *path, // Allow paths in mod path // Don't bother if write access isn't important, since it will be handled later - if (write_required || write_allowed != NULL) { - const ModSpec *mod = gamedef->getModSpec(mod_name); - if (mod) { - str = fs::AbsolutePath(mod->path); - if (!str.empty() && fs::PathStartsWith(abs_path, str)) { - if (write_allowed) *write_allowed = true; - return true; - } +// if (write_required || write_allowed != NULL) { + + // Do bother, rather than compare a few hundred strings when the mod is just trying to access its own stuff + const ModSpec *mod = gamedef->getModSpec(mod_name); + if (mod) { + str = fs::AbsolutePath(mod->path); + if (!str.empty() && fs::PathStartsWith(abs_path, str)) { + if (write_allowed) *write_allowed = true; + return true; } } +// } } lua_pop(L, 1); // Pop mod name @@ -820,7 +821,6 @@ int ScriptApiSecurity::sl_io_input(lua_State *L) return 1; } - int ScriptApiSecurity::sl_io_output(lua_State *L) { if (lua_isstring(L, 1)) { 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; } |