Advertisement
ForeverZer0

EmbeddedAssembly

Jun 8th, 2014
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Security.Cryptography;
  6.  
  7. /// <summary>
  8. /// Static class for loaded managed and unmanaged assemblies from embedded resources.
  9. /// </summary>
  10. public static class EmbeddedAssembly
  11. {
  12.     private static Dictionary<string, Assembly> _dictionary = null;
  13.  
  14.     /// <summary>
  15.     /// Gets a dictionary of assemblies loaded from memory, where each key is the
  16.     /// full name of the assembly to be retrieved.
  17.     /// </summary>
  18.     public static Dictionary<string, Assembly> Assemblies
  19.     {
  20.         get
  21.         {
  22.             if (_dictionary == null)
  23.                 _dictionary = new Dictionary<string, Assembly>();
  24.             return _dictionary;
  25.         }
  26.     }
  27.  
  28.     /// <summary>
  29.     /// Loads a managed/unmanaged assembly that is embedded as a resource file.
  30.     /// </summary>
  31.     /// <param name="resourceName">The full name of the resource file.</param>
  32.     /// <param name="filename">Filename</param>
  33.     public static void Load(string resourceName, string filename)
  34.     {
  35.         if (_dictionary == null)
  36.             _dictionary = new Dictionary<string, Assembly>();
  37.  
  38.         byte[] bytes;
  39.         var currentAssembly = Assembly.GetExecutingAssembly();
  40.         using (var stream = currentAssembly.GetManifestResourceStream(resourceName))
  41.         {
  42.             if (stream == null)
  43.                 throw new Exception(resourceName + " is not a valid resource.");
  44.             bytes = new byte[(int)stream.Length];
  45.             stream.Read(bytes, 0, (int)stream.Length);
  46.         }
  47.         var loaded = LoadManaged(bytes);
  48.         if (!loaded)
  49.             loaded = LoadUnmanaged(bytes, filename);
  50.         if (!loaded)
  51.             throw new Exception(resourceName + " could not be loaded from memory.");
  52.     }
  53.  
  54.     /// <summary>
  55.     /// Attempts to load a managed assembly from memory.
  56.     /// </summary>
  57.     /// <param name="bytes">Byte array of the assembly's resource stream.</param>
  58.     /// <returns>Flag indicating if load was successful.</returns>
  59.     private static bool LoadManaged(byte[] bytes)
  60.     {
  61.         try
  62.         {
  63.             var assembly = Assembly.Load(bytes);
  64.             _dictionary.Add(assembly.FullName, assembly);
  65.             return true;
  66.         }
  67.         catch { return false; }
  68.     }
  69.  
  70.     /// <summary>
  71.     /// Attempts to load an unmanaged assembly from memory.
  72.     /// </summary>
  73.     /// <param name="bytes">Byte array if the assemblie's resource stream.</param>
  74.     /// <param name="filename">Filename assembly will be temporarily saved as.</param>
  75.     /// <returns>Flag indicating if load was successful.</returns>
  76.     private static bool LoadUnmanaged(byte[] bytes, string filename)
  77.     {
  78.         var tempFile = Path.Combine(Path.GetTempPath(), filename);
  79.         if (File.Exists(tempFile))
  80.         {
  81.             // Confirm files are exactly the same, overwrite existing file if they are not.
  82.             using (var sha1 = new SHA1CryptoServiceProvider())
  83.             {
  84.                 var hash = BitConverter.ToString(sha1.ComputeHash(bytes)).Replace("-", String.Empty);
  85.                 var tempFileBytes = File.ReadAllBytes(tempFile);
  86.                 var tempHash = BitConverter.ToString(sha1.ComputeHash(tempFileBytes)).Replace("-", string.Empty);
  87.                 if (hash != tempHash)
  88.                     File.WriteAllBytes(tempFile, tempFileBytes);
  89.             }
  90.         }
  91.         else
  92.             File.WriteAllBytes(tempFile, bytes);
  93.         try
  94.         {
  95.             var assembly = Assembly.LoadFile(tempFile);
  96.             _dictionary.Add(assembly.FullName, assembly);
  97.             return true;
  98.         }
  99.         catch { return false; }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement