103 lines
2.4 KiB
Lua
103 lines
2.4 KiB
Lua
DBTextures = {}
|
|
local db = {}
|
|
|
|
local dbUploadStatus = {}
|
|
local isUploading = {}
|
|
|
|
function DBTextures.init()
|
|
InvokeEx.cancelInvoke(DBTextures.releaseTimeout)
|
|
InvokeEx.invoke(DBTextures.releaseTimeout, 60)
|
|
end
|
|
|
|
function DBTextures.clean()
|
|
InvokeEx.cancelInvoke(DBTextures.releaseTimeout)
|
|
for k, v in ipairs(db) do
|
|
GameObject.DestroyImmediate(v)
|
|
end
|
|
db = {}
|
|
|
|
---@param v UnityEngine.UnityWebRequest
|
|
for k, www in pairs(dbUploadStatus) do
|
|
if www then
|
|
www.Dispose()
|
|
www = nil
|
|
end
|
|
end
|
|
dbUploadStatus = {}
|
|
end
|
|
|
|
function DBTextures.releaseTimeout()
|
|
for k, v in ipairs(db) do
|
|
if DateEx.nowMS - v.lastUseTime > 300000 then
|
|
GameObject.DestroyImmediate(v.texture)
|
|
db[k] = nil
|
|
end
|
|
end
|
|
end
|
|
|
|
---@return UnityEngine.UnityWebRequest
|
|
function DBTextures.getByUrl(url, callback, orgs)
|
|
local tt = db[url]
|
|
if tt then
|
|
tt.lastUseTime = DateEx.nowMS
|
|
db[url] = tt
|
|
Utl.doCallback(callback, tt.texture, orgs)
|
|
return nil
|
|
end
|
|
|
|
local request =
|
|
WWWEx.get(
|
|
url,
|
|
nil,
|
|
CLAssetType.texture,
|
|
function(content)
|
|
db[url] = {texture = content, lastUseTime = DateEx.nowMS}
|
|
Utl.doCallback(callback, content, orgs)
|
|
end,
|
|
nil,
|
|
orgs,
|
|
true,
|
|
1
|
|
)
|
|
return request
|
|
end
|
|
|
|
function DBTextures.upload(path, uploadPath, finishCallback)
|
|
local key = joinStr(path, "_", uploadPath)
|
|
if (not dbUploadStatus[key]) and (not isUploading[key]) then
|
|
local www =
|
|
NetProto.uploadFile(
|
|
path,
|
|
uploadPath,
|
|
function(result, orgs)
|
|
isUploading[key] = nil
|
|
if result then
|
|
dbUploadStatus[key] = result
|
|
end
|
|
Utl.doCallback(finishCallback, result)
|
|
end
|
|
)
|
|
isUploading[key] = www
|
|
return www
|
|
else
|
|
if dbUploadStatus[key] then
|
|
Utl.doCallback(finishCallback, dbUploadStatus[key])
|
|
else
|
|
printe("进到这里来了,有问题!!!!!!!")
|
|
end
|
|
end
|
|
end
|
|
|
|
function DBTextures.cancelUpload(path, uploadPath)
|
|
local key = joinStr(path, "_", uploadPath)
|
|
local www = isUploading[key]
|
|
if www then
|
|
www.Dispose()
|
|
www = nil
|
|
dbUploadStatus[key] = nil
|
|
isUploading[key] = nil
|
|
end
|
|
end
|
|
|
|
return DBTextures
|