1+ namespace JavaScriptEngineSwitcher . V8
2+ {
3+ using System ;
4+ using System . IO ;
5+ using System . Reflection ;
6+ using System . Web ;
7+
8+ using Resources ;
9+
10+ /// <summary>
11+ /// Assembly resolver
12+ /// </summary>
13+ internal static class AssemblyResolver
14+ {
15+ /// <summary>
16+ /// Name of directory, that contains the Microsoft ClearScript.V8 assemblies
17+ /// </summary>
18+ private const string ASSEMBLY_DIRECTORY_NAME = "ClearScript.V8" ;
19+
20+ /// <summary>
21+ /// Name of the ClearScriptV8 assembly
22+ /// </summary>
23+ private const string ASSEMBLY_NAME = "ClearScriptV8" ;
24+
25+
26+ /// <summary>
27+ /// Initialize a assembly resolver
28+ /// </summary>
29+ public static void Initialize ( )
30+ {
31+ AppDomain . CurrentDomain . AssemblyResolve += AssemblyResolveHandler ;
32+ }
33+
34+ private static Assembly AssemblyResolveHandler ( object sender , ResolveEventArgs args )
35+ {
36+ if ( args . Name . StartsWith ( ASSEMBLY_NAME , StringComparison . OrdinalIgnoreCase ) )
37+ {
38+ var currentDomain = ( AppDomain ) sender ;
39+ string platform = Environment . Is64BitProcess ? "64" : "32" ;
40+
41+ string binDirectoryPath = currentDomain . SetupInformation . PrivateBinPath ;
42+ if ( string . IsNullOrEmpty ( binDirectoryPath ) )
43+ {
44+ // `PrivateBinPath` property is empty in test scenarios, so
45+ // need to use the `BaseDirectory` property
46+ binDirectoryPath = currentDomain . BaseDirectory ;
47+ }
48+
49+ string assemblyDirectoryPath = Path . Combine ( binDirectoryPath , ASSEMBLY_DIRECTORY_NAME ) ;
50+ string assemblyFileName = string . Format ( "{0}-{1}.dll" , ASSEMBLY_NAME , platform ) ;
51+ string assemblyFilePath = Path . Combine ( assemblyDirectoryPath , assemblyFileName ) ;
52+
53+ if ( ! Directory . Exists ( assemblyDirectoryPath ) )
54+ {
55+ if ( HttpContext . Current != null )
56+ {
57+ // Fix for WebMatrix
58+ string applicationRootPath = HttpContext . Current . Server . MapPath ( "~" ) ;
59+ assemblyDirectoryPath = Path . Combine ( applicationRootPath , ASSEMBLY_DIRECTORY_NAME ) ;
60+
61+ if ( ! Directory . Exists ( assemblyDirectoryPath ) )
62+ {
63+ throw new DirectoryNotFoundException (
64+ string . Format ( Strings . Engines_ClearScriptV8AssembliesDirectoryNotFound , assemblyDirectoryPath ) ) ;
65+ }
66+
67+ assemblyFilePath = Path . Combine ( assemblyDirectoryPath , assemblyFileName ) ;
68+ }
69+ else
70+ {
71+ throw new DirectoryNotFoundException (
72+ string . Format ( Strings . Engines_ClearScriptV8AssembliesDirectoryNotFound , assemblyDirectoryPath ) ) ;
73+ }
74+ }
75+
76+ if ( ! File . Exists ( assemblyFilePath ) )
77+ {
78+ throw new FileNotFoundException (
79+ string . Format ( Strings . Engines_ClearScriptV8AssemblyFileNotFound , assemblyFilePath ) ) ;
80+ }
81+
82+ return Assembly . LoadFile ( assemblyFilePath ) ;
83+ }
84+
85+ return null ;
86+ }
87+ }
88+ }
0 commit comments