Keep PlayerMetaRef via name not pointer

This commit is contained in:
sfan5 2024-10-07 21:08:47 +02:00
parent c8dc9c2b8d
commit 3778ed7466
3 changed files with 23 additions and 10 deletions

@ -1521,7 +1521,7 @@ int ObjectRef::l_get_meta(lua_State *L)
if (playersao == nullptr)
return 0;
PlayerMetaRef::create(L, &playersao->getMeta());
PlayerMetaRef::create(L, &getServer(L)->getEnv(), playersao->getPlayer()->getName());
return 1;
}

@ -21,6 +21,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "lua_api/l_playermeta.h"
#include "lua_api/l_internal.h"
#include "common/c_content.h"
#include "serverenvironment.h"
#include "remoteplayer.h"
#include "server/player_sao.h"
/*
PlayerMetaRef
@ -28,12 +31,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
IMetadata *PlayerMetaRef::getmeta(bool auto_create)
{
return metadata;
auto *player = m_env->getPlayer(m_name);
auto *sao = player ? player->getPlayerSAO() : nullptr;
return sao ? &sao->getMeta() : nullptr;
}
void PlayerMetaRef::clearMeta()
{
metadata->clear();
if (auto *meta = getmeta(true))
meta->clear();
}
void PlayerMetaRef::reportMetadataChange(const std::string *name)
@ -43,9 +49,9 @@ void PlayerMetaRef::reportMetadataChange(const std::string *name)
// Creates an PlayerMetaRef and leaves it on top of stack
// Not callable from Lua; all references are created on the C side.
void PlayerMetaRef::create(lua_State *L, IMetadata *metadata)
void PlayerMetaRef::create(lua_State *L, ServerEnvironment *env, std::string_view name)
{
PlayerMetaRef *o = new PlayerMetaRef(metadata);
PlayerMetaRef *o = new PlayerMetaRef(env, name);
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
luaL_getmetatable(L, className);
lua_setmetatable(L, -2);

@ -24,12 +24,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "lua_api/l_metadata.h"
#include "irrlichttypes_bloated.h"
#include "inventory.h"
#include "metadata.h"
class ServerEnvironment;
class PlayerMetaRef : public MetaDataRef
{
private:
IMetadata *metadata = nullptr;
ServerEnvironment *m_env;
std::string m_name;
static const luaL_Reg methods[];
@ -40,12 +42,17 @@ private:
virtual void reportMetadataChange(const std::string *name = nullptr);
public:
PlayerMetaRef(IMetadata *metadata) : metadata(metadata) {}
PlayerMetaRef(ServerEnvironment *env, std::string_view name) :
m_env(env), m_name(name)
{
assert(m_env);
assert(!m_name.empty());
}
~PlayerMetaRef() = default;
// Creates an ItemStackMetaRef and leaves it on top of stack
// Creates an PlayerMetaRef and leaves it on top of stack
// Not callable from Lua; all references are created on the C side.
static void create(lua_State *L, IMetadata *metadata);
static void create(lua_State *L, ServerEnvironment *env, std::string_view name);
static void Register(lua_State *L);