-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathAssemblyResolver.cs
More file actions
74 lines (63 loc) · 1.95 KB
/
AssemblyResolver.cs
File metadata and controls
74 lines (63 loc) · 1.95 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
#if NETFRAMEWORK
using System;
using System.IO;
using System.Runtime.InteropServices;
#if NET40
using PolyfillsForOldDotNet.System.Runtime.InteropServices;
#endif
using JavaScriptEngineSwitcher.Core.Utilities;
using JavaScriptEngineSwitcher.ChakraCore.Constants;
using JavaScriptEngineSwitcher.ChakraCore.Resources;
namespace JavaScriptEngineSwitcher.ChakraCore
{
/// <summary>
/// Assembly resolver
/// </summary>
internal static class AssemblyResolver
{
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool SetDllDirectory(string lpPathName);
/// <summary>
/// Initialize a assembly resolver
/// </summary>
public static void Initialize()
{
AppDomain currentDomain = AppDomain.CurrentDomain;
string baseDirectoryPath = currentDomain.SetupInformation.PrivateBinPath;
if (string.IsNullOrEmpty(baseDirectoryPath))
{
// `PrivateBinPath` property is empty in test scenarios, so
// need to use the `BaseDirectory` property
baseDirectoryPath = currentDomain.BaseDirectory;
}
Architecture architecture = RuntimeInformation.OSArchitecture;
string platform;
if (architecture == Architecture.X64 || architecture == Architecture.X86)
{
platform = Utils.Is64BitProcess() ? "x64" : "x86";
}
else if (architecture == Architecture.Arm64 || architecture == Architecture.Arm)
{
platform = Utils.Is64BitProcess() ? "arm64" : "arm";
}
else
{
return;
}
string assemblyFileName = DllName.ForWindows;
string assemblyDirectoryPath = Path.Combine(baseDirectoryPath, platform);
string assemblyFilePath = Path.Combine(assemblyDirectoryPath, assemblyFileName);
bool assemblyFileExists = File.Exists(assemblyFilePath);
if (!assemblyFileExists)
{
return;
}
if (!SetDllDirectory(assemblyDirectoryPath))
{
throw new InvalidOperationException(
string.Format(Strings.Engines_AddingDirectoryToDllSearchPathFailed, assemblyDirectoryPath));
}
}
}
}
#endif