From b7153d4ffa8d5efc20031c1bcb2b624f5902911a Mon Sep 17 00:00:00 2001 From: Stane034 Date: Sat, 25 Oct 2025 09:44:06 +0200 Subject: [PATCH 1/5] moved ESX.Streaming to esx_lib --- [core]/es_extended/client/compat.lua | 10 ++++++-- .../resource/streaming/client.lua} | 25 +++++++++++++------ 2 files changed, 25 insertions(+), 10 deletions(-) rename [core]/{es_extended/client/modules/streaming.lua => esx_lib/resource/streaming/client.lua} (71%) diff --git a/[core]/es_extended/client/compat.lua b/[core]/es_extended/client/compat.lua index a5c9b0302..8ca704087 100644 --- a/[core]/es_extended/client/compat.lua +++ b/[core]/es_extended/client/compat.lua @@ -1,2 +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.requestModel +ESX.Streaming.RequestStreamedTextureDict = xLib.requestStreamedTextureDict +ESX.Streaming.RequestNamedPtfxAsset = xLib.requestNamedPtfxAsset +ESX.Streaming.RequestAnimSet = xLib.requestAnimSet +ESX.Streaming.RequestAnimDict = xLib.requestAnimDict +ESX.Streaming.RequestWeaponAsset = xLib.requestWeaponAsset diff --git a/[core]/es_extended/client/modules/streaming.lua b/[core]/esx_lib/resource/streaming/client.lua similarity index 71% rename from [core]/es_extended/client/modules/streaming.lua rename to [core]/esx_lib/resource/streaming/client.lua index 8614c4fba..168d5fe6b 100644 --- a/[core]/es_extended/client/modules/streaming.lua +++ b/[core]/esx_lib/resource/streaming/client.lua @@ -1,9 +1,7 @@ -ESX.Streaming = {} - ---@param modelHash number | string ---@param cb? function ---@return number | nil -function ESX.Streaming.RequestModel(modelHash, cb) +xLib.requestModel = function(modelHash, cb) modelHash = type(modelHash) == "number" and modelHash or joaat(modelHash) if not IsModelInCdimage(modelHash) then return end @@ -17,7 +15,7 @@ end ---@param textureDict string ---@param cb? function ---@return string | nil -function ESX.Streaming.RequestStreamedTextureDict(textureDict, cb) +xLib.requestStreamedTextureDict = function(textureDict, cb) RequestStreamedTextureDict(textureDict, false) while not HasStreamedTextureDictLoaded(textureDict) do Wait(500) end @@ -28,7 +26,7 @@ end ---@param assetName string ---@param cb? function ---@return string | nil -function ESX.Streaming.RequestNamedPtfxAsset(assetName, cb) +xLib.requestNamedPtfxAsset = function(assetName, cb) RequestNamedPtfxAsset(assetName) while not HasNamedPtfxAssetLoaded(assetName) do Wait(500) end @@ -39,7 +37,7 @@ end ---@param animSet string ---@param cb? function ---@return string | nil -function ESX.Streaming.RequestAnimSet(animSet, cb) +xLib.requestAnimSet = function(animSet, cb) RequestAnimSet(animSet) while not HasAnimSetLoaded(animSet) do Wait(500) end @@ -50,7 +48,7 @@ end ---@param animDict string ---@param cb? function ---@return string | nil -function ESX.Streaming.RequestAnimDict(animDict, cb) +xLib.requestAnimDict = function(animDict, cb) RequestAnimDict(animDict) while not HasAnimDictLoaded(animDict) do Wait(500) end @@ -61,10 +59,21 @@ end ---@param weaponHash number | string ---@param cb? function ---@return string | number | nil -function ESX.Streaming.RequestWeaponAsset(weaponHash, cb) +xLib.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.requestAudioBank = function(bankName, cb) + RequestAudioBank(bankName, false) + + while not RequestScriptAudioBank(bankName, false) do Wait(500) end + + return cb and cb(bankName) or bankName +end From 27962a888fd24c7d81116344b257c07c82a5b3b8 Mon Sep 17 00:00:00 2001 From: Stane034 Date: Sat, 25 Oct 2025 18:53:09 +0200 Subject: [PATCH 2/5] made streaming table in xLib --- [core]/es_extended/client/compat.lua | 12 ++++++------ .../streaming/client.lua | 19 ++++++++++++------- 2 files changed, 18 insertions(+), 13 deletions(-) rename [core]/esx_lib/{resource => imports}/streaming/client.lua (78%) diff --git a/[core]/es_extended/client/compat.lua b/[core]/es_extended/client/compat.lua index 8ca704087..604f63ea1 100644 --- a/[core]/es_extended/client/compat.lua +++ b/[core]/es_extended/client/compat.lua @@ -1,8 +1,8 @@ --All client-side functions outsourced from the Core to the lib will be stored here for compatability, e.g: -ESX.Streaming.RequestModel = xLib.requestModel -ESX.Streaming.RequestStreamedTextureDict = xLib.requestStreamedTextureDict -ESX.Streaming.RequestNamedPtfxAsset = xLib.requestNamedPtfxAsset -ESX.Streaming.RequestAnimSet = xLib.requestAnimSet -ESX.Streaming.RequestAnimDict = xLib.requestAnimDict -ESX.Streaming.RequestWeaponAsset = xLib.requestWeaponAsset +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 diff --git a/[core]/esx_lib/resource/streaming/client.lua b/[core]/esx_lib/imports/streaming/client.lua similarity index 78% rename from [core]/esx_lib/resource/streaming/client.lua rename to [core]/esx_lib/imports/streaming/client.lua index 168d5fe6b..38f6479e4 100644 --- a/[core]/esx_lib/resource/streaming/client.lua +++ b/[core]/esx_lib/imports/streaming/client.lua @@ -1,7 +1,9 @@ +xLib.streaming = {} + ---@param modelHash number | string ---@param cb? function ---@return number | nil -xLib.requestModel = function(modelHash, cb) +xLib.streaming.requestModel = function(modelHash, cb) modelHash = type(modelHash) == "number" and modelHash or joaat(modelHash) if not IsModelInCdimage(modelHash) then return end @@ -15,7 +17,7 @@ end ---@param textureDict string ---@param cb? function ---@return string | nil -xLib.requestStreamedTextureDict = function(textureDict, cb) +xLib.streaming.requestStreamedTextureDict = function(textureDict, cb) RequestStreamedTextureDict(textureDict, false) while not HasStreamedTextureDictLoaded(textureDict) do Wait(500) end @@ -26,7 +28,7 @@ end ---@param assetName string ---@param cb? function ---@return string | nil -xLib.requestNamedPtfxAsset = function(assetName, cb) +xLib.streaming.requestNamedPtfxAsset = function(assetName, cb) RequestNamedPtfxAsset(assetName) while not HasNamedPtfxAssetLoaded(assetName) do Wait(500) end @@ -37,7 +39,7 @@ end ---@param animSet string ---@param cb? function ---@return string | nil -xLib.requestAnimSet = function(animSet, cb) +xLib.streaming.requestAnimSet = function(animSet, cb) RequestAnimSet(animSet) while not HasAnimSetLoaded(animSet) do Wait(500) end @@ -48,7 +50,7 @@ end ---@param animDict string ---@param cb? function ---@return string | nil -xLib.requestAnimDict = function(animDict, cb) +xLib.streaming.requestAnimDict = function(animDict, cb) RequestAnimDict(animDict) while not HasAnimDictLoaded(animDict) do Wait(500) end @@ -59,7 +61,7 @@ end ---@param weaponHash number | string ---@param cb? function ---@return string | number | nil -xLib.requestWeaponAsset = function(weaponHash, cb) +xLib.streaming.requestWeaponAsset = function(weaponHash, cb) RequestWeaponAsset(weaponHash, 31, 0) while not HasWeaponAssetLoaded(weaponHash) do Wait(500) end @@ -70,10 +72,13 @@ end ---@param bankName string ---@param cb? function ---@return string | nil -xLib.requestAudioBank = function(bankName, cb) +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 \ No newline at end of file From d5f8feafae091d5c69c996100b845645b0dcd351 Mon Sep 17 00:00:00 2001 From: Stane034 Date: Sun, 26 Oct 2025 00:14:41 +0200 Subject: [PATCH 3/5] Moved Timeouts and Await to esx_lib --- [core]/es_extended/client/compat.lua | 3 ++ [core]/es_extended/server/compat.lua | 4 ++ [core]/es_extended/shared/functions.lua | 39 ------------------ .../imports/timeout/shared.lua} | 9 ++-- [core]/esx_lib/imports/waitFor/shared.lua | 41 +++++++++++++++++++ 5 files changed, 54 insertions(+), 42 deletions(-) rename [core]/{es_extended/shared/modules/timeout.lua => esx_lib/imports/timeout/shared.lua} (77%) create mode 100644 [core]/esx_lib/imports/waitFor/shared.lua diff --git a/[core]/es_extended/client/compat.lua b/[core]/es_extended/client/compat.lua index 604f63ea1..f270d129f 100644 --- a/[core]/es_extended/client/compat.lua +++ b/[core]/es_extended/client/compat.lua @@ -6,3 +6,6 @@ ESX.Streaming.RequestNamedPtfxAsset = xLib.streaming.requestNamedPtfxAsset ESX.Streaming.RequestAnimSet = xLib.streaming.requestAnimSet ESX.Streaming.RequestAnimDict = xLib.streaming.requestAnimDict ESX.Streaming.RequestWeaponAsset = xLib.streaming.requestWeaponAsset +ESX.SetTimeout = xLib.timeout.setTimeout +ESX.ClearTimeout = xLib.timeout.clearTimeout +ESX.Await = xLib.waitFor \ No newline at end of file diff --git a/[core]/es_extended/server/compat.lua b/[core]/es_extended/server/compat.lua index dc345fcb0..6ad884de0 100644 --- a/[core]/es_extended/server/compat.lua +++ b/[core]/es_extended/server/compat.lua @@ -1,2 +1,6 @@ --[[ All server-side 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 \ No newline at end of file diff --git a/[core]/es_extended/shared/functions.lua b/[core]/es_extended/shared/functions.lua index e8fe8a760..71e1390e8 100644 --- a/[core]/es_extended/shared/functions.lua +++ b/[core]/es_extended/shared/functions.lua @@ -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 diff --git a/[core]/es_extended/shared/modules/timeout.lua b/[core]/esx_lib/imports/timeout/shared.lua similarity index 77% rename from [core]/es_extended/shared/modules/timeout.lua rename to [core]/esx_lib/imports/timeout/shared.lua index b0590d5a0..c68eec042 100644 --- a/[core]/es_extended/shared/modules/timeout.lua +++ b/[core]/esx_lib/imports/timeout/shared.lua @@ -1,10 +1,12 @@ +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) local id = TimeoutCount + 1 SetTimeout(msec, function() @@ -12,7 +14,6 @@ ESX.SetTimeout = function(msec, cb) CancelledTimeouts[id] = nil return end - cb() end) @@ -23,6 +24,8 @@ end ---@param id number ---@return nil -ESX.ClearTimeout = function(id) +xLib.timeout.clearTimeout = function(id) CancelledTimeouts[id] = true end + +return xLib.timeout diff --git a/[core]/esx_lib/imports/waitFor/shared.lua b/[core]/esx_lib/imports/waitFor/shared.lua new file mode 100644 index 000000000..31da0bfd8 --- /dev/null +++ b/[core]/esx_lib/imports/waitFor/shared.lua @@ -0,0 +1,41 @@ + +---@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 + + 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 + +return xLib.waitFor \ No newline at end of file From fbd11c52fa0cb017084fc22139c9c4581f89f3a5 Mon Sep 17 00:00:00 2001 From: zykem#0643 <86602828+Zykem@users.noreply.github.com> Date: Sun, 26 Oct 2025 15:34:48 +0100 Subject: [PATCH 4/5] refactor(lib/waitFor) Use verify instead of AssertType --- [core]/esx_lib/imports/waitFor/shared.lua | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/[core]/esx_lib/imports/waitFor/shared.lua b/[core]/esx_lib/imports/waitFor/shared.lua index 31da0bfd8..8202a1c67 100644 --- a/[core]/esx_lib/imports/waitFor/shared.lua +++ b/[core]/esx_lib/imports/waitFor/shared.lua @@ -10,13 +10,11 @@ xLib.waitFor = function(conditionFunc, errorMessage, timeoutMs) error("Timeout should be a positive number.") end - if not ESX.IsFunctionReference(conditionFunc) then - error("Condition Function should be a function reference.") - end + xLib.verify(conditionFunc, "function", true) -- 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.") + xLib.verify(errorMessage, "string", true) end local invokingResource = GetInvokingResource() From f4b951a173c5abde3f1cdacc9ff6e478ca443118 Mon Sep 17 00:00:00 2001 From: zykem#0643 <86602828+Zykem@users.noreply.github.com> Date: Sun, 26 Oct 2025 15:37:11 +0100 Subject: [PATCH 5/5] feat(lib/timeout) Type validation for cb function --- [core]/esx_lib/imports/timeout/shared.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/[core]/esx_lib/imports/timeout/shared.lua b/[core]/esx_lib/imports/timeout/shared.lua index c68eec042..e684f3a45 100644 --- a/[core]/esx_lib/imports/timeout/shared.lua +++ b/[core]/esx_lib/imports/timeout/shared.lua @@ -7,6 +7,8 @@ local CancelledTimeouts = {} ---@param cb function ---@return number xLib.timeout.setTimeout = function(msec, cb) + xLib.verify(cb, "function", true) + local id = TimeoutCount + 1 SetTimeout(msec, function()