Beispiel #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));
            }
        }
Beispiel #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);
        }
Beispiel #3
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));
            }
        }
Beispiel #4
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);
        }
Beispiel #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));
            }
        }
        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);
        }
Beispiel #7
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);
        }
Beispiel #8
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);
        }