Exemple #1
0
        public virtual IProgressResult <float, T> LoadAssetAsync <T>(string path) where T : Object
        {
            try
            {
                if (string.IsNullOrEmpty(path))
                {
                    throw new System.ArgumentNullException("path", "The path is null or empty!");
                }

                ProgressResult <float, T> result   = new ProgressResult <float, T>();
                AssetPathInfo             pathInfo = this.pathInfoParser.Parse(path);
                if (pathInfo == null)
                {
                    throw new System.Exception(string.Format("Not found the AssetBundle or parses the path info '{0}' failure.", path));
                }

                T asset = this.GetCache <T>(path);
                if (asset != null)
                {
                    result.UpdateProgress(1f);
                    result.SetResult(asset);
                    return(result);
                }

                IProgressResult <float, IBundle> bundleResult = this.LoadBundle(pathInfo.BundleName);
                float weight = bundleResult.IsDone ? 0f : DEFAULT_WEIGHT;
                bundleResult.Callbackable().OnProgressCallback(p => result.UpdateProgress(p * weight));
                bundleResult.Callbackable().OnCallback((r) =>
                {
                    if (r.Exception != null)
                    {
                        result.SetException(r.Exception);
                        return;
                    }

                    using (IBundle bundle = r.Result)
                    {
                        IProgressResult <float, T> assetResult = bundle.LoadAssetAsync <T>(pathInfo.AssetName);
                        assetResult.Callbackable().OnProgressCallback(p => result.UpdateProgress(weight + (1f - weight) * p));
                        assetResult.Callbackable().OnCallback((ar) =>
                        {
                            if (ar.Exception != null)
                            {
                                result.SetException(ar.Exception);
                            }
                            else
                            {
                                result.SetResult(ar.Result);
                                this.AddCache <T>(path, ar.Result);
                            }
                        });
                    }
                });
                return(result);
            }
            catch (System.Exception e)
            {
                return(new ImmutableProgressResult <float, T>(e, 0f));
            }
        }
Exemple #2
0
        public virtual IProgressResult <TProgress, TResult> Execute <TProgress, TResult>(Action <IProgressPromise <TProgress, TResult> > action)
        {
            ProgressResult <TProgress, TResult> result = new ProgressResult <TProgress, TResult>(true);

#if NETFX_CORE
            Task.Factory.StartNew(() =>
            {
                try
                {
                    if (result.IsCancellationRequested)
                    {
                        result.SetCancelled();
                        return;
                    }

                    action(result);
                    if (!result.IsDone)
                    {
                        result.SetResult(null);
                    }
                }
                catch (Exception e)
                {
                    if (!result.IsDone)
                    {
                        result.SetException(e);
                    }
                }
            });
#else
            ThreadPool.QueueUserWorkItem((state) =>
            {
                try
                {
                    if (result.IsCancellationRequested)
                    {
                        result.SetCancelled();
                        return;
                    }

                    action(result);
                    if (!result.IsDone)
                    {
                        result.SetResult(null);
                    }
                }
                catch (Exception e)
                {
                    if (!result.IsDone)
                    {
                        result.SetException(e);
                    }
                }
            });
#endif
            return(result);
        }
        public virtual IProgressResult <TProgress> Execute <TProgress>(Action <IProgressPromise <TProgress> > action)
        {
            ProgressResult <TProgress> result = new ProgressResult <TProgress>(true);

            Executors.RunAsyncNoReturn(() =>
            {
                try
                {
                    if (result.IsCancellationRequested)
                    {
                        result.SetCancelled();
                        return;
                    }

                    action(result);
                    if (!result.IsDone)
                    {
                        result.SetResult(null);
                    }
                }
                catch (Exception e)
                {
                    if (!result.IsDone)
                    {
                        result.SetException(e);
                    }
                }
            });
            return(result);
        }
Exemple #4
0
        private void InternalOpen <T>(Type type, ProgressResult <float, T> promise, ViewModel viewModel) where T : View
        {
            View view = null;
            var  path = (GetClassData(type).Attribute as UIAttribute).Path;

            promise.Callbackable().OnCallback(progressResult =>
            {
                Sort(view);
                view.Show();
            });
            if (openedSingleViews.TryGetValue(type, out var view1))
            {
                promise.UpdateProgress(1);
                promise.SetResult(view1);
            }
            else
            {
                view = ReflectionHelper.CreateInstance(type) as View;
                Executors.RunOnCoroutineNoReturn(CreateViewGo(promise, view, path, viewModel));
            }
            openedViews.Add(view);
            if (view.IsSingle)
            {
                openedSingleViews[type] = view;
            }
        }
