Apply feedback.
This commit is contained in:
parent
d576336406
commit
328c565b4e
@ -8326,8 +8326,8 @@ child will follow movement and rotation of that bone.
|
||||
}
|
||||
```
|
||||
* `get_guid()`: returns a global unique identifier (a string)
|
||||
* For players a global unique identiticator is a player name.
|
||||
* For non-player objects, it is unique generated string.
|
||||
* For players this is a player name.
|
||||
* For Lua entities, it is a unique generated string.
|
||||
|
||||
#### Lua entity only (no-op for other objects)
|
||||
|
||||
|
45
src/guid.cpp
45
src/guid.cpp
@ -1,21 +1,6 @@
|
||||
/*
|
||||
Minetest
|
||||
Copyright (C) 2021, DS
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 2.1 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License along
|
||||
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
// Luanti
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
// Copyright (C) 2024 SFENCE
|
||||
|
||||
#include "guid.h"
|
||||
#include <sstream>
|
||||
@ -26,24 +11,22 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
GUIdGenerator::GUIdGenerator() :
|
||||
m_uniform(0, UINT64_MAX)
|
||||
{
|
||||
m_rand_usable = m_rand.entropy() > 0.01;
|
||||
if (m_rand.entropy() <= 0.010)
|
||||
throw BaseException("The system's provided random generator does not match "
|
||||
"the entropy requirements for the GUId generator.");
|
||||
}
|
||||
|
||||
GUId GUIdGenerator::next(const std::string &prefix)
|
||||
GUId GUIdGenerator::next()
|
||||
{
|
||||
std::stringstream s_guid;
|
||||
|
||||
u64 a[2];
|
||||
if (m_rand_usable) {
|
||||
a[0] = m_uniform(m_rand);
|
||||
a[1] = m_uniform(m_rand);
|
||||
}
|
||||
else {
|
||||
a[0] = (static_cast<u64>(m_env->getGameTime()) << 32) + m_next;
|
||||
a[1] = m_uniform(m_rand);
|
||||
m_next++;
|
||||
}
|
||||
s_guid << prefix << base64_encode(std::string_view(reinterpret_cast<char *>(a), 16));
|
||||
s_guid << std::hex << std::setfill('0');
|
||||
s_guid << std::setw(8) << (m_uniform(m_rand) & 0xFFFFFFFF) << "-";
|
||||
s_guid << std::setw(4) << (m_uniform(m_rand) & 0xFFFF) << "-";
|
||||
s_guid << std::setw(4) << (m_uniform(m_rand) & 0xFFFF) << "-";
|
||||
s_guid << std::setw(4) << (m_uniform(m_rand) & 0xFFFF) << "-";
|
||||
s_guid << std::setw(8) << (m_uniform(m_rand) & 0xFFFFFFFF);
|
||||
s_guid << std::setw(4) << (m_uniform(m_rand) & 0xFFFF);
|
||||
|
||||
return s_guid.str();
|
||||
}
|
||||
|
30
src/guid.h
30
src/guid.h
@ -1,21 +1,6 @@
|
||||
/*
|
||||
Minetest
|
||||
Copyright (C) 2021, DS
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 2.1 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License along
|
||||
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
// Luanti
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
// Copyright (C) 2024 SFENCE
|
||||
|
||||
#pragma once
|
||||
|
||||
@ -40,9 +25,6 @@ class GUIdGenerator {
|
||||
public:
|
||||
/**
|
||||
* Creates a new uninitialized generator.
|
||||
* @param env ServerEnvironment where generator is running
|
||||
* @param prefix Prefix of generated GUId
|
||||
* @param next next value getted from loadMeta
|
||||
*/
|
||||
GUIdGenerator();
|
||||
|
||||
@ -51,9 +33,9 @@ public:
|
||||
|
||||
/**
|
||||
* Generates the next guid, which it will never return again.
|
||||
* @return the new guid, or "" if the generator is not yet initialized
|
||||
* @return the new guid
|
||||
*/
|
||||
GUId next(const std::string &prefix);
|
||||
GUId next();
|
||||
|
||||
private:
|
||||
void setServerEnvironment(ServerEnvironment *env) { m_env = env; }
|
||||
@ -61,8 +43,6 @@ private:
|
||||
ServerEnvironment *m_env;
|
||||
std::random_device m_rand;
|
||||
std::uniform_int_distribution<u64> m_uniform;
|
||||
bool m_rand_usable;
|
||||
u32 m_next;
|
||||
|
||||
friend class ServerEnvironment;
|
||||
};
|
||||
|
@ -109,9 +109,7 @@ int ObjectRef::l_get_guid(lua_State *L)
|
||||
if (sao == nullptr)
|
||||
return 0;
|
||||
|
||||
const std::string &guid = sao->getGuid();
|
||||
|
||||
lua_pushstring(L, guid.c_str());
|
||||
lua_pushstring(L, sao->getGuid().c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -434,10 +434,10 @@ bool LuaEntitySAO::setGuid(std::string &guid)
|
||||
}
|
||||
return false;
|
||||
}
|
||||
GUId LuaEntitySAO::getGuid()
|
||||
const GUId& LuaEntitySAO::getGuid()
|
||||
{
|
||||
if (m_guid.empty()) {
|
||||
m_guid = m_env->getGUIdGenerator().next(std::string("@"));
|
||||
m_guid = m_env->getGUIdGenerator().next();
|
||||
}
|
||||
return m_guid;
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ public:
|
||||
void setHP(s32 hp, const PlayerHPChangeReason &reason);
|
||||
u16 getHP() const;
|
||||
bool setGuid(std::string &guid);
|
||||
GUId getGuid() override;
|
||||
const GUId& getGuid() override;
|
||||
|
||||
/* LuaEntitySAO-specific */
|
||||
void setVelocity(v3f velocity);
|
||||
|
@ -410,7 +410,7 @@ void PlayerSAO::setPlayerYaw(const float yaw)
|
||||
UnitSAO::setRotation(rotation);
|
||||
}
|
||||
|
||||
GUId PlayerSAO::getGuid()
|
||||
const GUId& PlayerSAO::getGuid()
|
||||
{
|
||||
return m_guid;
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ public:
|
||||
void addPos(const v3f &added_pos) override;
|
||||
void moveTo(v3f pos, bool continuous) override;
|
||||
void setPlayerYaw(const float yaw);
|
||||
GUId getGuid() override;
|
||||
const GUId& getGuid() override;
|
||||
// Data should not be sent at player initialization
|
||||
void setPlayerYawAndSend(const float yaw);
|
||||
void setLookPitch(const float pitch);
|
||||
|
@ -143,7 +143,7 @@ public:
|
||||
{ return 0; }
|
||||
|
||||
// Returns always the same unique string for the same object.
|
||||
virtual GUId getGuid() = 0;
|
||||
virtual const GUId& getGuid() = 0;
|
||||
|
||||
virtual void setArmorGroups(const ItemGroupList &armor_groups)
|
||||
{}
|
||||
@ -220,11 +220,6 @@ public:
|
||||
about handling it.
|
||||
*/
|
||||
bool m_static_exists = false;
|
||||
/*
|
||||
Set this to true when the staticdata needs to be saved even though it
|
||||
looks like it did not change.
|
||||
*/
|
||||
bool m_force_write_staticdata = false;
|
||||
/*
|
||||
The block from which the object was loaded from, and in which
|
||||
a copy of the static data resides.
|
||||
|
@ -2258,9 +2258,7 @@ void ServerEnvironment::deactivateFarObjects(const bool _force_delete)
|
||||
While changes are always saved, blocks are only marked as modified
|
||||
if the object has moved or different staticdata. (see above)
|
||||
*/
|
||||
bool shall_be_written = (!stays_in_same_block || data_changed ||
|
||||
obj->m_force_write_staticdata);
|
||||
obj->m_force_write_staticdata = false;
|
||||
bool shall_be_written = (!stays_in_same_block || data_changed);
|
||||
u32 reason = shall_be_written ? MOD_REASON_STATIC_DATA_CHANGED : MOD_REASON_UNKNOWN;
|
||||
|
||||
// Delete old static object
|
||||
|
@ -14,5 +14,7 @@ public:
|
||||
virtual bool getCollisionBox(aabb3f *toset) const { return false; }
|
||||
virtual bool getSelectionBox(aabb3f *toset) const { return false; }
|
||||
virtual bool collideWithObjects() const { return false; }
|
||||
virtual std::string getGuid() {return "";}
|
||||
virtual const GUId& getGuid() {return m_guid;}
|
||||
private:
|
||||
GUId m_guid;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user