예제 #1
0
        private void InjectDependency(AssetDescriptor parent, AssetDescriptor dependedDesc)
        {
            using (Key.Lock(assets, assetDependencies)) {
                // Add the asset as a dependency of the parent asset
                List <AssetDescriptor> dependencies = assetDependencies.Get(parent, null);
                if (dependencies == null)
                {
                    dependencies = new List <AssetDescriptor>();
                    assetDependencies.Add(parent, dependencies);
                }
                dependencies.Add(dependedDesc);

                // If the asset is already loaded, increase its ref count
                if (IsLoaded(dependedDesc))
                {
                    IReferencedObject ro = assets[dependedDesc];
                    ++ro.RefCount;
                    IncrementRefCountedDependencies(dependedDesc);
                }
                else
                {
                    // Else, add a new task to load it
                    AddTask(dependedDesc);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Returns the asset associated with the specified path.
        /// Throws an AssetNotLoadedException if no asset is found.
        /// </summary>
        public T Get <T>(string path, IAssetLoaderParameters param = null)
        {
            Type type = typeof(T);

            if (type == null)
            {
                throw new AssetNotLoadedException(typeof(T), path);
            }

            using (Key.Lock(assets)) {
                IReferencedObject ro = assets.Get(new AssetDescriptor(type, path, param), null);
                if (ro == null)
                {
                    throw new AssetNotLoadedException(typeof(T), path);
                }

                object result = ro.Asset;
                if (result == null)
                {
                    throw new AssetNotLoadedException(typeof(T), path);
                }

                return((T)result);
            }
        }
예제 #3
0
 protected ObjectReferenceBase(IReferencedObject referent)
 {
    id = ++LastId;
    isDisposed = false;
    this.Referent = referent;
    if (Logger.Instance.LogLevel >= Logger.LogLevels.Development)
    {
       Logger.Instance.WriteDevToLog("Creating " + this.ToString());
       _instantiationTrace = new StackTrace(0, true);
    }
    referent.AddReference();
 }
예제 #4
0
        private void IncrementRefCountedDependencies(AssetDescriptor parent)
        {
            if (!assetDependencies.ContainsKey(parent))
            {
                return;
            }

            foreach (AssetDescriptor dependency in assetDependencies[parent])
            {
                IReferencedObject ro = assets[dependency];
                ++ro.RefCount;
                IncrementRefCountedDependencies(dependency);
            }
        }
예제 #5
0
        //
        // Methods used to dispose loaded assets
        //
        #region Dispose Methods

        private void DisposeDependencies(AssetDescriptor desc)
        {
            if (assetDependencies.ContainsKey(desc))
            {
                foreach (AssetDescriptor dependency in assetDependencies[desc])
                {
                    DisposeDependencies(dependency);
                }
            }

            if (assets.ContainsKey(desc))
            {
                IReferencedObject ro = assets[desc];
                Dispose(ro.Asset);
            }
        }
예제 #6
0
        /// <summary>
        /// Returns the asset associated with the specified path.
        /// Throws an AssetNotLoadedException if no asset is found.
        /// </summary>
        public object Get(Type type, string path)
        {
            using (Key.Lock(assets)) {
                IReferencedObject ro = assets.Get(new AssetDescriptor(type, path), null);
                if (ro == null)
                {
                    throw new AssetNotLoadedException(type, path);
                }

                object result = ro.Asset;
                if (result == null)
                {
                    throw new AssetNotLoadedException(type, path);
                }

                return(result);
            }
        }
예제 #7
0
        private void NextTask()
        {
            AssetDescriptor descriptor = loadQueue.Dequeue();

            if (IsLoaded(descriptor))
            {
                IReferencedObject asset = assets[descriptor];
                ++asset.RefCount;
                IncrementRefCountedDependencies(descriptor);
                if (descriptor.Params != null)
                {
                    descriptor.Params.FireOnLoaded(this, descriptor);
                }
                ++loaded;
            }
            else
            {
                AddTask(descriptor);
            }
        }
예제 #8
0
        public int GetRefCount <T>(T asset)
        {
            if (asset == null)
            {
                throw new ArgumentNullException("asset");
            }

            using (Key.Lock(assets)) {
                foreach (AssetDescriptor desc in assets.Keys)
                {
                    IReferencedObject ro = assets[desc];
                    if (ro.Asset.Equals(asset))
                    {
                        return(ro.RefCount);
                    }
                }

                return(0);
            }
        }
예제 #9
0
        /// <summary>
        /// Returns the asset specified by the descriptor.
        /// Throws an AssetNotLoadedException if no asset is found.
        /// </summary>
        public object Get(AssetDescriptor descriptor)
        {
            if (descriptor == null)
            {
                throw new ArgumentNullException("descriptor");
            }

            using (Key.Lock(assets)) {
                IReferencedObject ro = assets.Get(descriptor, null);
                if (ro == null)
                {
                    throw new AssetNotLoadedException(descriptor);
                }

                object result = ro.Asset;
                if (result == null)
                {
                    throw new AssetNotLoadedException(descriptor);
                }

                return(result);
            }
        }
예제 #10
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);
                }
            }
        }
예제 #11
0
 public TestObjectReference(IReferencedObject referent) : base(referent)
 {
 }