ZUpdateManager.lua.txt 24 KB

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