-- 子游戏更新 local SubGameUpdater = class("SubGameUpdater") --[[ gameConfig = { version = "x.x.x.x", newVersion = { [1] = "http://21down.dingdingqipai.com/download/bohu/release/review/1.1.4.9/", [2] = "http://120.76.202.80/download/bohu/release/review/1.1.4.9/", }, } percentCallback : 更新进度的回调 finishCallBack : 更新完成的回调 --]] local writablePath = cc.FileUtils:getInstance():getWritablePath() function SubGameUpdater:ctor(gameVersionInfo, percentCallback, finishCallBack) logI("SubGameUpdater:ctor()", table.tostring(gameConfig)) if not gameVersionInfo then return end -- 配置信息 self.gameVersionInfo = gameVersionInfo; -- 更新进度的回调 self.percentCallback = percentCallback -- 更新完成的回调 self.finishCallBack = finishCallBack self.gameId = self.gameVersionInfo.gameId self.newVersion = self.gameVersionInfo.ver local tempDir = os.date("%Y%m%d%H%M%S", os.time()); local tempFile = string.format("tempGameFile%d.zip",self.gameId) self.zipFileDir = writablePath .. tempDir .. '/'; self.zipFilePath = self.zipFileDir .. tempFile; if not cc.FileUtils:getInstance():isDirectoryExist(self.zipFileDir) then if not cc.FileUtils:getInstance():createDirectory(self.zipFileDir) then logD("SubGameUpdater:ctor() createDirectory failed :" .. self.zipFileDir) end end logD("SubGameUpdater:ctor() self.zipFileDir = ", self.zipFileDir) logD("SubGameUpdater:ctor() self.zipFilePath = ", self.zipFilePath) end -- 更新回调,替换之前的 function SubGameUpdater:updateCallback(percentCallback, finishCallBack) -- 更新进度的回调 self.percentCallback = percentCallback -- 更新完成的回调 self.finishCallBack = finishCallBack end -- 开始更新 function SubGameUpdater:start() logD("SubGameUpdater:start()") local function onState(state,msg) logD("SubGameUpdater:start() onState()", state, msg) if state=="progress" then local percent = msg*100 -- 回调更新进度 self:onUpdatePercent(percent) elseif state=="successed" then local percent = 100 -- 回调更新进度 self:onUpdatePercent(percent) runDelayWithTime(function() self:onFinishDownZip() end,0.01) end end downloadFileByUrls2(self.gameVersionInfo.url, onState, self.zipFilePath) end function SubGameUpdater:onFinishDownZip() logD("SubGameUpdater:onFinishDownZip()") local md5=cc.FileUtils:getInstance():md5File(self.zipFilePath) if md5~=self.gameVersionInfo.md5 then os.remove(self.zipFilePath) logE("md5 不对!"..md5.."!="..self.gameVersionInfo.md5) showTooltip("文件数据不正确!请尝试重新下载!") -- self:dispatchEvent("error",PLN.FILE_CHECKSUM_FAILED) return end -- self:dispatchEvent("zip",{text=PLN.UPDATING_RES,percent=1}) local isZipSuccessed=cc.FileUtils:getInstance():decompress(self.zipFilePath) os.remove(self.zipFilePath) if isZipSuccessed then local move_result = move_dir(self.zipFileDir, writablePath); if move_result then remove_dir(self.zipFileDir) self:onUpdateFinish() else logD("SubGameUpdater:onFinishDownZip() move_dir failed"); end else logD("SubGameUpdater:onFinishDownZip() unzip failed"); end end -- 更新进度 function SubGameUpdater:onUpdatePercent (percent) if self.percentCallback then self.percentCallback(percent) end end -- 更新完成 function SubGameUpdater:onUpdateFinish(jsonFileData) logD("SubGameUpdater:onUpdateFinish()", self.gameId, self.newVersion) -- 保存最新的update.json文件 -- logD("保存json:"..jsonFileData) -- saveStringToFile(jsonFileData, self.clientUpdateFile); --清除更新数据 self.gameVersionInfo.url="" -- 通知回调 if self.finishCallBack then self.finishCallBack(self.newVersion) end end return SubGameUpdater