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