static IEnumerator WhenAllE <T>(IEnumerable <Util.Future <T> > futures, Util.Promise <T[]> promise) { var futureArray = futures.ToArray(); Exception exception = null; foreach (var future in futureArray) { foreach (var v in future) { yield return(v); } if (future.HasException) { exception = future.Exception; } } if (exception != null) { promise.Exception = exception; } else { promise.Result = futureArray.Select(x => x.Result).ToArray(); } }
IEnumerator StartE(MonoBehaviour mono, Util.Promise <Util.Unit> promise) { Debug.Log(resourceUrl); using (var www = new WWW(resourceUrl)) { yield return(www); if (www.error != null) { throw new Exception(www.error); } resourceInfoDic = LitJson.JsonMapper.ToObject <Dictionary <string, ResourceInfo> >(www.text); } // ディレクトリが無ければ作る if (!Directory.Exists(storeBasePath)) { Directory.CreateDirectory(storeBasePath); #if !UNITY_EDITOR && UNITY_IOS UnityEngine.iOS.Device.SetNoBackupFlag(storeBasePath); #endif } bulkDownloader = new BulkDownloader(); foreach (var kv in resourceInfoDic) { var url = downloadBaseUrl + "/" + kv.Value.name; var storePath = storeBasePath + "/" + kv.Value.name; Debug.Log(string.Format("Register Masterdata name={0} path={1} url={2}", kv.Key, storePath, url)); bulkDownloader.Add(url, kv.Value.size, storePath); } var future = bulkDownloader.Start(mono); foreach (var v in future) { yield return(v); } promise.Result = future.Result; Debug.Log("Completed Masterdata Downloading"); }
IEnumerator StartE(Util.Promise <Util.Unit> promise) { while (true) { // ダウンロード可能なデータが無ければ終了する if (downloadInfoSet.Count == 0) { promise.Result = Util.Unit.Value; yield break; } // エラーが発生しているなら終了する var errorInfo = downloadInfoSet.FirstOrDefault(x => x.state == DownloadInfo.State.Error); if (errorInfo != null) { promise.Exception = errorInfo.exception; yield break; } var downloadInfo = GetNext(); if (downloadInfo == null) { // ダウンロード可能なデータが見つからないので少し待ってリトライ yield return(new WaitForSeconds(0.1f)); continue; } int retry = 0; while (true) { Exception exception = null; downloadInfo.state = DownloadInfo.State.Downloading; downloadInfo.www = new WWW(downloadInfo.Url); try { yield return(downloadInfo.www); if (downloadInfo.www.error != null) { exception = new Exception(downloadInfo.www.error); } else { try { using (var file = File.OpenWrite(downloadInfo.StorePath)) { file.Write(downloadInfo.www.bytes, 0, downloadInfo.www.bytes.Length); } } catch (Exception) { try { File.Delete(downloadInfo.StorePath); } catch (Exception) { } exception = new Exception(string.Format("failed to write file: {0}, Url={1}", downloadInfo.StorePath, downloadInfo.Url)); } } } finally { downloadInfo.www.Dispose(); downloadInfo.www = null; } if (exception != null) { if (retry < BulkDownloader.MaxRetry) { // 規定回数まではリトライし続ける retry += 1; continue; } else { downloadInfo.state = DownloadInfo.State.Error; downloadInfo.exception = exception; } } else { downloadInfo.state = DownloadInfo.State.Completed; } break; } // ダウンロードが完了したので downloadInfoSet から削除 downloadInfoSet.Remove(downloadInfo); } }