1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-01 17:38:41 +00:00

Add minetest.is_valid_player_name utility

This commit is contained in:
Lars Mueller 2024-08-13 02:50:36 +02:00 committed by sfan5
parent 44db47e64a
commit d3ca269c79
7 changed files with 32 additions and 15 deletions

View file

@ -859,12 +859,8 @@ int ObjectRef::l_set_observers(lua_State *L)
lua_pushnil(L);
while (lua_next(L, 2) != 0) {
std::string name = readParam<std::string>(L, -2);
if (name.empty())
throw LuaError("Observer name is empty");
if (name.size() > PLAYERNAME_SIZE)
throw LuaError("Observer name is too long");
if (!string_allowed(name, PLAYERNAME_ALLOWED_CHARS))
throw LuaError("Observer name contains invalid characters");
if (!is_valid_player_name(name))
throw LuaError("Observer name is not a valid player name");
if (!lua_toboolean(L, -1)) // falsy value?
throw LuaError("Values in the `observers` table need to be true");
observer_names.insert(std::move(name));

View file

@ -44,6 +44,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/sha1.h"
#include "my_sha256.h"
#include "util/png.h"
#include "player.h"
#include <cstdio>
// only available in zstd 1.3.5+
@ -674,6 +675,16 @@ int ModApiUtil::l_urlencode(lua_State *L)
return 1;
}
// is_valid_player_name(name)
int ModApiUtil::l_is_valid_player_name(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
auto s = readParam<std::string_view>(L, 1);
lua_pushboolean(L, is_valid_player_name(s));
return 1;
}
void ModApiUtil::Initialize(lua_State *L, int top)
{
API_FCT(log);
@ -722,6 +733,7 @@ void ModApiUtil::Initialize(lua_State *L, int top)
API_FCT(set_last_run_mod);
API_FCT(urlencode);
API_FCT(is_valid_player_name);
LuaSettings::create(L, g_settings, g_settings_path);
lua_setfield(L, top, "settings");

View file

@ -134,6 +134,9 @@ private:
// urlencode(value)
static int l_urlencode(lua_State *L);
// is_valid_player_name(name)
static int l_is_valid_player_name(lua_State *L);
public:
static void Initialize(lua_State *L, int top);
static void InitializeAsync(lua_State *L, int top);