/// <summary> /// Requests a resource to the content manager. /// </summary> /// <typeparam name="T">Type of the resource.</typeparam> /// <param name="resourcePath">The resource path.</param> /// <returns> /// The loaded resource. /// </returns> /// <exception cref="System.Exception">Trying to load the same asset with different types.</exception> public override T Load <T>(string resourcePath) { lock (this) { ResourceTracker tracker = null; string path = this.Normalize(resourcePath); if (this.resources.TryGetValue(path, out tracker)) { if (!(tracker.Content is T)) { throw new InvalidOperationException("Trying to load the same asset with different types."); } } else { tracker = new ResourceTracker() { Count = 1, Content = this.ReadAsset <T>(path, null) }; try { this.resources.Add(path, tracker); } catch (Exception e) { } } return((T)tracker.Content); } }
/// <summary> /// Unloads all loaded resources and disposes them when possible. /// </summary> public override void Unload() { lock (this) { Dictionary <string, ResourceTracker> .Enumerator enumer = this.resources.GetEnumerator(); while (enumer.MoveNext()) { ResourceTracker obj = enumer.Current.Value; IDisposable disposable = obj.Content as IDisposable; if (disposable != null) { disposable.Dispose(); } obj = null; } this.resources.Clear(); } }
/// <summary> /// Unloads a single asset from the content manager. /// </summary> /// <param name="resourcePath">The resource path.</param> public void Unload(string resourcePath) { lock (this) { ResourceTracker tracker = null; string path = this.Normalize(resourcePath); if (this.resources.TryGetValue(path, out tracker)) { tracker.Count--; if (tracker.Count == 0) { IDisposable disposable = tracker.Content as IDisposable; if (disposable != null) { disposable.Dispose(); } tracker = null; this.resources.Remove(path); } } } }