升级users模块并修复问题,可以加载用户头像了

This commit is contained in:
MrZ626
2021-04-19 21:39:44 +08:00
parent 546d3f9b18
commit ae1dd6dfaa

View File

@@ -1,31 +1,40 @@
local loadImage=love.graphics.newImage local gc,fs=love.graphics,love.filesystem
local fs=love.filesystem local function loadAvatar(path)
local img=gc.newImage(path)
local texture_noImage=DOGC{32,32, local canvas=gc.newCanvas(128,128)
{"setCL",0,0,0}, gc.push()
{"fRect",0,0,32,32}, gc.origin()
{"setCL",1,1,1}, gc.setColor(1,1,1)
{"setLW",3}, gc.setCanvas(canvas)
{"dLine",0,0,31,31}, mDraw(img,64,64,nil,128/math.max(img:getWidth(),img:getHeight()))
{"dLine",0,31,31,0}, gc.setCanvas()
} gc.pop()
return canvas
local function _getEmptyUser()
return{
username="[X]",
motto="Techmino haowan",
hash="",
new=false,
}
end end
local texture_noImage=DOGC{120,120,
{"setCL",0,0,0},
{"fRect",0,0,120,120},
{"setCL",1,1,1},
{"setLW",6},
{"dLine",9,9,110,110},
{"dLine",9,110,110,9},
}
local db_img={} local db_img={}
local db=setmetatable({},{__index=function(self,k) local db=setmetatable({},{__index=function(self,k)
local file="cache/user"..k..".dat" local file="cache/user"..k..".dat"
local d=fs.getInfo(file)and JSON.decode(fs.read(file))or _getEmptyUser() local d=
fs.getInfo(file)and JSON.decode(fs.read(file))or
{
username="[X]",
motto="Techmino haowan",
hash="",
new=false,
}
rawset(self,k,d) rawset(self,k,d)
if type(d.hash)=="string"and #d.hash>0 and fs.getInfo(d.hash)then if type(d.hash)=="string"and #d.hash>0 and fs.getInfo("cache/"..d.hash)then
db_img[k].avatar=loadImage(d.hash) db_img[k]=loadAvatar("cache/"..d.hash)
end end
return d return d
end}) end})
@@ -39,12 +48,12 @@ function USERS.updateUserData(data)
fs.write("cache/user"..uid..".dat",JSON.encode{ fs.write("cache/user"..uid..".dat",JSON.encode{
username=data.username, username=data.username,
motto=data.motto, motto=data.motto,
hash=data.hash, hash=data.hash or db[uid].hash,
}) })
if data.avatar then if data.avatar then
fs.write("cache/"..data.hash,data.avatar:sub(data.avatar:find","+1)) fs.write("cache/"..data.hash,love.data.decode("string","base64",data.avatar:sub(data.avatar:find","+1)))
db_img[uid].avatar=loadImage("cache/"..data.hash) db_img[uid]=loadAvatar("cache/"..data.hash)
db[uid].hash=type(data.hash)=="string"and data.hash>0 and data.hash db[uid].hash=type(data.hash)=="string"and #data.hash>0 and data.hash
db[uid].new=true db[uid].new=true
end end
end end