-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathResourceUtils.cs
46 lines (42 loc) · 1.98 KB
/
ResourceUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.IO;
using System.Linq;
using System.Reflection;
namespace RuntimeUnityEditor.Core.Utils
{
/// <summary>
/// Utility methods for working with embedded resources.
/// </summary>
public static class ResourceUtils
{
/// <summary>
/// Read all bytes starting at current position and ending at the end of the stream.
/// </summary>
public static byte[] ReadAllBytes(this Stream input)
{
var buffer = new byte[16 * 1024];
using (var ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
ms.Write(buffer, 0, read);
return ms.ToArray();
}
}
/// <summary>
/// Get a file set as "Embedded Resource" from the assembly that is calling this code, or optionally from a specified assembly.
/// The filename is matched to the end of the resource path, no need to give the full path.
/// If 0 or more than 1 resources match the provided filename, an exception is thrown.
/// For example if you have a file "ProjectRoot\Resources\icon.png" set as "Embedded Resource", you can use this to load it by
/// doing <code>GetEmbeddedResource("icon.png"), assuming that no other embedded files have the same name.</code>
/// </summary>
public static byte[] GetEmbeddedResource(string resourceFileName, Assembly containingAssembly = null)
{
if (containingAssembly == null)
containingAssembly = Assembly.GetCallingAssembly();
var resourceName = containingAssembly.GetManifestResourceNames().Single(str => str.EndsWith(resourceFileName));
using (var stream = containingAssembly.GetManifestResourceStream(resourceName))
return ReadAllBytes(stream ?? throw new InvalidOperationException($"The resource {resourceFileName} was not found"));
}
}
}