feat(resource/streaming): set timeout length and return requested item on success (#4)

* feat: set timeout length

* fix: adjust variable names and set default to 100 ticks

* fix: make timeout local again
This commit is contained in:
DokaDoka
2021-12-12 14:14:06 +11:00
committed by GitHub
parent ec1a98b83a
commit 34bd55e4a6

View File

@@ -1,57 +1,59 @@
local function hasLoaded(fn, name)
local timeout = 0
while not fn(name) do
local function hasLoaded(fn, type, request, limit)
local timeout = limit or 100
while not fn(request) do
Wait(0)
timeout += 1
if timeout > 20 then return false end
timeout -= 1
if timeout < 1 then
return print(('Unable to load %s after %s ticks (%s)'):format(type, limit, request))
end
end
return true
return request
end
---@param dict string
---@param timeout number
--- Loads an animation dictionary.
function lib.requestAnimDict(dict)
if HasAnimDictLoaded(dict) then return true end
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)
if hasLoaded(HasAnimDictLoaded, dict) then return true end
print(('Unable to load animdict after 20 ticks (%s)'):format(dict))
return hasLoaded(HasModelLoaded, 'animdict', dict, timeout)
end
---@param set string
---@param timeout number
--- Loads an animation clipset.
function lib.requestAnimSet(set)
if HasAnimSetLoaded(set) then return true end
function lib.requestAnimSet(set, timeout)
if HasAnimSetLoaded(set) then return set end
RequestAnimSet(set)
if hasLoaded(HasAnimSetLoaded, set) then return true end
print(('Unable to load animset after 20 ticks (%s)'):format(set))
return hasLoaded(HasModelLoaded, 'animset', set, timeout)
end
---@param model string|number
---@param timeout number
--- Loads a model.
function lib.requestModel(model)
model = type(model) == 'number' and model or joaat(model)
if HasModelLoaded(model) then return true end
function lib.requestModel(model, timeout)
model = tonumber(model) or joaat(model)
if HasModelLoaded(model) then return model end
assert(IsModelValid(model), ('Attempted to load an invalid model (%s)'):format(model))
RequestModel(model)
if hasLoaded(HasModelLoaded, model) then return true end
print(('Unable to load model after 20 ticks (%s)'):format(model))
return hasLoaded(HasModelLoaded, 'model', model, timeout)
end
---@param dict string
---@param timeout number
--- Loads a texture dictionary.
function lib.requestStreamedTextureDict(dict)
if HasStreamedTextureDictLoaded(dict) then return true end
function lib.requestStreamedTextureDict(dict, timeout)
if HasStreamedTextureDictLoaded(dict) then return dict end
RequestStreamedTextureDict(dict)
if hasLoaded(HasStreamedTextureDictLoaded, dict) then return true end
print(('Unable to load texture dict after 20 ticks (%s)'):format(dict))
return hasLoaded(HasModelLoaded, 'texture dict', dict, timeout)
end
---@param fxName string
---@param timeout number
--- Loads a named particle effect.
function lib.requestNamedPtfxAsset(fxName)
if HasNamedPtfxAssetLoaded(fxName) then return true end
function lib.requestNamedPtfxAsset(fxName, timeout)
if HasNamedPtfxAssetLoaded(fxName) then return fxName end
RequestNamedPtfxAsset(fxName)
if hasLoaded(RequestNamedPtfxAsset, fxName) then return true end
print(('Unable to load named ptfx after 20 ticks (%s)'):format(fxName))
return hasLoaded(HasModelLoaded, 'named ptfx', fxName, timeout)
end