1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-09-30 19:22:14 +00:00

Add persistent unique identifiers for objects (#14135)

This commit is contained in:
sfence 2025-07-09 10:40:26 +02:00 committed by GitHub
parent e0f8243629
commit 4f42b4308c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 257 additions and 19 deletions

View file

@ -50,7 +50,14 @@ LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos, const std::string &d
rotation.X = readF1000(is);
rotation.Z = readF1000(is);
// if (version2 < 2)
if (version2 < 2) {
m_guid = env->getGUIDGenerator().next();
break;
}
m_guid.deSerialize(is);
// if (version2 < 3)
// break;
// <read new values>
break;
@ -70,6 +77,14 @@ LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos, const std::string &d
m_rotation = rotation;
}
LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos, const std::string &name,
const std::string &state) :
UnitSAO(env, pos),
m_init_name(name), m_init_state(state),
m_guid(env->getGUIDGenerator().next())
{
}
LuaEntitySAO::~LuaEntitySAO()
{
if(m_registered){
@ -294,11 +309,13 @@ void LuaEntitySAO::getStaticData(std::string *result) const
writeF1000(os, m_rotation.Y);
// version2. Increase this variable for new values
writeU8(os, 1); // PROTOCOL_VERSION >= 37
writeU8(os, 2);
writeF1000(os, m_rotation.X);
writeF1000(os, m_rotation.Z);
m_guid.serialize(os);
// <write new values>
*result = os.str();
@ -414,6 +431,13 @@ u16 LuaEntitySAO::getHP() const
return m_hp;
}
std::string LuaEntitySAO::getGUID() const
{
// The "@" ensures that entity GUIDs are easily recognizable
// and makes it obvious that they can't collide with player names.
return "@" + m_guid.base64();
}
void LuaEntitySAO::setVelocity(v3f velocity)
{
m_velocity = velocity;

View file

@ -6,6 +6,7 @@
#pragma once
#include "unit_sao.h"
#include "util/guid.h"
class LuaEntitySAO : public UnitSAO
{
@ -15,11 +16,7 @@ public:
LuaEntitySAO(ServerEnvironment *env, v3f pos, const std::string &data);
// Used by the Lua API
LuaEntitySAO(ServerEnvironment *env, v3f pos, const std::string &name,
const std::string &state) :
UnitSAO(env, pos),
m_init_name(name), m_init_state(state)
{
}
const std::string &state);
~LuaEntitySAO();
ActiveObjectType getType() const { return ACTIVEOBJECT_TYPE_LUAENTITY; }
@ -47,6 +44,7 @@ public:
void setHP(s32 hp, const PlayerHPChangeReason &reason);
u16 getHP() const;
std::string getGUID() const;
/* LuaEntitySAO-specific */
void setVelocity(v3f velocity);
@ -86,6 +84,8 @@ private:
std::string m_init_state;
bool m_registered = false;
MyGUID m_guid;
v3f m_velocity;
v3f m_acceleration;

View file

@ -14,6 +14,7 @@ PlayerSAO::PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, session_t p
bool is_singleplayer):
UnitSAO(env_, v3f(0,0,0)),
m_player(player_),
m_player_name(player_->getName()),
m_peer_id_initial(peer_id_),
m_is_singleplayer(is_singleplayer)
{

View file

@ -78,6 +78,7 @@ public:
void addPos(const v3f &added_pos) override;
void moveTo(v3f pos, bool continuous) override;
void setPlayerYaw(const float yaw);
std::string getGUID() const override { return m_player_name; }
// Data should not be sent at player initialization
void setPlayerYawAndSend(const float yaw);
void setLookPitch(const float pitch);
@ -182,6 +183,8 @@ private:
std::string generateUpdatePhysicsOverrideCommand() const;
RemotePlayer *m_player = nullptr;
// Extra variable because during shutdown m_player is unavailable, but we still need to know.
std::string m_player_name; ///< used as GUID
session_t m_peer_id_initial = 0; ///< only used to initialize RemotePlayer
// Cheat prevention

View file

@ -141,6 +141,10 @@ public:
virtual u16 getHP() const
{ return 0; }
/// @brief Returns an unique ID for this object (persistent across unload, server restarts).
/// @note Because these strings are very short, copying them is not expensive.
virtual std::string getGUID() const = 0;
virtual void setArmorGroups(const ItemGroupList &armor_groups)
{}
virtual const ItemGroupList &getArmorGroups() const