using System; using System.Collections.Generic; using System.IO; using Chernobyl.Collections.Generic; using Chernobyl.Collections.Generic.Event; using Chernobyl.Event; namespace Chernobyl.Resources { /// /// An that is used to store /// all resources that are properly loaded. /// /// The type of the resource being loaded and /// written out. This type must be a class because this class holds the /// resources in objects public class ResourceRegistry : ResourceProcessor where TResource : class { /// /// Initializes a new instance of the class. /// public ResourceRegistry() { ResourcesDecoratingEventList = new DecoratingEventList(new WeakReferenceList()); } /// /// An even that is invoked when a resource has been added. /// public event EventHandler> ResourcesAdded { add { ResourcesDecoratingEventList.ItemsAdded += value; } remove { ResourcesDecoratingEventList.ItemsAdded -= value; } } /// /// An even that is invoked when a resource has been removed. /// public event EventHandler> ResourcesRemoved { add { ResourcesDecoratingEventList.ItemsRemoved += value; } remove { ResourcesDecoratingEventList.ItemsRemoved -= value; } } /// /// Should process the stream or resource that is passed to it. A /// resource processor must call its next resource processor (provided /// it isn't null) if it is unable to generate the resource from the /// passed in stream. If it is able to /// generate it, then it should do so, invoke the /// method, and NOT call the next resource /// processor. If the next resource processor is null, then the resource /// processor should throw an exception stating that the resource could /// not be generated from the stream because no resource processor /// existed that could perform the task. /// /// The callback that will be invoked when the /// resource has been loaded. The resource can be accessed through the /// property of the /// passed to the /// (it will need to be casted to ). /// The stream to load the resource from. /// Options for the loading or processing of the /// resource. public override void From(ResourceProcessCallback callback, Stream readFrom, params IOption[] options) { ResourceProcessCallback addResourceCallback = result => { TResource resource = result.Resource; if (resource != null && Resources.Contains(resource) == false) Resources.Add(resource); callback(result); }; CheckedNextFrom(addResourceCallback, readFrom, options); } /// /// Writes the resource to the stream specified. /// /// The resource to write to the stream. /// The stream to have the resource written to. /// Options for the writing or processing of the resource. public override void To(TResource resource, Stream writeTo, params IOption[] options) { CheckedNextTo(resource, writeTo, options); } /// /// Holds the resources that have already been loaded. /// public IList Resources { get { return ResourcesDecoratingEventList; } } /// /// The list as an . /// protected DecoratingEventList ResourcesDecoratingEventList { get; set; } } }