ModuleManager_launcher.lua 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. local ModuleManager = class("ModuleManager")
  2. local AssetsManager = requireLauncher( "res_check/assets_manager" )
  3. local Module = requireLauncher("logic/Module")
  4. local cjson = require("cjson")
  5. local Tags = "ModulePuller"
  6. local NotAlphaFileName = "not_alpha"
  7. function ModuleManager:ctor()
  8. self._currentModule = nil
  9. self._launcherModule = Module.new( "mod_launcher", "launcher", global.VersionName.default )
  10. self:CheckModuleVersion(self._launcherModule, true)
  11. self._downloader = nil
  12. self._isVersionChanged = false
  13. self._referModuleID = nil
  14. self._modules = {}
  15. self._updateCount = 0
  16. self._imagePath = {}
  17. end
  18. function ModuleManager:SetReferModuleID( id )
  19. self._referModuleID = id
  20. end
  21. function ModuleManager:GetReferModuleID()
  22. return self._referModuleID
  23. end
  24. function ModuleManager:InitModules( configs )
  25. for idx, cfg in ipairs( configs ) do
  26. self._modules[idx] = Module.new( cfg.id, cfg.name, global.VersionName.default, cfg.version_suffix, cfg, cfg.subMod, cfg.severlistSign, cfg.refer )
  27. end
  28. end
  29. function ModuleManager:GetAllModules()
  30. return self._modules
  31. end
  32. function ModuleManager:GetModuleByID(id)
  33. for _, module in pairs(self._modules) do
  34. if module:GetID() == id then
  35. return module
  36. end
  37. end
  38. return nil
  39. end
  40. function ModuleManager:InitImagePre( configs )
  41. self._imagePath = configs
  42. end
  43. function ModuleManager:GetImagePre()
  44. return self._imagePath
  45. end
  46. function ModuleManager:RunCurrentModule()
  47. global.L_LoadingBarManager:CloseLayer()
  48. global.L_LoadingManager:CloseLayer()
  49. global.L_SystemTipsManager:CloseLayer()
  50. global.L_ModuleChooseManager:CloseLayer()
  51. global.L_GameLayerManager:Close()
  52. local main = "main.lua"
  53. -- clean up last require cache path
  54. if package.loaded[main] then
  55. package.loaded[main] = nil
  56. end
  57. cc.FileUtils:getInstance():purgeCachedEntries()
  58. require( main )
  59. end
  60. function ModuleManager:RegisterModuleByID( id )
  61. local registerModule = self:GetModuleByID(id)
  62. if not registerModule then
  63. releasePrint( "module config not existing", id )
  64. return false
  65. end
  66. self:RegisterModule( registerModule )
  67. return true
  68. end
  69. function ModuleManager:RegisterModule( module )
  70. if not module or self._currentModule == module then
  71. return false
  72. end
  73. -- check subMod
  74. if not module:GetCurrentSubMod() then
  75. return false
  76. end
  77. -- set to current module
  78. self._currentModule = module
  79. -- check module version state
  80. self:CheckModuleVersion( module )
  81. --
  82. self:addSubModSearchOrder(module)
  83. -- dev search path
  84. self:addDevSearchPath()
  85. local moduleID = module:GetID()
  86. local version = module:GetVersion()
  87. local suffix = module:GetSuffix()
  88. releasePrint( string.format("register module:[%s,%s,%s]", moduleID, version, module:GetSubModPath()) )
  89. local searchPaths = global.FileUtilCtl:getSearchPaths()
  90. dump(searchPaths, "_______________SearchPaths")
  91. UserData:setVersionPath( module:GetSubModPath() )
  92. return true
  93. end
  94. local function checkAndAddPath(srcPaths, addPath)
  95. for k, v in pairs(srcPaths) do
  96. if v == addPath then
  97. table.remove(srcPaths, k)
  98. break
  99. end
  100. end
  101. table.insert(srcPaths, 1, addPath)
  102. return srcPaths
  103. end
  104. -- 添加模块 搜索路径
  105. function ModuleManager:addModuleSearchPath( module )
  106. if not module then
  107. return false
  108. end
  109. -- mod_fgcq/submod cache
  110. -- mod_fgcq/submod root
  111. -- [mod_fgcq/alpha cache]
  112. -- [mod_fgcq/alpha root]
  113. -- mod_fgcq/stab cache
  114. -- mod_fgcq/stab root
  115. -- [mod_launcher/alpha cache]
  116. -- [mod_launcher/alpha root]
  117. -- mod_launcher/stab cache
  118. -- mod_launcher/stab root
  119. local modulePath = global.FileUtilCtl:getDefaultResourceRootPath() .. getNewFoldername(tostring(module:GetID())) .. "/"
  120. local cachePath = global.FileUtilCtl:getWritablePath() .. getNewFoldername(tostring(module:GetID())) .. "/"
  121. releasePrint( "add search path:", modulePath )
  122. releasePrint( "add search path:", cachePath )
  123. local searchPaths = global.FileUtilCtl:getSearchPaths()
  124. checkAndAddPath(searchPaths, modulePath)
  125. checkAndAddPath(searchPaths, cachePath)
  126. global.FileUtilCtl:setSearchPaths(searchPaths)
  127. return true
  128. end
  129. -- 添加开发 搜索路径
  130. function ModuleManager:addDevSearchPath()
  131. if not global.isWindows then
  132. return false
  133. end
  134. if not global.isDebugMode and not global.isGMMode then
  135. return false
  136. end
  137. local devPath = global.FileUtilCtl:getDefaultResourceRootPath() .. "dev"
  138. local searchPaths = global.FileUtilCtl:getSearchPaths()
  139. checkAndAddPath(searchPaths, devPath)
  140. global.FileUtilCtl:setSearchPaths(searchPaths)
  141. if not global.FileUtilCtl:isDirectoryExist(devPath) then
  142. global.FileUtilCtl:createDirectory(devPath)
  143. end
  144. return true
  145. end
  146. -- 添加模块 子版本 搜索策略
  147. function ModuleManager:addModuleSearchOrder( module )
  148. if not module then
  149. return false
  150. end
  151. local moduleWritePath = global.FileUtilCtl:getWritablePath() .. module:GetPath()
  152. if not global.FileUtilCtl:isDirectoryExist( moduleWritePath ) then
  153. releasePrint( "create dir:", moduleWritePath )
  154. global.FileUtilCtl:createDirectory( moduleWritePath )
  155. end
  156. local versionOrder = module:GetVersion()
  157. if versionOrder and string.len( versionOrder ) > 0 then
  158. local baseVersionOrder = getNewFoldername(tostring(versionOrder))
  159. local newVersionOrder = module:GetSuffix() and string.format( "%s/%s/", baseVersionOrder, tostring(module:GetSuffix()) ) or baseVersionOrder .. "/"
  160. local modulePath = global.FileUtilCtl:getDefaultResourceRootPath() .. getNewFoldername(tostring(module:GetID())) .. "/" .. newVersionOrder
  161. local cachePath = global.FileUtilCtl:getWritablePath() .. getNewFoldername(tostring(module:GetID())) .. "/" .. newVersionOrder
  162. releasePrint( "add search path:", modulePath )
  163. releasePrint( "add search path:", cachePath )
  164. local searchPaths = global.FileUtilCtl:getSearchPaths()
  165. checkAndAddPath(searchPaths, modulePath)
  166. checkAndAddPath(searchPaths, cachePath)
  167. global.FileUtilCtl:setSearchPaths(searchPaths)
  168. end
  169. return true
  170. end
  171. -- 添加子模块 自定义文件夹搜索策略
  172. function ModuleManager:addSubModSearchOrder(module)
  173. if not module then
  174. return false
  175. end
  176. local subMod = module:GetCurrentSubMod()
  177. if not subMod then
  178. return false
  179. end
  180. -- check & create directory
  181. local writePath = global.FileUtilCtl:getWritablePath() .. module:GetSubModPath()
  182. if not global.FileUtilCtl:isDirectoryExist( writePath ) then
  183. releasePrint( "create dir:", writePath )
  184. global.FileUtilCtl:createDirectory( writePath )
  185. end
  186. --
  187. local subModPath = getNewFoldername(subMod:GetID()) .. "/"
  188. local modulePath = global.FileUtilCtl:getDefaultResourceRootPath() .. getNewFoldername(tostring(module:GetID())) .. "/" .. subModPath
  189. local cachePath = global.FileUtilCtl:getWritablePath() .. getNewFoldername(tostring(module:GetID())) .. "/" .. subModPath
  190. releasePrint( "add search path:", modulePath )
  191. releasePrint( "add search path:", cachePath )
  192. local searchPaths = global.FileUtilCtl:getSearchPaths()
  193. checkAndAddPath(searchPaths, modulePath)
  194. checkAndAddPath(searchPaths, cachePath)
  195. global.FileUtilCtl:setSearchPaths(searchPaths)
  196. -- gm res key
  197. if subMod:GetXK() and subMod:GetXK() ~= nil and LuaBridgeCtl:Inst().SetXXTEAKeyGM then
  198. LuaBridgeCtl:Inst():SetXXTEAKeyGM(subMod:GetXK())
  199. end
  200. end
  201. -- 存储模块子版本状态
  202. function ModuleManager:storeModuleVersionState( module, stateName )
  203. stateName = stateName or module:GetVersion()
  204. local stateFilePath = string.format( "%s%s/state_%s", global.FileUtilCtl:getWritablePath(), getNewFoldername(module:GetID()), stateName )
  205. releasePrint( "store module state:", stateName, stateFilePath )
  206. global.FileUtilCtl:writeStringToFile( stateName, stateFilePath )
  207. end
  208. -- 清理模块子版本状态
  209. function ModuleManager:cleanModuleVersionState( module, stateName )
  210. stateName = stateName or module:GetVersion()
  211. local stateFilePath = string.format( "%s%s/state_%s", global.FileUtilCtl:getWritablePath(), getNewFoldername(module:GetID()), stateName )
  212. if global.FileUtilCtl:isFileExist( stateFilePath ) then
  213. global.FileUtilCtl:removeFile( stateFilePath )
  214. end
  215. end
  216. -- 模块子版本状态是否存在
  217. function ModuleManager:isModuleVersionStateExist( module, stateName )
  218. stateName = stateName or module:GetVersion()
  219. local stateFilePath = string.format( "%s%s/state_%s", global.FileUtilCtl:getWritablePath(), getNewFoldername(module:GetID()), stateName )
  220. return global.FileUtilCtl:isFileExist( stateFilePath )
  221. end
  222. -- 检测各个版本状态,并切换
  223. function ModuleManager:CheckModuleVersion( module, isLauncher )
  224. -- search path
  225. self:addModuleSearchPath( module )
  226. -- search order
  227. ---------------------- stab ----------------------
  228. self:SetModule2StabVersion( module, true )
  229. ---------------------- stab ----------------------
  230. ---------------------- alpha ----------------------
  231. local imgPath = "%s/%s/res/private/login/open_door/00.png"
  232. if isLauncher then
  233. imgPath = "%s/%s/res_launcher/private/login/open_door/00.png"
  234. end
  235. local alphaPath = string.format( imgPath, getNewFoldername(module:GetID()), getNewFoldername(global.VersionName.alpha) )
  236. local alphaExist = global.FileUtilCtl:isFileExist( alphaPath )
  237. releasePrint( "alpha exist?:", alphaPath, module:GetID(), alphaExist )
  238. if alphaExist then
  239. local launcherMod = self:GetLauncherModule()
  240. if launcherMod and not self:isModuleVersionStateExist( launcherMod, NotAlphaFileName ) then
  241. self:SetModule2AlphaVersion( module )
  242. -- reset to stab, only for search alpha. write to stab
  243. module:SetVersion( global.VersionName.stab )
  244. end
  245. end
  246. ---------------------- alpha ----------------------
  247. ---------------------- beta ----------------------
  248. if self:isModuleVersionStateExist( module, global.VersionName.beta ) then
  249. self:SetModule2BetaVersion( module )
  250. end
  251. ---------------------- beta ----------------------
  252. end
  253. -- 切到stab. 清理beta
  254. function ModuleManager:SetModule2StabVersion( module, cleanup )
  255. if not module then
  256. return
  257. end
  258. local version = global.VersionName.stab
  259. module:SetVersion( version )
  260. self:addModuleSearchOrder( module )
  261. -- clean beta state
  262. if cleanup then
  263. self:cleanModuleVersionState( module, global.VersionName.beta )
  264. end
  265. g_isBeta = false
  266. end
  267. -- 切到beta.
  268. function ModuleManager:SetModule2BetaVersion( module )
  269. if not module then
  270. return
  271. end
  272. local version = global.VersionName.beta
  273. module:SetVersion( version )
  274. self:addModuleSearchOrder( module )
  275. -- store beta state
  276. self:storeModuleVersionState( module )
  277. g_isBeta = true
  278. end
  279. -- 切到alpha
  280. function ModuleManager:SetModule2AlphaVersion( module )
  281. if not module then
  282. return
  283. end
  284. local version = global.VersionName.alpha
  285. module:SetVersion( version )
  286. self:addModuleSearchOrder( module )
  287. releasePrint( "alpha flag:in alpha" )
  288. g_isAlpha = true
  289. end
  290. function ModuleManager:IsCurrentModuleAlphaStateExist()
  291. local module = self._currentModule or self._launcherModule
  292. if not module then
  293. return false
  294. end
  295. return self:isModuleVersionStateExist( module, NotAlphaFileName )
  296. end
  297. function ModuleManager:CleanCurrentModuleAlphaState()
  298. local module = self._currentModule or self._launcherModule
  299. if not module then
  300. return
  301. end
  302. return self:cleanModuleVersionState( module, NotAlphaFileName )
  303. end
  304. function ModuleManager:StoreCurrentModuleAlphaState()
  305. local module = self._currentModule or self._launcherModule
  306. if not module then
  307. return
  308. end
  309. self:storeModuleVersionState( module, NotAlphaFileName )
  310. end
  311. function ModuleManager:GetCurrentModule()
  312. return self._currentModule
  313. end
  314. function ModuleManager:GetCurrentModuleID()
  315. if not self._currentModule then
  316. return ""
  317. end
  318. return self._currentModule:GetID()
  319. end
  320. function ModuleManager:GetCurrentModuleVersion()
  321. if not self._currentModule then
  322. return global.VersionName.default
  323. end
  324. return self._currentModule:GetVersion()
  325. end
  326. function ModuleManager:GetCurrentModulePath()
  327. if not self._currentModule then
  328. return ""
  329. end
  330. return self._currentModule:GetPath()
  331. end
  332. function ModuleManager:GetLauncherModule()
  333. return self._launcherModule
  334. end
  335. function ModuleManager:GetLauncherModuleID()
  336. if not self._launcherModule then
  337. return ""
  338. end
  339. return self._launcherModule:GetID()
  340. end
  341. function ModuleManager:GetLauncherModuleVersion()
  342. if not self._launcherModule then
  343. return global.VersionName.default
  344. end
  345. return self._launcherModule:GetVersion()
  346. end
  347. function ModuleManager:GetLauncherModulePath()
  348. if not self._launcherModule then
  349. return ""
  350. end
  351. return self._launcherModule:GetPath()
  352. end
  353. function ModuleManager:GetAllGameID()
  354. local items = {}
  355. for _, module in pairs(self._modules) do
  356. local subMods = module:GetSubMods()
  357. for _, subMod in ipairs(subMods) do
  358. table.insert(items, subMod:GetOperID())
  359. end
  360. end
  361. return items
  362. end
  363. ---------------------------------- hot update ------------------------------------
  364. function ModuleManager:ReportError(str, callback)
  365. local tipsData = {
  366. btnType = 1,
  367. str = str,
  368. callback = callback
  369. }
  370. global.L_CommonTipsManager:OpenLayer( tipsData )
  371. end
  372. function ModuleManager:CleanupDownloader()
  373. if self._downloader then
  374. self._downloader:Cleanup()
  375. self._downloader = nil
  376. end
  377. end
  378. function ModuleManager:handleUpdateFinished()
  379. -- don't need restart
  380. self:handleAlreadyUpToDate()
  381. end
  382. function ModuleManager:handleAlreadyUpToDate()
  383. -- record remote manifest ver
  384. if self._downloader and self:GetCurrentModule() then
  385. local version = self._downloader:GetRemoteManifestVer()
  386. self:GetCurrentModule():SetRemoteVersion( version )
  387. end
  388. self:CleanupDownloader()
  389. global.L_HotUpdateGMAssets:CheckUpdate()
  390. end
  391. function ModuleManager:CheckModuleLocalStorage()
  392. if global.L_GameEnvManager:IsReview() then
  393. self:handleAlreadyUpToDate()
  394. return false
  395. end
  396. if self._downloader then
  397. releasePrint( "pulling module!!" )
  398. return false
  399. end
  400. local module = self:GetCurrentModule()
  401. if not module then
  402. releasePrint( "can not find current module!!" )
  403. return false
  404. end
  405. -- check version, stab or beta?
  406. self._isVersionChanged = self:checkVersion()
  407. -- check local storage is existing
  408. local moduleGameEnv = module:GetGameEnv()
  409. local modulePath = module:GetPath()
  410. local moduleID = module:GetID()
  411. local localManifestName = module:GetManifestFileName()
  412. local isExist = global.FileUtilCtl:isFileExist( localManifestName )
  413. -- check resurl, it will be break on mobile.
  414. if (not moduleGameEnv:GetResDownloadUrl() or string.len(moduleGameEnv:GetResDownloadUrl()) == 0)
  415. and (not moduleGameEnv:GetResDownloadUrl2() or string.len(moduleGameEnv:GetResDownloadUrl2()) == 0) then
  416. self:ReportError("远程资源路径未发现!", function()
  417. global.L_NativeBridgeManager:GN_accountLogout()
  418. global.L_GameEnvManager:RestartGame()
  419. end)
  420. return false
  421. end
  422. local function versionCompare(str1, str2)
  423. if str1 == "999.1.1" then
  424. -- 内网测试版本
  425. return 1
  426. end
  427. if str1 ~= str2 then
  428. return -1
  429. end
  430. return 1
  431. end
  432. local downloader = AssetsManager:new()
  433. downloader:SetEventDelegate( self )
  434. downloader:SetVersionCompareHandle(versionCompare)
  435. local localVersionName = module:GetVersionFileName()
  436. local localManifestName = module:GetManifestFileName()
  437. local serverChannel = moduleGameEnv:GetSelectedServerUrlSuffix()
  438. local storagePath = cc.FileUtils:getInstance():getWritablePath() .. modulePath
  439. local tempManifestName = "project.manifest.temp"
  440. local tempDirName = "/_temp"
  441. local rootDomain = self._updateCount == 0 and moduleGameEnv:GetResDownloadUrl() or moduleGameEnv:GetResDownloadUrl2()
  442. local resUrl = string.format("%s%s", rootDomain, tostring(module:GetRemotePath(serverChannel)))
  443. local manifestUrl = resUrl .. localManifestName
  444. local versionUrl = resUrl .. localVersionName
  445. local resVersion = moduleGameEnv:GetResVersion()
  446. local jobs = 2
  447. local ignoreLocal = not isExist
  448. -- verbose
  449. releasePrint( "-------------------------------------------------------------" )
  450. releasePrint( "pull module:", moduleID )
  451. releasePrint( manifestUrl )
  452. releasePrint( versionUrl )
  453. releasePrint( resVersion )
  454. releasePrint( "ignore local:" .. tostring(ignoreLocal) )
  455. releasePrint( "local storage path:" .. tostring(storagePath) )
  456. releasePrint( "-------------------------------------------------------------" )
  457. -- request download
  458. local ret = downloader:DownLoadRequest( "module_pull", localManifestName, storagePath, tempManifestName, tempDirName,
  459. resUrl, manifestUrl, versionUrl, resVersion, jobs, ignoreLocal )
  460. if not ret then
  461. downloader:Cleanup()
  462. return false
  463. end
  464. self._downloader = downloader
  465. local url2 = moduleGameEnv:GetResDownloadUrl2()
  466. if self._updateCount == 0 and url2 and string.len(url2) > 0 then
  467. self._moduleUpdateSchedule = PerformWithDelayGlobal(function ()
  468. self._moduleUpdateSchedule = nil
  469. self._updateCount = self._updateCount + 1
  470. self:CleanupDownloader()
  471. self:CheckModuleLocalStorage()
  472. end
  473. , 10)
  474. else
  475. self._moduleUpdateSchedule = PerformWithDelayGlobal(function ()
  476. self._moduleUpdateSchedule = nil
  477. -- 触发重新下载
  478. local module = self:GetCurrentModule()
  479. local moduleID = self:GetCurrentModuleID()
  480. local tips = "?"
  481. local logs = "?"
  482. if module then
  483. tips = string.format("%s(%s)", module:GetVersion(), module:GetGameEnv():GetSelectedServerUrlSuffix())
  484. logs = module:GetID() .. ":" .. module:GetGameEnv():GetResDownloadUrl()
  485. end
  486. local errorString = string.format("[%s][%s]远程清单访问超时!\n点击下方\"确认\"重试", tostring(moduleID), tostring(tips))
  487. RecordBuglyLogIm( Tags, logs )
  488. ReportBuglyError( Tags, errorString )
  489. local function callback()
  490. self._updateCount = 0
  491. self:CleanupDownloader()
  492. self:CheckModuleLocalStorage()
  493. end
  494. self:ReportError(errorString, callback)
  495. end
  496. , 10)
  497. end
  498. return true
  499. end
  500. function ModuleManager:onLocalManifestError()
  501. self:CleanupDownloaderSchedule()
  502. self:CleanupDownloader()
  503. local module = self:GetCurrentModule()
  504. local moduleGameEnv = module:GetGameEnv()
  505. local url2 = moduleGameEnv:GetResDownloadUrl2()
  506. if self._updateCount == 0 and url2 and string.len(url2) > 0 then
  507. self._updateCount = self._updateCount + 1
  508. self:CheckModuleLocalStorage()
  509. else
  510. local module = self:GetCurrentModule()
  511. local moduleID = self:GetCurrentModuleID()
  512. local tips = "?"
  513. local logs = "?"
  514. if module then
  515. tips = module:GetID()
  516. logs = module:GetID() .. ":" .. module:GetGameEnv():GetResDownloadUrl()
  517. end
  518. local errorString = string.format("[%s][%s]本地清单异常!\n点击下方\"确认\"重试", tostring(moduleID), tostring(tips))
  519. RecordBuglyLogIm( Tags, logs )
  520. ReportBuglyError( Tags, errorString )
  521. local function callback()
  522. self._updateCount = 0
  523. self:CheckModuleLocalStorage()
  524. end
  525. self:ReportError(errorString, callback)
  526. end
  527. end
  528. function ModuleManager:onRemoteManifestError()
  529. self:CleanupDownloaderSchedule()
  530. self:CleanupDownloader()
  531. local module = self:GetCurrentModule()
  532. local moduleGameEnv = module:GetGameEnv()
  533. local url2 = moduleGameEnv:GetResDownloadUrl2()
  534. if self._updateCount == 0 and url2 and string.len(url2) > 0 then
  535. self._updateCount = self._updateCount + 1
  536. self:CheckModuleLocalStorage()
  537. else
  538. -- 触发重新下载
  539. local module = self:GetCurrentModule()
  540. local moduleID = self:GetCurrentModuleID()
  541. local tips = "?"
  542. local logs = "?"
  543. if module then
  544. tips = string.format("%s(%s)", module:GetVersion(), module:GetGameEnv():GetSelectedServerUrlSuffix())
  545. logs = module:GetID() .. ":" .. module:GetGameEnv():GetResDownloadUrl()
  546. end
  547. local errorString = string.format("[%s][%s]远程清单异常!\n点击下方\"确认\"重试", tostring(moduleID), tostring(tips))
  548. RecordBuglyLogIm( Tags, logs )
  549. ReportBuglyError( Tags, errorString )
  550. local function callback()
  551. self._updateCount = 0
  552. self:CheckModuleLocalStorage()
  553. end
  554. self:ReportError(errorString, callback)
  555. end
  556. end
  557. function ModuleManager:onCheckVersionInfo()
  558. self:CleanupDownloaderSchedule()
  559. global.L_LoadingManager:OpenLayer({isCheckVersion=true, delay=true})
  560. end
  561. function ModuleManager:onNewVersion( versionSize )
  562. RecordBuglyLogIm( Tags, "New Version Found:" .. tostring( versionSize / (1024 * 1024) ) )
  563. self:CleanupDownloaderSchedule()
  564. global.L_LoadingManager:CloseLayer()
  565. global.L_LoadingManager:OpenLayer({isCheckVersion=true})
  566. -- start update
  567. local function startUpdate()
  568. global.L_LoadingManager:CloseLayer()
  569. global.L_LoadingManager:OpenLayer({isDetails=true})
  570. global.L_LoadingPlayManager:OpenLayer()
  571. local scheduler = cc.Director:getInstance():getScheduler()
  572. local handle = nil
  573. handle = scheduler:scheduleScriptFunc(
  574. function()
  575. scheduler:unscheduleScriptEntry(handle)
  576. if self._downloader then
  577. self._downloader:StartUpdate()
  578. end
  579. end,
  580. 1/60,
  581. false
  582. )
  583. -- 资源开始加载 进入登录页 上报自定义事件
  584. local moduleGameEnv = global.L_ModuleManager:GetCurrentModule():GetGameEnv()
  585. local selSerInfo = moduleGameEnv:GetSelectedServer()
  586. local server_id = selSerInfo and selSerInfo.serverId
  587. local server_name = selSerInfo and selSerInfo.serverName
  588. local dataParam = {
  589. event = {
  590. time = os.time(), name = "startLoading", type = "track"
  591. },
  592. properities = {
  593. servid = tostring(server_id), server_name = tostring(server_name)
  594. }
  595. }
  596. local main_servid = selSerInfo and selSerInfo.mainServerId
  597. local main_server_name = selSerInfo and selSerInfo.mainServerName
  598. dataParam.properities.main_servid = main_servid and tostring(main_servid) or tostring(server_id)
  599. dataParam.properities.main_server_name = main_server_name and tostring(main_server_name) or tostring(server_name)
  600. local userId = global.L_GameEnvManager:GetEnvDataByKey("user_id") or global.L_GameEnvManager:GetEnvDataByKey("uid")
  601. if userId and userId ~= "" then
  602. dataParam.user = {id = userId}
  603. end
  604. global.L_DataRePort:SendRePortData( dataParam )
  605. end
  606. -- 根据配置决定更新确认提醒
  607. local currentModule = self:GetCurrentModule()
  608. if not currentModule then
  609. startUpdate()
  610. return
  611. end
  612. local modGameEnv = currentModule:GetGameEnv()
  613. if not modGameEnv then
  614. startUpdate()
  615. return
  616. end
  617. -- 根据网络类型决定是否需要手动确认更新
  618. local netType = global.L_GameEnvManager:GetNetType()
  619. local limitSize = 0
  620. if netType == 0 then
  621. limitSize = tonumber(modGameEnv:GetCustomDataByKey("hotSizeWifi")) or 20
  622. else
  623. limitSize = tonumber(modGameEnv:GetCustomDataByKey("hotSize4G")) or 10
  624. end
  625. if (versionSize > limitSize*1024*1024) then
  626. local tipsData = {
  627. btnType = 1,
  628. str = string.format("检测到版本,大小%.2fM,建议在WIFI环境下\n更新,点击确认开始下载", versionSize / (1024 * 1024)),
  629. callback = startUpdate
  630. }
  631. global.L_CommonTipsManager:OpenLayer(tipsData)
  632. else
  633. startUpdate()
  634. end
  635. end
  636. function ModuleManager:onAlreadyUpToDate()
  637. self:CleanupDownloaderSchedule()
  638. RecordBuglyLogIm( Tags, "Already Up To Date")
  639. self:handleAlreadyUpToDate()
  640. end
  641. function ModuleManager:onUpdatePercent( percent )
  642. releasePrint(Tags, "onUpdatePercentFile", percent)
  643. self:CleanupDownloaderSchedule()
  644. -- global.L_LoadingManager:UpdatePercent(percent)
  645. end
  646. function ModuleManager:onUpdatePercentSize(percentSize, totalSize)
  647. releasePrint(Tags, "onUpdatePercentSize", percentSize)
  648. self:CleanupDownloaderSchedule()
  649. global.L_LoadingManager:UpdatePercent(percentSize, totalSize)
  650. end
  651. function ModuleManager:onAssetError( assetID, msg )
  652. self:CleanupDownloaderSchedule()
  653. RecordBuglyLogIm( Tags, "Asset Error! " .. tostring( assetID ) )
  654. end
  655. function ModuleManager:onDownloadFinished()
  656. RecordBuglyLogIm( Tags, "Update Finished" )
  657. self:CleanupDownloaderSchedule()
  658. self:handleUpdateFinished()
  659. -- 资源加载完成 进入登录页 上报自定义事件
  660. local moduleGameEnv = global.L_ModuleManager:GetCurrentModule():GetGameEnv()
  661. local selSerInfo = moduleGameEnv:GetSelectedServer()
  662. local server_id = selSerInfo and selSerInfo.serverId
  663. local server_name = selSerInfo and selSerInfo.serverName
  664. local dataParam = {
  665. event = {
  666. time = os.time(), name = "finishLoading", type = "track"
  667. },
  668. properities = {
  669. servid = tostring(server_id), server_name = tostring(server_name)
  670. }
  671. }
  672. local main_servid = selSerInfo and selSerInfo.mainServerId
  673. local main_server_name = selSerInfo and selSerInfo.mainServerName
  674. dataParam.properities.main_servid = main_servid and tostring(main_servid) or tostring(server_id)
  675. dataParam.properities.main_server_name = main_server_name and tostring(main_server_name) or tostring(server_name)
  676. local userId = global.L_GameEnvManager:GetEnvDataByKey("user_id") or global.L_GameEnvManager:GetEnvDataByKey("uid")
  677. if userId and userId ~= "" then
  678. dataParam.user = {id = userId}
  679. end
  680. global.L_DataRePort:SendRePortData( dataParam )
  681. end
  682. function ModuleManager:onDownloadFailed()
  683. RecordBuglyLogIm( Tags, "Update Failed!" )
  684. self:CleanupDownloaderSchedule()
  685. if self._downloader then
  686. self._downloader:Retry()
  687. end
  688. end
  689. -- check beta or stab
  690. function ModuleManager:checkVersion()
  691. local currentModule = self:GetCurrentModule()
  692. if not currentModule then
  693. return false
  694. end
  695. local selectServer = currentModule:GetGameEnv():GetSelectedServer()
  696. local verStr = currentModule:GetVersion()
  697. if verStr ~= selectServer.stab then
  698. if selectServer.stab == global.VersionName.beta then
  699. global.L_ModuleManager:SetModule2BetaVersion( currentModule )
  700. else
  701. global.L_ModuleManager:SetModule2StabVersion( currentModule, true )
  702. end
  703. return true
  704. else
  705. return false
  706. end
  707. return false
  708. end
  709. function ModuleManager:onChangeVersion()
  710. local function callback( aType, custom )
  711. global.L_NativeBridgeManager:GN_accountLogout()
  712. global.L_GameEnvManager:RestartGame()
  713. end
  714. local tipsData = {
  715. btnType = 1,
  716. str = "当前客户端版本不一致,进入该服需要重启客户端<br>是否重启并进入该服?",
  717. callback = callback
  718. }
  719. global.L_CommonTipsManager:OpenLayer( tipsData )
  720. end
  721. function ModuleManager:CleanupDownloaderSchedule()
  722. if self._moduleUpdateSchedule then
  723. global.Scheduler:unscheduleScriptEntry(self._moduleUpdateSchedule)
  724. self._moduleUpdateSchedule = nil
  725. end
  726. end
  727. ---------------------------------- hot update ------------------------------------
  728. return ModuleManager