using System.IO;
using System.Reflection;
using CommunityToolkit.Diagnostics;
#nullable enable
namespace ComputeSharp.SwapChain.Uwp.Extensions;
///
/// Helpers for the type.
///
public static class AssemblyExtensions
{
///
/// Tries to read a from a specified file in the target .
///
/// The target to read data from.
/// The target filename to read from.
/// The resulting text, if the read was successful.
/// The contents of the specified file.
public static bool TryReadAllTextFromManifestFile(this Assembly assembly, string filename, out string? text)
{
Guard.IsNotNull(assembly);
Guard.IsNotNull(filename);
try
{
string manifestFilename = $"{assembly.GetName().Name}.{filename.Replace('/', '.').Replace('\\', '.')}";
using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(manifestFilename);
using StreamReader reader = new(stream);
text = reader.ReadToEnd().Trim();
return true;
}
catch
{
text = null;
return false;
}
}
}