Exemple #5
0
        public virtual IProgressResult <float, IBundle> LoadBundle(BundleInfo bundleInfo, int priority)
        {
            try
            {
                if (bundleInfo == null)
                {
                    throw new ArgumentNullException("The bundleInfo is null!");
                }

                DefaultBundle bundle = this.GetOrCreateBundle(bundleInfo, priority);
                var           result = bundle.Load();

                ProgressResult <float, IBundle> resultCopy = new ProgressResult <float, IBundle>();
                result.Callbackable().OnProgressCallback(p => resultCopy.UpdateProgress(p));
                result.Callbackable().OnCallback((r) =>
                {
                    if (r.Exception != null)
                    {
                        resultCopy.SetException(r.Exception);
                    }
                    else
                    {
                        resultCopy.SetResult(new InternalBundleWrapper(bundle));
                    }
                });
                return(resultCopy);
            }
            catch (Exception e)
            {
                return(new ImmutableProgressResult <float, IBundle>(e, 0f));
            }
        }
Exemple #6
0
        public static IProgressResult <TProgress, TResult> RunAsync <TProgress, TResult>(Action <IProgressPromise <TProgress, TResult> > action)
        {
            ProgressResult <TProgress, TResult> result = new ProgressResult <TProgress, TResult>();

            DoRunAsync(() =>
            {
                try
                {
                    CheckDisposed();
                    action(result);
                    if (!result.IsDone)
                    {
                        result.SetResult(null);
                    }
                }
                catch (Exception e)
                {
                    if (!result.IsDone)
                    {
                        result.SetException(e);
                    }
                }
            });
            return(result);
        }
Exemple #7
0
        public IProgressResult <float, T> InstantiateAsync <T>(string key, Vector3 position, Quaternion rotation,
                                                               Transform parent = null) where T : Component
        {
            var progress = InstantiateAsync(key, position, rotation, parent);
            ProgressResult <float, T> result = new ProgressResult <float, T>(true);

            progress.Callbackable().OnProgressCallback(result.UpdateProgress);
            progress.Callbackable().OnCallback(progressResult => result.SetResult(progressResult.Result.GetComponent <T>()));
            return(result);
        }
Exemple #8
0
        public IProgressResult <float, T> InstantiateAsync <T>(string key, Transform parent = null,
                                                               bool instantiateInWorldSpace = false) where T : Component
        {
            var progress = InstantiateAsync(key, parent, instantiateInWorldSpace);
            ProgressResult <float, T> result = new ProgressResult <float, T>(true);

            progress.Callbackable().OnProgressCallback(result.UpdateProgress);
            progress.Callbackable().OnCallback(progressResult => result.SetResult(progressResult.Result.GetComponent <T>()));
            return(result);
        }
Exemple #9
0
 /// <summary>
 /// 返回一个完成的IProgressResult<float>
 /// </summary>
 /// <returns></returns>
 public new static IProgressResult <TProgress> Void()
 {
     if (voidResult == null)
     {
         var result = new ProgressResult <TProgress>();
         result.SetResult();
         voidResult = result;
     }
     return(voidResult);
 }
Exemple #10
0
        /// <summary>
        /// 等待多久
        /// </summary>
        /// <param name="duration">单位s</param>
        /// <returns></returns>
        public static IProgressResult <float> WaitAsync(float duration)
        {
            ProgressResult <float> result = new ProgressResult <float>(true);
            Timer timer = new Timer();

            timer.Init(duration, onComplete: () => result.SetResult(), onUpdate: val =>
            {
                result.UpdateProgress(val / duration);
            }, false, false, null);
            Timer._manager.RegisterTimer(timer);
            return(result);
        }
Exemple #11
0
        public override IProgressResult <float, GameObject> InstantiateAsync(string key, Vector3 position, Quaternion rotation, Transform parent = null,
                                                                             bool trackHandle = true)
        {
            ProgressResult <float, GameObject> progressResult = new ProgressResult <float, GameObject>();

            progressResult.Callbackable().OnCallback((result =>
            {
                var ins = Object.Instantiate(result.Result);
                SetTransform(ins, position, rotation, parent, trackHandle);
                progressResult.SetResult(ins);
            }));
            loadAssetAsync(key, progressResult);
            return(progressResult);
        }
Exemple #12
0
        public virtual IProgressResult <float, Object[]> LoadAllAssetsAsync(string bundleName, System.Type type)
        {
            try
            {
                if (bundleName == null)
                {
                    throw new System.ArgumentNullException("bundleName");
                }

                if (type == null)
                {
                    throw new System.ArgumentNullException("type");
                }

                ProgressResult <float, Object[]> result       = new ProgressResult <float, Object[]>();
                IProgressResult <float, IBundle> bundleResult = this.LoadBundle(bundleName);
                float weight = bundleResult.IsDone ? 0f : DEFAULT_WEIGHT;
                bundleResult.Callbackable().OnProgressCallback(p => result.UpdateProgress(p * weight));
                bundleResult.Callbackable().OnCallback((r) =>
                {
                    if (r.Exception != null)
                    {
                        result.SetException(r.Exception);
                        return;
                    }

                    using (IBundle bundle = r.Result)
                    {
                        IProgressResult <float, Object[]> assetResult = bundle.LoadAllAssetsAsync(type);
                        assetResult.Callbackable().OnProgressCallback(p => result.UpdateProgress(weight + (1f - weight) * p));
                        assetResult.Callbackable().OnCallback((ar) =>
                        {
                            if (ar.Exception != null)
                            {
                                result.SetException(ar.Exception);
                            }
                            else
                            {
                                result.SetResult(ar.Result);
                            }
                        });
                    }
                });
                return(result);
            }
            catch (System.Exception e)
            {
                return(new ImmutableProgressResult <float, Object[]>(e, 0f));
            }
        }
