mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
tweak(client/streaming): rewrite
Was going to split into separate imports, then remembered why I didn't in the first place. Improved annotations. Changed timeout into a for loop, and increase default to 500 ticks. Less variable reassignments. Allow functions to be called outside threads, but will not check if the request was successful (has loaded).
This commit is contained in:
@@ -1,59 +1,72 @@
|
||||
local function hasLoaded(fn, type, request, limit)
|
||||
local timeout = limit or 100
|
||||
while not fn(request) do
|
||||
Wait(0)
|
||||
timeout -= 1
|
||||
if timeout < 1 then
|
||||
return print(('Unable to load %s after %s ticks (%s)'):format(type, limit or 100, request))
|
||||
local function request(native, hasLoaded, requestType, name, timeout)
|
||||
native(name)
|
||||
|
||||
if coroutine.running() then
|
||||
for i = 1, timeout or 5 do
|
||||
Wait(0)
|
||||
if hasLoaded(name) then
|
||||
return name
|
||||
end
|
||||
end
|
||||
|
||||
print(("Failed to load %s '%s' after %s ticks (%s)"):format(requestType, name, timeout, animDict))
|
||||
end
|
||||
return request
|
||||
|
||||
return name
|
||||
end
|
||||
|
||||
---@param dict string
|
||||
---@param timeout number
|
||||
--- Loads an animation dictionary.
|
||||
function lib.requestAnimDict(dict, timeout)
|
||||
if HasAnimDictLoaded(dict) then return dict end
|
||||
assert(DoesAnimDictExist(dict), ('Attempted to load an invalid animdict (%s)'):format(dict))
|
||||
RequestAnimDict(dict)
|
||||
return hasLoaded(HasAnimDictLoaded, 'animdict', dict, timeout)
|
||||
---Load an animation dictionary. When called from a thread, it will yield until it has loaded.
|
||||
---@param animDict string
|
||||
---@param timeout number? Number of ticks to wait for the dictionary to load. Default is 500.
|
||||
---@return string? animDict
|
||||
function lib.requestAnimDict(animDict, timeout)
|
||||
if HasAnimDictLoaded(animDict) then return animDict end
|
||||
|
||||
if not DoesAnimDictExist(animDict) then
|
||||
return error(("Attempted to load invalid animDict (%s)"):format(animDict))
|
||||
end
|
||||
|
||||
return request(RequestAnimDict, HasAnimDictLoaded, 'animDict', animDict, timeout)
|
||||
end
|
||||
|
||||
---@param set string
|
||||
---@param timeout number
|
||||
--- Loads an animation clipset.
|
||||
function lib.requestAnimSet(set, timeout)
|
||||
if HasAnimSetLoaded(set) then return set end
|
||||
RequestAnimSet(set)
|
||||
return hasLoaded(HasAnimSetLoaded, 'animset', set, timeout)
|
||||
---Load an animation clipset. When called from a thread, it will yield until it has loaded.
|
||||
---@param animSet string
|
||||
---@param timeout number? Number of ticks to wait for the clipset to load. Default is 500.
|
||||
---@return string? animSet
|
||||
function lib.requestAnimSet(animSet, timeout)
|
||||
if HasAnimSetLoaded(animSet) then return animSet end
|
||||
return request(RequestAnimSet, HasAnimSetLoaded, 'animSet', animSet, timeout)
|
||||
end
|
||||
|
||||
---@param model string|number
|
||||
---@param timeout number
|
||||
--- Loads a model.
|
||||
---Load a model. When called from a thread, it will yield until it has loaded.
|
||||
---@param model number
|
||||
---@param timeout number? Number of ticks to wait for the model to load. Default is 500.
|
||||
---@return number? model
|
||||
function lib.requestModel(model, timeout)
|
||||
model = tonumber(model) or joaat(model)
|
||||
if not tonumber(model) then model = joaat(model) end
|
||||
if HasModelLoaded(model) then return model end
|
||||
assert(IsModelValid(model), ('Attempted to load an invalid model (%s)'):format(model))
|
||||
RequestModel(model)
|
||||
return hasLoaded(HasModelLoaded, 'model', model, timeout)
|
||||
|
||||
if not IsModelValid(model) then
|
||||
return error(("Attempted to load invalid model (%s)"):format(model))
|
||||
end
|
||||
|
||||
return request(RequestModel, HasModelLoaded, 'model', model, timeout)
|
||||
end
|
||||
|
||||
---@param dict string
|
||||
---@param timeout number
|
||||
--- Loads a texture dictionary.
|
||||
function lib.requestStreamedTextureDict(dict, timeout)
|
||||
if HasStreamedTextureDictLoaded(dict) then return dict end
|
||||
RequestStreamedTextureDict(dict)
|
||||
return hasLoaded(HasStreamedTextureDictLoaded, 'texture dict', dict, timeout)
|
||||
---Load a texture dictionary. When called from a thread, it will yield until it has loaded.
|
||||
---@param textureDict string
|
||||
---@param timeout number? Number of ticks to wait for the dictionary to load. Default is 500.
|
||||
---@return string? textureDict
|
||||
function lib.requestStreamedTextureDict(textureDict, timeout)
|
||||
if HasStreamedTextureDictLoaded(textureDict) then return textureDict end
|
||||
return request(RequestStreamedTextureDict, HasStreamedTextureDictLoaded, 'textureDict', textureDict, timeout)
|
||||
end
|
||||
|
||||
---Load a named particle effect. When called from a thread, it will yield until it has loaded.
|
||||
---@param fxName string
|
||||
---@param timeout number
|
||||
--- Loads a named particle effect.
|
||||
---@param timeout number? Number of ticks to wait for the particle effect to load. Default is 500.
|
||||
---@return string? fxName
|
||||
function lib.requestNamedPtfxAsset(fxName, timeout)
|
||||
if HasNamedPtfxAssetLoaded(fxName) then return fxName end
|
||||
RequestNamedPtfxAsset(fxName)
|
||||
return hasLoaded(RequestNamedPtfxAsset, 'named ptfx', fxName, timeout)
|
||||
return request(RequestNamedPtfxAsset, HasNamedPtfxAssetLoaded, 'fxName', fxName, timeout)
|
||||
end
|
||||
Reference in New Issue
Block a user