// // 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); } }
// // Things used internally by the AssetManager, that are not intended to be accessed // by anyone outside. // #region Internals // // Methods pertaining to how the AssetManager handles loading tasks internally. // #region Load Task Handling private void InjectDependencies(AssetDescriptor parent, List <AssetDescriptor> dependedDescs) { HashSet <AssetDescriptor> injected = new HashSet <AssetDescriptor>(); foreach (AssetDescriptor desc in dependedDescs) { // Ignore subsequent dependencies if there are duplicates. if (injected.Contains(desc)) { continue; } injected.Add(desc); InjectDependency(parent, desc); } injected.Clear(); }
public int GetRefCount <T>(string path, IAssetLoaderParameters param = null) { if (path == null) { throw new ArgumentNullException("path"); } using (Key.Lock(assets)) { AssetDescriptor desc = AssetDescriptor.Create <T>(path, param); if (assets.ContainsKey(desc)) { return(assets[desc].RefCount); } return(0); } }
public void Clear() { using (Key.Lock(loadQueue, taskStack, assets, assetDependencies)) { loadQueue.Clear(); FinishLoading(); Dictionary <AssetDescriptor, int> dependencyCount = new Dictionary <AssetDescriptor, int>(); while (assets.Count > 0) { dependencyCount.Clear(); AssetDescriptor[] descs = new AssetDescriptor[assets.Keys.Count]; assets.Keys.CopyTo(descs, 0); foreach (AssetDescriptor desc in descs) { if (!assetDependencies.ContainsKey(desc)) { continue; } List <AssetDescriptor> dependencies = assetDependencies[desc]; foreach (AssetDescriptor dependency in dependencies) { int count = dependencyCount.Get(dependency, 0); dependencyCount[dependency] = ++count; } } // Only dispose of assets that are root assets (not referenced) foreach (AssetDescriptor desc in descs) { if (dependencyCount.Get(desc, 0) == 0) { Unload(desc); } } } assets.Clear(); assetDependencies.Clear(); loadQueue.Clear(); taskStack.Clear(); } }
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); } }
/// <summary> /// Schedules the specified asset for loading. The request will not be processed /// until Update() is called. /// </summary> public void Load(AssetDescriptor descriptor) { AssetLoader loader = GetLoader(descriptor.Type); if (loader == null) { throw new Exception("No loader for type: " + descriptor.Type); } using (Key.Lock(loadQueue)) { // Reset progress stats if (loadQueue.Count == 0) { loaded = 0; toLoad = 0; timeStarted = 0; } loadQueue.Enqueue(descriptor); ++toLoad; } }
/// <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); } }
public AssetLoadingTask(AssetManager manager, AssetDescriptor descriptor, AssetLoader loader) { this.manager = manager; this.descriptor = descriptor; this.loader = loader; }
public bool IsLoaded(AssetDescriptor desc) { using (Key.Lock(assets)) { return(assets.ContainsKey(desc)); } }
/// <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); } } }
public AssetNotLoadedException(AssetDescriptor desc) : this(desc.Type, desc.Path, desc.Params) { }