5 changed files with 44 additions and 115 deletions
@ -1,107 +0,0 @@
@@ -1,107 +0,0 @@
|
||||
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
|
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.IO; |
||||
using System.Linq; |
||||
using System.Text; |
||||
using ICSharpCode.Core; |
||||
using ICSharpCode.SharpDevelop; |
||||
using ICSharpCode.SharpDevelop.Parser; |
||||
using Mono.Cecil; |
||||
|
||||
namespace ICSharpCode.ILSpyAddIn |
||||
{ |
||||
class ILSpyAssemblyResolver : IAssemblyResolver |
||||
{ |
||||
readonly DirectoryInfo directoryInfo; |
||||
readonly IDictionary<string, AssemblyDefinition> cache; |
||||
readonly IDictionary<string, AssemblyDefinition> localAssembliesCache; |
||||
|
||||
public ILSpyAssemblyResolver(string decompiledAssemblyFolder) |
||||
{ |
||||
if (string.IsNullOrEmpty(decompiledAssemblyFolder)) |
||||
throw new ArgumentException("Invalid working folder"); |
||||
|
||||
FolderPath = decompiledAssemblyFolder; |
||||
this.directoryInfo = new DirectoryInfo(decompiledAssemblyFolder); |
||||
this.cache = new Dictionary<string, AssemblyDefinition> (); |
||||
this.localAssembliesCache = new Dictionary<string, AssemblyDefinition>(); |
||||
|
||||
ReadLocalAssemblies(); |
||||
} |
||||
|
||||
public string FolderPath { |
||||
get; private set; |
||||
} |
||||
|
||||
public AssemblyDefinition Resolve(AssemblyNameReference name) |
||||
{ |
||||
return this.Resolve(name, new ReaderParameters()); |
||||
} |
||||
|
||||
public AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters) |
||||
{ |
||||
if (name == null) |
||||
throw new ArgumentNullException("name"); |
||||
|
||||
if (parameters == null) |
||||
throw new ArgumentNullException("parameters"); |
||||
|
||||
try { |
||||
AssemblyDefinition assembly = null; |
||||
if (cache.TryGetValue(name.FullName, out assembly)) |
||||
return assembly; |
||||
|
||||
// search into assemblyDecompiledFolder
|
||||
if (localAssembliesCache.ContainsKey(name.FullName)) { |
||||
assembly = localAssembliesCache[name.FullName]; |
||||
} |
||||
|
||||
if (assembly == null) { |
||||
// search using ILSpy's GacInterop.FindAssemblyInNetGac()
|
||||
string fileInGac = SD.GlobalAssemblyCache.FindAssemblyInNetGac(new DomAssemblyName(name.FullName)); |
||||
if (!string.IsNullOrEmpty(fileInGac)) { |
||||
assembly = AssemblyDefinition.ReadAssembly(fileInGac, parameters); |
||||
} |
||||
} |
||||
|
||||
// update caches
|
||||
if (assembly != null) { |
||||
this.cache.Add(assembly.FullName, assembly); |
||||
} |
||||
return assembly; |
||||
} catch (Exception ex) { |
||||
LoggingService.Error("Exception (ILSpyAssemblyResolver): " + ex.Message); |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
public AssemblyDefinition Resolve(string fullName) |
||||
{ |
||||
return this.Resolve(fullName, new ReaderParameters()); |
||||
} |
||||
|
||||
public AssemblyDefinition Resolve(string fullName, ReaderParameters parameters) |
||||
{ |
||||
if (string.IsNullOrEmpty(fullName)) |
||||
throw new ArgumentException("fullName is null or empty"); |
||||
|
||||
return Resolve(AssemblyNameReference.Parse(fullName), parameters); |
||||
} |
||||
|
||||
void ReadLocalAssemblies() |
||||
{ |
||||
// read local assemblies
|
||||
foreach (var file in this.directoryInfo.GetFiles()) { |
||||
try { |
||||
var localAssembly = AssemblyDefinition.ReadAssembly(file.FullName); |
||||
localAssembliesCache.Add(localAssembly.FullName, localAssembly); |
||||
} catch { |
||||
// unable to read assembly file
|
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue