Browse Source

implement CreateCompilationForSingleFile in ILSpyParser

newNRILSpyDebugger
Siegfried Pammer 12 years ago
parent
commit
009356e2f2
  1. 1
      src/AddIns/DisplayBindings/ILSpyAddIn/ILSpyAddIn.csproj
  2. 107
      src/AddIns/DisplayBindings/ILSpyAddIn/ILSpyAssemblyResolver.cs
  3. 39
      src/AddIns/DisplayBindings/ILSpyAddIn/ILSpyDecompilerService.cs
  4. 5
      src/AddIns/DisplayBindings/ILSpyAddIn/ILSpyParser.cs
  5. 7
      src/AddIns/DisplayBindings/ILSpyAddIn/ILSpyUnresolvedFile.cs

1
src/AddIns/DisplayBindings/ILSpyAddIn/ILSpyAddIn.csproj

@ -67,7 +67,6 @@
</Compile> </Compile>
<Compile Include="DebuggerTextOutput.cs" /> <Compile Include="DebuggerTextOutput.cs" />
<Compile Include="ILSpyDecompilerService.cs" /> <Compile Include="ILSpyDecompilerService.cs" />
<Compile Include="ILSpyAssemblyResolver.cs" />
<Compile Include="ILSpyFullParseInformation.cs" /> <Compile Include="ILSpyFullParseInformation.cs" />
<Compile Include="ILSpyParser.cs" /> <Compile Include="ILSpyParser.cs" />
<Compile Include="ILSpySymbolSource.cs" /> <Compile Include="ILSpySymbolSource.cs" />

107
src/AddIns/DisplayBindings/ILSpyAddIn/ILSpyAssemblyResolver.cs

@ -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
}
}
}
}
}

39
src/AddIns/DisplayBindings/ILSpyAddIn/ILSpyDecompilerService.cs