Exemple #13
0
        public override IProgressResult <float, GameObject> InstantiateAsync(string key, Transform parent = null, bool instantiateInWorldSpace = false,
                                                                             bool trackHandle             = true)
        {
            ProgressResult <float, GameObject> progressResult = new ProgressResult <float, GameObject>();

            progressResult.Callbackable().OnCallback((result =>
            {
                var ins = Object.Instantiate(result.Result);
                SetTransform(ins, parent, instantiateInWorldSpace);
                progressResult.SetResult(ins);
            }));
            loadAssetAsync(key, progressResult);
            return(progressResult);
        }
Exemple #14
0
        public IProgressResult <float, GameObject> InstantiateAsync(string key, Transform parent = null,
                                                                    bool instantiateInWorldSpace = false)
        {
            ProgressResult <float, GameObject> loadProgress   = new ProgressResult <float, GameObject>(true);
            ProgressResult <float, GameObject> resultProgress = new ProgressResult <float, GameObject>(true);

            loadProgress.Callbackable().OnCallback((result =>
            {
                if (loadProgress.IsCancelled)
                {
                    return;
                }
                var go = Object.Instantiate(result.Result);
                go.transform.SetParent(parent, instantiateInWorldSpace);
                resultProgress.SetResult(go);
            }));
            loadAssetAsync(key, loadProgress);
            return(resultProgress);
        }
Exemple #15
0
 private void InternalOpen <T>(Type type, ProgressResult <float, T> promise, ViewModel viewModel) where T : View
 {
     promise.Callbackable().OnCallback(progressResult =>
     {
         var _view = progressResult.Result;
         Sort(_view);
         _view.Show();
         if (_view.IsSingle)
         {
             _openedViews[type] = _view;
         }
     });
     if (_openedViews.TryGetValue(type, out var view))
     {
         promise.UpdateProgress(1);
         promise.SetResult(view);
     }
     else
     {
         Executors.RunOnCoroutineNoReturn(CreateView(promise, type, viewModel));
     }
 }
Exemple #16
0
        public IProgressResult <float, GameObject> InstantiateAsync(string key, Vector3 position,
                                                                    Quaternion rotation,
                                                                    Transform parent = null)
        {
            ProgressResult <float, GameObject> loadProgress   = new ProgressResult <float, GameObject>(true);
            ProgressResult <float, GameObject> resultProgress = new ProgressResult <float, GameObject>(true);

            loadProgress.Callbackable().OnCallback((result =>
            {
                if (loadProgress.IsCancelled)
                {
                    return;
                }
                var trans = Object.Instantiate(result.Result).transform;
                trans.SetParent(parent);
                trans.localPosition = position;
                trans.localRotation = rotation;
                resultProgress.SetResult(trans.gameObject);
            }));
            loadAssetAsync(key, loadProgress);
            return(resultProgress);
        }
Exemple #17
0
        public virtual IProgressResult <float, IBundle> Load()
        {
            if (this.result == null || this.result.Exception != null)
            {
                this.result = this.Execute <float, IBundle>(promise => Wrap(DoLoadBundleAndDependencies(promise)));
            }

            ProgressResult <float, IBundle> resultCopy = new ProgressResult <float, IBundle>();

            this.result.Callbackable().OnProgressCallback(p => resultCopy.UpdateProgress(p));
            this.result.Callbackable().OnCallback((r) =>
            {
                if (r.Exception != null)
                {
                    resultCopy.SetException(r.Exception);
                }
                else
                {
                    resultCopy.SetResult(new InternalBundleWrapper(this, r.Result));
                }
            });
            return(resultCopy);
        }
Exemple #18
0
        public static IProgressResult <TProgress> RunAsync <TProgress>(Action <IProgressPromise <TProgress> > action)
        {
            ProgressResult <TProgress> result = new ProgressResult <TProgress>(true);

            DoRunAsync(() =>
            {
                try
                {
                    action(result);
                    if (!result.IsDone)
                    {
                        result.SetResult(null);
                    }
                }
                catch (Exception e)
                {
                    if (!result.IsDone)
                    {
                        result.SetException(e);
                    }
                }
            });
            return(result);
        }