// get_inventory(location) int ModApiInventory::l_get_inventory(lua_State *L) { InventoryLocation loc; std::string type = checkstringfield(L, 1, "type"); if(type == "node"){ MAP_LOCK_REQUIRED; lua_getfield(L, 1, "pos"); v3s16 pos = check_v3s16(L, -1); loc.setNodeMeta(pos); if(getServer(L)->getInventory(loc) != NULL) InvRef::create(L, loc); else lua_pushnil(L); return 1; } else { NO_MAP_LOCK_REQUIRED; if(type == "player"){ std::string name = checkstringfield(L, 1, "name"); loc.setPlayer(name); } else if(type == "detached"){ std::string name = checkstringfield(L, 1, "name"); loc.setDetached(name); } if(getServer(L)->getInventory(loc) != NULL) InvRef::create(L, loc); else lua_pushnil(L); return 1; // END NO_MAP_LOCK_REQUIRED; } }
static const char * optstringfield(lua_State *L, int index, const char *k, const char *def) { const char *r; int got_type; got_type = lua_type(L, -1); lua_pop(L, 1); if (got_type == LUA_TNONE || got_type == LUA_TNIL) return def; return checkstringfield(L, index, k); }
/* Populate a sockaddr_storage with the info from the given lua table */ static int sockaddr_from_lua(lua_State *L, int index, struct sockaddr_storage *sa, socklen_t *addrlen) { int family, r = -1; luaL_checktype(L, index, LUA_TTABLE); family = checkintfield(L, index, "family"); memset(sa, 0, sizeof *sa); switch (family) { case AF_INET: { struct sockaddr_in *sa4 = (struct sockaddr_in *)sa; int port = checkintfield(L, index, "port"); const char *addr = checkstringfield(L, index, "addr"); checkfieldnames (L, index, Safinet_fields); if (inet_pton(AF_INET, addr, &sa4->sin_addr) == 1) { sa4->sin_family = family; sa4->sin_port = htons(port); *addrlen = sizeof(*sa4); r = 0; } break; } case AF_INET6: { struct sockaddr_in6 *sa6= (struct sockaddr_in6 *)sa; int port = checkintfield(L, index, "port"); const char *addr = checkstringfield(L, index, "addr"); checkfieldnames (L, index, Safinet_fields); if (inet_pton(AF_INET6, addr, &sa6->sin6_addr) == 1) { sa6->sin6_family= family; sa6->sin6_port = htons(port); *addrlen = sizeof(*sa6); r = 0; } break; } case AF_UNIX: { struct sockaddr_un *sau = (struct sockaddr_un *)sa; const char *path = checkstringfield(L, index, "path"); checkfieldnames (L, index, Safunix_fields); sau->sun_family = family; strlcpy(sau->sun_path, path, sizeof(sau->sun_path)); sau->sun_path[sizeof(sau->sun_path) - 1]= '\0'; *addrlen = sizeof(*sau); r = 0; break; } #if HAVE_LINUX_NETLINK_H case AF_NETLINK: { struct sockaddr_nl *san = (struct sockaddr_nl *)sa; san->nl_family = family; san->nl_pid = checkintfield(L, index, "pid"); san->nl_groups = checkintfield(L, index, "groups"); *addrlen = sizeof(*san); checkfieldnames (L, index, Safnetlink_fields); r = 0; break; } #endif default: lua_pushfstring(L, "unsupported family type %d", family); luaL_argcheck(L, 0, index, lua_tostring (L, -1)); lua_pop (L, 1); break; } return r; }