-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuuid.gd
43 lines (33 loc) · 1.13 KB
/
uuid.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# uuid - static uuid generator for Godot Engine
# https://github.com/binogure-studio/godot-uuid
# Note: The code might not be as pretty it could be, since it's written
# in a way that maximizes performance. Methods are inlined and loops are avoided.
extends Node
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 on 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 v4():
# 16 random bytes with the bytes on 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]
]