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
print(("Failed to load %s '%s' after %s ticks (%s)"):format(requestType, name, timeout, animDict))
end
return name
end end
---@param dict string ---Load an animation dictionary. When called from a thread, it will yield until it has loaded.
---@param timeout number ---@param animDict string
--- Loads an animation dictionary. ---@param timeout number? Number of ticks to wait for the dictionary to load. Default is 500.
function lib.requestAnimDict(dict, timeout) ---@return string? animDict
if HasAnimDictLoaded(dict) then return dict end function lib.requestAnimDict(animDict, timeout)
assert(DoesAnimDictExist(dict), ('Attempted to load an invalid animdict (%s)'):format(dict)) if HasAnimDictLoaded(animDict) then return animDict end
RequestAnimDict(dict)
return hasLoaded(HasAnimDictLoaded, 'animdict', dict, timeout) if not DoesAnimDictExist(animDict) then
return error(("Attempted to load invalid animDict (%s)"):format(animDict))
end
return request(RequestAnimDict, HasAnimDictLoaded, 'animDict', animDict, timeout)
end end
---@param set string ---Load an animation clipset. When called from a thread, it will yield until it has loaded.
---@param timeout number ---@param animSet string
--- Loads an animation clipset. ---@param timeout number? Number of ticks to wait for the clipset to load. Default is 500.
function lib.requestAnimSet(set, timeout) ---@return string? animSet
if HasAnimSetLoaded(set) then return set end function lib.requestAnimSet(animSet, timeout)
RequestAnimSet(set) if HasAnimSetLoaded(animSet) then return animSet end
return hasLoaded(HasAnimSetLoaded, 'animset', set, timeout) return request(RequestAnimSet, HasAnimSetLoaded, 'animSet', animSet, timeout)
end end
---@param model string|number ---Load a model. When called from a thread, it will yield until it has loaded.
---@param timeout number ---@param model number
--- Loads a model. ---@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
return request(RequestModel, HasModelLoaded, 'model', model, timeout)
end end
---@param dict string ---Load a texture dictionary. When called from a thread, it will yield until it has loaded.
---@param timeout number ---@param textureDict string
--- Loads a texture dictionary. ---@param timeout number? Number of ticks to wait for the dictionary to load. Default is 500.
function lib.requestStreamedTextureDict(dict, timeout) ---@return string? textureDict
if HasStreamedTextureDictLoaded(dict) then return dict end function lib.requestStreamedTextureDict(textureDict, timeout)
RequestStreamedTextureDict(dict) if HasStreamedTextureDictLoaded(textureDict) then return textureDict end
return hasLoaded(HasStreamedTextureDictLoaded, 'texture dict', dict, timeout) return request(RequestStreamedTextureDict, HasStreamedTextureDictLoaded, 'textureDict', textureDict, timeout)
end 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