-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec24ecb
commit ccfcff2
Showing
3 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# vault configurations | ||
[meta] | ||
# friendly name | ||
title = "ULID" | ||
# contributors | ||
author = "Timmy" | ||
# version | ||
version = "1.0.0" | ||
|
||
# script configurations | ||
[script] | ||
# whether to force the custom map package to do not load | ||
force_no_map_package = false | ||
# auto destroy all entities spawned by this package when it unloads | ||
auto_cleanup = true | ||
# whether to load all level entities on client - only enable it if your package needs to use level static meshes entities | ||
load_level_entities = false | ||
# the game version (major.minor) at the time this package was created, for granting compatibility between breaking changes | ||
compatibility_version = "1.64" | ||
# packages requirements | ||
packages_requirements = [ | ||
|
||
] | ||
# asset packs requirements | ||
assets_requirements = [ | ||
|
||
] | ||
# compatible game modes | ||
compatible_game_modes = [ | ||
|
||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Package.Require("Shared/ulid.lua") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
local rand = math.random | ||
local concat = table.concat | ||
local format = string.format | ||
local sub = string.sub | ||
local osTime = os.time | ||
|
||
ULID = ULID or {} | ||
Package.Export("ULID", ULID) | ||
|
||
local __tULIDMap = {} | ||
|
||
-- Number of entropy characters (total length will be 10 + this value) | ||
local iEntropyChars = 16 | ||
-- ULID regex pattern, used for validation | ||
local sULIDPattern = "^"..string.rep("[0-9a-f]", 10 + iEntropyChars).."$" | ||
|
||
-- Cache hex code, for faster lookup | ||
local tHexMap = {} | ||
for i = 0, 15 do tHexMap[i] = format("%x", i) end | ||
|
||
-- Internal function that generates the ULID hash | ||
local function generateULID() | ||
local tEntropy = {} | ||
for i = 1, iEntropyChars do | ||
tEntropy[#tEntropy + 1] = tHexMap[rand(0, 15)] | ||
end | ||
return sub(format("%010x", osTime() * 100), -10)..concat(tEntropy) | ||
end | ||
|
||
---`🔸 Client`<br>`🔹 Server`<br> | ||
---Generate a random ULID | ||
---@param xData? any @The data to bind to the ULID, defaults to `true` | ||
---@return string @The generated ULID | ||
--- | ||
function ULID.Generate(xData) | ||
local sULID = generateULID() | ||
if (__tULIDMap[sULID] ~= nil) then | ||
return ULID.Generate() | ||
end | ||
|
||
__tULIDMap[sULID] = (xData ~= nil) and xData or true | ||
return sULID | ||
end | ||
|
||
---`🔸 Client`<br>`🔹 Server`<br> | ||
---Returns the ULID registry | ||
---@return table<string, ULID> @The ULID registry | ||
---@see ULID.Get | ||
--- | ||
function ULID.GetTable() | ||
return __tULIDMap | ||
end | ||
|
||
---`🔸 Client`<br>`🔹 Server`<br> | ||
---Return a value binded to the passed ULID | ||
---@param sULID string @The ULID to search | ||
---@return any @The value binded to the ULID, defaults to `true` | ||
---@see ULID.GetTable | ||
--- | ||
function ULID.Get(sULID) | ||
return __tULIDMap[sULID] | ||
end | ||
|
||
---`🔸 Client`<br>`🔹 Server`<br> | ||
---Adds an ULID to the cache | ||
---@param sULID string @The ULID to add | ||
---@param xData? any @The data to bind to the ULID, defaults to `true` | ||
---@see ULID.Clear | ||
--- | ||
function ULID.Store(sULID, xData) | ||
if (type(sULID) ~= "string") or not sULID:IsULID() then return end | ||
__tULIDMap[sULID] = (xData ~= nil) and xData or true | ||
end | ||
|
||
---`🔸 Client`<br>`🔹 Server`<br> | ||
---Removes the ULID from the cache | ||
---@param sULID string @The ULID to remove<br> | ||
---@see ULID.Store | ||
--- | ||
function ULID.Clear(sULID) | ||
if (type(sULID) ~= "string") then return end | ||
__tULIDMap[sULID] = nil | ||
end | ||
|
||
---`🔸 Client`<br>`🔹 Server`<br> | ||
---Returns wether the string is an ULID | ||
---@param self string @The string to check | ||
---@return boolean @Wether the string is an ULID | ||
--- | ||
function string.IsULID(self) | ||
if not self or (#self ~= (10 + iEntropyChars)) then return false end | ||
return string.match(self, sULIDPattern) ~= nil | ||
end |