void Unzip(string source, string dest) { m_State = ePatchState.Processing; unzip_source_path = source; if (m_DeltaVersion > 0) { unzip_dest_path = dest + delta_filename; } else { unzip_dest_path = dest + AssetManager.AssetBundleFilename; } Debug.Log(unzip_source_path); Debug.Log(unzip_dest_path); if (Directory.Exists(dest) == false) { Directory.CreateDirectory(dest); } #if !UNITY_WEBPLAYER uncompressed_bytes[0] = 0; uncompressed_size = LZ4.uncompressedSize(unzip_source_path); //Debug.LogFormat("[Uncompressed Byte:{0}MB] [Available Size:{1}MB]", uncompressed_size / MAGABYTE, GetStorageFreeSpace()); if (uncompressed_size / MEGABYTE > GetStorageFreeSpace()) { Popup.Instance.ShowCallbackKey(new PopupCallback.Callback(new Action(Start), null), string.Format("Not Enough Free Space! \n [Need:{0}] [Now Free Space:{1}]", uncompressed_size / MEGABYTE, GetStorageFreeSpace())); return; } #if USE_THREAD Thread th = new Thread(UnZipThread); th.Start(); #else UnZipThread(); #endif #endif }
// Update is called once per frame void Update() //更新文本和进度条 { switch (m_State) { case ePatchState.Ready: m_ProgressBar.value = 0f; m_DownloadLabel.text = Localization.Get("ReadytoDownload"); break; case ePatchState.Download: m_ProgressBar.value = m_ProgressValue; m_DownloadLabel.text = string.Format("{0} : {1:n1}%", m_State, m_ProgressBar.value * 100); break; case ePatchState.DownloadFull: m_DeltaVersion = 0; InitDownload(); break; case ePatchState.Processing: //if (lzma.trueTotalFiles == 0) return; m_ProgressBar.value = (float)uncompressed_bytes[0] / uncompressed_size; m_DownloadLabel.text = string.Format("{0} : {1:n1}%", m_State, m_ProgressBar.value * 100); if (uncompressed_bytes[0] == uncompressed_size) { //Debug.LogFormat("[m_DeltaVersion{0}]", m_DeltaVersion); File.Delete(unzip_source_path); if (m_DeltaVersion > 0) { ApplyDelta(); m_State = ePatchState.ApplyDelta; } else { m_State = ePatchState.PatchEnd; } } break; case ePatchState.ApplyDelta: m_ProgressBar.value = m_DeltaApplyProgress.Percent; if (m_DeltaApplyProgress.IsFail == true) { m_State = ePatchState.DownloadFull; } else if (m_DeltaApplyProgress.IsSuccess == true) { m_State = ePatchState.PatchEnd; } else { if (m_SkipHashCheck == true && m_DeltaApplyProgress.IsEnd) { m_DownloadLabel.text = string.Format("{0} : {1:n1}%", "CheckFile", m_ProgressBar.value * 100); } else { m_DownloadLabel.text = string.Format("{0} : {1:n1}%", m_State, m_ProgressBar.value * 100); } } break; case ePatchState.PatchEnd: m_State = ePatchState.LoadScene; SHSavedData.Instance.BundleVersion = m_BundleVersion; UnityEngine.SceneManagement.SceneManager.LoadScene("title", UnityEngine.SceneManagement.LoadSceneMode.Single); //进入title场景 Debug.LogFormat("PatchComplete! [m_BundleVersion:{0}] [m_DeltaVersion:{1}]", m_BundleVersion, m_DeltaVersion); break; } }
void OnDownload(int bundleVersion) { #if UNITY_EDITOR //是否在 当前的unity编辑器中运行 Debug.Log("OnDownload"); #endif HTTPManager.IsCachingDisabled = true; //是否正在缓存 HTTPManager.IsCookiesEnabled = false; // HTTPManager.ConnectTimeout = TimeSpan.FromSeconds(5f); HTTPManager.RequestTimeout = TimeSpan.FromSeconds(120f); string status; AssetManager.bundleVersion = bundleVersion; m_DownloadDir = AssetManager.AssetPath; if (Directory.Exists(m_DownloadDir) == false) { Directory.CreateDirectory(m_DownloadDir); } if (m_DeltaVersion > 0) { #if UNITY_ANDROID m_DownloadFilename = string.Format("{0}_Android.zip", m_DeltaVersion); #elif UNITY_IOS m_DownloadFilename = string.Format("{0}_iOS.zip", m_DeltaVersion); #else m_DownloadFilename = string.Format("{0}_StandaloneWindows.zip1", m_DeltaVersion); #endif } else { #if UNITY_ANDROID m_DownloadFilename = "Android.zip"; #elif UNITY_IOS m_DownloadFilename = "iOS.zip"; #else m_DownloadFilename = "StandaloneWindows.zip"; #endif } string URL = GetDownloadURL(m_DownloadFilename); //生成下载链接 if (File.Exists(m_DownloadDir + m_DownloadFilename) == true) { File.Delete(m_DownloadDir + m_DownloadFilename); //删除以前的包 } m_Request = new HTTPRequest(new Uri(URL), false, true, (req, resp) => //这是一个lambda 表达式 参数 是req 和resp 的匿名函数 这是一个http 的请求 { //check available space if (pre_download_size_check == false) { pre_download_size_check = true; //Debug.LogFormat("[Download File Size:{0}MB] [AvailableSize:{1}MB]", req.DownloadLength / MAGABYTE, GetStorageFreeSpace()); if (((req.DownloadLength / MEGABYTE) < GetStorageFreeSpace()) == false) //判断剩余的控件够不够 如果不够 { //弹出提示框 Popup.Instance.ShowCallbackKey(new PopupCallback.Callback(new Action(Start), null), string.Format("Not Enough Free Space! \n [Need:{0}] [Now Free Space:{1}]", req.DownloadLength / MEGABYTE, GetStorageFreeSpace())); req.Abort(); //放弃更新 m_Request = null; return; } } switch (req.State) //判断返回的状态 { // The request is currently processed. With UseStreaming == true, we can get the streamed fragments here case HTTPRequestStates.Processing: //如果是正在进行 // Get the fragments, and save them ProcessFragments(resp.GetStreamedFragments()); //根据返回的数据创建文件 m_ProgressValue = req.Downloaded / (float)req.DownloadLength; //更新进度条 break; // The request finished without any problem. case HTTPRequestStates.Finished: //完成 if (resp.IsSuccess) { // Save any remaining fragments ProcessFragments(resp.GetStreamedFragments()); //根据返回的数据创建文件 // Completly finished if (resp.IsStreamingFinished) //是不是传输完成 { Unzip(); //解压 m_Request = null; } } else { if (resp.StatusCode == 404 && m_DeltaVersion > 0) { m_State = ePatchState.DownloadFull; } else { Popup.Instance.ShowMessage("{0} {1}", resp.StatusCode, resp.Message); status = string.Format("Request finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2}", resp.StatusCode, resp.Message, resp.DataAsText); Debug.LogWarning(URL); Debug.LogWarning(status); } m_Request = null; } break; // The request finished with an unexpected error. The request's Exception property may contain more info about the error. case HTTPRequestStates.Error: //http访问出错 status = "Request Finished with Error! " + (req.Exception != null ? (req.Exception.Message + "\n" + req.Exception.StackTrace) : "No Exception"); Debug.LogError(status); Popup.Instance.ShowMessage(req.Exception != null ? req.Exception.Message : "HTTPRequestStates.Error"); m_Request = null; break; // The request aborted, initiated by the user. case HTTPRequestStates.Aborted: //放弃 status = "Request Aborted!"; Debug.LogWarning(status); Popup.Instance.ShowMessage(status); m_Request = null; break; // Ceonnecting to the server is timed out. case HTTPRequestStates.ConnectionTimedOut: //htp连接超时 status = "Connection Timed Out!"; Debug.LogError(status); Popup.Instance.ShowMessage(status); m_Request = null; break; // The request didn't finished in the given time. case HTTPRequestStates.TimedOut: //http访问超时 status = "Processing the request Timed Out!"; Debug.LogError(status); Popup.Instance.ShowMessage(status); m_Request = null; break; } }); //// Are there any progress, that we can continue? //if (PlayerPrefs.HasKey("DownloadProgress")) // // Set the range header // request.SetRangeHeader(PlayerPrefs.GetInt("DownloadProgress")); //else // // This is a new request // PlayerPrefs.SetInt("DownloadProgress", 0); // If we are writing our own file set it true(disable), so don't duplicate it on the filesystem m_Request.DisableCache = true; // We want to access the downloaded bytes while we are still downloading m_Request.UseStreaming = true; // Set a reasonable high fragment size. Here it is 5 megabytes. m_Request.StreamFragmentSize = HTTPResponse.MinBufferSize; // Start Processing the request m_Request.Send(); m_State = ePatchState.Download; }