示例#1
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));
            }
        }
示例#2
0
 protected virtual void Dispose(bool disposing)
 {
     if (!disposed)
     {
         try
         {
             if (this.bundle != null)
             {
                 /* Must be released in the main thread  */
                 Executors.RunOnMainThread(() =>
                 {
                     this.bundle.Release();
                     this.bundle = null;
                 });
             }
         }
         catch (System.Exception)
         {
         }
         finally
         {
             disposed = true;
         }
     }
 }
示例#3
0
        public virtual void RemoveBundle(DefaultBundle bundle)
        {
            if (this.bundles == null)
            {
                return;
            }

            this.bundles.Remove(bundle.Name);
        }
示例#4
0
        public virtual void AddBundle(DefaultBundle bundle)
        {
            if (this.bundles == null)
            {
                return;
            }

            this.bundles.Add(bundle.Name, bundle);
        }
示例#5
0
        public virtual DefaultBundle GetOrCreateBundle(BundleInfo bundleInfo, int priority)
        {
            DefaultBundle bundle;

            if (this.bundles.TryGetValue(bundleInfo.Name, out bundle))
            {
                return(bundle);
            }

            bundle          = new DefaultBundle(bundleInfo, this);
            bundle.Priority = priority;
            return(bundle);
        }
示例#6
0
        protected virtual IEnumerator DoLoadBundle(IProgressPromise <float, IBundle[]> promise, BundleInfo[] bundleInfos, int priority)
        {
            List <IBundle> bundles   = new List <IBundle>();
            Exception      exception = new Exception("unkown");
            List <IProgressResult <float, IBundle> > bundleResults = new List <IProgressResult <float, IBundle> >();

            for (int i = 0; i < bundleInfos.Length; i++)
            {
                try
                {
                    DefaultBundle bundle = this.GetOrCreateBundle(bundleInfos[i], priority);
                    IProgressResult <float, IBundle> bundleResult = bundle.Load();
                    bundleResult.Callbackable().OnCallback(r =>
                    {
                        if (r.Exception != null)
                        {
                            exception = r.Exception;
                            if (log.IsWarnEnabled)
                            {
                                log.WarnFormat("Loads Bundle failure! Error:{0}", r.Exception);
                            }
                        }
                        else
                        {
                            bundles.Add(new InternalBundleWrapper((DefaultBundle)r.Result));
                        }
                    });

                    if (!bundleResult.IsDone)
                    {
                        bundleResults.Add(bundleResult);
                    }
                }
                catch (Exception e)
                {
                    exception = e;
                    if (log.IsWarnEnabled)
                    {
                        log.WarnFormat("Loads Bundle '{0}' failure! Error:{1}", bundleInfos[i], e);
                    }
                }
            }

            bool  finished = false;
            float progress = 0f;
            int   count    = bundleResults.Count;

            while (!finished && count > 0)
            {
                yield return(null);

                progress = 0f;
                finished = true;
                for (int i = 0; i < count; i++)
                {
                    var result = bundleResults[i];
                    if (!result.IsDone)
                    {
                        finished = false;
                    }

                    progress += result.Progress;
                }
                promise.UpdateProgress(progress / count);
            }

            promise.UpdateProgress(1f);
            if (bundles.Count > 0)
            {
                promise.SetResult(bundles.ToArray());
            }
            else
            {
                promise.SetException(exception);
            }
        }
示例#7
0
 public InternalBundleWrapper(DefaultBundle bundle)
 {
     this.bundle = bundle;
     this.bundle.Retain();
 }