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:
Linden
2022-08-03 22:57:31 +10:00
parent a45584ac78
commit 1b05f87c8e

View File

@@ -1,59 +1,72 @@
local function hasLoaded(fn, type, request, limit) local function request(native, hasLoaded, requestType, name, timeout)
local timeout = limit or 100 native(name)
while not fn(request) do
if coroutine.running() then
for i = 1, timeout or 5 do
Wait(0) Wait(0)
timeout -= 1 if hasLoaded(name) then
if timeout < 1 then return name
return print(('Unable to load %s after %s ticks (%s)'):format(type, limit or 100, request))
end end
end end
return request
end
---@param dict string print(("Failed to load %s '%s' after %s ticks (%s)"):format(requestType, name, timeout, animDict))
---@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)
end end
---@param set string return name
---@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)
end end
---@param model string|number ---Load an animation dictionary. When called from a thread, it will yield until it has loaded.
---@param timeout number ---@param animDict string
--- Loads a model. ---@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
---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
---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) 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 if HasModelLoaded(model) then return model end
assert(IsModelValid(model), ('Attempted to load an invalid model (%s)'):format(model))
RequestModel(model) if not IsModelValid(model) then
return hasLoaded(HasModelLoaded, 'model', model, timeout) return error(("Attempted to load invalid model (%s)"):format(model))
end end
---@param dict string return request(RequestModel, HasModelLoaded, 'model', model, timeout)
---@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)
end end
---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 fxName string
---@param timeout number ---@param timeout number? Number of ticks to wait for the particle effect to load. Default is 500.
--- Loads a named particle effect. ---@return string? fxName
function lib.requestNamedPtfxAsset(fxName, timeout) function lib.requestNamedPtfxAsset(fxName, timeout)
if HasNamedPtfxAssetLoaded(fxName) then return fxName end if HasNamedPtfxAssetLoaded(fxName) then return fxName end
RequestNamedPtfxAsset(fxName) return request(RequestNamedPtfxAsset, HasNamedPtfxAssetLoaded, 'fxName', fxName, timeout)
return hasLoaded(RequestNamedPtfxAsset, 'named ptfx', fxName, timeout)
end end