123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- using System.IO;
- using System.Linq;
- using System.Reflection;
- namespace CefBridgeDataExchange.Tests.Resources
- {
- internal static class ResourceLoader
- {
- private static readonly string MyNameSpaceFull = typeof(ResourceLoader).Namespace;
- //TODO: add PreRelease conf to debug conditions
- #if !DEBUG
- private static readonly Dictionary<string, byte[]> _cachedBinResource;
- static ResourceLoader()
- {
- var assembly = Assembly.GetExecutingAssembly();
- _cachedBinResource = assembly.GetManifestResourceNames()
- .Where(p => p.StartsWith(MyNameSpaceFull))
- .ToDictionary(p => p, p => assembly.GetManifestResourceStream(p).ToBytes());
- }
- #endif
- private static byte[] ToBytes(this Stream me)
- {
- using (var tmp = new MemoryStream())
- {
- me.CopyTo(tmp);
- return tmp.ToArray();
- }
- }
- public static byte[] GetBinResource(string path)
- {
- #if DEBUG
- var localFile = Path.Combine(DebugSupport.ResourcePath, path);
- return File.ReadAllBytes(localFile);
- #else
- var resName = path.Replace("/", ".");
- return CachedBinResource.TryGetValue($"{MyNameSpaceFull}.{resName}", out var bin) ? bin : null;
- #endif
- }
- }
- }
|