2026-01-20 21:19:33 +02:00

52 lines
1.1 KiB
C++

#pragma once
#include <cstdint>
#include <iomanip>
#include <sstream>
#include <string>
namespace {
inline uint64_t fnv1a_64(const std::string& s) {
uint64_t hash = 14695981039346656037ULL;
for (unsigned char c : s) {
hash ^= c;
hash *= 1099511628211ULL;
}
return hash;
}
}
namespace mce {
class UUID {
public:
static UUID* EMPTY;
static mce::UUID (*fromString)(mcpe::string const&);
char filler[0x14];
static inline mce::UUID fromText(const std::string& text) {
uint64_t h1 = fnv1a_64(text);
uint64_t h2 = fnv1a_64("salt:" + text);
uint16_t version = ((h1 >> 16) & 0x0FFF) | 0x5000; // v5
uint16_t variant = ((h2 >> 48) & 0x3FFF) | 0x8000;
std::ostringstream oss;
oss << std::hex << std::setfill('0')
<< std::setw(8) << (uint32_t)(h1 >> 32) << "-"
<< std::setw(4) << (uint16_t)(h1 >> 16) << "-"
<< std::setw(4) << version << "-"
<< std::setw(4) << variant << "-"
<< std::setw(12) << (h2 & 0xFFFFFFFFFFFFULL);
mcpe::string uuidStr(oss.str().c_str());
return fromString(uuidStr);
}
};
}