Exemplo n.º 1
0
        private bool UpdateTask()
        {
            AssetLoadingTask task = taskStack.Peek();

            if (task.Update())
            {
                AddAsset(task.AssetDesc, task.Asset);

                if (taskStack.Count == 1)
                {
                    ++loaded;
                }
                taskStack.Pop();

                if (task.Cancelled)
                {
                    Unload(task.AssetDesc.Type, task.AssetDesc.Path);
                }
                else
                {
                    if (task.AssetDesc.Params != null)
                    {
                        task.AssetDesc.Params.FireOnLoaded(this, task.AssetDesc);
                    }
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
 private void StopAndResetLoadingTask()
 {
     if (m_LoadingTask != null)
     {
         Owner.StopAndResetAssetLoadingTask(m_LoadingTask);
         m_LoadingTask = null;
     }
 }
                protected override void Update(TimeStruct timeStruct)
                {
                    switch (Status)
                    {
                    case AssetCacheStatus.WaitingForSlot:
                        if (IsScene)
                        {
                            SucceedAndNotify();
                        }
                        else if (Owner.m_RunningAssetLoadingTasks.Count < Owner.m_RunningAssetLoadingTasks.Capacity)
                        {
                            m_LoadingTask = Owner.RunAssetLoadingTask(Path, Owner.EnsureResourceCache(ResourcePath).ResourceObject);
                            CoreLog.DebugFormat("[AssetCache Update] {0} start loading.", Path);
                            Status = AssetCacheStatus.Loading;
                        }

                        break;

                    case AssetCacheStatus.Loading:
                        if (!string.IsNullOrEmpty(m_LoadingTask.ErrorMessage))
                        {
                            ErrorMessage = m_LoadingTask.ErrorMessage;
                            CoreLog.DebugFormat("[AssetCache Update] {0} loading fail.", Path);
                            FailAndNotify();
                        }
                        else if (m_LoadingTask.IsDone)
                        {
                            AssetObject = m_LoadingTask.AssetObject;
                            CoreLog.DebugFormat("[AssetCache Update] {0} loading success.", Path);
                            SucceedAndNotify();
                        }
                        else
                        {
                            if (LoadingProgress != m_LastLoadingProgress)
                            {
                                m_LastLoadingProgress = LoadingProgress;
                                ProgressAndNotify();
                            }
                        }

                        break;
                    }
                }
                internal override void OnSlotReady()
                {
                    if (Status != AssetCacheStatus.WaitingForSlot)
                    {
                        throw new InvalidOperationException($"Oops! '{nameof(OnSlotReady)}' cannot be called on status '{Status}'.");
                    }

                    if (IsScene)
                    {
                        SucceedAndNotify();
                    }
                    else
                    {
                        m_LoadingTask = Owner.RunAssetLoadingTask(Path, Owner.EnsureResourceCache(ResourcePath).ResourceObject);
                        InternalLog.DebugFormat("[AssetCache Update] {0} start loading.", Path);
                        Status = AssetCacheStatus.Loading;
                        StartTicking();
                    }
                }
Exemplo n.º 5
0
        private void HandleTaskError(Exception ex, bool panic = true)
        {
            if (taskStack.Count == 0)
            {
                if (panic)
                {
                    throw ex;
                }
                else
                {
                    Debug.LogError(ex);
                }
            }

            // Remove the faulty task from the stack
            AssetLoadingTask task = taskStack.Pop();

            // Remove the task's dependencies
            if (task.depsLoaded && task.dependencies != null)
            {
                foreach (AssetDescriptor desc in task.dependencies)
                {
                    Unload(desc);
                }
            }

            taskStack.Clear();

            if (panic)
            {
                throw ex;
            }
            else
            {
                Debug.LogErrorFormat("Could not load '{0}' due to an error.\n\n{1}", task.AssetDesc.Path, ex);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Unloads the asset associated with the specified path.
        /// If there are no references left to the asset, it is removed and destroyed, if needed.
        /// </summary>
        public void Unload(AssetDescriptor desc)
        {
            using (Key.Lock(loadQueue, taskStack, assets)) {
                // Check if the asset is not scheduled for loading first
                int foundIndex = -1;
                for (int i = 0; i < loadQueue.Count; ++i)
                {
                    if (loadQueue[i].Path == desc.Path)
                    {
                        foundIndex = i;
                        break;
                    }
                }
                if (foundIndex != -1)
                {
                    --toLoad;
                    loadQueue.RemoveAt(foundIndex);
                    return;
                }

                if (taskStack.Count > 0)
                {
                    AssetLoadingTask task = taskStack[0];
                    if (task.AssetDesc.Path == desc.Path)
                    {
                        task.Cancel();
                        return;
                    }
                }

                if (!assets.ContainsKey(desc))
                {
                    return;
                }

                IReferencedObject ro = assets[desc];

                // Decrement reference count, and get rid of the asset if there are no references left
                --ro.RefCount;
                if (ro.RefCount <= 0)
                {
                    Dispose(ro.Asset);

                    assets.Remove(desc);
                }

                // Remove any dependencies (or just decrement their ref count)
                if (assetDependencies.ContainsKey(desc))
                {
                    foreach (AssetDescriptor dependency in assetDependencies[desc])
                    {
                        if (IsLoaded(dependency.Type, dependency.Path))
                        {
                            Unload(dependency.Type, dependency.Path);
                        }
                    }
                }

                // Remove dependencies if ref count <= 0
                if (ro.RefCount <= 0)
                {
                    assetDependencies.Remove(desc);
                }
            }
        }
Exemplo n.º 7
0
 private void StopAndResetAssetLoadingTask(AssetLoadingTask task)
 {
     m_RunningAssetLoadingTasks.Remove(task);
     task.OnReset();
     m_AssetLoadingTaskPool.Release(task);
 }