bool TryLoadFromLocalStorage(AssetBundleRecord assetBundleRecord, Action <LoaderResult> onFinished) { var localPath = AssetBundleUtility.GetLocalStoragePath(assetBundleRecord.AssetBundleName); if (!File.Exists(localPath)) { return(false); } Debug.Log($"load from local storage: {localPath}"); byte[] bytes; try { using (FileStream fileStream = new FileStream(localPath, FileMode.Open, FileAccess.Read)) { bytes = new byte[fileStream.Length]; fileStream.Read(bytes, 0, bytes.Length); fileStream.Close(); } } catch (Exception e) { Debug.LogError(e); return(false); } var result = new LoaderResult(assetBundleRecord, bytes, null); onFinished.Invoke(result); return(true); }
public IEnumerator LoadAsync(AssetBundleRecord assetBundleRecord, Action <LoaderResult> onFinished) { string path = $"{url}{assetBundleRecord.AssetBundleName}"; Debug.Log($"download from web: {path}"); var request = UnityWebRequest.Get(path); request.downloadHandler = new DownloadHandlerBuffer(); request.SendWebRequest(); yield return(null); while (!request.isDone) { yield return(null); } if (request.isHttpError || request.isNetworkError) { var errorResult = new LoaderResult(assetBundleRecord, null, AssetBundleDownloadErrorType.Network); onFinished.Invoke(errorResult); yield break; } byte[] bytes = request.downloadHandler.data; var localPath = AssetBundleUtility.GetLocalStoragePath(assetBundleRecord.AssetBundleName); try { var dirPath = Path.GetDirectoryName(localPath); if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } using (FileStream fileStream = new FileStream(localPath, FileMode.OpenOrCreate, FileAccess.Write)) { fileStream.Write(bytes, 0, bytes.Length); fileStream.Close(); } } catch (Exception e) { Debug.LogError(e); var errorResult = new LoaderResult(assetBundleRecord, bytes, AssetBundleDownloadErrorType.IO); onFinished.Invoke(errorResult); yield break; } var successResult = new LoaderResult(assetBundleRecord, bytes, null); onFinished.Invoke(successResult); }
IEnumerator LoadTableAsync(bool forciblyDownload) { isTableLoading = true; #if UNITY_EDITOR if (localMode) { string localFilePath = GetLocalFilePath(tablePath); ResourceRequest resourceRequest = Resources.LoadAsync(localFilePath); while (!resourceRequest.isDone) { yield return(null); } var localAsset = resourceRequest.asset as UnityEngine.Object; if (localAsset != null) { while (linker.IsPaused || TimeUtility.DropFrameExists()) { yield return(null); } OnTableAssetBundleLoaded(localAsset); yield break; } } #endif if (forciblyDownload) { // delete table cache string localTablePath = AssetBundleUtility.GetLocalStoragePath(tablePath); try { if (File.Exists(localTablePath)) { File.Delete(localTablePath); } } catch (Exception e) { Debug.LogError(e); errorReceiver.OnError(AssetBundleErrorCode.FailureToDeleteTable, $"cannot delete table: {tablePath}"); yield break; } } linker.LoadTableAsset(tablePath, OnTableAssetBundleLoaded); }