ZUpdateManager.lua.txt 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. require("Base/GlobalFunctions.lua")
  2. -- my
  3. local serverIP = "http://192.168.1.187"
  4. local serverPort = "88"
  5. local filePath = "/files/sframework_test"
  6. -- local serverIP = "http://192.168.0.200"
  7. -- local serverPort = "80"
  8. -- local filePath = "/vr-meeting-hotfix/files/sframework_update"
  9. local serverHost = serverIP .. ":" ..serverPort
  10. local tUpdateConfig = {
  11. -- ignoreUpdate = true,
  12. ignoreUpdate = false,
  13. hotfixLuaUrl = serverHost .. filePath .. "/hotfix.lua",
  14. httpRequestTimeoutTime = 3,
  15. resourceVersionUrlBase = serverHost .. filePath .. "/Bundles/";
  16. resourceVersionName = "version.json",
  17. }
  18. local ZUpdateManager = ZUpdateManager or {}
  19. local tUpdateState = {
  20. usInvaild = "usInvaild",
  21. usStart = "usStart",
  22. usShowUI = "usShowUI",
  23. usHotfixLua = "usHotfixLua",
  24. usGetResVersion = "usGetResVersion",
  25. usCalcUpdateResInfo = "usCalcUpdateResInfo",
  26. usDownloadResource = "usDownloadResource",
  27. usFinish = "usFinish"
  28. }
  29. ZUpdateManager._updateState = tUpdateState.usInvaild
  30. ZUpdateManager._rVersionInfo = nil
  31. ZUpdateManager._lVersionInfo = nil
  32. ZUpdateManager._rVersionInfoJson = nil
  33. ZUpdateManager._willDownloadBytes = 0
  34. ZUpdateManager._willDownloadFileList = nil
  35. local function stepState(newState)
  36. local oldState = ZUpdateManager._updateState
  37. print("----> ZUpdateManager StepState " .. oldState .. " -> " .. newState)
  38. ZUpdateManager._updateState = newState
  39. end
  40. local function retryUpdate()
  41. print("----------> retryUpdate")
  42. require("Base/CoroutineHelper.lua"):Start(ZUpdateManager.ProcessUpdate)
  43. end
  44. function ZUpdateManager.Awake(luaRoot)
  45. print("ZUpdateManager.Awake ~")
  46. local dataPath = CS.UnityEngine.Application.dataPath
  47. local streamingAssetsPath = CS.UnityEngine.Application.streamingAssetsPath
  48. local persistentDataPath = CS.UnityEngine.Application.persistentDataPath
  49. print("Application.dataPath -> " .. dataPath)
  50. print("Application.streamingAssetsPath -> " .. streamingAssetsPath)
  51. print("Application.persistentDataPath -> " .. persistentDataPath)
  52. end
  53. function ZUpdateManager.Start(luaRoot)
  54. print("ZUpdateManager.Start ~")
  55. stepState(tUpdateState.usStart)
  56. -- Register Event
  57. local eventDispatchCenter = require("Base/ZEventDispatchCenter.lua")
  58. eventDispatchCenter:RegisterEvent(eventDispatchCenter.EventType.UPDATE_RETRY, retryUpdate)
  59. -- Process Show Update UI.
  60. require("Base/UIHelper.lua"):OpenUI("Update/ZUIPanelUpdate.lua")
  61. stepState(tUpdateState.usShowUI)
  62. -- Check Ignore Update.
  63. if tUpdateConfig.ignoreUpdate == true then
  64. stepState(tUpdateState.usFinish)
  65. print("ZUpdateManager.ignoreUpdate == true, Jump Update!")
  66. require("Base/CoroutineHelper.lua"):Start(ZUpdateManager.ProcessUpdate)
  67. return
  68. end
  69. -- Process Update.
  70. stepState(tUpdateState.usHotfixLua)
  71. require("Base/CoroutineHelper.lua"):Start(ZUpdateManager.ProcessUpdate)
  72. end
  73. function ZUpdateManager.OnDestroy()
  74. print("ZUpdateManager.OnDestroy ~")
  75. local eventDispatchCenter = require("Base/ZEventDispatchCenter.lua")
  76. eventDispatchCenter:UnregisterEvent(eventDispatchCenter.EventType.UPDATE_RETRY, retryUpdate)
  77. end
  78. local function GetbIsNetworkErrorVerson()
  79. local bIsNetworkError
  80. local szUnityVersion = CS.UnityEngine.Application.unityVersion
  81. -- 2020 luaRequest.result
  82. -- 2018, 2019 luaRequest.isNetworkError
  83. local nCheckYear = 2020
  84. local szUnityVersionYear = require("Base/Utils.lua"):Sub(szUnityVersion, 1, 4)
  85. local nUnityVersionYear = tonumber(szUnityVersionYear)
  86. bIsNetworkError = nUnityVersionYear < nCheckYear
  87. return bIsNetworkError
  88. end
  89. function ZUpdateManager.tryHotfixLuaLogic()
  90. print("----> Process usHotfixLua Start ...")
  91. require("Base/CoroutineHelper.lua"):WaitForEndOfFrame()
  92. local eventDispatchCenter = require("Base/ZEventDispatchCenter.lua")
  93. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, "链接服务器 ...")
  94. require("Base/CoroutineHelper.lua"):Wait(0.1)
  95. local hotfixLuaUrl = tUpdateConfig.hotfixLuaUrl
  96. --print("-------------> hotfixLuaUrl: " .. hotfixLuaUrl)
  97. local luaRequest = CS.UnityEngine.Networking.UnityWebRequest.Get(hotfixLuaUrl)
  98. luaRequest.timeout = tUpdateConfig.httpRequestTimeoutTime
  99. require("Base/CoroutineHelper.lua"):WaitSendWebRequest(luaRequest)
  100. local bIsNetworkError = GetbIsNetworkErrorVerson()
  101. local bLuaRequestError
  102. if bIsNetworkError then
  103. bLuaRequestError = luaRequest.isNetworkError or luaRequest.isHttpError
  104. else
  105. bLuaRequestError = (luaRequest.result == CS.UnityEngine.Networking.UnityWebRequest.Result.ConnectionError or luaRequest.result == CS.UnityEngine.Networking.UnityWebRequest.Result.ProtocolError)
  106. end
  107. if bLuaRequestError then
  108. Warn("ZUpdateManager -> UpdateLogic luaRequest Error -> " .. tostring(luaRequest.error))
  109. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, "链接服务器失败: " .. tostring(luaRequest.error))
  110. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_SHOW_TIPS, "链接失败", "链接服务器失败!\n请检查当前网络状态。")
  111. --print("-------> Hotfix Step Failed")
  112. return
  113. end
  114. local luaCode = luaRequest.downloadHandler.text
  115. CS.SFramework.SLuaEnv.Instance:DoString(luaCode)
  116. print("----> Process usHotfixLua Finish ...")
  117. -- eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, "链接服务器成功")
  118. -- require("Base/CoroutineHelper.lua"):Wait(0.1)
  119. stepState(tUpdateState.usGetResVersion)
  120. require("Base/CoroutineHelper.lua"):Start(ZUpdateManager.ProcessUpdate)
  121. end
  122. function ZUpdateManager.tryGetResVersionLogic()
  123. print("----> Process usGetResVersion Start ...")
  124. require("Base/CoroutineHelper.lua"):WaitForEndOfFrame()
  125. local eventDispatchCenter = require("Base/ZEventDispatchCenter.lua")
  126. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, "链接资源服务 ...")
  127. require("Base/CoroutineHelper.lua"):Wait(0.1)
  128. local platformName = CS.SFramework.SPlatformInfoR.GetBuildPlatformName()
  129. local resVersionUrl = tUpdateConfig.resourceVersionUrlBase .. platformName .. "/" .. tUpdateConfig.resourceVersionName
  130. --print("------------> resVersionUrl: " .. tostring(resVersionUrl))
  131. local luaRequest = CS.UnityEngine.Networking.UnityWebRequest.Get(resVersionUrl)
  132. luaRequest.timeout = tUpdateConfig.httpRequestTimeoutTime
  133. require("Base/CoroutineHelper.lua"):WaitSendWebRequest(luaRequest)
  134. local bIsNetworkError = GetbIsNetworkErrorVerson()
  135. local bLuaRequestError
  136. if bIsNetworkError then
  137. bLuaRequestError = luaRequest.isNetworkError or luaRequest.isHttpError
  138. else
  139. bLuaRequestError = (luaRequest.result == CS.UnityEngine.Networking.UnityWebRequest.Result.ConnectionError or luaRequest.result == CS.UnityEngine.Networking.UnityWebRequest.Result.ProtocolError)
  140. end
  141. if bLuaRequestError then
  142. -- Notice User, Choose;
  143. Warn("ZUpdateManager -> UpdateLogic resVersionRequest Error -> " .. tostring(luaRequest.error))
  144. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, "链接资源服务失败: " .. tostring(luaRequest.error))
  145. --print("-------> GetResVersion Step Failed")
  146. return
  147. end
  148. local jsonConverter = require("Base/json.lua")
  149. local remoteVersionJson = luaRequest.downloadHandler.text
  150. print("remoteVersionJson -> " .. remoteVersionJson)
  151. local localVersionJson = CS.SFramework.SResourceManagerR.GetLocalVersionJson()
  152. print("localVersionJson -> " .. localVersionJson)
  153. --require("Base/CoroutineHelper.lua"):Wait(1)
  154. local remoteVersionInfo = jsonConverter:decode(remoteVersionJson)
  155. local localVersionInfo = jsonConverter:decode(localVersionJson)
  156. local rversion = remoteVersionInfo.version
  157. local lversion = localVersionInfo.version
  158. local stateMessage = string.format("发行版本: %d 本地版本: %d", rversion, lversion)
  159. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, stateMessage)
  160. ZUpdateManager._lVersionInfo = localVersionInfo
  161. ZUpdateManager._rVersionInfo = remoteVersionInfo
  162. ZUpdateManager._rVersionInfoJson = remoteVersionJson
  163. require("Base/CoroutineHelper.lua"):Wait(0.1)
  164. -- eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, "链接资源服务成功")
  165. -- require("Base/CoroutineHelper.lua"):Wait(0.1)
  166. if rversion == lversion then
  167. print("lversion == rversion no need update.")
  168. stepState(tUpdateState.usFinish)
  169. require("Base/CoroutineHelper.lua"):Start(ZUpdateManager.ProcessUpdate)
  170. return
  171. end
  172. print("----> Process usGetResVersion Finish ...")
  173. --require("Base/CoroutineHelper.lua"):Wait(1)
  174. stepState(tUpdateState.usCalcUpdateResInfo)
  175. require("Base/CoroutineHelper.lua"):Start(ZUpdateManager.ProcessUpdate)
  176. end
  177. function ZUpdateManager.tryCalcUpdateResInfoLogic()
  178. print("----> Process usCalcUpdateResInfo Start ...")
  179. require("Base/CoroutineHelper.lua"):WaitForEndOfFrame()
  180. local eventDispatchCenter = require("Base/ZEventDispatchCenter.lua")
  181. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, "资源校对中 ...")
  182. require("Base/CoroutineHelper.lua"):Wait(0.1)
  183. local lVersionInfo = ZUpdateManager._lVersionInfo
  184. local rVersionInfo = ZUpdateManager._rVersionInfo
  185. local localResourceCache = {}
  186. local localResourceCount = 0
  187. for _, oneFileInfo in pairs(lVersionInfo.fileInfo) do
  188. localResourceCache[oneFileInfo.fileName] = oneFileInfo
  189. localResourceCount = localResourceCount + 1
  190. end
  191. print("----> Cache Local Resource Count: " .. tostring(localResourceCount))
  192. local willDownloadFileCount = 0
  193. local willDownloadBytes = 0
  194. local willDownloadFileList = {}
  195. for _, oneFileInfo in pairs(rVersionInfo.fileInfo) do
  196. local lFile = localResourceCache[oneFileInfo.fileName]
  197. -- lFile == nil => add file edit file
  198. if lFile == nil or (lFile ~= nil and lFile.hashInfo ~= oneFileInfo.hashInfo) then
  199. willDownloadFileCount = willDownloadFileCount + 1
  200. willDownloadBytes = willDownloadBytes + oneFileInfo.fileSize
  201. willDownloadFileList[oneFileInfo.fileName] = oneFileInfo
  202. end
  203. -- remove file
  204. localResourceCache[oneFileInfo.fileName] = nil
  205. end
  206. for _, removeFileInfo in pairs(localResourceCache) do
  207. willDownloadFileCount = willDownloadFileCount + 1
  208. willDownloadBytes = willDownloadBytes + removeFileInfo.fileSize
  209. willDownloadFileList[removeFileInfo.fileName] = removeFileInfo
  210. end
  211. ZUpdateManager._willDownloadBytes = willDownloadBytes
  212. ZUpdateManager._willDownloadFileList = willDownloadFileList
  213. ZUpdateManager._willDownloadFileCount = willDownloadFileCount
  214. print("----> will download file count: " .. tostring(willDownloadFileCount))
  215. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, "资源校对完成")
  216. local totalSize = CS.System.Math.Round(willDownloadBytes / 1024 / 1024, 2)
  217. local tipsInfo = string.format(
  218. "发现新版本:%s \n 资源大小:%s MB",
  219. rVersionInfo.version, tostring(totalSize)
  220. )
  221. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_SHOW_TIPS, "更新提示", tipsInfo)
  222. print("----> Process usCalcUpdateResInfo Finish ...")
  223. stepState(tUpdateState.usDownloadResource)
  224. --require("Base/CoroutineHelper.lua"):Start(ZUpdateManager.ProcessUpdate)
  225. end
  226. function ZUpdateManager.tryDownloadResourceLogic()
  227. print("----> Process usDownloadResource Start ...")
  228. require("Base/CoroutineHelper.lua"):WaitForEndOfFrame()
  229. local eventDispatchCenter = require("Base/ZEventDispatchCenter.lua")
  230. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, "正在准备资源...")
  231. require("Base/CoroutineHelper.lua"):Wait(0.1)
  232. local willDownloadFileList = ZUpdateManager._willDownloadFileList
  233. local willDownloadFileCount = ZUpdateManager._willDownloadFileCount
  234. -- for backups
  235. --[[
  236. -- Step 1: Try To Delete All Local File(PersistentPath) That Need Update.
  237. local cleanedCount = 1
  238. for fileName, fileInfo in pairs(willDownloadFileList) do
  239. CS.SFramework.SResourceManagerR.TryDeleteAssetBundle(fileInfo.fileName)
  240. local stateMessage = string.format("Clean Local Files(%d/%d) -> %s", cleanedCount, willDownloadFileCount, fileName)
  241. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, stateMessage)
  242. cleanedCount = cleanedCount + 1
  243. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_PROGRESS_PERCENT, cleanedCount / willDownloadFileCount)
  244. require("Base/CoroutineHelper.lua"):Wait(0.1)
  245. end
  246. print("----> Success Clean Local File Count: " .. tostring(cleanedCount))
  247. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, "Clean All Local Resources ... Success")
  248. require("Base/CoroutineHelper.lua"):Wait(0.1)
  249. ]]
  250. -- for backups
  251. --[[
  252. -- Step 2: Download Each File One By One And Write It On Right Path.
  253. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, "Prepare To Download Remote Resources ...")
  254. local downloadStartTime = CS.UnityEngine.Time.realtimeSinceStartup
  255. local downloadedBytes = 0
  256. local downloadSpeed = ""
  257. for fileName, fileInfo in pairs(willDownloadFileList) do
  258. local baseDownloadedBytes = downloadedBytes
  259. local platformName = CS.SFramework.SPlatformInfoR.GetBuildPlatformName()
  260. local donwloadLink = tUpdateConfig.resourceVersionUrlBase .. platformName .. "/" .. fileInfo.fileName
  261. local luaRequest = CS.UnityEngine.Networking.UnityWebRequest.Get(donwloadLink)
  262. luaRequest.timeout = tUpdateConfig.httpRequestTimeoutTime
  263. local downloadPercent = 0
  264. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_PROGRESS_PERCENT, downloadPercent)
  265. luaRequest:SendWebRequest()
  266. repeat
  267. downloadPercent = luaRequest.downloadProgress
  268. if downloadPercent < 0 then downloadPercent = 0 end
  269. local fileDownloadBytes = baseDownloadedBytes + luaRequest.downloadedBytes
  270. downloadSpeed = CS.SFramework.SToolFunctionR.GetDownloadSpeed(fileDownloadBytes, CS.UnityEngine.Time.realtimeSinceStartup - downloadStartTime)
  271. -- update download info to show information.
  272. local stateMessage = string.format(
  273. "Downloading: %s From %s \nPercent: %.2f Speed:%s",
  274. fileName, donwloadLink, downloadPercent * 100, downloadSpeed
  275. )
  276. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, stateMessage)
  277. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_PROGRESS_PERCENT, downloadPercent)
  278. require("Base/CoroutineHelper.lua"):WaitForEndOfFrame()
  279. until luaRequest.isDone
  280. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_PROGRESS_PERCENT, 1)
  281. downloadedBytes = downloadedBytes + luaRequest.downloadedBytes
  282. downloadSpeed = CS.SFramework.SToolFunctionR.GetDownloadSpeed(downloadedBytes, CS.UnityEngine.Time.realtimeSinceStartup - downloadStartTime)
  283. -- update download speed
  284. local stateMessage = string.format("Downloading: %s \nPercent: %.2f Speed:%s Success", fileName, downloadPercent * 100, downloadSpeed)
  285. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, stateMessage)
  286. require("Base/CoroutineHelper.lua"):Wait(0.1)
  287. CS.SFramework.SResourceManagerR.SaveAssetBundle(fileInfo.fileName, luaRequest.downloadHandler.data)
  288. stateMessage = string.format("Download And Save %s Success", fileName)
  289. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, stateMessage)
  290. require("Base/CoroutineHelper.lua"):Wait(0.1)
  291. end
  292. downloadSpeed = CS.SFramework.SToolFunctionR.GetDownloadSpeed(downloadedBytes, CS.UnityEngine.Time.realtimeSinceStartup - downloadStartTime)
  293. -- update download speed
  294. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, "All Remote Resource Download Compelete ... Speed: " .. tostring(downloadSpeed))
  295. require("Base/CoroutineHelper.lua"):Wait(0.1)
  296. ]]
  297. -- Step 1: Try To Delete All Local File(PersistentPath) That Need Update
  298. local nCleanedCount = 0
  299. for _, fileInfo in pairs(willDownloadFileList) do
  300. CS.SFramework.SResourceManagerR.TryDeleteAssetBundle(fileInfo.fileName)
  301. nCleanedCount = nCleanedCount + 1
  302. local nDeletePercent = nCleanedCount / willDownloadFileCount
  303. -- local szStateMessage = string.format("资源加载中: %.2f", nDeletePercent * 100)
  304. -- eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, szStateMessage .. "%")
  305. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_PROGRESS_PERCENT, nDeletePercent)
  306. require("Base/CoroutineHelper.lua"):Wait(0.01)
  307. end
  308. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, "资源准备完成")
  309. require("Base/CoroutineHelper.lua"):Wait(0.1)
  310. -- Step 2: Show All Download Percent
  311. local szStateMessage = "更新中..."
  312. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, szStateMessage)
  313. require("Base/CoroutineHelper.lua"):Wait(0.1)
  314. local nDownloadStartTime = CS.UnityEngine.Time.realtimeSinceStartup
  315. local nWillDownloadBytes = ZUpdateManager._willDownloadBytes
  316. local nDownloadedBytes = 0
  317. local szDownloadSpeed = ""
  318. for _, fileInfo in pairs(willDownloadFileList) do
  319. local szPlatformName = CS.SFramework.SPlatformInfoR.GetBuildPlatformName()
  320. local szDownloadFileLink = tUpdateConfig.resourceVersionUrlBase .. szPlatformName .. "/" .. fileInfo.fileName
  321. local luaRequest = CS.UnityEngine.Networking.UnityWebRequest.Get(szDownloadFileLink)
  322. luaRequest.timeout = tUpdateConfig.httpRequestTimeoutTime
  323. luaRequest:SendWebRequest()
  324. local nDownloadPercent = 0
  325. local nDownloadFileBytes = fileInfo.fileSize
  326. local nCurrentDownloadedBytes = nDownloadedBytes
  327. repeat
  328. local nFileDownloadPercent = luaRequest.downloadProgress
  329. local nFileDownloadedBytes = nDownloadFileBytes * nFileDownloadPercent
  330. local nCurrentFileDownloadedBytes = nCurrentDownloadedBytes + nFileDownloadedBytes
  331. nDownloadPercent = nCurrentFileDownloadedBytes / nWillDownloadBytes
  332. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_PROGRESS_PERCENT, nDownloadPercent)
  333. -- szDownloadSpeed = CS.SFramework.SToolFunctionR.GetDownloadSpeed(nDownloadedBytes, CS.UnityEngine.Time.realtimeSinceStartup - nDownloadStartTime)
  334. -- local szStateMessage = string.format("Downloading Remote Resources. Speed: %s", szDownloadSpeed)
  335. -- eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, szStateMessage)
  336. require("Base/CoroutineHelper.lua"):WaitForEndOfFrame()
  337. until luaRequest.isDone
  338. nDownloadedBytes = nDownloadedBytes + luaRequest.downloadedBytes
  339. local szDownloadPercent = string.format("%.2f", nDownloadPercent * 100) .. "%"
  340. szDownloadSpeed = CS.SFramework.SToolFunctionR.GetDownloadSpeed(nDownloadedBytes, CS.UnityEngine.Time.realtimeSinceStartup - nDownloadStartTime)
  341. local nDownloadSize = CS.System.Math.Round(nDownloadedBytes / 1024 / 1024, 2)
  342. local nTotleSize = CS.System.Math.Round(nWillDownloadBytes / 1024 / 1024, 2)
  343. -- local szStateMessage = string.format("更新中:%s (%s MB /%s MB)... 下载速度: %s", szDownloadPercent, tostring(nDownloadSize), tostring(nTotleSize), szDownloadSpeed)
  344. -- eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, szStateMessage)
  345. local szUpdateDetail = string.format("(%s MB /%s MB)", tostring(nDownloadSize), tostring(nTotleSize))
  346. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_SHOW_UPDATE_DETAIL, szDownloadPercent, szUpdateDetail, szDownloadSpeed)
  347. CS.SFramework.SResourceManagerR.SaveAssetBundle(fileInfo.fileName, luaRequest.downloadHandler.data)
  348. end
  349. local nDownloadPercent = 1
  350. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_PROGRESS_PERCENT, nDownloadPercent)
  351. local szStateMessage = "更新完成"
  352. eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, szStateMessage)
  353. require("Base/CoroutineHelper.lua"):Wait(0.1)
  354. -- Step 3: Try Write version.json(Remote Version) To PersistentPath.
  355. CS.SFramework.SResourceManagerR.SaveRemoteVersionJson(ZUpdateManager._rVersionInfoJson)
  356. -- eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, "所有资源加载完成 ...")
  357. print("----> Process usDownloadResource Finish ...")
  358. require("Base/CoroutineHelper.lua"):Wait(1)
  359. -- Step 4: Change currentState And retry
  360. stepState(tUpdateState.usFinish)
  361. require("Base/CoroutineHelper.lua"):Start(ZUpdateManager.ProcessUpdate)
  362. end
  363. function ZUpdateManager.tryFinishLogic()
  364. print("----> Process usFinish Start ...")
  365. require("Base/CoroutineHelper.lua"):WaitForEndOfFrame()
  366. local eventDispatchCenter = require("Base/ZEventDispatchCenter.lua")
  367. -- eventDispatchCenter:DispatchEvent(eventDispatchCenter.EventType.UPDATE_STATE_INFO, "更新成功!")
  368. -- require("Base/CoroutineHelper.lua"):Wait(0.1)
  369. print("----> Process usFinish Finish ...")
  370. require("Base/CoroutineHelper.lua"):Wait(0.1)
  371. -- jump scene
  372. -- LoadResource("Scenes/Start.unity")
  373. --LoadResource("scenes/gameb.unity")
  374. --CS.UnityEngine.SceneManagement.SceneManager.LoadScene("GameA")
  375. CS.UnityEngine.SceneManagement.SceneManager.LoadScene("Start")
  376. end
  377. function ZUpdateManager.ProcessUpdate()
  378. local currentState = ZUpdateManager._updateState
  379. print("ZUpdateManager.ProcessUpdate Start ... ... State: " .. tostring(currentState))
  380. if currentState == tUpdateState.usHotfixLua then
  381. require("Base/CoroutineHelper.lua"):Start(ZUpdateManager.tryHotfixLuaLogic)
  382. return
  383. end
  384. if currentState == tUpdateState.usGetResVersion then
  385. require("Base/CoroutineHelper.lua"):Start(ZUpdateManager.tryGetResVersionLogic)
  386. return
  387. end
  388. if currentState == tUpdateState.usCalcUpdateResInfo then
  389. require("Base/CoroutineHelper.lua"):Start(ZUpdateManager.tryCalcUpdateResInfoLogic)
  390. return
  391. end
  392. if currentState == tUpdateState.usDownloadResource then
  393. require("Base/CoroutineHelper.lua"):Start(ZUpdateManager.tryDownloadResourceLogic)
  394. return
  395. end
  396. if currentState == tUpdateState.usFinish then
  397. require("Base/CoroutineHelper.lua"):Start(ZUpdateManager.tryFinishLogic)
  398. return
  399. end
  400. print("ZUpdateManager.ProcessUpdate All Step Finish But Run ... ... Error State: " .. tostring(currentState))
  401. end
  402. return ZUpdateManager