1
0
mirror of https://github.com/luanti-org/luanti.git synced 2025-10-24 21:35:21 +02: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
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