checheche/Robo-Bus-A31-HMI/common/util/uuid_util.gd
2024-12-06 21:22:11 +08:00

41 lines
946 B
GDScript

class_name UuidUtil
extends RefCounted
const MODULO_8_BIT = 256
static func getRandomInt():
# Randomize every time to minimize the risk of collisions
randomize()
return randi() % MODULO_8_BIT
static func uuidbin():
# 16 random bytes with the bytes checked index 6 and 8 modified
return [
getRandomInt(), getRandomInt(), getRandomInt(), getRandomInt(),
getRandomInt(), getRandomInt(), ((getRandomInt()) & 0x0f) | 0x40, getRandomInt(),
((getRandomInt()) & 0x3f) | 0x80, getRandomInt(), getRandomInt(), getRandomInt(),
getRandomInt(), getRandomInt(), getRandomInt(), getRandomInt(),
]
static func uuid():
# 16 random bytes with the bytes checked index 6 and 8 modified
var b = uuidbin()
return '%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x' % [
# low
b[0], b[1], b[2], b[3],
# mid
b[4], b[5],
# hi
b[6], b[7],
# clock
b[8], b[9],
# clock
b[10], b[11], b[12], b[13], b[14], b[15]
]