Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion [core]/es_extended/client/compat.lua
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
--All client-side functions outsourced from the Core to the lib will be stored here for compatability, e.g:
--All client-side functions outsourced from the Core to the lib will be stored here for compatability, e.g:

ESX.Streaming.RequestModel = xLib.streaming.requestModel
ESX.Streaming.RequestStreamedTextureDict = xLib.streaming.requestStreamedTextureDict
ESX.Streaming.RequestNamedPtfxAsset = xLib.streaming.requestNamedPtfxAsset
ESX.Streaming.RequestAnimSet = xLib.streaming.requestAnimSet
ESX.Streaming.RequestAnimDict = xLib.streaming.requestAnimDict
ESX.Streaming.RequestWeaponAsset = xLib.streaming.requestWeaponAsset
3 changes: 2 additions & 1 deletion [core]/es_extended/server/compat.lua
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
--All server-side functions outsourced from the Core to the lib will be stored here for compatability, e.g:
--[[ All server-side functions outsourced from the Core to the lib will be stored here for compatability, e.g:
--]]
6 changes: 5 additions & 1 deletion [core]/es_extended/shared/compat.lua
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
--All shared functions outsourced from the Core to the lib will be stored here for compatability, e.g:
--All shared functions outsourced from the Core to the lib will be stored here for compatability, e.g:

ESX.SetTimeout = xLib.timeout.setTimeout
ESX.ClearTimeout = xLib.timeout.clearTimeout
ESX.Await = xLib.waitFor
39 changes: 0 additions & 39 deletions [core]/es_extended/shared/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -178,45 +178,6 @@ function ESX.IsFunctionReference(val)
return typeVal == "function" or (typeVal == "table" and type(getmetatable(val)?.__call) == "function")
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 allowDigits boolean? Allow numbers if necessary
---@return boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
ESX.Streaming = {}
xLib.streaming = {}

---@param modelHash number | string
---@param cb? function
---@return number | nil
function ESX.Streaming.RequestModel(modelHash, cb)
xLib.streaming.requestModel = function(modelHash, cb)
modelHash = type(modelHash) == "number" and modelHash or joaat(modelHash)

if not IsModelInCdimage(modelHash) then return end
Expand All @@ -17,7 +17,7 @@ end
---@param textureDict string
---@param cb? function
---@return string | nil
function ESX.Streaming.RequestStreamedTextureDict(textureDict, cb)
xLib.streaming.requestStreamedTextureDict = function(textureDict, cb)
RequestStreamedTextureDict(textureDict, false)

while not HasStreamedTextureDictLoaded(textureDict) do Wait(500) end
Expand All @@ -28,7 +28,7 @@ end
---@param assetName string
---@param cb? function
---@return string | nil
function ESX.Streaming.RequestNamedPtfxAsset(assetName, cb)
xLib.streaming.requestNamedPtfxAsset = function(assetName, cb)
RequestNamedPtfxAsset(assetName)

while not HasNamedPtfxAssetLoaded(assetName) do Wait(500) end
Expand All @@ -39,7 +39,7 @@ end
---@param animSet string
---@param cb? function
---@return string | nil
function ESX.Streaming.RequestAnimSet(animSet, cb)
xLib.streaming.requestAnimSet = function(animSet, cb)
RequestAnimSet(animSet)

while not HasAnimSetLoaded(animSet) do Wait(500) end
Expand All @@ -50,7 +50,7 @@ end
---@param animDict string
---@param cb? function
---@return string | nil
function ESX.Streaming.RequestAnimDict(animDict, cb)
xLib.streaming.requestAnimDict = function(animDict, cb)
RequestAnimDict(animDict)

while not HasAnimDictLoaded(animDict) do Wait(500) end
Expand All @@ -61,10 +61,24 @@ end
---@param weaponHash number | string
---@param cb? function
---@return string | number | nil
function ESX.Streaming.RequestWeaponAsset(weaponHash, cb)
xLib.streaming.requestWeaponAsset = function(weaponHash, cb)
RequestWeaponAsset(weaponHash, 31, 0)

while not HasWeaponAssetLoaded(weaponHash) do Wait(500) end

return cb and cb(weaponHash) or weaponHash
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
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
xLib.timeout = {}

local TimeoutCount = 0
local CancelledTimeouts = {}

---@param msec number
---@param cb function
---@return number
ESX.SetTimeout = function(msec, cb)
xLib.timeout.setTimeout = function(msec, cb)
xLib.verify(cb, "function", true)

local id <const> = TimeoutCount + 1

SetTimeout(msec, function()
if CancelledTimeouts[id] then
CancelledTimeouts[id] = nil
return
end

cb()
end)

Expand All @@ -23,6 +26,8 @@ end

---@param id number
---@return nil
ESX.ClearTimeout = function(id)
xLib.timeout.clearTimeout = function(id)
CancelledTimeouts[id] = true
end

return xLib.timeout
39 changes: 39 additions & 0 deletions [core]/esx_lib/imports/waitFor/shared.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

---@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.
xLib.waitFor = function(conditionFunc, errorMessage, timeoutMs)
timeoutMs = timeoutMs or 1000

if timeoutMs < 0 then
error("Timeout should be a positive number.")
end

xLib.verify(conditionFunc, "function", true)

-- since errorMessage is optional, we only validate it if the user provided it.
if errorMessage then
xLib.verify(errorMessage, "string", true)
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

return xLib.waitFor