Merge remote-tracking branch 'upstream/esx_lib' into esx_lib

This commit is contained in:
TostusUK
2026-07-15 23:01:45 -05:00
5 changed files with 46 additions and 74 deletions
+4
View File
@@ -15,3 +15,7 @@ ESX.Table.TableContains = xLib.table.contains
ESX.Table.Sort = xLib.table.sort ESX.Table.Sort = xLib.table.sort
ESX.Table.ToArray = xLib.table.toArray ESX.Table.ToArray = xLib.table.toArray
ESX.Table.Wipe = xLib.table.wipe ESX.Table.Wipe = xLib.table.wipe
ESX.SetTimeout = xLib.timeout.setTimeout
ESX.ClearTimeout = xLib.timeout.clearTimeout
ESX.Await = xLib.waitFor
-39
View File
@@ -178,45 +178,6 @@ function ESX.IsFunctionReference(val)
return typeVal == "function" or (typeVal == "table" and type(getmetatable(val)?.__call) == "function") return typeVal == "function" or (typeVal == "table" and type(getmetatable(val)?.__call) == "function")
end end
---@param conditionFunc function A function that is repeatedly called until it returns a truthy value or the timeout is exceeded.
---@param errorMessage? string Optional. If set, an error will be thrown with this message if the condition is not met within the timeout. If not set, no error will be thrown.
---@param timeoutMs? number Optional. The maximum time to wait (in milliseconds) for the condition to be met. Defaults to 1000ms.
---@return boolean, any: Returns success status and the returned value of the condition function.
function ESX.Await(conditionFunc, errorMessage, timeoutMs)
timeoutMs = timeoutMs or 1000
if timeoutMs < 0 then
error("Timeout should be a positive number.")
end
if not ESX.IsFunctionReference(conditionFunc) then
error("Condition Function should be a function reference.")
end
-- since errorMessage is optional, we only validate it if the user provided it.
if errorMessage then
ESX.AssertType(errorMessage, "string", "errorMessage should be a string.")
end
local invokingResource = GetInvokingResource()
local startTimeMs = GetGameTimer()
while GetGameTimer() - startTimeMs < timeoutMs do
local result = conditionFunc()
if result then
return true, result
end
Wait(0)
end
if errorMessage then
error(("[%s] -> %s"):format(invokingResource, errorMessage))
end
return false
end
---@param str string ---@param str string
---@param allowDigits boolean? Allow numbers if necessary ---@param allowDigits boolean? Allow numbers if necessary
---@return boolean ---@return boolean
+33 -31
View File
@@ -4,7 +4,7 @@ xLib.streaming = {}
---@param modelHash number | string ---@param modelHash number | string
---@param cb? function ---@param cb? function
---@return number | nil ---@return number | nil
function xLib.streaming.requestModel(modelHash, cb) xLib.streaming.requestModel = function(modelHash, cb)
modelHash = type(modelHash) == "number" and modelHash or joaat(modelHash) modelHash = type(modelHash) == "number" and modelHash or joaat(modelHash)
if not IsModelInCdimage(modelHash) then return end if not IsModelInCdimage(modelHash) then return end
@@ -21,66 +21,68 @@ end
---@param textureDict string ---@param textureDict string
---@param cb? function ---@param cb? function
---@return string | nil ---@return string | nil
function xLib.streaming.requestStreamedTextureDict(textureDict, cb) xLib.streaming.requestStreamedTextureDict = function(textureDict, cb)
RequestStreamedTextureDict(textureDict, false) RequestStreamedTextureDict(textureDict, false)
while not HasStreamedTextureDictLoaded(textureDict) do while not HasStreamedTextureDictLoaded(textureDict) do Wait(500) end
Wait(500)
end
return cb and cb(textureDict) or textureDict return cb and cb(textureDict) or textureDict
end end
---@param assetName string ---@param assetName string
---@param cb? function ---@param cb? function
---@return string | nil ---@return string | nil
function xLib.streaming.requestNamedPtfxAsset(assetName, cb) xLib.streaming.requestNamedPtfxAsset = function(assetName, cb)
RequestNamedPtfxAsset(assetName) RequestNamedPtfxAsset(assetName)
while not HasNamedPtfxAssetLoaded(assetName) do while not HasNamedPtfxAssetLoaded(assetName) do Wait(500) end
Wait(500)
end
return cb and cb(assetName) or assetName return cb and cb(assetName) or assetName
end end
---@param animSet string ---@param animSet string
---@param cb? function ---@param cb? function
---@return string | nil ---@return string | nil
function xLib.streaming.requestAnimSet(animSet, cb) xLib.streaming.requestAnimSet = function(animSet, cb)
RequestAnimSet(animSet) RequestAnimSet(animSet)
while not HasAnimSetLoaded(animSet) do while not HasAnimSetLoaded(animSet) do Wait(500) end
Wait(500)
end
return cb and cb(animSet) or animSet return cb and cb(animSet) or animSet
end end
---@param animDict string ---@param animDict string
---@param cb? function ---@param cb? function
---@return string | nil ---@return string | nil
function xLib.streaming.requestAnimDict(animDict, cb) xLib.streaming.requestAnimDict = function(animDict, cb)
RequestAnimDict(animDict) RequestAnimDict(animDict)
while not HasAnimDictLoaded(animDict) do while not HasAnimDictLoaded(animDict) do Wait(500) end
Wait(500)
end
return cb and cb(animDict) or animDict return cb and cb(animDict) or animDict
end end
---@param weaponHash number | string ---@param weaponHash number | string
---@param cb? function ---@param cb? function
---@return string | number | nil ---@return string | number | nil
function xLib.streaming.requestWeaponAsset(weaponHash, cb) xLib.streaming.requestWeaponAsset = function(weaponHash, cb)
RequestWeaponAsset(weaponHash, 31, 0) RequestWeaponAsset(weaponHash, 31, 0)
while not HasWeaponAssetLoaded(weaponHash) do while not HasWeaponAssetLoaded(weaponHash) do Wait(500) end
Wait(500)
end
return cb and cb(weaponHash) or weaponHash return cb and cb(weaponHash) or weaponHash
end end
---@param bankName string
---@param cb? function
---@return string | nil
xLib.streaming.requestAudioBank = function(bankName, cb)
RequestAudioBank(bankName, false)
while not RequestScriptAudioBank(bankName, false) do Wait(500) end
return cb and cb(bankName) or bankName
end
return xLib.streaming return xLib.streaming
@@ -1,10 +1,14 @@
xLib.timeout = {}
local TimeoutCount = 0 local TimeoutCount = 0
local CancelledTimeouts = {} local CancelledTimeouts = {}
---@param msec number ---@param msec number
---@param cb function ---@param cb function
---@return number ---@return number
ESX.SetTimeout = function(msec, cb) xLib.timeout.setTimeout = function(msec, cb)
xLib.verify(cb, "function", true)
local id <const> = TimeoutCount + 1 local id <const> = TimeoutCount + 1
SetTimeout(msec, function() SetTimeout(msec, function()
@@ -12,7 +16,6 @@ ESX.SetTimeout = function(msec, cb)
CancelledTimeouts[id] = nil CancelledTimeouts[id] = nil
return return
end end
cb() cb()
end) end)
@@ -23,6 +26,8 @@ end
---@param id number ---@param id number
---@return nil ---@return nil
ESX.ClearTimeout = function(id) xLib.timeout.clearTimeout = function(id)
CancelledTimeouts[id] = true CancelledTimeouts[id] = true
end end
return xLib.timeout