using System;
using System.Reflection;
using System.IO;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace SBRL.Utilities
{
///
/// A collection of static methods for manipulating embedded resources.
///
///
/// v0.5, by Starbeamrainbowlabs
/// Last updated 8th August 2016.
/// Licensed under MPL-2.0.
///
/// Changelog:
/// v0.1 (25th July 2016):
/// - Initial release.
/// v0.2 (8th August 2016):
/// - Changed namespace.
/// v0.3 (21st January 2017):
/// - Added GetRawReader().
/// v0.4 (8th April 2017):
/// - Removed unnecessary using statement.
/// v0.5 (3rd September 2017):
/// - Changed namespace
///
public static class EmbeddedFiles
{
///
/// An array of the filenames of all the resources embedded in the calling assembly.
///
/// The resource list.
public static string[] ResourceList
{
get
{
return Assembly.GetCallingAssembly().GetManifestResourceNames();
}
}
public static string GetResourceListText()
{
StringWriter result = new StringWriter();
result.WriteLine("Files embedded in {0}:", Assembly.GetCallingAssembly().GetName().Name);
foreach (string filename in ResourceList)
result.WriteLine(" - {0}", filename);
return result.ToString();
}
///
/// Writes a list of embedded resources to the Console's standard output.
///
public static void WriteResourceList()
{
Console.WriteLine(GetResourceListText());
}
///
/// Gets a StreamReader attached to the specified embedded resource.
///
/// The filename of the embedded resource to get a StreamReader of.
/// A StreamReader attached to the specified embedded resource.
public static StreamReader GetReader(string filename)
{
return new StreamReader(GetRawReader(filename));
}
///
/// Gets a raw Stream that's attached to the specified embedded resource.
/// Useful when you want to copy an embedded resource to some other stream.
///
/// The path to the embedded resource.
/// A raw Stream object attached to the specified file..
public static Stream GetRawReader(string filename)
{
return Assembly.GetCallingAssembly().GetManifestResourceStream(filename);
}
///
/// Gets the specified embedded resource's content as a byte array.
///
/// The filename of the embedded resource to get conteent of.
/// The specified embedded resource's content as a byte array.
public static byte[] ReadAllBytes(string filename)
{
// Referencing the Result property will block until the async method completes
return ReadAllBytesAsync(filename).Result;
}
///
/// Gets the specified embedded resource's content as a byte array asynchronously.
///
/// The filename of the embedded resource to get conteent of.
/// The specified embedded resource's content as a byte array.
public static async Task ReadAllBytesAsync(string filename)
{
using (Stream resourceStream = Assembly.GetCallingAssembly().GetManifestResourceStream(filename))
using (MemoryStream temp = new MemoryStream())
{
await resourceStream.CopyToAsync(temp);
return temp.ToArray();
}
}
///
/// Gets all the text stored in the specified embedded resource.
///
/// The filename to fetch the content of.
/// All the text stored in the specified embedded resource.
public static string ReadAllText(string filename)
{
using (StreamReader resourceReader = new StreamReader(Assembly.GetCallingAssembly().GetManifestResourceStream(filename)))
{
return resourceReader.ReadToEnd();
}
}
///
/// Gets all the text stored in the specified embedded resource asynchronously.
///
/// The filename to fetch the content of.
/// All the text stored in the specified embedded resource.
public static async Task ReadAllTextAsync(string filename)
{
using (StreamReader resourceReader = new StreamReader(Assembly.GetCallingAssembly().GetManifestResourceStream(filename)))
{
return await resourceReader.ReadToEndAsync();
}
}
///
/// Enumerates the lines of text in the specified embedded resource.
///
/// The filename of the embedded resource to enumerate.
/// An IEnumerator that enumerates the specified embedded resource.
public static IEnumerator EnumerateLines(string filename)
{
using (StreamReader resourceReader = new StreamReader(Assembly.GetCallingAssembly().GetManifestResourceStream(filename)))
{
string nextLine;
while ((nextLine = resourceReader.ReadLine()) != null)
{
yield return nextLine;
}
}
}
///
/// Enumerates the lines of text in the specified embedded resource asynchronously.
/// Each successive call returns a task that, when complete, returns the next line of text stored
/// in the embedded resource.
///
/// The filename of the embedded resource to enumerate.
/// An IEnumerator that enumerates the specified embedded resource.
public static IEnumerable> EnumerateLinesAsync(string filename)
{
using (StreamReader resourceReader = new StreamReader(Assembly.GetCallingAssembly().GetManifestResourceStream(filename)))
{
while (!resourceReader.EndOfStream)
{
yield return resourceReader.ReadLineAsync();
}
}
}
///
/// Gets all the lines of text in the specified embedded resource.
/// You might find EnumerateLines(string filename) more useful depending on your situation.
///
/// The filename to obtain the lines of text from.
/// A list of lines in the specified embedded resource.
public static List GetAllLines(string filename)
{
// Referencing the Result property will block until the async method completes
return GetAllLinesAsync(filename).Result;
}
///
/// Gets all the lines of text in the specified embedded resource asynchronously.
///
/// The filename to obtain the lines of text from.
/// A list of lines in the specified embedded resource.
public static async Task> GetAllLinesAsync(string filename)
{
List lines = new List();
IEnumerable> lineIterator = EnumerateLinesAsync(filename);
foreach (Task nextLine in lineIterator)
{
lines.Add(await nextLine);
}
return lines;
}
}
}