diff options
Diffstat (limited to 'Archipelago/vendor/uuid.gd')
-rw-r--r-- | Archipelago/vendor/uuid.gd | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/Archipelago/vendor/uuid.gd b/Archipelago/vendor/uuid.gd new file mode 100644 index 0000000..b63fa04 --- /dev/null +++ b/Archipelago/vendor/uuid.gd | |||
@@ -0,0 +1,195 @@ | |||
1 | # Note: The code might not be as pretty it could be, since it's written | ||
2 | # in a way that maximizes performance. Methods are inlined and loops are avoided. | ||
3 | extends Node | ||
4 | |||
5 | const BYTE_MASK: int = 0b11111111 | ||
6 | |||
7 | |||
8 | static func uuidbin(): | ||
9 | randomize() | ||
10 | # 16 random bytes with the bytes on index 6 and 8 modified | ||
11 | return [ | ||
12 | randi() & BYTE_MASK, | ||
13 | randi() & BYTE_MASK, | ||
14 | randi() & BYTE_MASK, | ||
15 | randi() & BYTE_MASK, | ||
16 | randi() & BYTE_MASK, | ||
17 | randi() & BYTE_MASK, | ||
18 | ((randi() & BYTE_MASK) & 0x0f) | 0x40, | ||
19 | randi() & BYTE_MASK, | ||
20 | ((randi() & BYTE_MASK) & 0x3f) | 0x80, | ||
21 | randi() & BYTE_MASK, | ||
22 | randi() & BYTE_MASK, | ||
23 | randi() & BYTE_MASK, | ||
24 | randi() & BYTE_MASK, | ||
25 | randi() & BYTE_MASK, | ||
26 | randi() & BYTE_MASK, | ||
27 | randi() & BYTE_MASK, | ||
28 | ] | ||
29 | |||
30 | |||
31 | static func uuidbinrng(rng: RandomNumberGenerator): | ||
32 | rng.randomize() | ||
33 | return [ | ||
34 | rng.randi() & BYTE_MASK, | ||
35 | rng.randi() & BYTE_MASK, | ||
36 | rng.randi() & BYTE_MASK, | ||
37 | rng.randi() & BYTE_MASK, | ||
38 | rng.randi() & BYTE_MASK, | ||
39 | rng.randi() & BYTE_MASK, | ||
40 | ((rng.randi() & BYTE_MASK) & 0x0f) | 0x40, | ||
41 | rng.randi() & BYTE_MASK, | ||
42 | ((rng.randi() & BYTE_MASK) & 0x3f) | 0x80, | ||
43 | rng.randi() & BYTE_MASK, | ||
44 | rng.randi() & BYTE_MASK, | ||
45 | rng.randi() & BYTE_MASK, | ||
46 | rng.randi() & BYTE_MASK, | ||
47 | rng.randi() & BYTE_MASK, | ||
48 | rng.randi() & BYTE_MASK, | ||
49 | rng.randi() & BYTE_MASK, | ||
50 | ] | ||
51 | |||
52 | |||
53 | static func v4(): | ||
54 | # 16 random bytes with the bytes on index 6 and 8 modified | ||
55 | var b = uuidbin() | ||
56 | |||
57 | return ( | ||
58 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x" | ||
59 | % [ | ||
60 | # low | ||
61 | b[0], | ||
62 | b[1], | ||
63 | b[2], | ||
64 | b[3], | ||
65 | # mid | ||
66 | b[4], | ||
67 | b[5], | ||
68 | # hi | ||
69 | b[6], | ||
70 | b[7], | ||
71 | # clock | ||
72 | b[8], | ||
73 | b[9], | ||
74 | # clock | ||
75 | b[10], | ||
76 | b[11], | ||
77 | b[12], | ||
78 | b[13], | ||
79 | b[14], | ||
80 | b[15] | ||
81 | ] | ||
82 | ) | ||
83 | |||
84 | |||
85 | static func v4_rng(rng: RandomNumberGenerator): | ||
86 | # 16 random bytes with the bytes on index 6 and 8 modified | ||
87 | var b = uuidbinrng(rng) | ||
88 | |||
89 | return ( | ||
90 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x" | ||
91 | % [ | ||
92 | # low | ||
93 | b[0], | ||
94 | b[1], | ||
95 | b[2], | ||
96 | b[3], | ||
97 | # mid | ||
98 | b[4], | ||
99 | b[5], | ||
100 | # hi | ||
101 | b[6], | ||
102 | b[7], | ||
103 | # clock | ||
104 | b[8], | ||
105 | b[9], | ||
106 | # clock | ||
107 | b[10], | ||
108 | b[11], | ||
109 | b[12], | ||
110 | b[13], | ||
111 | b[14], | ||
112 | b[15] | ||
113 | ] | ||
114 | ) | ||
115 | |||
116 | |||
117 | var _uuid: Array | ||
118 | |||
119 | |||
120 | func _init(rng := RandomNumberGenerator.new()) -> void: | ||
121 | _uuid = uuidbinrng(rng) | ||
122 | |||
123 | |||
124 | func as_array() -> Array: | ||
125 | return _uuid.duplicate() | ||
126 | |||
127 | |||
128 | func as_dict(big_endian := true) -> Dictionary: | ||
129 | if big_endian: | ||
130 | return { | ||
131 | "low": (_uuid[0] << 24) + (_uuid[1] << 16) + (_uuid[2] << 8) + _uuid[3], | ||
132 | "mid": (_uuid[4] << 8) + _uuid[5], | ||
133 | "hi": (_uuid[6] << 8) + _uuid[7], | ||
134 | "clock": (_uuid[8] << 8) + _uuid[9], | ||
135 | "node": | ||
136 | ( | ||
137 | (_uuid[10] << 40) | ||
138 | + (_uuid[11] << 32) | ||
139 | + (_uuid[12] << 24) | ||
140 | + (_uuid[13] << 16) | ||
141 | + (_uuid[14] << 8) | ||
142 | + _uuid[15] | ||
143 | ) | ||
144 | } | ||
145 | else: | ||
146 | return { | ||
147 | "low": _uuid[0] + (_uuid[1] << 8) + (_uuid[2] << 16) + (_uuid[3] << 24), | ||
148 | "mid": _uuid[4] + (_uuid[5] << 8), | ||
149 | "hi": _uuid[6] + (_uuid[7] << 8), | ||
150 | "clock": _uuid[8] + (_uuid[9] << 8), | ||
151 | "node": | ||
152 | ( | ||
153 | _uuid[10] | ||
154 | + (_uuid[11] << 8) | ||
155 | + (_uuid[12] << 16) | ||
156 | + (_uuid[13] << 24) | ||
157 | + (_uuid[14] << 32) | ||
158 | + (_uuid[15] << 40) | ||
159 | ) | ||
160 | } | ||
161 | |||
162 | |||
163 | func as_string() -> String: | ||
164 | return ( | ||
165 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x" | ||
166 | % [ | ||
167 | # low | ||
168 | _uuid[0], | ||
169 | _uuid[1], | ||
170 | _uuid[2], | ||
171 | _uuid[3], | ||
172 | # mid | ||
173 | _uuid[4], | ||
174 | _uuid[5], | ||
175 | # hi | ||
176 | _uuid[6], | ||
177 | _uuid[7], | ||
178 | # clock | ||
179 | _uuid[8], | ||
180 | _uuid[9], | ||
181 | # node | ||
182 | _uuid[10], | ||
183 | _uuid[11], | ||
184 | _uuid[12], | ||
185 | _uuid[13], | ||
186 | _uuid[14], | ||
187 | _uuid[15] | ||
188 | ] | ||
189 | ) | ||
190 | |||
191 | |||
192 | func is_equal(other) -> bool: | ||
193 | # Godot Engine compares Array recursively | ||
194 | # There's no need for custom comparison here. | ||
195 | return _uuid == other._uuid | ||