@ -14,6 +14,7 @@ using ICSharpCode.Decompiler.Ast;
using ICSharpCode.NRefactory.TypeSystem; using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.SharpDevelop; using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Dom.ClassBrowser; using ICSharpCode.SharpDevelop.Dom.ClassBrowser;
using ICSharpCode.SharpDevelop.Parser;
using Mono.Cecil; using Mono.Cecil;
namespace ICSharpCode.ILSpyAddIn namespace ICSharpCode.ILSpyAddIn
@ -23,6 +24,38 @@ namespace ICSharpCode.ILSpyAddIn
/// </summary> /// </summary>
public static class ILSpyDecompilerService public static class ILSpyDecompilerService
{ {
class ILSpyAssemblyResolver : DefaultAssemblySearcher, IAssemblyResolver
{
public ILSpyAssemblyResolver(FileName fileName)
: base(fileName)
{
}
public AssemblyDefinition Resolve(AssemblyNameReference name)
{
return Resolve(name, new ReaderParameters());
}
public AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
{
var file = FindAssembly(new DomAssemblyName(name.FullName));
if (file == null) return null;
return AssemblyDefinition.ReadAssembly(file, parameters);
}
public AssemblyDefinition Resolve(string fullName)
{
return Resolve(fullName, new ReaderParameters());
}
public AssemblyDefinition Resolve(string fullName, ReaderParameters parameters)
{
var file = FindAssembly(new DomAssemblyName(fullName));
if (file == null) return null;
return AssemblyDefinition.ReadAssembly(file, parameters);
}
}
public static ILSpyUnresolvedFile DecompileType(DecompiledTypeReference name) public static ILSpyUnresolvedFile DecompileType(DecompiledTypeReference name)
{ {
if (name == null) if (name == null)
@ -32,9 +65,9 @@ namespace ICSharpCode.ILSpyAddIn
public static async Task<ILSpyUnresolvedFile> DecompileTypeAsync(DecompiledTypeReference name, CancellationToken cancellationToken) public static async Task<ILSpyUnresolvedFile> DecompileTypeAsync(DecompiledTypeReference name, CancellationToken cancellationToken)
{ {
return Task.Run( return await Task.Run(
delegate() { return DoDecompile(name, cancellationToken); }, delegate() { return DoDecompile(name, cancellationToken); },
cancellationToken).Result; cancellationToken);
} }
static AstBuilder CreateAstBuilder(DecompiledTypeReference name, CancellationToken cancellationToken = default(CancellationToken)) static AstBuilder CreateAstBuilder(DecompiledTypeReference name, CancellationToken cancellationToken = default(CancellationToken))
@ -42,7 +75,7 @@ namespace ICSharpCode.ILSpyAddIn
ReaderParameters readerParameters = new ReaderParameters(); ReaderParameters readerParameters = new ReaderParameters();
// Use new assembly resolver instance so that the AssemblyDefinitions // Use new assembly resolver instance so that the AssemblyDefinitions
// can be garbage-collected once the code is decompiled. // can be garbage-collected once the code is decompiled.
readerParameters.AssemblyResolver = new ILSpyAssemblyResolver(Path.GetDirectoryName(name.AssemblyFile)); readerParameters.AssemblyResolver = new ILSpyAssemblyResolver(name.AssemblyFile);
ModuleDefinition module = ModuleDefinition.ReadModule(name.AssemblyFile, readerParameters); ModuleDefinition module = ModuleDefinition.ReadModule(name.AssemblyFile, readerParameters);
TypeDefinition typeDefinition = module.GetType(name.Type.ReflectionName); TypeDefinition typeDefinition = module.GetType(name.Type.ReflectionName);

5
src/AddIns/DisplayBindings/ILSpyAddIn/ILSpyParser.cs

@ -69,8 +69,9 @@ namespace ICSharpCode.ILSpyAddIn
{ {
DecompiledTypeReference reference = DecompiledTypeReference.FromFileName(fileName); DecompiledTypeReference reference = DecompiledTypeReference.FromFileName(fileName);
if (reference != null) { if (reference != null) {
// var model = SD.GetService<IClassBrowser>().FindAssemblyModel(reference.AssemblyFile); var model = SD.GetService<IClassBrowser>().FindAssemblyModel(reference.AssemblyFile);
// return SD.AssemblyParserService.CreateCompilationForAssembly(model); if (model != null)
return SD.AssemblyParserService.CreateCompilationForAssembly(model, true);
} }
return new CSharpProjectContent() return new CSharpProjectContent()
.AddAssemblyReferences(defaultReferences.Value) .AddAssemblyReferences(defaultReferences.Value)

7
src/AddIns/DisplayBindings/ILSpyAddIn/ILSpyUnresolvedFile.cs

@ -34,9 +34,9 @@ namespace ICSharpCode.ILSpyAddIn
syntaxTree.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true }); syntaxTree.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true });
var outputFormatter = TokenWriter.WrapInWriterThatSetsLocationsInAST(output); var outputFormatter = TokenWriter.WrapInWriterThatSetsLocationsInAST(output);
syntaxTree.AcceptVisitor(new CSharpOutputVisitor(outputFormatter, FormattingOptionsFactory.CreateSharpDevelop())); syntaxTree.AcceptVisitor(new CSharpOutputVisitor(outputFormatter, FormattingOptionsFactory.CreateSharpDevelop()));
ILSpyUnresolvedFile file = new ILSpyUnresolvedFile(name, builder.SyntaxTree.Errors); ILSpyUnresolvedFile file = new ILSpyUnresolvedFile(name, syntaxTree.Errors);
builder.SyntaxTree.FileName = name.ToFileName(); builder.SyntaxTree.FileName = name.ToFileName();
var ts = builder.SyntaxTree.ToTypeSystem(); var ts = syntaxTree.ToTypeSystem();
file.topLevel = ts.TopLevelTypeDefinitions; file.topLevel = ts.TopLevelTypeDefinitions;
file.MemberLocations = output.MemberLocations; file.MemberLocations = output.MemberLocations;
file.DebugSymbols = output.DebugSymbols; file.DebugSymbols = output.DebugSymbols;
@ -67,10 +67,12 @@ namespace ICSharpCode.ILSpyAddIn
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public IUnresolvedTypeDefinition GetInnermostTypeDefinition(TextLocation location) public IUnresolvedTypeDefinition GetInnermostTypeDefinition(TextLocation location)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public IUnresolvedMember GetMember(TextLocation location) public IUnresolvedMember GetMember(TextLocation location)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
@ -100,6 +102,7 @@ namespace ICSharpCode.ILSpyAddIn
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }
public IList<IUnresolvedAttribute> ModuleAttributes { public IList<IUnresolvedAttribute> ModuleAttributes {
get { get {
throw new NotImplementedException(); throw new NotImplementedException();

Loading…
Cancel
Save