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

501 lines
14 KiB

StepPackModule = StepPackModule or BaseClass()
local StepPackModule = StepPackModule
local Util = Util
local os = os
local setTimeout = setTimeout
local AppConst_CdnUrl = AppConst.CdnUrl
local Util_DataPath = Util.DataPath
local AppConst_OpenDownloadLog = AppConst.OpenDownloadLog
StepPackModule.PRELOAD_MAP_RES = "" --"1001" --新手200级使用12个地图资源的顺序
function StepPackModule:__init()
if StepPackModule.Instance ~= nil then
error("attempt to create singleton(StepPackModule) twice!")
return
end
StepPackModule.Instance = self
self.hight_res_list = nil
self.need_retry_res_list = {}
self.update_res_count = 0
self.update_res_size = 0
self.cur_update_index = 1
self.cur_download_size = 0
self.all_download_size = 0
self.all_download_count = 0
self.last_download_speed = 0
self.last_download_start_time = os.time()
self.earch_download_wait_time = 1
self.need_retry_count = 0
self.wait_download_time = nil
self.work_state = false
self.download_now = false
self.pause_state = false
self.auto_pause = false
self.res_checking_state = false
self.download_finish_state = false
self.callback_func = nil
self.last_show_speed_size = 0
self.last_show_speed_time = 0
self.newplayer_map_res_finish = false
self.max_download_speed = false
self.max_speed_mode = false -- 外部设置加速模式
local check_start = function()
self:CheckAutoUpdate()
end
local scene_start_event = nil
local scene_start = function()
GlobalEventSystem:UnBind(scene_start_event)
if not self.work_state and not self.download_finish_state then
setTimeout(check_start,15) --避开scene_start后10秒时设置的帧率流程
end
end
scene_start_event = GlobalEventSystem:Bind(EventName.SCENE_LOAD_VIEW_COMPLETE,scene_start)
self:CheckNewPlayerResState()
local callfunc = function(module_name,func)
if module_name == "download" and func == "maxspeed" then
self.max_download_speed = true
elseif module_name == "download" and func == "smallspeed" then
self.max_download_speed = false
end
end
GlobalEventSystem:Bind(EventName.DO_MODULE_DEBUG_FUNC,callfunc)
local lock_screen = function()
self.max_download_speed = true
end
GlobalEventSystem:Bind(SceneEventType.SCREEN_BRIGHT_DEL_EVENT, lock_screen)
local normal_screen = function()
if not self.max_speed_mode then
self.max_download_speed = false
end
end
GlobalEventSystem:Bind(SceneEventType.SCREEN_BRIGHT_ADD_EVENT, normal_screen)
end
function StepPackModule:SetMaxSpeedMode( state )
self.max_speed_mode = state
self.max_download_speed = state
end
function StepPackModule:CheckNewPlayerResState()
-- if self.check_newplayer_res_state or self.newplayer_map_res_finish then
-- return
-- end
-- self.check_newplayer_res_state = true
-- local data = Split(StepPackModule.PRELOAD_MAP_RES,",")
-- if data and #data > 0 then
-- self.map_list = data
-- end
-- local data = CookieWrapper.Instance:GetCookie(CookieLevelType.Common, CookieKey.NEWPLAYER_MAP_RES_DOWNLOAD_DATA)
-- if data then
-- local info = Split(data,",")
-- if info and #info >= 3 then
-- local finish = info[1]
-- if tonumber(finish) == 1 then
-- self.newplayer_map_res_finish = true
-- end
-- if not self.newplayer_map_res_finish then
-- local last_map = tonumber(info[2])
-- local last_index = tonumber(info[3])
-- if last_map and last_index then
-- self.last_newplayer_map = last_map
-- self.last_newplayer_index = last_index
-- end
-- end
-- end
-- else
-- --检测是老号并且无新手下载记录,则不下载新手资源
-- local last_role_id = CookieWrapper.Instance:GetCookie(CookieLevelType.Common, CookieKey.LAST_LOGIN_ROLE_ID)
-- if last_role_id then
-- self.newplayer_map_res_finish = true
-- else
-- if self.map_list and #self.map_list >= 2 then
-- self.last_newplayer_map = self.map_list[2]
-- self.last_newplayer_index = 1
-- CookieWrapper.Instance:SaveCookie(CookieLevelType.Common, CookieTimeType.TYPE_ALWAYS,CookieKey.NEWPLAYER_MAP_RES_DOWNLOAD_DATA,"0," .. self.last_newplayer_map .. "," .. self.last_newplayer_index)
-- CookieWrapper.Instance:WriteAll()
-- end
-- end
-- end
end
function StepPackModule:getInstance()
if StepPackModule.Instance == nil then
StepPackModule.New()
end
return StepPackModule.Instance
end
function StepPackModule:AddDownloadSize( size )
self.all_download_size = self.all_download_size + size
self.all_download_count = self.all_download_count + 1
self:CheckDownloadFinish()
end
function StepPackModule:CheckDownloadFinish()
if self.all_download_count >= self.update_res_count * 0.99 or self.all_download_size >= self.update_res_size * 0.99 then
self.download_finish_state = true
self.all_download_size = self.update_res_size
self.work_state = false
if self.update_speed_timer then
TimerQuest.CancelQuest(GlobalTimerQuest, self.update_speed_timer)
self.update_speed_timer = nil
end
if self.delay_write_version_timer then
--延迟的版本全部写入
Util.AutoWriteVersionFile(9999)
TimerQuest.CancelQuest(GlobalTimerQuest, self.delay_write_version_timer)
self.delay_write_version_timer = nil
end
if self.callback_func then
self.callback_func(self.all_download_size,self.update_res_size,0,self.download_finish_state)
end
CookieWrapper.Instance:SaveCookie(CookieLevelType.Common,CookieTimeType.TYPE_ALWAYS,CookieKey.SILENT_ASSETS_FINISH,"finish")
CookieWrapper.Instance:WriteAll()
end
--Debugger.Log("---------download count--------------" .. self.all_download_count .. "/" .. self.update_res_count)
end
function StepPackModule:Update(now_time, elapse_time)
-- 自动暂停状态时,检测到有wifi,则取消暂停
if self.auto_pause and Util.IsWifi then
self.pause_state = false
end
if not self.pause_state and not self.download_now and self.update_res_count + self.need_retry_count >= self.cur_update_index then
-- 下载中检测到流量网络状态,并且非手动在流量状态开始的,则自动暂停
if not Util.IsWifi and not self.no_wifi_download then
self.pause_state = true
self.auto_pause = true
return
end
if not Util.NetAvailable then
return
end
local pass_time = os.time() - self.last_download_start_time
if pass_time < self.earch_download_wait_time and not self.max_download_speed then
return
end
local res = nil
if self.cur_update_index <= self.update_res_count then
res = self.hight_res_list[self.cur_update_index-1]
else
res = self.need_retry_res_list[self.cur_update_index - self.update_res_count]
end
self.cur_update_index = self.cur_update_index + 1
local exist = resMgr:ExistDownloadHistory(res)
if exist then
self.all_download_count = self.all_download_count + 1
self:CheckDownloadFinish()
--print("--------normal exist------------------",res)
return
end
self.last_download_start_time = os.time()
--local update = Util.FileNeedUpdate(res)
self.download_now = true
print("--------normal download------------------",res)
self:DownloadFile(res)
local check_last_res
check_last_res = function()
if not self.pause_state and self.download_now and Util.NetAvailable then
if res == self.cur_download_file then
self.download_now = false
self.all_download_count = self.all_download_count + 1
end
else
setTimeout(check_last_res,10)
end
end
setTimeout(check_last_res,10)
end
end
function StepPackModule:DownloadError()
table.insert(self.need_retry_res_list,self.cur_download_file)
self.need_retry_count = self.need_retry_count + 1
end
function StepPackModule:DoNewPlayerOperate( )
-- if not self.newplayer_map_res_finish then
-- CookieWrapper.Instance:SaveCookie(CookieLevelType.Common, CookieTimeType.TYPE_ALWAYS, CookieKey.NEWPLAYER_MAP_RES_DOWNLOAD_DATA,"0," .. self.last_newplayer_map .. "," .. self.last_newplayer_index)
-- self.last_newplayer_index = self.last_newplayer_index + 1
-- end
end
function download_func(state,download_size)
-- StepPackModule.Instance:DoNewPlayerOperate()
StepPackModule.Instance.download_now = false
StepPackModule.Instance:AddDownloadSize(download_size)
if state == false and download_size == 0 then
--local cur_file = StepPackModule.Instance.cur_download_file
--local res = Util.GetAssetsBundlePathFromBase64(cur_file)
--Debugger.LogWarning("download error:" .. res)
else
--print("---------------download size-------------",download_size)
end
end
function StepPackModule:DownloadFile( res )
local remote_path = AppConst_CdnUrl .. res
if AppConst_EnglineVer >= 50 then
remote_path = AppConst_CdnUrl .. string.sub(res,2,-1)
end
-- local ingore_str = "/client/unitybundle/"
-- if SystemRuntimePlatform.IsIphone() then
-- ingore_str = "/client/iosbundle/"
-- elseif SystemRuntimePlatform.IsAndroid() then
-- ingore_str = "/client/androidbundle/"
-- end
-- local download_path = Util_DataPath .. string.gsub(res,ingore_str,"")
local ignore_list = {"/client/unitybundle/", "/client/iosbundle/", "/client/androidbundle/"}
local path = res
for k,v in pairs(ignore_list) do
path = string.gsub(path, v, "")
end
local download_path = Util_DataPath .. path
self.cur_download_file = res
if AppConst_OpenDownloadLog then
Debugger.Log("download request:" .. remote_path)
end
Util.DownloadFile(remote_path, download_path, 19000)
end
-- 以下是对外接口
-- 开始下载,调用前先设置回调函数
function StepPackModule:Start()
if self:CheckFinish() then
if self.callback_func then
self.callback_func(0,0,0,true)
end
return
end
-- 记录在流量状态下手动开始标识
if not Util.IsWifi then
self.no_wifi_download = true
end
if self.work_state or self.download_finish_state then
return
end
self.work_state = true
Runner.Instance:AddRunObj(self, 10)
Util.SetDownloadFileFunc(download_func)
local func = function(update_size,update_count,update_list)
self.res_checking_state = false
self.update_res_count = update_count
self.update_res_size = update_size
--Debugger.Log("---update_res---" .. self.update_res_count .. "/" .. self.update_res_size)
if self.update_res_count == 0 then
self.download_finish_state = true
self.work_state = false
if self.callback_func then
self.callback_func(0,0,0,true)
end
CookieWrapper.Instance:SaveCookie(CookieLevelType.Common,CookieTimeType.TYPE_ALWAYS,CookieKey.SILENT_ASSETS_FINISH,"finish")
CookieWrapper.Instance:WriteAll()
return
end
self.hight_res_list = update_list
end
self.res_checking_state = true
local frame_check_count = 15
if SystemMemoryLevel.Cur == SystemMemoryLevel.Low then
frame_check_count = 5
elseif SystemMemoryLevel.Cur == SystemMemoryLevel.Middle then
frame_check_count = 10
end
Util.CheckUpdateFileList("unitybundle",func,frame_check_count)
local update_func = function()
if self.all_download_size > 0 and not self.pause_state then
local add = self.all_download_size - self.last_show_speed_size
self.last_download_speed = add / self.earch_download_wait_time
if self.callback_func then
self.callback_func(self.all_download_size,self.update_res_size,self.last_download_speed,self.download_finish_state)
end
self.last_show_speed_size = self.all_download_size
end
end
self.update_speed_timer = TimerQuest.AddPeriodQuest(GlobalTimerQuest, update_func, self.earch_download_wait_time, -1)
if tonumber(AppConst.EnglineVer) > 83 then
local write_func = function()
Util.AutoWriteVersionFile(20)
end
self.delay_write_version_timer = TimerQuest.AddPeriodQuest(GlobalTimerQuest, write_func, 20, -1)
end
end
-- 设置暂停状态
function StepPackModule:SetPauseState( state )
self.pause_state = state
self.download_now = false
-- 记录在流量状态下手动开始标识
if not self.pause_state and not Util.IsWifi then
self.no_wifi_download = true
end
end
-- 设置下载回调函数
-- func(已下载量,总更新量,当前更新速度,是否已更新完成)
function StepPackModule:SetCallbackFunc( func )
self.callback_func = func
if self.callback_func and self.all_download_size > 0 then
self.callback_func(self.all_download_size,self.update_res_size,self.last_download_speed,self.download_finish_state)
end
end
function StepPackModule:IsResCheckState()
return self.res_checking_state
end
-- 状态: 0 未开始 1 暂停 2 下载中 3 已下载完成
function StepPackModule:CurrentPackState()
if self:CheckFinish() then
return 3
end
if self.pause_state then
return 1
end
if self.work_state then
return 2
end
if self.download_finish_state then
return 3
end
return 0
end
-- 是否支持静默下载
function StepPackModule:IsSupport()
if AppConst_EnglineVer > 44 and AppConst.UpdateMode and SystemMemoryLevel.Cur ~= SystemMemoryLevel.Low then
return true
end
return false
end
function StepPackModule:CheckFinish()
local flag = CookieWrapper.Instance:GetCookie(CookieLevelType.Common, CookieKey.SILENT_ASSETS_FINISH)
local assets_update = false
if AppConst_EnglineVer > 41 then
assets_update = AppConst.SilentAssetsUpdate
end
if flag == "finish" and not assets_update then
self.download_finish_state = true
self.work_state = false
end
return self.download_finish_state
end
function StepPackModule:CheckAutoUpdate()
local real_start = function()
self:Start()
GlobalEventSystem:Fire(EventName.AFTER_AUTO_UPDATE_RES)
CookieWrapper.Instance:SaveCookie(CookieLevelType.Common,CookieTimeType.TYPE_ALWAYS,CookieKey.SILENT_ASSETS_FINISH,"no")
CookieWrapper.Instance:WriteAll()
end
if self:IsSupport() and Util.IsWifi and ClientConfig.auto_slient_update and (RoleManager and RoleManager.Instance.mainRoleInfo.level > 120) then
if not self:CheckFinish() then
real_start()
end
else
-- --进入游戏时非wifi,一分钟定时检测如果切换到wifi则自动开启
-- if self:IsSupport() and not Util.IsWifi and not self:CheckFinish() and ClientConfig.auto_slient_update then
-- local cancel_check = function()
-- if self.check_wifi_open then
-- TimerQuest.CancelQuest(GlobalTimerQuest, self.check_wifi_open)
-- self.check_wifi_open = nil
-- end
-- end
-- cancel_check()
-- local check_func = function()
-- if self.work_state or self:CheckFinish() then
-- cancel_check()
-- return
-- end
-- if Util.IsWifi then
-- cancel_check()
-- real_start()
-- end
-- end
-- self.check_wifi_open = TimerQuest.AddPeriodQuest(GlobalTimerQuest, check_func, 60, -1)
-- end
end
end