using System; using System.IO; using Chernobyl.Collections.Generic.Event; using Chernobyl.Dependency; using Chernobyl.Reflection.Template; using Chernobyl.Resources; namespace Chernobyl.Reflection.Resources { /// /// An that loads in /// instances from the /// and then returns the value /// of the property. This is helper /// class to make loading in instances from XML files easier. This /// accepts the /// if an object with a /// specific name is needed from an content file. /// public class ObjectContentResourceProcessor : ResourceProcessor { /// /// Initializes a new instance of the /// class. /// /// The instance that /// gives and takes services. public ObjectContentResourceProcessor(IEventCollection services) { services.Inject(this); } /// /// 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 stream to load the resource from. /// Options for the loading or processing of the /// resource. public void From(ResourceProcessCallback callback, Stream readFrom, params IOption[] options) { ResourceProcessCallback objectCallback = result => callback(new ResourceProcessResult((TInstance)result.Resource, result.AsyncWaitHandle, result.CompletedSynchronously, result.IsCompleted)); // Cast to a ResourceProcessor to prevent a recursive call. ((ResourceProcessor)this).From(objectCallback, readFrom, options); } /// /// 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 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 componentCallback = result => { IComponent component = result.Resource; if (component != null) { object objectResource = ((IInstance)component).TheInstance; callback(new ResourceProcessResult(objectResource, result.AsyncWaitHandle, result.CompletedSynchronously, result.IsCompleted)); } else CheckedNextFrom(callback, readFrom, options); }; ComponentResourceProcessor.From(componentCallback, readFrom, options); } /// /// This method is currently not supported. /// /// 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(object resource, Stream writeTo, params IOption[] options) { throw new NotImplementedException(); } /// /// The that loads in /// instances. This resource processor is used /// to load in the objects. /// [Inject] public IResourceProcessor ComponentResourceProcessor { get; set; } } }