diff --git a/src/guid.cpp b/src/guid.cpp index b27c835ba8ec0..7a228e0d82829 100644 --- a/src/guid.cpp +++ b/src/guid.cpp @@ -21,14 +21,31 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include "serverenvironment.h" +#include "util/base64.h" + +GUIdGenerator::GUIdGenerator() +{ + m_rand_usable = m_rand.entropy() > 0.01; +} GUId GUIdGenerator::next(const std::string &prefix) { std::stringstream s_guid; - s_guid << prefix << std::hex << m_next << '-' << std::hex << m_env->getGameTime(); - - m_next++; + std::mt19937_64 gen(m_rand()); + std::uniform_int_distribution m_uniform(0, UINT64_MAX); + + u64 a[2]; + if (m_rand_usable) { + a[0] = m_uniform(m_rand); + a[1] = m_uniform(m_rand); + } + else { + a[0] = (static_cast(m_env->getGameTime()) << 32) + m_next; + a[1] = m_uniform(m_rand); + m_next++; + } + s_guid << prefix << base64_encode(reinterpret_cast(a), 16); return s_guid.str(); } diff --git a/src/guid.h b/src/guid.h index e619fdd1f16e2..a04fdc49e2b5d 100644 --- a/src/guid.h +++ b/src/guid.h @@ -21,7 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "irrlichttypes.h" #include "util/basic_macros.h" -#include +#include #include class ServerEnvironment; @@ -44,7 +44,7 @@ class GUIdGenerator { * @param prefix Prefix of generated GUId * @param next next value getted from loadMeta */ - GUIdGenerator() = default; + GUIdGenerator(); ~GUIdGenerator() = default; DISABLE_CLASS_COPY(GUIdGenerator) @@ -56,25 +56,13 @@ class GUIdGenerator { GUId next(const std::string &prefix); private: - /** - * Set ServerEnvironment, - * @param env ServerEnvironment. - */ - void setServerEnvironment(ServerEnvironment *env, u64 state) { m_env = env; m_next = state; }; - /** - * Set state to defined/known state, - * @param next New state value. - */ - void setState(u64 state) { m_next = state; }; - /** - * Get state, - * @return Return aactual state value. - */ - u64 getState() const { return m_next; }; - -private: + void setServerEnvironment(ServerEnvironment *env) { m_env = env; } + ServerEnvironment *m_env; - u64 m_next; - + std::random_device m_rand; + //std::uniform_int_distribution<> m_uniform(0, UINT64_MAX); + bool m_rand_usable; + u32 m_next; + friend class ServerEnvironment; }; diff --git a/src/serverenvironment.cpp b/src/serverenvironment.cpp index 4822da6af56e1..06ee092a0616f 100644 --- a/src/serverenvironment.cpp +++ b/src/serverenvironment.cpp @@ -457,7 +457,7 @@ ServerEnvironment::ServerEnvironment(ServerMap *map, m_active_object_gauge = mb->addGauge( "minetest_env_active_objects", "Number of active objects"); - m_guid_generator.setServerEnvironment(this, porting::getTimeS()); + m_guid_generator.setServerEnvironment(this); } void ServerEnvironment::init()