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