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