Last active
July 11, 2016 18:06
-
-
Save Perikles/067431c2bb12b2f13fcd to your computer and use it in GitHub Desktop.
A utility service that allows loading Awesomium.NET assemblies and native binaries from a location other than the executable's folder or GAC.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using System.Globalization; | |
namespace Awesomium.Core | |
{ | |
/// <summary> | |
/// Utility class that allows loading of Awesomium.NET assemblies, | |
/// from a path other than the base directory of your application. | |
/// </summary> | |
internal static class AwesomiumResolverService | |
{ | |
#region Fields | |
private const string DLL = ".dll"; | |
private static String[] dependencies; | |
private static String[] resources; | |
private static string awesomiumPath; | |
#endregion | |
#region Ctor | |
static AwesomiumResolverService() | |
{ | |
dependencies = new String[] { | |
"awesomium.core", | |
"awesomium.windows.controls", | |
"awesomium.windows.forms" | |
}; | |
resources = new String[] { | |
"awesomium.core.resources", | |
"awesomium.windows.controls.resources" | |
}; | |
} | |
#endregion | |
#region Methods | |
/// <summary> | |
/// Initializes and activates the <see cref="AwesomiumResolverService"/>. | |
/// </summary> | |
/// <param name="directoryPath"> | |
/// The path to the directory containing the Awesomium.NET assemblies | |
/// and its native Awesomium references. | |
/// </param> | |
/// <exception cref="ArgumentNullException"> | |
/// A null reference or an empty string specified for <paramref name="directoryPath"/>. | |
/// </exception> | |
/// <exception cref="ArgumentException"> | |
/// The directory specified does not exist or it does not contain the | |
/// necessary Awesomium.NET assemblies. | |
/// </exception> | |
public static void Initialize( string directoryPath ) | |
{ | |
if ( String.IsNullOrEmpty( directoryPath ) ) | |
throw new ArgumentNullException( "directoryPath" ); | |
if ( !directoryPath.EndsWith( Path.DirectorySeparatorChar.ToString() ) ) | |
directoryPath += Path.DirectorySeparatorChar; | |
String dllPath = String.Format( "{0}{1}{2}", directoryPath, dependencies[ 0 ], DLL ); | |
if ( !File.Exists( dllPath ) ) | |
throw new ArgumentException( "The directory specified does not contain the Awesomium.NET assemblies.", "directoryPath" ); | |
awesomiumPath = directoryPath; | |
AppDomain.CurrentDomain.AssemblyResolve += ResolveAwesomium; | |
} | |
/// <summary> | |
/// Stops monitoring for requested Awesomium.NET assemblies. | |
/// </summary> | |
public static void Stop() | |
{ | |
AppDomain.CurrentDomain.AssemblyResolve -= ResolveAwesomium; | |
} | |
#endregion | |
#region Event Handlers | |
private static Assembly ResolveAwesomium( object sender, ResolveEventArgs args ) | |
{ | |
String unresolved = args.Name.ToLower(); | |
var resourcesDll = resources.SingleOrDefault( item => unresolved.StartsWith( item ) ); | |
if ( !String.IsNullOrEmpty( resourcesDll ) ) | |
{ | |
String resourceID = CultureInfo.CurrentUICulture.IetfLanguageTag; | |
if ( String.Compare( resourceID, "en-US", true ) != 0 ) | |
{ | |
String resourcesPath = String.Format( @"{0}{1}{2}{3}{4}", awesomiumPath, resourceID, Path.DirectorySeparatorChar, resourcesDll, DLL ); | |
if ( File.Exists( resourcesPath ) ) | |
return Assembly.LoadFrom( resourcesPath ); | |
} | |
} | |
var dependencyDll = dependencies.SingleOrDefault( item => unresolved.StartsWith( item ) ); | |
if ( !String.IsNullOrEmpty( dependencyDll ) ) | |
{ | |
String dependencyPath = String.Format( @"{0}{1}{2}{3}", awesomiumPath, Path.DirectorySeparatorChar, dependencyDll, DLL ); | |
if ( File.Exists( dependencyPath ) ) | |
return Assembly.LoadFrom( dependencyPath ); | |
} | |
return null; | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment