private IPromise <string[]> Download() { Promise <string[]> promise = Promise <string[]> .Create(); App.Core.Net.Get(publishedGoogleSheetUrl, downloadTimeout).Then(body => { if (string.IsNullOrEmpty(body) == false) { try { if (File.Exists(CachedSheetPath)) { File.Delete(CachedSheetPath); } File.WriteAllText(CachedSheetPath, body); } catch (Exception exception) { Debug.LogError($"Cannot write localization file: {exception.Message}"); } promise.Resolve(body.Split('\n')); } else { promise.Resolve(GetCachedSheet()); } }) .Catch(e => { promise.Resolve(GetCachedSheet()); }); return(promise); }
public IPromise <byte[]> DownloadRaw(string url, Action <long> progressBytes = null) { Promise <byte[]> result = Promise <byte[]> .Create(); UnityWebRequest request = UnityWebRequest.Get(url); var operation = request.SendWebRequest(); StartCoroutine(ProgressRoutine(result, operation, progressBytes)); operation.completed += response => { if (request.isHttpError || request.isNetworkError) { result.Reject(new Exception(request.error)); } else { if (request.downloadHandler == null || request.downloadHandler.data == null) { result.Reject(new Exception("Empty response")); } else { result.Resolve(request.downloadHandler.data); } } }; return(result); }
public IPromise AddReward() { Promise promise = Promise.Create(); Load().Then(r => r.AddReward().Channel(promise)).Catch(e => promise.Reject(e)); return(promise); }
public IPromise <Texture2D> DownloadImage(string url) { Promise <Texture2D> result = Promise <Texture2D> .Create(); UnityWebRequest request = UnityWebRequestTexture.GetTexture(url); var operation = request.SendWebRequest(); operation.completed += response => { if (request.isHttpError || request.isNetworkError) { result.Reject(new Exception(request.error)); } else { DownloadHandlerTexture handler = request.downloadHandler as DownloadHandlerTexture; if (handler == null || handler.texture == null) { result.Reject(new Exception("Empty response")); } else { result.Resolve(handler.texture); } } }; return(result); }
public IPromise <string> Get(string url, int timeoutSeconds = 5) { Promise <string> result = Promise <string> .Create(); UnityWebRequest request = UnityWebRequest.Get(url); request.timeout = timeoutSeconds; var operation = request.SendWebRequest(); operation.completed += response => { if (request.isHttpError || request.isNetworkError) { result.Reject(new Exception(request.error)); } else { if (string.IsNullOrEmpty(request.downloadHandler.text)) { result.Reject(new Exception("Empty response")); } else { result.Resolve(request.downloadHandler.text); } } }; return(result); }
public IPromise WaitForMainThread() { Promise promise = Promise.Create(); EnqueueForMainThread(() => promise.Resolve()); return(promise); }
public static IPromise GetPromise(this AsyncOperationHandle handle) { Promise promise = Promise.Create(); if (handle.IsDone) { if (handle.Status == AsyncOperationStatus.Succeeded) { promise.Resolve(); } else { promise.Reject(handle.OperationException); } } else { handle.Completed += o => { if (o.Status == AsyncOperationStatus.Succeeded) { promise.Resolve(); } else { promise.Reject(handle.OperationException); } }; } return(promise); }
private IEnumerator RestartRoutine() { bootPromise = Promise.Create(); OnAppWillBeRestarted(); App.Core.Unload(); while (IsReadyToRestart() == false) { yield return(null); } Resources.UnloadUnusedAssets(); GC.Collect(); App.Create(this, GetLogger(), new ModuleFactory(), debugMode).Then(() => { OnInitialized(); OnAppInitialized(); if (onAppInitialized.HasValue) { onAppInitialized.Value.Invoke(); } bootPromise.Resolve(); isRestarting = false; }) .LogException(); }
protected override IPromise GetInitPromise() { Promise initPromise = Promise.Create(); InitNextModule(0, initPromise, App); return(initPromise); }
public IPromise <T> CreateAsync <T>(ViewLink link) where T : class, IView { Promise <T> promise = Promise <T> .Create(); CreateAsync <T>(link, r => promise.Resolve(r), e => promise.Reject(e)); return(promise); }
protected override IPromise GetInitPromise() { downloadPromise = Promise.Create(); config = GetModuleConfig <LocalizationConfig>(); SetLanguage(); sheets.AddRange(AssetCache.GetAssets <LocalizationSheet>()); LoadNextSheet(0); return(downloadPromise); }
protected override IPromise GetInitPromise() { Promise result = Promise.Create(); GetUnixNow().Done(now => { result.Resolve(); }); return(result); }
public IPromise <T> Load <T>(ViewLink stageLink) where T : class, IStage { if (IsLoading.Value) { return(Promise <T> .Rejected(new InvalidOperationException("Already loading stage"))); } isLoading.SetValue(true); Promise <T> promise = Promise <T> .Create(); Promise loadPromise = Promise.Create(); App.Core.MakeTransition(loadPromise, () => { if (ActiveStageLink != null && ActiveStageLink.GetPath() != stageLink.GetPath()) { history.Push(ActiveStageLink); ActiveStage.CloseStage(this); } void SetActive(ViewLink link, T stage) { ActiveStage = stage; ActiveStageLink = link; stage.OpenStage(this).Done(() => { isLoading.SetValue(false); loadPromise.Resolve(); promise.Resolve(stage); if (loadedStages.ContainsKey(link) == false) { loadedStages.Add(link, stage); } OnStageLoaded(link); }); } if (loadedStages.ContainsKey(stageLink)) { T loaded = loadedStages[stageLink] as T; SetActive(stageLink, loaded); } else { App.Core.Views.CreateAsync <T>(stageLink).Done(stage => { SetActive(stageLink, stage); }); } }); return(promise); }
public IPromise <T> GetModuleConfigAsync <T>() where T : ModuleConfig { Promise <T> promise = Promise <T> .Create(); configLink.Load().Then(c => { promise.Resolve(c as T); }) .Catch(e => promise.Reject(e)); return(promise); }
public IPromise <bool> Pay(float discount = 0) { Promise <bool> promise = Promise <bool> .Create(); Load().Then(p => { p.Pay(amount, discount).Channel(promise); }).Catch(e => { Debug.Log($"Cannot load price {GetPath()}: {e.Message}"); promise.Resolve(false); }); return(promise); }
public IPromise <T> Instantiate() { Promise <T> promise = Promise <T> .Create(); Load().Then(prefab => { GameObject instance = UnityEngine.Object.Instantiate(cachedGameObject); promise.Resolve(instance.GetComponent <T>()); }) .Catch(e => promise.Reject(e)); return(promise); }
public override IPromise <string[]> LoadSheet(Action <string[]> onLazeLoad) { Promise <string[]> promise = Promise <string[]> .Create(); if (downloadSheetOnLoad == false) { Download().Done(onLazeLoad); promise.Resolve(GetCachedSheet()); return(promise); } return(Download()); }
public IPromise <TScriptable> LoadOrCreate <TScriptable>() where TScriptable : ScriptableObject, T { Promise <TScriptable> result = Promise <TScriptable> .Create(); Load().Then(value => result.Resolve(value as TScriptable), e => { TScriptable instance = ScriptableObject.CreateInstance <TScriptable>(); instance.hideFlags = HideFlags.DontSave; result.Resolve(instance); }); return(result); }
public IPromise <bool> Load(LoadSceneMode mode) { Promise <bool> promise = Promise <bool> .Create(); #if USE_ADDRESSABLES Addressables.LoadSceneAsync(Path, mode, true).GetPromise().Then(scene => { sceneInstance = scene; promise.Resolve(true); }) .Catch(e => promise.Resolve(false)); #else SceneManager.LoadSceneAsync("Resources/" + Path, mode).GetPromise().Then(() => promise.Resolve(true)).Catch(e => promise.Resolve(false)); #endif return(promise); }
public static IPromise <T> GetPromise <T>(this ResourceRequest resourceRequest) where T : UnityEngine.Object { Promise <T> promise = Promise <T> .Create(); resourceRequest.completed += r => { if (r.isDone) { promise.Resolve(resourceRequest.asset as T); } else { promise.Reject(new Exception("Cannot load resource")); } }; return(promise); }
public IPromise <T> GetModuleConfigAsync <T>() where T : ModuleConfig { Promise <T> promise = Promise <T> .Create(); if (configLink == null) { promise.Reject(new KeyNotFoundException("Module config not found")); return(promise); } configLink.Load().Then(c => { promise.Resolve(c as T); }) .Catch(e => promise.Reject(e)); return(promise); }
public static IPromise GetPromise(this AsyncOperation asyncOperation) { Promise promise = Promise.Create(); asyncOperation.completed += r => { if (r.isDone) { promise.Resolve(); } else { promise.Reject(new Exception()); } }; return(promise); }
public IPromise Then(Action <T> onSuccess = null, Action <Exception> onError = null) { Promise promise = Promise.Create(); Progress(p => { promise.ReportProgress(p); }); Done(r => { onSuccess?.Invoke(r); promise.Resolve(); }); Catch(e => { onError?.Invoke(e); promise.Reject(e); }); return(promise); }
public IPromise Init() { Promise promise = Promise.Create(); AssetCache.PreloadAll(AddrLabels.Module).Always(manifests => { modules.Clear(); foreach (Object manifest in manifests) { if (manifest is ModuleManifest moduleManifest) { if (moduleManifest.State == ModuleState.Enabled) { modules.Add(moduleManifest); } } } promise.Resolve(); }); return(promise); }
public virtual IPromise <T> Load() { if (loadPromise != null) { return(loadPromise); } loadPromise = Promise <T> .Create(); if (IsGenerated()) { loadPromise.Resolve(Value); return(loadPromise); } if (HasValue == false) { loadPromise.Reject(new EntryPointNotFoundException($"Link doesn't have any value: {GetPath()}")); return(loadPromise); } #if USE_ADDRESSABLES if (loaded || AssetCache.Loaded(Path)) { loadPromise.Resolve(Value); return(loadPromise); } loadHandle = Addressables.LoadAssetAsync <ScriptableObject>(Path); loadHandle.Value.Completed += a => { if (Loaded == false) { if (a.Status == AsyncOperationStatus.Succeeded) { cachedAsset = a.Result as T; Initialize(cachedAsset); loaded = cachedAsset != null; loadPromise.Resolve(cachedAsset); } else { loadPromise.Reject(new EntryPointNotFoundException($"Cannot load value from link: {Path}")); } } }; #else if (loaded) { loadPromise.Resolve(Value); return(loadPromise); } loadHandle = Resources.LoadAsync <ScriptableObject>(Path); loadHandle.completed += a => { if (Loaded == false) { if (a.isDone) { cachedAsset = loadHandle.asset as T; Initialize(cachedAsset); loaded = cachedAsset != null; loadPromise.Resolve(cachedAsset); } else { loadPromise.Reject(new EntryPointNotFoundException($"Cannot load value from link: {Path}")); } } }; #endif return(loadPromise); }
public IPromise WaitUntil(Func <bool> condition) { Promise promise = Promise.Create(); return(StartNew(WaitUntilCoroutine(condition, promise), promise)); }
public virtual IPromise <T> Load() { if (loadPromise != null) { return(loadPromise); } loadPromise = Promise <T> .Create(); if (HasValue == false) { loadPromise.Reject(new EntryPointNotFoundException("Link doesn't have any value")); return(loadPromise); } #if USE_ADDRESSABLES if (loaded || AssetCache.Loaded(Path)) { loadPromise.Resolve(Value); return(loadPromise); } if (IsGenerated()) { cachedAsset = App.Core.Storage.Load <T>(this); Initialize(cachedAsset); loaded = cachedAsset != null; loadPromise.Resolve(cachedAsset); return(loadPromise); } loadHandle = Addressables.LoadAssetAsync <T>(Path); loadHandle.Value.Completed += a => { if (Loaded == false) { if (a.Status == AsyncOperationStatus.Succeeded) { cachedAsset = a.Result; Initialize(cachedAsset); loaded = cachedAsset != null; loadPromise.Resolve(a.Result); } else { var e = new EntryPointNotFoundException($"Cannot load value from link: {Path}"); UnityEngine.Debug.LogException(e); loadPromise.Reject(e); } } }; #else if (loaded) { loadPromise.Resolve(Value); return(loadPromise); } if (IsGenerated()) { cachedAsset = App.Core.Storage.Load <T>(this); Initialize(cachedAsset); loaded = cachedAsset != null; loadPromise.Resolve(cachedAsset); return(loadPromise); } loadHandle = Resources.LoadAsync <T>(Path); loadHandle.completed += r => { if (Loaded == false && loadHandle.isDone && loadHandle.asset) { cachedAsset = loadHandle.asset as T; Initialize(cachedAsset); loaded = cachedAsset != null; loadPromise.Resolve(cachedAsset); } else { loadPromise.Reject(new EntryPointNotFoundException($"Cannot load resource of type {typeof(T)} at path {Path}")); } }; #endif return(loadPromise); }
public IPromise Evaluate(float duration, Action <float> callback) { Promise promise = Promise.Create(); return(StartNew(EvaluateCoroutine(duration, callback, promise), promise)); }
public IPromise WaitFor(float seconds) { Promise promise = Promise.Create(); return(StartNew(WaitForCoroutine(seconds, promise), promise)); }
public IPromise WaitForAll(IEnumerable <Action> actions) { Promise promise = Promise.Create(); return(StartNew(WaitForAllCoroutine(actions, promise), promise)); }