问题:我们知道每次在google play和appstore上提审包总会提升版本号,然后就提醒玩家游戏需要升级,这时候有些玩家会出现黑屏,登录不了等等比较严重的问题,主动清理缓存数据就会好,但是好多初级玩家并不知道,这样会造成比较不好的印象。
原因分析:就我所在项目而言,用的是quick3.3,我们在游戏发布后做了多次热更新,热更新是在缓存目录的,覆盖安装并没有删除该目录内容。然后安装之后进入游戏,读取的还是缓存目录的旧资源或者代码;如果新代码加了登录协议或者是登录流程,在就代码中没有,就会出现登录卡在旧代码相对于新代码的缺失部分。如果是资源,在类似情况下就会出现资源加载不出来或者加载就资源的情况。
问题的处理:1、提示玩家主动清理缓存,或者卸载重装(好像不太友好的样子,但也是方案之一哈)
2、每次提审完了后立马做一次更新(这样旧包覆盖安装之后下载最新文件到缓存目录,然后用缓存目录里面的新文件,这种我还没试过哈,因为的确比较愚蠢的做法)
3、重点讨论第三种方案,就是覆盖安装前检查是不是覆盖安装(对比main中版本号和记录的是不是一样,不一样就清除缓存,清理了缓存记得把版本号写进本地文件,下次覆盖安装再读取),这些处理我是放在main中(比较冒险哈,毕竟有问题只能重新提审新包),因为放在后面的文件容易被热更新,那样的话就不能清理正在执行的文件(至少清了会有问题吧)。安卓亲测有效。代码如下:
----删除函数------ function removeDir(path) require("lfs") local mode = lfs.attributes(path, "mode") if mode and mode == "directory" then local function _rmdir(path) local iter, dir_obj = lfs.dir(path) local i = 0 while i < 20 do --防止死循环的问题 ,递归不让超过20层 i = i + 1 local dir = iter(dir_obj) if dir == nil then break end if dir ~= "." and dir ~= ".." then local curDir = path .. "/" .. dir local mode = lfs.attributes(curDir, "mode") if mode == "directory" then _rmdir(curDir.."/") elseif mode == "file" then local oFile = io.open(curDir, "w+b") if oFile then if oFile:write(GAME_BASE_VERSION) == nil then return false else print("文件写入成功1") end io.close(oFile) end os.remove(curDir) end end end return lfs.rmdir(path) end _rmdir(path) end end local function writeVersion(sPath) local sPath = cc.FileUtils:getInstance():getWritablePath() .. CACHE_VERSION local oFile = io.open(sPath, "w+b") if oFile then if oFile:write(GAME_BASE_VERSION) == nil then print("版本文件写入失败") else print("文件写入成功") end io.close(oFile) end end local function checkCachFile() --清理缓存控制 local sPath = cc.FileUtils:getInstance():getWritablePath() .. CACHE_VERSION local oFile = io.open(sPath, "rb") if oFile then local sContent = oFile:read("*a") if sContent and string.len(sContent) > 0 and sContent ~= GAME_BASE_VERSION then local cacheDir = cc.FileUtils:getInstance():getWritablePath() .. "cache/" removeDir(cacheDir .. "res") removeDir(cacheDir .. "src") else print("版本相同不清除", GAME_BASE_VERSION) end io.close(oFile) else local cacheDir = cc.FileUtils:getInstance():getWritablePath() .. "cache/" removeDir(cacheDir .. "res") removeDir(cacheDir .. "src") end end先检查(检查删除),之后写文件写版本号。然后在下一步。