86 lines
2.8 KiB
Plaintext

print(love.filesystem.createDirectory("Cache"))
local multi,thread = require("multi"):init()
GLOBAL,THREAD = require("multi.integration.loveManager"):init()
local queue = multi:newSystemThreadedJobQueue(16)
local LoadImage = queue:newFunction(function(path)
return love.image.newImageData(path)
end)
local DownloadImage = queue:newFunction(function(url,hash)
print = THREAD:getConsole().print
require("love.image")
require("love.filesystem")
require("love.data")
local http = require("socket.http")
local request = require("luajit-request")
function download(path)
if path:find("https") then
return request.send(path).body
elseif path:find("http") then
return http.request(path)
end
end
local data = download(url)
function IsImage(str)
return str:find("\0")~=nil -- Image Data will contain nul character
end
love.filesystem.createDirectory("Cache")
if IsImage(data or "") then
love.filesystem.write("Cache/"..hash..".jpg", data)
return love.image.newImageData("Cache/"..hash..".jpg")
else
return "noimage"
end
end)
local cache = {}
function gui:SetImage(i,inthread,backup)
if not i then return end
if type(i) == "userdata" and i:type() == "Image" then
self.Image=i
self.ImageHeigth=self.Image:getHeight()
self.ImageWidth=self.Image:getWidth()
self.Quad=love.graphics.newQuad(0,0,self.width,self.height,self.ImageWidth,self.ImageHeigth)
elseif type(i)=="string" then
if cache[i] then
self:SetImage(cache[i])
else
if i:match([[https?://]]) then
local hash = love.data.encode("string", "hex",love.data.hash("md5", i))
if love.filesystem.getInfo("Cache/"..hash..".jpg") then
self:SetImage("Cache/"..hash..".jpg")
return
end
DownloadImage(i,hash).connect(function(img)
if img == "noimage" then
print("Need backup")
if backup then
return self:SetImage(backup)
else
return nil
end
end
self.Image = love.graphics.newImage(img)
cache[i]=self.Image
self.ImageHeigth=self.Image:getHeight()
self.ImageWidth=self.Image:getWidth()
self.Quad=love.graphics.newQuad(0,0,self.width,self.height,self.ImageWidth,self.ImageHeigth)
end)
else
if inthread or self.threadable then
func(i).connect(function(img)
self.Image = love.graphics.newImage(img)
cache[i]=self.Image
self.ImageHeigth=self.Image:getHeight()
self.ImageWidth=self.Image:getWidth()
self.Quad=love.graphics.newQuad(0,0,self.width,self.height,self.ImageWidth,self.ImageHeigth)
end)
else
self.Image = love.graphics.newImage(i)
cache[i]=self.Image
self.ImageHeigth=self.Image:getHeight()
self.ImageWidth=self.Image:getWidth()
self.Quad=love.graphics.newQuad(0,0,self.width,self.height,self.ImageWidth,self.ImageHeigth)
end
end
end
end
end