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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.MemoryMappedFiles;
using Mono.Collections.Generic;
using Mono.Cecil;
#if FEATURE_ILLINK
namespace Mono.Linker {
public abstract class DirectoryAssemblyResolver : IAssemblyResolver {
readonly Collection<string> directories;
protected readonly Dictionary<AssemblyDefinition, string> assemblyToPath = new Dictionary<AssemblyDefinition, string> ();
readonly List<MemoryMappedViewStream> viewStreams = new List<MemoryMappedViewStream> ();
readonly ReaderParameters defaultReaderParameters;
public void AddSearchDirectory (string directory)
{
directories.Add (directory);
}
protected DirectoryAssemblyResolver ()
{
defaultReaderParameters = new ReaderParameters ();
defaultReaderParameters.AssemblyResolver = this;
directories = new Collection<string> (2) { "." };
}
protected AssemblyDefinition GetAssembly (string file, ReaderParameters parameters)
{
if (parameters.AssemblyResolver == null)
parameters.AssemblyResolver = this;
MemoryMappedViewStream viewStream = null;
try {
// Create stream because CreateFromFile(string, ...) uses FileShare.None which is too strict
using var fileStream = new FileStream (file, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, false);
using var mappedFile = MemoryMappedFile.CreateFromFile (
fileStream, null, fileStream.Length, MemoryMappedFileAccess.Read, HandleInheritability.None, true);
viewStream = mappedFile.CreateViewStream (0, 0, MemoryMappedFileAccess.Read);
AssemblyDefinition result = ModuleDefinition.ReadModule (viewStream, parameters).Assembly;
assemblyToPath.Add (result, file);
viewStreams.Add (viewStream);
// We transferred the ownership of the viewStream to the collection.
viewStream = null;
return result;
} finally {
if (viewStream != null)
viewStream.Dispose ();
}
}
public virtual AssemblyDefinition Resolve (AssemblyNameReference name)
{
return Resolve (name, defaultReaderParameters);
}
public virtual AssemblyDefinition Resolve (AssemblyNameReference name, ReaderParameters parameters)
{
if (name == null)
throw new ArgumentNullException ("name");
if (parameters == null)
throw new ArgumentNullException ("parameters");
var assembly = SearchDirectory (name, directories, parameters);
if (assembly != null)
return assembly;
throw new AssemblyResolutionException (name, new FileNotFoundException ($"Unable to find '{name.Name}.dll' or '{name.Name}.exe' file"));
}
AssemblyDefinition SearchDirectory (AssemblyNameReference name, IEnumerable<string> directories, ReaderParameters parameters)
{
var extensions = new [] { ".dll", ".exe" };
foreach (var directory in directories) {
foreach (var extension in extensions) {
string file = Path.Combine (directory, name.Name + extension);
if (!File.Exists (file))
continue;
try {
return GetAssembly (file, parameters);
} catch (BadImageFormatException) {
continue;
}
}
}
return null;
}
public void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{
if (disposing) {
foreach (var viewStream in viewStreams) {
viewStream.Dispose ();
}
viewStreams.Clear ();
}
}
}
}
#endif
|