using System; using System.IO; using System.Linq; using Chernobyl.Collections.Generic.Event; using Chernobyl.Dependency; using Chernobyl.Resources; using Microsoft.Xna.Framework.Content; namespace Chernobyl.Graphics.Xna.Resources { /// /// A IResourceProcessor that uses XNA ContentManager to load resources /// (using ). The /// resources loaded by this processor must be added to the Visual Studio /// project that you used to compile your application. Note that, this /// processor does not load from the Stream passed into the From method. For /// this reason, you can pass in null for the Stream parameter on the From /// method. In order to load a resource with this processor, you will need to /// pass in a ResourceName option into the options parameter where the /// ResourceName.Name property is set to the name of the resource you want /// to load. /// /// The Chernobyl resource that is being /// loaded. For example, if textures were being loaded with this processor /// then this type would be set to 'ITexture'. /// The XNA resource that is loaded from /// . For example, /// if a font was being loaded then this type would be set to 'SpriteFont'. public class XnaResourceProcessor : ResourceProcessor { /// /// Constructor. /// /// The services instance to grab services from /// and give services to. /// A method that can take an XNA resource /// (XnaResourceType) and turn it into the resource type you want to /// load (ResourceType). public XnaResourceProcessor(IEventCollection services, Func factory) { services.Inject(this); Factory = factory; } /// /// Loads the specified XNA resource type. /// /// 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) { ResourceName resourceName = options.OfType().FirstOrDefault(); if (resourceName != null) { ResourceType resource = Factory(Content.Load(resourceName.Name)); callback(new ResourceProcessResult(resource, null, true, true)); } else CheckedNextFrom(callback, readFrom, options); } /// /// Because XNA cannot write out resources using the ContentManager, this /// method just sends the processing to the next resource processing using /// ResourceProcessor.CheckedNextTo(). /// /// The resource to write out to the stream. /// The stream to write the resource out to. /// Options for controlling the writing of the resource. public override void To(ResourceType resource, System.IO.Stream writeTo, params IOption[] options) { CheckedNextTo(resource, writeTo, options); } /// /// The XNA ContentManager to load content with. /// [Inject] public ContentManager Content { get; set; } /// /// The method that creates the ResourceType from the XnaResourceType /// that was loaded from a call to /// . /// public Func Factory { get; set; } } }