コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        public IPromise AddReward()
        {
            Promise promise = Promise.Create();

            Load().Then(r => r.AddReward().Channel(promise)).Catch(e => promise.Reject(e));
            return(promise);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        public IPromise WaitForMainThread()
        {
            Promise promise = Promise.Create();

            EnqueueForMainThread(() => promise.Resolve());
            return(promise);
        }
コード例 #7
0
        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);
        }
コード例 #8
0
ファイル: AppBoot.cs プロジェクト: studentutu/swift-framework
        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();
        }
コード例 #9
0
        protected override IPromise GetInitPromise()
        {
            Promise initPromise = Promise.Create();

            InitNextModule(0, initPromise, App);

            return(initPromise);
        }
コード例 #10
0
        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);
        }
コード例 #11
0
 protected override IPromise GetInitPromise()
 {
     downloadPromise = Promise.Create();
     config          = GetModuleConfig <LocalizationConfig>();
     SetLanguage();
     sheets.AddRange(AssetCache.GetAssets <LocalizationSheet>());
     LoadNextSheet(0);
     return(downloadPromise);
 }
コード例 #12
0
ファイル: Clock.cs プロジェクト: studentutu/swift-framework
        protected override IPromise GetInitPromise()
        {
            Promise result = Promise.Create();

            GetUnixNow().Done(now =>
            {
                result.Resolve();
            });
            return(result);
        }
コード例 #13
0
        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);
        }
コード例 #14
0
ファイル: Module.cs プロジェクト: studentutu/swift-framework
        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);
        }
コード例 #15
0
ファイル: IPrice.cs プロジェクト: studentutu/swift-framework
        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);
        }
コード例 #16
0
        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);
        }
コード例 #17
0
        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());
        }
コード例 #18
0
        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);
        }
コード例 #19
0
        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);
        }
コード例 #20
0
        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);
        }
コード例 #21
0
        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);
        }
コード例 #22
0
        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);
        }
コード例 #23
0
ファイル: Promise.cs プロジェクト: studentutu/swift-framework
        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);
        }
コード例 #24
0
        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);
        }
コード例 #25
0
        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);
        }
コード例 #26
0
        public IPromise WaitUntil(Func <bool> condition)
        {
            Promise promise = Promise.Create();

            return(StartNew(WaitUntilCoroutine(condition, promise), promise));
        }
コード例 #27
0
ファイル: LinkTo.cs プロジェクト: dimaswift/swift-framework
        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);
        }
コード例 #28
0
        public IPromise Evaluate(float duration, Action <float> callback)
        {
            Promise promise = Promise.Create();

            return(StartNew(EvaluateCoroutine(duration, callback, promise), promise));
        }
コード例 #29
0
        public IPromise WaitFor(float seconds)
        {
            Promise promise = Promise.Create();

            return(StartNew(WaitForCoroutine(seconds, promise), promise));
        }
コード例 #30
0
        public IPromise WaitForAll(IEnumerable <Action> actions)
        {
            Promise promise = Promise.Create();

            return(StartNew(WaitForAllCoroutine(actions, promise), promise));
        }