HotUpdateGMAssets_launcher.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. local HotUpdateGMAssets = class('HotUpdateGMAssets')
  2. HotUpdateGMAssets.NAME = "HotUpdateGMAssets"
  3. local AssetsManager = requireLauncher( "res_check/assets_manager" )
  4. local Tags = "HotUpdateGMAssets"
  5. function HotUpdateGMAssets:ctor()
  6. self._downloader = nil
  7. self._updateCount = 0
  8. end
  9. function HotUpdateGMAssets:CleanupDownloader()
  10. if self._downloader then
  11. self._downloader:Cleanup()
  12. self._downloader = nil
  13. end
  14. end
  15. function HotUpdateGMAssets:handleAlreadyUpToDate()
  16. if self._downloader and global.L_ModuleManager:GetCurrentModule() and global.L_ModuleManager:GetCurrentModule():GetCurrentSubMod() then
  17. local currentSubMod = global.L_ModuleManager:GetCurrentModule():GetCurrentSubMod()
  18. local version = self._downloader:GetRemoteManifestVer()
  19. currentSubMod:SetRemoteVersion( version )
  20. end
  21. global.L_LoadingManager:CloseLayer()
  22. global.L_LoadingPlayManager:CloseLayer()
  23. self:CleanupDownloader()
  24. global.L_ModuleManager:RunCurrentModule()
  25. end
  26. function HotUpdateGMAssets:CheckUpdate()
  27. if global.L_GameEnvManager:IsReview() then
  28. self:handleAlreadyUpToDate()
  29. return
  30. end
  31. if not global.L_ModuleManager and not global.L_ModuleManager:GetCurrentModule() then
  32. releasePrint("can not find current module!!")
  33. return
  34. end
  35. -- GM Init Res URL
  36. local module = global.L_ModuleManager:GetCurrentModule()
  37. local moduleGameEnv = module:GetGameEnv()
  38. if (not moduleGameEnv:GetGMResURL() or string.len(moduleGameEnv:GetGMResURL()) == 0)
  39. and (not moduleGameEnv:GetGMResURL2() or string.len(moduleGameEnv:GetGMResURL2()) == 0) then
  40. releasePrint("can not find GMResURL !!")
  41. self:handleAlreadyUpToDate()
  42. return
  43. end
  44. if not moduleGameEnv:GetGMResVer() or string.len(moduleGameEnv:GetGMResVer()) == 0 then
  45. releasePrint("can not find GMResVer !!")
  46. self:handleAlreadyUpToDate()
  47. return
  48. end
  49. -- new downloader
  50. local function versionCompare(str1, str2)
  51. if str1 == "999.1.1" then
  52. -- 内网测试版本
  53. return 1
  54. end
  55. if str1 ~= str2 then
  56. return -1
  57. end
  58. return 1
  59. end
  60. local downloader = AssetsManager:new()
  61. downloader:SetEventDelegate(self)
  62. downloader:SetVersionCompareHandle(versionCompare)
  63. local fileUtil = cc.FileUtils:getInstance()
  64. local writablePath = fileUtil:getWritablePath()
  65. local module = global.L_ModuleManager:GetCurrentModule()
  66. local modulePath = module:GetSubModPath()
  67. local moduleGameEnv = module:GetGameEnv()
  68. local storagePath = writablePath .. modulePath
  69. local localManifestName = "gm_assets_project.manifest"
  70. local localVersionName = "gm_assets_version.manifest"
  71. local tempManifestName = "gm_assets_project.manifest.temp"
  72. local tempDirName = "/_gm_assets_temp"
  73. local resUrl = string.format("%s",self._updateCount == 0 and moduleGameEnv:GetGMResURL() or moduleGameEnv:GetGMResURL2())
  74. local gmurlsuffix = moduleGameEnv:GetSelectedServerGmurlsuffix()
  75. if gmurlsuffix and gmurlsuffix ~= "" then
  76. resUrl = resUrl .. gmurlsuffix .. "/"
  77. end
  78. local resVersionOri = moduleGameEnv:GetGMInitResVer()
  79. local manifestUrl = resUrl .. localManifestName
  80. local versionUrl = resUrl .. localVersionName
  81. local jobs = 1
  82. local ignoreLocal = true
  83. local subMod = module:GetCurrentSubMod()
  84. local subModInfo = subMod:GetSubModInfo()
  85. local gameid = subMod:GetOperID()
  86. local xk = subMod:GetXK()
  87. local channelID = global.L_GameEnvManager:GetChannelID() or 1
  88. local sign = GetCdnSign(gameid, global.modListSrvTime, xk or "")
  89. local param = string.format("gameId=%s&time=%s&sign=%s&channelID=%s", gameid, global.modListSrvTime, sign, channelID)
  90. local resVersion = string.format("%s&%s", resVersionOri, param)
  91. -- verbose
  92. releasePrint( "-------------------------------------------------------------" )
  93. releasePrint( "pull gm_assets:" )
  94. releasePrint( manifestUrl )
  95. releasePrint( versionUrl )
  96. releasePrint( resVersion )
  97. releasePrint( "ignore local:" .. tostring(ignoreLocal) )
  98. releasePrint( "local storage path:" .. tostring(storagePath) )
  99. releasePrint( "-------------------------------------------------------------" )
  100. -- request download
  101. local ret = downloader:DownLoadRequest( "gm_assets", localManifestName, storagePath, tempManifestName, tempDirName,
  102. resUrl, manifestUrl, versionUrl, resVersion, jobs, ignoreLocal)
  103. if not ret then
  104. downloader:Cleanup()
  105. releasePrint("GMRES Hotupdate ERR DownLoadRequest failed!!")
  106. self:handleAlreadyUpToDate()
  107. return
  108. end
  109. self._downloader = downloader
  110. local url2 = moduleGameEnv:GetGMResURL2()
  111. if self._updateCount == 0 and url2 and string.len(url2) > 0 then
  112. self._gmResUpdateSchedule = PerformWithDelayGlobal(function ()
  113. self._gmResUpdateSchedule = nil
  114. self._updateCount = self._updateCount + 1
  115. self:CleanupDownloader()
  116. self:CheckUpdate()
  117. end, 10)
  118. else
  119. self._gmResUpdateSchedule = PerformWithDelayGlobal(function ()
  120. self._gmResUpdateSchedule = nil
  121. self:handleAlreadyUpToDate()
  122. end, 10)
  123. end
  124. end
  125. function HotUpdateGMAssets:onLocalManifestError()
  126. self:CleanupDownloaderSchedule()
  127. releasePrint(Tags, "Local Manifest Error!")
  128. local module = global.L_ModuleManager:GetCurrentModule()
  129. local moduleGameEnv = module:GetGameEnv()
  130. local url2 = moduleGameEnv:GetGMResURL2()
  131. if self._updateCount == 0 and url2 and string.len(url2) > 0 then
  132. self:CleanupDownloader()
  133. self._updateCount = self._updateCount + 1
  134. self:CheckUpdate()
  135. else
  136. -- skip
  137. self:handleAlreadyUpToDate()
  138. end
  139. end
  140. function HotUpdateGMAssets:onRemoteManifestError()
  141. self:CleanupDownloaderSchedule()
  142. releasePrint(Tags, "Remote Manifest Error!")
  143. local module = global.L_ModuleManager:GetCurrentModule()
  144. local moduleGameEnv = module:GetGameEnv()
  145. local url2 = moduleGameEnv:GetGMResURL2()
  146. if self._updateCount == 0 and url2 and string.len(url2) > 0 then
  147. self:CleanupDownloader()
  148. self._updateCount = self._updateCount + 1
  149. self:CheckUpdate()
  150. else
  151. -- skip
  152. self:handleAlreadyUpToDate()
  153. end
  154. end
  155. function HotUpdateGMAssets:onCheckVersionInfo()
  156. self:CleanupDownloaderSchedule()
  157. releasePrint(Tags, "onCheckVersionInfo")
  158. end
  159. function HotUpdateGMAssets:onNewVersion(versionSize)
  160. self:CleanupDownloaderSchedule()
  161. self:CleanupTipsSchedule()
  162. local sizeInMB = versionSize / (1024 * 1024)
  163. releasePrint(Tags, "New Version Found:" .. tostring(sizeInMB))
  164. global.L_LoadingManager:CloseLayer()
  165. global.L_LoadingManager:OpenLayer({loadingType=2, isDetails=true})
  166. local scheduler = cc.Director:getInstance():getScheduler()
  167. local handle = nil
  168. handle = scheduler:scheduleScriptFunc(
  169. function()
  170. scheduler:unscheduleScriptEntry(handle)
  171. if self._downloader then
  172. self._downloader:StartUpdate()
  173. end
  174. end,
  175. 1/60,
  176. false
  177. )
  178. local moduleGameEnv = global.L_ModuleManager:GetCurrentModule():GetGameEnv()
  179. local selSerInfo = moduleGameEnv:GetSelectedServer()
  180. local server_id = selSerInfo and selSerInfo.serverId
  181. local server_name = selSerInfo and selSerInfo.serverName
  182. local dataParam = {
  183. event = {
  184. time = os.time(), name = "startLoading2", type = "track"
  185. },
  186. properities = {
  187. servid = tostring(server_id), server_name = tostring(server_name)
  188. }
  189. }
  190. local main_servid = selSerInfo and selSerInfo.mainServerId
  191. local main_server_name = selSerInfo and selSerInfo.mainServerName
  192. dataParam.properities.main_servid = main_servid and tostring(main_servid) or tostring(server_id)
  193. dataParam.properities.main_server_name = main_server_name and tostring(main_server_name) or tostring(server_name)
  194. local userId = global.L_GameEnvManager:GetEnvDataByKey("user_id") or global.L_GameEnvManager:GetEnvDataByKey("uid")
  195. if userId and userId ~= "" then
  196. dataParam.user = {id = userId}
  197. end
  198. global.L_DataRePort:SendRePortData( dataParam )
  199. self._waitTime = 5
  200. local currentModule = global.L_ModuleManager:GetCurrentModule()
  201. if currentModule then
  202. local modGameEnv = currentModule:GetGameEnv()
  203. if modGameEnv then
  204. local time = modGameEnv:GetCustomDataByKey("updateWaitTime")
  205. time = time and tonumber(time)
  206. if time and time > 0 then
  207. self._waitTime = time
  208. end
  209. end
  210. end
  211. self._checkTipsScheduleID = scheduler:scheduleScriptFunc(
  212. function()
  213. self:CleanupTipsSchedule()
  214. self:ShowCloudTipsView()
  215. end,
  216. self._waitTime,
  217. false
  218. )
  219. end
  220. function HotUpdateGMAssets:CleanupTipsSchedule()
  221. if self._checkTipsScheduleID then
  222. local scheduler = cc.Director:getInstance():getScheduler()
  223. scheduler:unscheduleScriptEntry(self._checkTipsScheduleID)
  224. self._checkTipsScheduleID = nil
  225. end
  226. end
  227. function HotUpdateGMAssets:ShowCloudTipsView()
  228. local updateWaitTips = "点击这里,一键体验烈火云手机!"
  229. local followFloatDismiss = false
  230. local currentModule = global.L_ModuleManager:GetCurrentModule()
  231. if currentModule then
  232. local modGameEnv = currentModule:GetGameEnv()
  233. if modGameEnv then
  234. local str = modGameEnv:GetCustomDataByKey("updateWaitTips")
  235. if str and string.len(str) > 0 then
  236. updateWaitTips = str
  237. end
  238. local isFollowFloatDismiss = modGameEnv:GetCustomDataByKey("isFollowFloatDismiss")
  239. followFloatDismiss = isFollowFloatDismiss == 1 or isFollowFloatDismiss == "1"
  240. end
  241. end
  242. local data = {
  243. tipsStr = updateWaitTips,
  244. isFollowFloatDismiss = followFloatDismiss
  245. }
  246. global.L_NativeBridgeManager:GN_showTipsView(data)
  247. end
  248. function HotUpdateGMAssets:onAlreadyUpToDate()
  249. releasePrint(Tags, "Already Up To Date")
  250. self:CleanupDownloaderSchedule()
  251. self:handleAlreadyUpToDate()
  252. end
  253. function HotUpdateGMAssets:onUpdatePercent(percentFile)
  254. self:CleanupDownloaderSchedule()
  255. releasePrint(Tags, "onUpdatePercent", percentFile)
  256. -- global.L_LoadingManager:UpdatePercent(percentFile)
  257. end
  258. function HotUpdateGMAssets:onUpdatePercentSize(percentSize, totalSize)
  259. self:CleanupDownloaderSchedule()
  260. releasePrint(Tags, "onUpdatePercentSize", percentSize)
  261. global.L_LoadingManager:UpdatePercent(percentSize, totalSize)
  262. end
  263. function HotUpdateGMAssets:onAssetError(assetID, msg)
  264. self:CleanupDownloaderSchedule()
  265. self:CleanupTipsSchedule()
  266. releasePrint(Tags, "Asset Error! " .. tostring(assetID))
  267. global.L_SystemTipsManager:ShowTips("ASSET ERROR " .. tostring(assetID))
  268. end
  269. function HotUpdateGMAssets:onDownloadFinished()
  270. releasePrint(Tags, "DownloadFinished!")
  271. self:CleanupDownloaderSchedule()
  272. self:CleanupTipsSchedule()
  273. local moduleGameEnv = global.L_ModuleManager:GetCurrentModule():GetGameEnv()
  274. local selSerInfo = moduleGameEnv:GetSelectedServer()
  275. local server_id = selSerInfo and selSerInfo.serverId
  276. local server_name = selSerInfo and selSerInfo.serverName
  277. local dataParam = {
  278. event = {
  279. time = os.time(), name = "finishLoading2", type = "track"
  280. },
  281. properities = {
  282. servid = tostring(server_id), server_name = tostring(server_name)
  283. }
  284. }
  285. local main_servid = selSerInfo and selSerInfo.mainServerId
  286. local main_server_name = selSerInfo and selSerInfo.mainServerName
  287. dataParam.properities.main_servid = main_servid and tostring(main_servid) or tostring(server_id)
  288. dataParam.properities.main_server_name = main_server_name and tostring(main_server_name) or tostring(server_name)
  289. local userId = global.L_GameEnvManager:GetEnvDataByKey("user_id") or global.L_GameEnvManager:GetEnvDataByKey("uid")
  290. if userId and userId ~= "" then
  291. dataParam.user = {id = userId}
  292. end
  293. global.L_DataRePort:SendRePortData( dataParam )
  294. self:handleAlreadyUpToDate()
  295. end
  296. function HotUpdateGMAssets:onDownloadFailed()
  297. releasePrint(Tags, "DownloadFailed!")
  298. self:CleanupDownloaderSchedule()
  299. self:CleanupTipsSchedule()
  300. if self._downloader then
  301. self._downloader:Retry()
  302. end
  303. end
  304. function HotUpdateGMAssets:CleanupDownloaderSchedule()
  305. if self._gmResUpdateSchedule then
  306. global.Scheduler:unscheduleScriptEntry(self._gmResUpdateSchedule)
  307. self._gmResUpdateSchedule = nil
  308. end
  309. end
  310. return HotUpdateGMAssets