forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssemblyResolver.cs
More file actions
74 lines (63 loc) · 2.28 KB
/
AssemblyResolver.cs
File metadata and controls
74 lines (63 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using JavaScriptEngineSwitcher.ChakraCore.Resources;
namespace JavaScriptEngineSwitcher.ChakraCore
{
/// <summary>
/// Assembly resolver
/// </summary>
internal static class AssemblyResolver
{
/// <summary>
/// Name of directory, that contains the ChakraCore assemblies
/// </summary>
private const string ASSEMBLY_DIRECTORY_NAME = "ChakraCore";
/// <summary>
/// Regular expression for working with the `bin` directory path
/// </summary>
private static readonly Regex _binDirectoryRegex = new Regex(@"\\bin\\?$", RegexOptions.IgnoreCase);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool SetDllDirectory(string lpPathName);
/// <summary>
/// Initialize a assembly resolver
/// </summary>
public static void Initialize()
{
var currentDomain = AppDomain.CurrentDomain;
string platform = Environment.Is64BitProcess ? "x64" : "x86";
string binDirectoryPath = currentDomain.SetupInformation.PrivateBinPath;
if (string.IsNullOrEmpty(binDirectoryPath))
{
// `PrivateBinPath` property is empty in test scenarios, so
// need to use the `BaseDirectory` property
binDirectoryPath = currentDomain.BaseDirectory;
}
string assemblyDirectoryPath = Path.Combine(binDirectoryPath, ASSEMBLY_DIRECTORY_NAME, platform);
if (!Directory.Exists(assemblyDirectoryPath))
{
if (_binDirectoryRegex.IsMatch(binDirectoryPath))
{
string applicationRootPath = _binDirectoryRegex.Replace(binDirectoryPath, string.Empty);
assemblyDirectoryPath = Path.Combine(applicationRootPath, ASSEMBLY_DIRECTORY_NAME, platform);
if (!Directory.Exists(assemblyDirectoryPath))
{
throw new DirectoryNotFoundException(
string.Format(Strings.Engines_ChakraCoreAssemblyDirectoryNotFound, assemblyDirectoryPath));
}
}
else
{
throw new DirectoryNotFoundException(
string.Format(Strings.Engines_ChakraCoreAssemblyDirectoryNotFound, assemblyDirectoryPath));
}
}
if (!SetDllDirectory(assemblyDirectoryPath))
{
throw new InvalidOperationException(
string.Format(Strings.Engines_AddingDirectoryToDllSearchPathFailed, assemblyDirectoryPath));
}
}
}
}