源战役客户端
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.

500 lines
14 KiB

  1. StepPackModule = StepPackModule or BaseClass()
  2. local StepPackModule = StepPackModule
  3. local Util = Util
  4. local os = os
  5. local setTimeout = setTimeout
  6. local AppConst_CdnUrl = AppConst.CdnUrl
  7. local Util_DataPath = Util.DataPath
  8. local AppConst_OpenDownloadLog = AppConst.OpenDownloadLog
  9. StepPackModule.PRELOAD_MAP_RES = "" --"1001" --新手200级使用12个地图资源的顺序
  10. function StepPackModule:__init()
  11. if StepPackModule.Instance ~= nil then
  12. error("attempt to create singleton(StepPackModule) twice!")
  13. return
  14. end
  15. StepPackModule.Instance = self
  16. self.hight_res_list = nil
  17. self.need_retry_res_list = {}
  18. self.update_res_count = 0
  19. self.update_res_size = 0
  20. self.cur_update_index = 1
  21. self.cur_download_size = 0
  22. self.all_download_size = 0
  23. self.all_download_count = 0
  24. self.last_download_speed = 0
  25. self.last_download_start_time = os.time()
  26. self.earch_download_wait_time = 1
  27. self.need_retry_count = 0
  28. self.wait_download_time = nil
  29. self.work_state = false
  30. self.download_now = false
  31. self.pause_state = false
  32. self.auto_pause = false
  33. self.res_checking_state = false
  34. self.download_finish_state = false
  35. self.callback_func = nil
  36. self.last_show_speed_size = 0
  37. self.last_show_speed_time = 0
  38. self.newplayer_map_res_finish = false
  39. self.max_download_speed = false
  40. self.max_speed_mode = false -- 外部设置加速模式
  41. local check_start = function()
  42. self:CheckAutoUpdate()
  43. end
  44. local scene_start_event = nil
  45. local scene_start = function()
  46. GlobalEventSystem:UnBind(scene_start_event)
  47. if not self.work_state and not self.download_finish_state then
  48. setTimeout(check_start,15) --避开scene_start后10秒时设置的帧率流程
  49. end
  50. end
  51. scene_start_event = GlobalEventSystem:Bind(EventName.SCENE_LOAD_VIEW_COMPLETE,scene_start)
  52. self:CheckNewPlayerResState()
  53. local callfunc = function(module_name,func)
  54. if module_name == "download" and func == "maxspeed" then
  55. self.max_download_speed = true
  56. elseif module_name == "download" and func == "smallspeed" then
  57. self.max_download_speed = false
  58. end
  59. end
  60. GlobalEventSystem:Bind(EventName.DO_MODULE_DEBUG_FUNC,callfunc)
  61. local lock_screen = function()
  62. self.max_download_speed = true
  63. end
  64. GlobalEventSystem:Bind(SceneEventType.SCREEN_BRIGHT_DEL_EVENT, lock_screen)
  65. local normal_screen = function()
  66. if not self.max_speed_mode then
  67. self.max_download_speed = false
  68. end
  69. end
  70. GlobalEventSystem:Bind(SceneEventType.SCREEN_BRIGHT_ADD_EVENT, normal_screen)
  71. end
  72. function StepPackModule:SetMaxSpeedMode( state )
  73. self.max_speed_mode = state
  74. self.max_download_speed = state
  75. end
  76. function StepPackModule:CheckNewPlayerResState()
  77. -- if self.check_newplayer_res_state or self.newplayer_map_res_finish then
  78. -- return
  79. -- end
  80. -- self.check_newplayer_res_state = true
  81. -- local data = Split(StepPackModule.PRELOAD_MAP_RES,",")
  82. -- if data and #data > 0 then
  83. -- self.map_list = data
  84. -- end
  85. -- local data = CookieWrapper.Instance:GetCookie(CookieLevelType.Common, CookieKey.NEWPLAYER_MAP_RES_DOWNLOAD_DATA)
  86. -- if data then
  87. -- local info = Split(data,",")
  88. -- if info and #info >= 3 then
  89. -- local finish = info[1]
  90. -- if tonumber(finish) == 1 then
  91. -- self.newplayer_map_res_finish = true
  92. -- end
  93. -- if not self.newplayer_map_res_finish then
  94. -- local last_map = tonumber(info[2])
  95. -- local last_index = tonumber(info[3])
  96. -- if last_map and last_index then
  97. -- self.last_newplayer_map = last_map
  98. -- self.last_newplayer_index = last_index
  99. -- end
  100. -- end
  101. -- end
  102. -- else
  103. -- --检测是老号并且无新手下载记录,则不下载新手资源
  104. -- local last_role_id = CookieWrapper.Instance:GetCookie(CookieLevelType.Common, CookieKey.LAST_LOGIN_ROLE_ID)
  105. -- if last_role_id then
  106. -- self.newplayer_map_res_finish = true
  107. -- else
  108. -- if self.map_list and #self.map_list >= 2 then
  109. -- self.last_newplayer_map = self.map_list[2]
  110. -- self.last_newplayer_index = 1
  111. -- CookieWrapper.Instance:SaveCookie(CookieLevelType.Common, CookieTimeType.TYPE_ALWAYS,CookieKey.NEWPLAYER_MAP_RES_DOWNLOAD_DATA,"0," .. self.last_newplayer_map .. "," .. self.last_newplayer_index)
  112. -- CookieWrapper.Instance:WriteAll()
  113. -- end
  114. -- end
  115. -- end
  116. end
  117. function StepPackModule:getInstance()
  118. if StepPackModule.Instance == nil then
  119. StepPackModule.New()
  120. end
  121. return StepPackModule.Instance
  122. end
  123. function StepPackModule:AddDownloadSize( size )
  124. self.all_download_size = self.all_download_size + size
  125. self.all_download_count = self.all_download_count + 1
  126. self:CheckDownloadFinish()
  127. end
  128. function StepPackModule:CheckDownloadFinish()
  129. if self.all_download_count >= self.update_res_count * 0.99 or self.all_download_size >= self.update_res_size * 0.99 then
  130. self.download_finish_state = true
  131. self.all_download_size = self.update_res_size
  132. self.work_state = false
  133. if self.update_speed_timer then
  134. TimerQuest.CancelQuest(GlobalTimerQuest, self.update_speed_timer)
  135. self.update_speed_timer = nil
  136. end
  137. if self.delay_write_version_timer then
  138. --延迟的版本全部写入
  139. Util.AutoWriteVersionFile(9999)
  140. TimerQuest.CancelQuest(GlobalTimerQuest, self.delay_write_version_timer)
  141. self.delay_write_version_timer = nil
  142. end
  143. if self.callback_func then
  144. self.callback_func(self.all_download_size,self.update_res_size,0,self.download_finish_state)
  145. end
  146. CookieWrapper.Instance:SaveCookie(CookieLevelType.Common,CookieTimeType.TYPE_ALWAYS,CookieKey.SILENT_ASSETS_FINISH,"finish")
  147. CookieWrapper.Instance:WriteAll()
  148. end
  149. --Debugger.Log("---------download count--------------" .. self.all_download_count .. "/" .. self.update_res_count)
  150. end
  151. function StepPackModule:Update(now_time, elapse_time)
  152. -- 自动暂停状态时,检测到有wifi,则取消暂停
  153. if self.auto_pause and Util.IsWifi then
  154. self.pause_state = false
  155. end
  156. if not self.pause_state and not self.download_now and self.update_res_count + self.need_retry_count >= self.cur_update_index then
  157. -- 下载中检测到流量网络状态,并且非手动在流量状态开始的,则自动暂停
  158. if not Util.IsWifi and not self.no_wifi_download then
  159. self.pause_state = true
  160. self.auto_pause = true
  161. return
  162. end
  163. if not Util.NetAvailable then
  164. return
  165. end
  166. local pass_time = os.time() - self.last_download_start_time
  167. if pass_time < self.earch_download_wait_time and not self.max_download_speed then
  168. return
  169. end
  170. local res = nil
  171. if self.cur_update_index <= self.update_res_count then
  172. res = self.hight_res_list[self.cur_update_index-1]
  173. else
  174. res = self.need_retry_res_list[self.cur_update_index - self.update_res_count]
  175. end
  176. self.cur_update_index = self.cur_update_index + 1
  177. local exist = resMgr:ExistDownloadHistory(res)
  178. if exist then
  179. self.all_download_count = self.all_download_count + 1
  180. self:CheckDownloadFinish()
  181. --print("--------normal exist------------------",res)
  182. return
  183. end
  184. self.last_download_start_time = os.time()
  185. --local update = Util.FileNeedUpdate(res)
  186. self.download_now = true
  187. print("--------normal download------------------",res)
  188. self:DownloadFile(res)
  189. local check_last_res
  190. check_last_res = function()
  191. if not self.pause_state and self.download_now and Util.NetAvailable then
  192. if res == self.cur_download_file then
  193. self.download_now = false
  194. self.all_download_count = self.all_download_count + 1
  195. end
  196. else
  197. setTimeout(check_last_res,10)
  198. end
  199. end
  200. setTimeout(check_last_res,10)
  201. end
  202. end
  203. function StepPackModule:DownloadError()
  204. table.insert(self.need_retry_res_list,self.cur_download_file)
  205. self.need_retry_count = self.need_retry_count + 1
  206. end
  207. function StepPackModule:DoNewPlayerOperate( )
  208. -- if not self.newplayer_map_res_finish then
  209. -- CookieWrapper.Instance:SaveCookie(CookieLevelType.Common, CookieTimeType.TYPE_ALWAYS, CookieKey.NEWPLAYER_MAP_RES_DOWNLOAD_DATA,"0," .. self.last_newplayer_map .. "," .. self.last_newplayer_index)
  210. -- self.last_newplayer_index = self.last_newplayer_index + 1
  211. -- end
  212. end
  213. function download_func(state,download_size)
  214. -- StepPackModule.Instance:DoNewPlayerOperate()
  215. StepPackModule.Instance.download_now = false
  216. StepPackModule.Instance:AddDownloadSize(download_size)
  217. if state == false and download_size == 0 then
  218. --local cur_file = StepPackModule.Instance.cur_download_file
  219. --local res = Util.GetAssetsBundlePathFromBase64(cur_file)
  220. --Debugger.LogWarning("download error:" .. res)
  221. else
  222. --print("---------------download size-------------",download_size)
  223. end
  224. end
  225. function StepPackModule:DownloadFile( res )
  226. local remote_path = AppConst_CdnUrl .. res
  227. if AppConst_EnglineVer >= 50 then
  228. remote_path = AppConst_CdnUrl .. string.sub(res,2,-1)
  229. end
  230. -- local ingore_str = "/client/unitybundle/"
  231. -- if SystemRuntimePlatform.IsIphone() then
  232. -- ingore_str = "/client/iosbundle/"
  233. -- elseif SystemRuntimePlatform.IsAndroid() then
  234. -- ingore_str = "/client/androidbundle/"
  235. -- end
  236. -- local download_path = Util_DataPath .. string.gsub(res,ingore_str,"")
  237. local ignore_list = {"/client/unitybundle/", "/client/iosbundle/", "/client/androidbundle/"}
  238. local path = res
  239. for k,v in pairs(ignore_list) do
  240. path = string.gsub(path, v, "")
  241. end
  242. local download_path = Util_DataPath .. path
  243. self.cur_download_file = res
  244. if AppConst_OpenDownloadLog then
  245. Debugger.Log("download request:" .. remote_path)
  246. end
  247. Util.DownloadFile(remote_path, download_path, 19000)
  248. end
  249. -- 以下是对外接口
  250. -- 开始下载,调用前先设置回调函数
  251. function StepPackModule:Start()
  252. if self:CheckFinish() then
  253. if self.callback_func then
  254. self.callback_func(0,0,0,true)
  255. end
  256. return
  257. end
  258. -- 记录在流量状态下手动开始标识
  259. if not Util.IsWifi then
  260. self.no_wifi_download = true
  261. end
  262. if self.work_state or self.download_finish_state then
  263. return
  264. end
  265. self.work_state = true
  266. Runner.Instance:AddRunObj(self, 10)
  267. Util.SetDownloadFileFunc(download_func)
  268. local func = function(update_size,update_count,update_list)
  269. self.res_checking_state = false
  270. self.update_res_count = update_count
  271. self.update_res_size = update_size
  272. --Debugger.Log("---update_res---" .. self.update_res_count .. "/" .. self.update_res_size)
  273. if self.update_res_count == 0 then
  274. self.download_finish_state = true
  275. self.work_state = false
  276. if self.callback_func then
  277. self.callback_func(0,0,0,true)
  278. end
  279. CookieWrapper.Instance:SaveCookie(CookieLevelType.Common,CookieTimeType.TYPE_ALWAYS,CookieKey.SILENT_ASSETS_FINISH,"finish")
  280. CookieWrapper.Instance:WriteAll()
  281. return
  282. end
  283. self.hight_res_list = update_list
  284. end
  285. self.res_checking_state = true
  286. local frame_check_count = 15
  287. if SystemMemoryLevel.Cur == SystemMemoryLevel.Low then
  288. frame_check_count = 5
  289. elseif SystemMemoryLevel.Cur == SystemMemoryLevel.Middle then
  290. frame_check_count = 10
  291. end
  292. Util.CheckUpdateFileList("unitybundle",func,frame_check_count)
  293. local update_func = function()
  294. if self.all_download_size > 0 and not self.pause_state then
  295. local add = self.all_download_size - self.last_show_speed_size
  296. self.last_download_speed = add / self.earch_download_wait_time
  297. if self.callback_func then
  298. self.callback_func(self.all_download_size,self.update_res_size,self.last_download_speed,self.download_finish_state)
  299. end
  300. self.last_show_speed_size = self.all_download_size
  301. end
  302. end
  303. self.update_speed_timer = TimerQuest.AddPeriodQuest(GlobalTimerQuest, update_func, self.earch_download_wait_time, -1)
  304. if tonumber(AppConst.EnglineVer) > 83 then
  305. local write_func = function()
  306. Util.AutoWriteVersionFile(20)
  307. end
  308. self.delay_write_version_timer = TimerQuest.AddPeriodQuest(GlobalTimerQuest, write_func, 20, -1)
  309. end
  310. end
  311. -- 设置暂停状态
  312. function StepPackModule:SetPauseState( state )
  313. self.pause_state = state
  314. self.download_now = false
  315. -- 记录在流量状态下手动开始标识
  316. if not self.pause_state and not Util.IsWifi then
  317. self.no_wifi_download = true
  318. end
  319. end
  320. -- 设置下载回调函数
  321. -- func(已下载量,总更新量,当前更新速度,是否已更新完成)
  322. function StepPackModule:SetCallbackFunc( func )
  323. self.callback_func = func
  324. if self.callback_func and self.all_download_size > 0 then
  325. self.callback_func(self.all_download_size,self.update_res_size,self.last_download_speed,self.download_finish_state)
  326. end
  327. end
  328. function StepPackModule:IsResCheckState()
  329. return self.res_checking_state
  330. end
  331. -- 状态: 0 未开始 1 暂停 2 下载中 3 已下载完成
  332. function StepPackModule:CurrentPackState()
  333. if self:CheckFinish() then
  334. return 3
  335. end
  336. if self.pause_state then
  337. return 1
  338. end
  339. if self.work_state then
  340. return 2
  341. end
  342. if self.download_finish_state then
  343. return 3
  344. end
  345. return 0
  346. end
  347. -- 是否支持静默下载
  348. function StepPackModule:IsSupport()
  349. if AppConst_EnglineVer > 44 and AppConst.UpdateMode and SystemMemoryLevel.Cur ~= SystemMemoryLevel.Low then
  350. return true
  351. end
  352. return false
  353. end
  354. function StepPackModule:CheckFinish()
  355. local flag = CookieWrapper.Instance:GetCookie(CookieLevelType.Common, CookieKey.SILENT_ASSETS_FINISH)
  356. local assets_update = false
  357. if AppConst_EnglineVer > 41 then
  358. assets_update = AppConst.SilentAssetsUpdate
  359. end
  360. if flag == "finish" and not assets_update then
  361. self.download_finish_state = true
  362. self.work_state = false
  363. end
  364. return self.download_finish_state
  365. end
  366. function StepPackModule:CheckAutoUpdate()
  367. local real_start = function()
  368. self:Start()
  369. GlobalEventSystem:Fire(EventName.AFTER_AUTO_UPDATE_RES)
  370. CookieWrapper.Instance:SaveCookie(CookieLevelType.Common,CookieTimeType.TYPE_ALWAYS,CookieKey.SILENT_ASSETS_FINISH,"no")
  371. CookieWrapper.Instance:WriteAll()
  372. end
  373. if self:IsSupport() and Util.IsWifi and ClientConfig.auto_slient_update and (RoleManager and RoleManager.Instance.mainRoleInfo.level > 120) then
  374. if not self:CheckFinish() then
  375. real_start()
  376. end
  377. else
  378. -- --进入游戏时非wifi,一分钟定时检测如果切换到wifi则自动开启
  379. -- if self:IsSupport() and not Util.IsWifi and not self:CheckFinish() and ClientConfig.auto_slient_update then
  380. -- local cancel_check = function()
  381. -- if self.check_wifi_open then
  382. -- TimerQuest.CancelQuest(GlobalTimerQuest, self.check_wifi_open)
  383. -- self.check_wifi_open = nil
  384. -- end
  385. -- end
  386. -- cancel_check()
  387. -- local check_func = function()
  388. -- if self.work_state or self:CheckFinish() then
  389. -- cancel_check()
  390. -- return
  391. -- end
  392. -- if Util.IsWifi then
  393. -- cancel_check()
  394. -- real_start()
  395. -- end
  396. -- end
  397. -- self.check_wifi_open = TimerQuest.AddPeriodQuest(GlobalTimerQuest, check_func, 60, -1)
  398. -- end
  399. end
  400. end