You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
3.7 KiB

  1. -- 子游戏更新
  2. local SubGameUpdater = class("SubGameUpdater")
  3. --[[
  4. gameConfig =
  5. {
  6. version = "x.x.x.x",
  7. newVersion =
  8. {
  9. [1] = "http://21down.dingdingqipai.com/download/bohu/release/review/1.1.4.9/",
  10. [2] = "http://120.76.202.80/download/bohu/release/review/1.1.4.9/",
  11. },
  12. }
  13. percentCallback : 更新进度的回调
  14. finishCallBack : 更新完成的回调
  15. --]]
  16. local writablePath = cc.FileUtils:getInstance():getWritablePath()
  17. function SubGameUpdater:ctor(gameVersionInfo, percentCallback, finishCallBack)
  18. logI("SubGameUpdater:ctor()", table.tostring(gameConfig))
  19. if not gameVersionInfo then
  20. return
  21. end
  22. -- 配置信息
  23. self.gameVersionInfo = gameVersionInfo;
  24. -- 更新进度的回调
  25. self.percentCallback = percentCallback
  26. -- 更新完成的回调
  27. self.finishCallBack = finishCallBack
  28. self.gameId = self.gameVersionInfo.gameId
  29. self.newVersion = self.gameVersionInfo.ver
  30. local tempDir = os.date("%Y%m%d%H%M%S", os.time());
  31. local tempFile = string.format("tempGameFile%d.zip",self.gameId)
  32. self.zipFileDir = writablePath .. tempDir .. '/';
  33. self.zipFilePath = self.zipFileDir .. tempFile;
  34. if not cc.FileUtils:getInstance():isDirectoryExist(self.zipFileDir) then
  35. if not cc.FileUtils:getInstance():createDirectory(self.zipFileDir) then
  36. logD("SubGameUpdater:ctor() createDirectory failed :" .. self.zipFileDir)
  37. end
  38. end
  39. logD("SubGameUpdater:ctor() self.zipFileDir = ", self.zipFileDir)
  40. logD("SubGameUpdater:ctor() self.zipFilePath = ", self.zipFilePath)
  41. end
  42. -- 更新回调,替换之前的
  43. function SubGameUpdater:updateCallback(percentCallback, finishCallBack)
  44. -- 更新进度的回调
  45. self.percentCallback = percentCallback
  46. -- 更新完成的回调
  47. self.finishCallBack = finishCallBack
  48. end
  49. -- 开始更新
  50. function SubGameUpdater:start()
  51. logD("SubGameUpdater:start()")
  52. local function onState(state,msg)
  53. logD("SubGameUpdater:start() onState()", state, msg)
  54. if state=="progress" then
  55. local percent = msg*100
  56. -- 回调更新进度
  57. self:onUpdatePercent(percent)
  58. elseif state=="successed" then
  59. local percent = 100
  60. -- 回调更新进度
  61. self:onUpdatePercent(percent)
  62. runDelayWithTime(function()
  63. self:onFinishDownZip()
  64. end,0.01)
  65. end
  66. end
  67. downloadFileByUrls2(self.gameVersionInfo.url, onState, self.zipFilePath)
  68. end
  69. function SubGameUpdater:onFinishDownZip()
  70. logD("SubGameUpdater:onFinishDownZip()")
  71. local md5=cc.FileUtils:getInstance():md5File(self.zipFilePath)
  72. if md5~=self.gameVersionInfo.md5 then
  73. os.remove(self.zipFilePath)
  74. logE("md5 不对!"..md5.."!="..self.gameVersionInfo.md5)
  75. showTooltip("文件数据不正确!请尝试重新下载!")
  76. -- self:dispatchEvent("error",PLN.FILE_CHECKSUM_FAILED)
  77. return
  78. end
  79. -- self:dispatchEvent("zip",{text=PLN.UPDATING_RES,percent=1})
  80. local isZipSuccessed=cc.FileUtils:getInstance():decompress(self.zipFilePath)
  81. os.remove(self.zipFilePath)
  82. if isZipSuccessed then
  83. local move_result = move_dir(self.zipFileDir, writablePath);
  84. if move_result then
  85. remove_dir(self.zipFileDir)
  86. self:onUpdateFinish()
  87. else
  88. logD("SubGameUpdater:onFinishDownZip() move_dir failed");
  89. end
  90. else
  91. logD("SubGameUpdater:onFinishDownZip() unzip failed");
  92. end
  93. end
  94. -- 更新进度
  95. function SubGameUpdater:onUpdatePercent (percent)
  96. if self.percentCallback then
  97. self.percentCallback(percent)
  98. end
  99. end
  100. -- 更新完成
  101. function SubGameUpdater:onUpdateFinish(jsonFileData)
  102. logD("SubGameUpdater:onUpdateFinish()", self.gameId, self.newVersion)
  103. -- 保存最新的update.json文件
  104. -- logD("保存json:"..jsonFileData)
  105. -- saveStringToFile(jsonFileData, self.clientUpdateFile);
  106. --清除更新数据
  107. self.gameVersionInfo.url=""
  108. -- 通知回调
  109. if self.finishCallBack then
  110. self.finishCallBack(self.newVersion)
  111. end
  112. end
  113. return SubGameUpdater