diff --git a/DTLib/Filesystem/EmbeddedResources.cs b/DTLib/Filesystem/EmbeddedResources.cs new file mode 100644 index 0000000..21ab829 --- /dev/null +++ b/DTLib/Filesystem/EmbeddedResources.cs @@ -0,0 +1,27 @@ +using System.IO; +using System.Reflection; + +namespace DTLib.Filesystem; + +public static class EmbeddedResources +{ + public static Stream GetResourceStream(string resourcePath, Assembly assembly = null) => + (assembly ?? Assembly.GetCallingAssembly()) + .GetManifestResourceStream(resourcePath) + ?? throw new Exception($"embedded resource <{resourcePath}> not found"); + + public static byte[] ReadBynary(string resourcePath, Assembly assembly = null) + { + if (assembly == null) assembly = Assembly.GetCallingAssembly(); + using var reader = new BinaryReader(GetResourceStream(resourcePath, assembly)); + return reader.ReadBytes(int.MaxValue); + } + + + public static string ReadText(string resourcePath, Assembly assembly = null) + { + if (assembly == null) assembly = Assembly.GetCallingAssembly(); + using var reader = new StreamReader(GetResourceStream(resourcePath, assembly)); + return reader.ReadToEnd(); + } +} \ No newline at end of file