Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AssemblyLoader.cs « MonoDevelop.AssemblyBrowser « MonoDevelop.AssemblyBrowser « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8baaf723a1eeef25b044ec1d2bcd6cc261d3afd7 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// 
// AssemblyWrapper.cs
//  
// Author:
//       Mike Krüger <mkrueger@xamarin.com>
// 
// Copyright (c) 2012 Xamarin <http://xamarin.com>
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.TypeSystem;
using System.Threading.Tasks;
using MonoDevelop.Core;
using System.IO;
using System.Threading;
using System.Collections.Generic;
using ICSharpCode.Decompiler.Metadata;
using System.Reflection.Metadata;
using System.Linq;
using ICSharpCode.Decompiler.TypeSystem.Implementation;
using ICSharpCode.Decompiler.CSharp.Transforms;

namespace MonoDevelop.AssemblyBrowser
{
	class AssemblyLoader : IDisposable
	{
		readonly CancellationTokenSource src = new CancellationTokenSource ();
		readonly AssemblyBrowserWidget widget;

		public string FileName {
			get;
			private set;
		}

		Task<PEFile> assemblyLoaderTask;
		TaskCompletionSource<PEFile> assemblyDefinitionTaskSource;

		public Task<PEFile> LoadingTask {
			get {
				return assemblyLoaderTask;
			}
			set {
				assemblyLoaderTask = value;
			}
		}

		public PEFile Assembly => AssemblyTask.Result;
		public Task<PEFile> AssemblyTask => assemblyDefinitionTaskSource.Task;

		CSharpDecompiler csharpDecompiler;

		public CSharpDecompiler CSharpDecompiler {
			get {
				if (csharpDecompiler == null) {
					csharpDecompiler = new CSharpDecompiler (DecompilerTypeSystem, new ICSharpCode.Decompiler.DecompilerSettings (LanguageVersion.Latest));
					csharpDecompiler.AstTransforms.Add (new EscapeInvalidIdentifiers ());
				}

				return csharpDecompiler;
			}
		}

		DecompilerTypeSystem decompilerTypeSystem;
		public DecompilerTypeSystem DecompilerTypeSystem { 
			get {
				if (decompilerTypeSystem == null) {
					decompilerTypeSystem = new DecompilerTypeSystem (Assembly, new AssemblyResolver (Assembly, widget));
				}
				return decompilerTypeSystem;
			}
		}

		public Error Error { get; internal set; }

		public bool IsLoaded { get; private set; }

		public AssemblyLoader (AssemblyBrowserWidget widget, string fileName)
		{
			if (widget == null)
				throw new ArgumentNullException (nameof (widget));
			if (fileName == null)
				throw new ArgumentNullException (nameof (fileName));
			this.widget = widget;
			FileName = fileName;
			if (!File.Exists (fileName))
				throw new ArgumentException ("File doesn't exist.", nameof (fileName));

			assemblyDefinitionTaskSource = new TaskCompletionSource<PEFile> ();

			assemblyLoaderTask = Task.Run (() => {
				try {
					var peFile = new PEFile (FileName, System.Reflection.PortableExecutable.PEStreamOptions.PrefetchEntireImage);
					assemblyDefinitionTaskSource.SetResult (peFile);
					return peFile;
				} catch (Exception e) {
					LoggingService.LogError ("Error while reading assembly " + FileName, e);
					Error = new Error(e.Message);
					assemblyDefinitionTaskSource.SetResult (null);
					return null;
				} finally { IsLoaded = true; }
			}, src.Token);
		}

		class MyUniversalAssemblyResolver : UniversalAssemblyResolver
		{
			public MyUniversalAssemblyResolver (string mainAssemblyFileName, bool throwOnError, string targetFramework) : base (mainAssemblyFileName, throwOnError, targetFramework)
			{
			}
		}
		class AssemblyResolver : IAssemblyResolver
		{
			readonly PEFile assembly;
			readonly AssemblyBrowserWidget widget;

			public AssemblyResolver (PEFile assembly, AssemblyBrowserWidget widget)
			{
				this.assembly = assembly;
				this.widget = widget;
			}

			public PEFile Resolve (IAssemblyReference reference)
			{
				try {
					var targetFramework = assembly.Reader.DetectTargetFrameworkId () ?? "";
					var resolver = new MyUniversalAssemblyResolver (assembly.FileName, false, targetFramework);
					var fileName = resolver.FindAssemblyFile (reference);
					if (fileName != null && File.Exists (fileName))
						return widget.AddReferenceByFileName (fileName)?.Assembly;
				} catch (Exception e) {
					LoggingService.LogInternalError ($"Error while resolving assembly {reference.FullName} for {assembly.FileName}.", e);
				}

				return widget.AddReferenceByAssemblyName (reference.FullName)?.Assembly;
			}

			public PEFile ResolveModule (PEFile mainModule, string moduleName)
			{
				return widget.AddReferenceByFileName (mainModule.FileName)?.Assembly;
			}
		}

		class FastNonInterningProvider : InterningProvider
		{
			Dictionary<string, string> stringDict = new Dictionary<string, string> ();

			public override string Intern (string text)
			{
				if (text == null)
					return null;

				string output;
				if (stringDict.TryGetValue (text, out output))
					return output;
				stringDict [text] = text;
				return text;
			}

			public override ISupportsInterning Intern (ISupportsInterning obj)
			{
				return obj;
			}

			public override IList<T> InternList<T> (IList<T> list)
			{
				return list;
			}

			public override object InternValue (object obj)
			{
				return obj;
			}
		}

		public string LookupAssembly (string fullAssemblyName)
		{
			var assemblyFile = Runtime.SystemAssemblyService.DefaultAssemblyContext.GetAssemblyLocation (fullAssemblyName, null);
			if (assemblyFile != null && File.Exists (assemblyFile))
				return assemblyFile;

			var name = AssemblyNameReference.Parse (fullAssemblyName);
			var path = Path.GetDirectoryName (FileName);

			var dll = Path.Combine (path, name.Name + ".dll");
			if (File.Exists (dll))
				return dll;
			var exe = Path.Combine (path, name.Name + ".exe");
			if (File.Exists (exe))
				return exe;

			foreach (var asm in Runtime.SystemAssemblyService.DefaultAssemblyContext.GetAssemblies ()) {
				if (string.Equals (asm.Name, fullAssemblyName, StringComparison.OrdinalIgnoreCase))
					return asm.Location;
			}

			return null;
		}

		#region IDisposable implementation

		public void Dispose ()
		{
			if (assemblyLoaderTask == null)
				return;
			src.Cancel ();
			src.Dispose ();
			assemblyLoaderTask = null;
		}
		#endregion
	}
}