public IEnumerator CachingBundle(string pBundleName, OnProgressByRatio onProgress = null, OnCompleteCaching pCallbackComplete = null, OnError pCallbackError = null) { yield return(new WaitForEndOfFrame()); string pSavePath = "file:///" + xSystem.AppAllocatePath(pBundleName); //string pSavePath = GetAssetBundleFilePath(pBundleName); using (WWW caching = WWW.LoadFromCacheOrDownload(pSavePath, this.serverBundleHash.GetAssetBundleHash(pBundleName))) { if (onProgress != null) { while (!caching.isDone && string.IsNullOrEmpty(caching.error)) { if (onProgress != null) { onProgress(caching.progress); } yield return(null); } } else { yield return(caching); } if (string.IsNullOrEmpty(caching.error)) { //Debug.Log( "Caching Complete" ); this.loadedBundles.Add(pBundleName, new STLoadedBundle(caching.assetBundle)); if (onProgress != null) { onProgress(caching.progress); } if (pCallbackComplete != null) { pCallbackComplete(pBundleName); } } else { if (pCallbackError != null) { pCallbackError(caching.error); } Debug.Log("CachingBundle:" + caching.error); } } }
private IEnumerator WriteLocalFile(string pBundleName, byte[] pBytes, OnProgressByRatio pOnProgress = null, OnError pOnError = null) { string pSavePath = xSystem.AppAllocatePath(pBundleName); using (System.IO.FileStream fs = new System.IO.FileStream(pSavePath, System.IO.FileMode.Create, System.IO.FileAccess.Write)) { using (System.IO.MemoryStream ms = new System.IO.MemoryStream(pBytes)) { int len = (int)ms.Length; int read = 0; byte[] buffer = new byte[1024 * 1024 * 10]; while ((read = ms.Read(buffer, 0, buffer.Length)) > 0) { try { fs.Write(buffer, 0, read); if (pOnProgress != null) { pOnProgress(read / len * 0.1f + 0.9f); } } catch (System.Exception e) { Debug.Log(e.ToString()); ms.Close(); fs.Close(); if (pOnError != null) { pOnError(e.ToString()); } yield break; } yield return(null); } ms.Close(); } fs.Close(); } Debug.Log("Write Bundle From Local Complete : " + pBundleName); }
public IEnumerator DownloadUpdateFromServer(string pBundleName, OnProgressByRatio onProgress = null, OnComplete pOnCompleted = null) { yield return(new WaitForEndOfFrame()); string pSavePath = GetAssetBundleFilePath(pBundleName); using (WWW caching = WWW.LoadFromCacheOrDownload(pSavePath, this.serverBundleHash.GetAssetBundleHash(pBundleName))) { if (onProgress != null) { while (!caching.isDone && string.IsNullOrEmpty(caching.error)) { if (onProgress != null) { Debug.Log("DownloadUpdateFromServer = " + caching.progress); onProgress(caching.progress); } yield return(null); } } else { yield return(caching); } if (string.IsNullOrEmpty(caching.error)) { this.loadedBundles.Add(pBundleName, new STLoadedBundle(caching.assetBundle)); if (onProgress != null) { onProgress(caching.progress); } if (pOnCompleted != null) { pOnCompleted(); } } else { Debug.Log("DownloadUpdateFromServer:" + caching.error); } } }
/* #region BundleDownLoad * /// <summary> * /// 개별 번들 다운로드 * /// </summary> * /// <param name="targetBundle">The target bundle.</param> * /// <param name="pOnProgress">The p on progress.</param> * /// <param name="pOnCompleted">The p on completed.</param> * /// <returns></returns> * public IEnumerator DownloadUpdateFromServer(string targetBundle, OnProgressByRatio pOnProgress = null, OnComplete pOnCompleted = null) * { * float duration = Time.time; * List<System.IO.FileInfo> needRemoveFiles = GetNeedRemoveFiles(); * for (int i = 0; i < needRemoveFiles.Count; i++) * { #if UNITY_EDITOR * Debug.Log("Delete :" + needRemoveFiles[i].Name); #endif * xSystem.RemoveFile(needRemoveFiles[i].FullName); * yield return null; * } #if UNITY_EDITOR * Debug.Log(targetBundle + " >>> Total Amount : " + needRemoveFiles.Count + " Remove TIme :<color=#ff0000>" + (Time.time - duration) + "</color>"); #endif * * duration = Time.time; * List<string> needUpdateFromServerBundles = GetNeedRequestServerBundles(targetBundle); * int totalAmount = needUpdateFromServerBundles.Count; * * yield return null; * * for (int i = 0; i < totalAmount; i++) * { * string bundleName = needUpdateFromServerBundles[i]; * byte[] pResultBytes = null; * // bundle download from webserver process * yield return StartCoroutine(DownLoadBundle(bundleName, pOnProgress, (pBytes) => * { * pResultBytes = pBytes; * })); * * while (pResultBytes == null) * yield return null; * * // downloaded bundle write local hard * yield return StartCoroutine(WriteLocalFile(bundleName, pResultBytes, pOnProgress, (pWriteError) => * { * Debug.Log("Error Incorrect : " + pWriteError); * ShowErrorMsg("ERROR_STORAGE_INSUFFICIENT", () => * { * Caching.CleanCache(); * Application.Quit(); * }); * })); * * // add hash bundle write complete file * if (this.localBundleHash.ContainsKey(bundleName)) * this.localBundleHash.Remove(bundleName); * * // update playerpref hash file * this.localBundleHash.Add(bundleName, this.serverBundleHash.GetAssetBundleHash(bundleName).ToString()); * * SaveBundleHashPrefs(PREFS_KEY, this.localBundleHash); * } * #if UNITY_EDITOR * Debug.Log("Total Amount : " + totalAmount + " Download & Write TIme :<color=#ff0000>" + (Time.time - duration) + "</color>"); #endif * //if (totalAmount > 0) * { * duration = Time.time; * //List<string> requestCachingList = new List<string>(this.serverBundleHash.GetAllAssetBundles()); * //requestCachingList.Clear(); * //requestCachingList.Add(targetBundle); * //bool isErrorOccur = false; * //for (int i = 0; i < requestCachingList.Count; i++) * { * yield return StartCoroutine(CachingBundle(targetBundle, null, (pCompleteName) => * { * //requestCachingList.Remove(pCompleteName); * }, (pErrorMsg) => * { * Debug.Log("Caching Disk Error :" + pErrorMsg); * //isErrorOccur = true; * })); * } * } * int remainMB = (int)(Caching.maximumAvailableDiskSpace / 1024 / 1024); * if (remainMB < 1) * { * Debug.Log("Remain Disk : " + remainMB + " Free : " + remainMB); * ShowErrorMsg("ERROR_STORAGE_INSUFFICIENT", () => * { * Caching.CleanCache(); * Application.Quit(); * }); * yield break; * } * #if UNITY_EDITOR * //Debug.Log("Total Amount : " + totalAmount + " Caching TIme :<color=#ff0000>" + (Time.time - duration) + "</color>"); * Debug.Log(" Used :" + (Caching.spaceOccupied / 1024 / 1024) + "||" + (Caching.spaceFree / 1024 / 1024) + "||" + (Caching.maximumAvailableDiskSpace / 1024 / 1024)); #endif * * if (pOnCompleted != null) * { * pOnCompleted(); * } * } #endregion */ #region BundleDownloadFromServer private IEnumerator DownLoadBundle(string pBundleName, OnProgressByRatio pOnProgress = null, OnCompleteBytes pOnCallbackComplete = null) { string versionAssetPath = GetAssetBundleFilePath(pBundleName); Debug.Log(versionAssetPath); using (WWW loader = new WWW(versionAssetPath)) { while (!loader.isDone && string.IsNullOrEmpty(loader.error)) { if (pOnProgress != null) { pOnProgress(loader.progress * 0.9f); } yield return(null); } if (!string.IsNullOrEmpty(loader.error)) { Debug.LogWarning("###### DownLoadBundle Error@@@!!! : " + loader.error + " Path : " + loader.url); ShowErrorMsg("ERROR_NET_INSTABILITY", () => { StartCoroutine(DownLoadBundle(pBundleName, pOnProgress, pOnCallbackComplete)); }, () => { Application.Quit(); }); yield break; } else { Debug.Log("DownLoad From Server Bundle Complete : " + pBundleName); } if (pOnCallbackComplete != null) { pOnCallbackComplete(loader.bytes); } } }
/// <summary> /// 메니페스트에 있는 모든 번들 다운로드 처리 /// </summary> /// <param name="pOnProgress">The p on progress.</param> /// <param name="pOnCompleted">The p on completed.</param> /// <returns></returns> public IEnumerator DownloadUpdateFromServer(OnProgressByRatio pOnProgress = null, OnComplete pOnCompleted = null) { float duration = Time.time; List <System.IO.FileInfo> needRemoveFiles = GetNeedRemoveFiles(); for (int i = 0; i < needRemoveFiles.Count; i++) { #if UNITY_EDITOR Debug.Log("Delete :" + needRemoveFiles[i].Name); #endif xSystem.RemoveFile(needRemoveFiles[i].FullName); yield return(null); } #if UNITY_EDITOR Debug.Log("Total Amount : " + needRemoveFiles.Count + " Remove TIme :<color=#ff0000>" + (Time.time - duration) + "</color>"); #endif duration = Time.time; List <string> needUpdateFromServerBundles = GetNeedRequestServerBundles(); int totalAmount = needUpdateFromServerBundles.Count; yield return(null); for (int i = 0; i < totalAmount; i++) { string bundleName = needUpdateFromServerBundles[i]; byte[] pResultBytes = null; // bundle download from webserver process yield return(StartCoroutine(DownLoadBundle(bundleName, pOnProgress, (pBytes) => { pResultBytes = pBytes; }))); while (pResultBytes == null) { yield return(null); } // downloaded bundle write local hard yield return(StartCoroutine(WriteLocalFile(bundleName, pResultBytes, pOnProgress, (pWriteError) => { Debug.Log("Error Incorrect : " + pWriteError); ShowErrorMsg("ERROR_STORAGE_INSUFFICIENT", () => { Caching.CleanCache(); Application.Quit(); }); }))); // add hash bundle write complete file if (this.localBundleHash.ContainsKey(bundleName)) { this.localBundleHash.Remove(bundleName); } // update playerpref hash file this.localBundleHash.Add(bundleName, this.serverBundleHash.GetAssetBundleHash(bundleName).ToString()); SaveBundleHashPrefs(PREFS_KEY, this.localBundleHash); } #if UNITY_EDITOR Debug.Log("Total Amount : " + totalAmount + " Download & Write TIme :<color=#ff0000>" + (Time.time - duration) + "</color>"); #endif duration = Time.time; List <string> requestCachingList = new List <string>(this.serverBundleHash.GetAllAssetBundles()); bool isErrorOccur = false; for (int i = 0; i < requestCachingList.Count; i++) { StartCoroutine(CachingBundle(requestCachingList[i], null, (pCompleteName) => { requestCachingList.Remove(pCompleteName); }, (pErrorMsg) => { Debug.Log("Caching Disk Error :" + pErrorMsg); isErrorOccur = true; })); } int cachingtotalAmount = requestCachingList.Count; int cachingAmount = 0; while (requestCachingList.Count > 0) { if (cachingAmount != cachingtotalAmount - requestCachingList.Count) { cachingAmount = cachingtotalAmount - requestCachingList.Count; pOnProgress((float)cachingAmount / cachingtotalAmount); } if (isErrorOccur) { Debug.Log("Error Incorrect : " + isErrorOccur); //UISceneController.Instance.AlertBox(true, new AlertBox.AlertParam(AlertBox.AlertType.SELECTABLE, new Vector2(700, 400), NetTable.GetSystemTxt("ERROR_NET_SOCKET_NORESPONSE")), (pResult) => { // Application.Quit(); //}); yield break; } yield return(null); } int remainMB = (int)(Caching.maximumAvailableDiskSpace / 1024 / 1024); if (remainMB < 1) { Debug.Log("Remain Disk : " + remainMB + " Free : " + remainMB); ShowErrorMsg("ERROR_STORAGE_INSUFFICIENT", () => { Caching.CleanCache(); Application.Quit(); }); yield break; } #if UNITY_EDITOR //Debug.Log("Total Amount : " + totalAmount + " Caching TIme :<color=#ff0000>" + (Time.time - duration) + "</color>"); Debug.Log(" Used :" + (Caching.spaceOccupied / 1024 / 1024) + "||" + (Caching.spaceFree / 1024 / 1024) + "||" + (Caching.maximumAvailableDiskSpace / 1024 / 1024)); #endif if (pOnCompleted != null) { pOnCompleted(); } }
public IEnumerator DownloadUpdateFromServer(string pBundleName, OnProgressByRatio onProgress = null, OnComplete pOnCompleted = null, OnError pOnError = null) { yield return(new WaitForEndOfFrame()); string pSavePath = GetAssetBundleFilePath(pBundleName); Debug.Log(pSavePath); if (IsBundleCachingVersionCheck(pBundleName)) { onProgress = null; if (GetBundle(pBundleName)) { if (pOnCompleted != null) { pOnCompleted(); } yield break; } } using (WWW caching = WWW.LoadFromCacheOrDownload(pSavePath, this.serverBundleHash.GetAssetBundleHash(pBundleName))) { if (onProgress != null) { while (!caching.isDone && string.IsNullOrEmpty(caching.error)) { //yield return new WaitForSeconds(2f); //if (caching != null) { caching.Dispose(); } if (onProgress != null) { //Debug.Log(pBundleName + " DownloadUpdateFromServer = " + caching.progress); onProgress(caching.progress); } yield return(new WaitForEndOfFrame()); } } else { //if (caching != null) { caching.Dispose(); } yield return(caching); } if (string.IsNullOrEmpty(caching.error)) { this.loadedBundles.Add(pBundleName, new STLoadedBundle(caching.assetBundle)); Debug.Log("Complete DownloadUpdateFromServer = " + caching.progress); // add hash bundle write complete file if (this.localBundleHash.ContainsKey(pBundleName)) { this.localBundleHash.Remove(pBundleName); } // update playerpref hash file this.localBundleHash.Add(pBundleName, this.serverBundleHash.GetAssetBundleHash(pBundleName).ToString()); SaveBundleHashPrefs(PREFS_KEY, this.localBundleHash); if (onProgress != null) { onProgress(caching.progress); } if (pOnCompleted != null) { pOnCompleted(); } } else { Debug.Log("DownloadUpdateFromServer:" + caching.error); if (pOnError != null) { pOnError(caching.error); } } } }