2022-05-26 13:05:04 +00:00
|
|
|
--[[
|
|
|
|
uuid generation
|
|
|
|
]]
|
|
|
|
|
|
|
|
local path = (...):gsub("uuid", "")
|
|
|
|
|
|
|
|
local uuid = {}
|
|
|
|
|
|
|
|
--helper for optionally passed random; defaults to love.math.random if present, otherwise math.random
|
|
|
|
local _global_random = math.random
|
|
|
|
if love and love.math and love.math.random then
|
|
|
|
_global_random = love.math.random
|
|
|
|
end
|
|
|
|
|
|
|
|
local function _random(min, max, r)
|
|
|
|
return r and r:random(min, max)
|
|
|
|
or _global_random(min, max)
|
|
|
|
end
|
|
|
|
|
2023-01-09 15:02:32 +00:00
|
|
|
local uuid4_template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
|
2022-05-26 13:05:04 +00:00
|
|
|
|
|
|
|
--generate a UUID version 4 (random)
|
2023-01-09 15:06:07 +00:00
|
|
|
function uuid.uuid4(rng)
|
2023-01-09 15:02:32 +00:00
|
|
|
return uuid4_template:gsub("[xy]", function (c)
|
2022-05-26 13:05:04 +00:00
|
|
|
-- x should be 0x0-0xF, the single y should be 0x8-0xB
|
|
|
|
-- 4 should always just be 4 (denoting uuid version)
|
2023-01-09 15:06:07 +00:00
|
|
|
return string.format("%x", c == "x" and _random(0x0, 0xF, rng) or _random(0x8, 0xB, rng))
|
2022-05-26 13:05:04 +00:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
return uuid
|