using System; using System.Collections.Generic; using System.IO; using System.Linq; using Chernobyl.Collections.Generic.Event; namespace Chernobyl.Resources { /// /// A class that works with resources and provides helper methods for dealing /// with resources. /// public static class Resource { /// /// Reads a resource from a file or from some other location that uses /// a string based resource naming convention (such as XNA), specifically, /// it calls on the main texture processor to processor the requested /// resource. This method will first check to see if the string passed in /// exists somewhere in the instance. If not /// it will assume the resource is a named resource and attempt to load /// it that way. If a file does exist, then the file will be read in from /// a FileStream created using and /// . /// /// The type of the resource being loaded. /// For example, if you wanted to load a texture from Chernobyl.Graphics, /// this type would be ITexture. /// The object that holds all of the services. /// The callback method that is to be invoked when /// the resource is finished loading. /// The path to the file to read the /// ResourceType from or a name to the resource to load. /// The options to pass to the /// main ResourceType IResourceProcessor. You don't have to pass anything /// for this parameter unless an IResourceProcessor requires an option /// to be set. public static void From(IEventCollection services, ResourceProcessCallback callback, string filepathOrName, params IOption[] resourceProcessorOptions) { FileStream fileStream = null; // see if the file path points to a file if(File.Exists(filepathOrName) == true) fileStream = new FileStream(filepathOrName, FileMode.Open, FileAccess.Read); else { ISearchPaths searchPaths = services.OfType().First(); if (searchPaths == null) throw new Exception("Unable to find an instance of type \"" + typeof(ISearchPaths).FullName + "\" in the " + "IEventCollection object passed in. Please ensure " + "all plug-ins and that this type is being " + "properly injected. "); IEnumerable paths = searchPaths.FilePaths(filepathOrName); if(paths.Any() == true) fileStream = new FileStream(paths.First(), FileMode.Open, FileAccess.Read); } // if the filepath points to a existing file then we will use that file if(fileStream != null) From(services, callback, fileStream, resourceProcessorOptions); else FromNamed(services, callback, filepathOrName, resourceProcessorOptions); } /// /// Reads the ResourceType from a stream, specifically, it calls on the /// main ResourceType IResourceProcessor to processor this ResourceType /// and take it from the stream provided. A ResourceName IResourceOption /// is added to the resource processor options to load in a named /// resource using as the name of the /// resource. The Stream parameter on the IResourceProcessor.From() /// method is set to null since it is not used. /// /// The type of the resource being loaded. /// For example, if you wanted to load a texture from Chernobyl.Graphics, /// this type would be ITexture. /// The object that holds all of the services. /// The callback method that is to be invoked when /// the resource is finished loading. /// The name of the resource to load. /// The options to pass to the /// main ResourceType IResourceProcessor. You don't have to pass anything /// for this parameter unless an IResourceProcessor requires an option /// to be set. public static void FromNamed(IEventCollection services, ResourceProcessCallback callback, string resourceName, params IOption[] resourceProcessorOptions) { List options = new List(resourceProcessorOptions) { new ResourceName(resourceName) }; From(services, callback, (Stream)null, options.ToArray()); } /// /// Reads the ResourceType from a stream, specifically, it calls on the /// main ResourceType IResourceProcessor to processor this ResourceType /// and take it from the stream provided. /// /// The type of the resource being loaded. /// For example, if you wanted to load a texture from Chernobyl.Graphics, /// this type would be ITexture. /// The object that holds all of the services. /// The callback method that is to be invoked when /// the resource is finished loading. /// The stream to read the ResourceType from. /// The options to pass to the /// main ResourceType IResourceProcessor. You don't have to pass anything /// for this parameter unless an IResourceProcessor requires an option /// to be set. public static void From(IEventCollection services, ResourceProcessCallback callback, Stream stream, params IOption[] resourceProcessorOptions) { IResourceProcessor resourceProcessor = services.OfType>().First(); if (resourceProcessor == null) throw new Exception("Unable to find an IResourceProcessor in the IEventCollection object that can load resources " + " of type \"" + typeof(TResource).FullName + "\". Please ensure that at least on of the " + "following are true: " + Environment.NewLine + "1) Ensure your resource type is correct and that an IResourceProcess exists to handle it." + Environment.NewLine + "2) Ensure your plug-ins are being loaded."); resourceProcessor.From(callback, stream, resourceProcessorOptions); } } }