@ -38,7 +38,6 @@ namespace ICSharpCode.ILSpy
readonly AssemblyList assemblyList ;
readonly AssemblyList assemblyList ;
readonly string fileName ;
readonly string fileName ;
readonly string shortName ;
readonly string shortName ;
readonly Dictionary < string , UnresolvedAssemblyNameReference > loadedAssemblyReferences = new Dictionary < string , UnresolvedAssemblyNameReference > ( ) ;
public LoadedAssembly ( AssemblyList assemblyList , string fileName , Stream stream = null )
public LoadedAssembly ( AssemblyList assemblyList , string fileName , Stream stream = null )
{
{
@ -63,7 +62,7 @@ namespace ICSharpCode.ILSpy
return assembly ? . DetectTargetFrameworkId ( ) ? ? string . Empty ;
return assembly ? . DetectTargetFrameworkId ( ) ? ? string . Empty ;
}
}
public Dictionary < string , UnresolvedAssemblyNameReference > LoadedAssemblyReferencesInfo = > loadedAssemblyReferences ;
public ReferenceLoadInfo LoadedAssemblyReferencesInfo { get ; } = new ReferenceLoadInfo ( ) ;
/// <summary>
/// <summary>
/// Gets the Cecil ModuleDefinition.
/// Gets the Cecil ModuleDefinition.
@ -206,18 +205,6 @@ namespace ICSharpCode.ILSpy
var node = parent . LookupReferencedAssembly ( name ) ;
var node = parent . LookupReferencedAssembly ( name ) ;
return node ! = null ? node . GetAssemblyDefinitionAsync ( ) . Result : null ;
return node ! = null ? node . GetAssemblyDefinitionAsync ( ) . Result : null ;
}
}
public AssemblyDefinition Resolve ( string fullName )
{
var node = parent . LookupReferencedAssembly ( fullName ) ;
return node ! = null ? node . GetAssemblyDefinitionAsync ( ) . Result : null ;
}
public AssemblyDefinition Resolve ( string fullName , ReaderParameters parameters )
{
var node = parent . LookupReferencedAssembly ( fullName ) ;
return node ! = null ? node . GetAssemblyDefinitionAsync ( ) . Result : null ;
}
public void Dispose ( )
public void Dispose ( )
{
{
@ -234,16 +221,11 @@ namespace ICSharpCode.ILSpy
if ( name = = null )
if ( name = = null )
throw new ArgumentNullException ( nameof ( name ) ) ;
throw new ArgumentNullException ( nameof ( name ) ) ;
if ( name . IsWindowsRuntime ) {
if ( name . IsWindowsRuntime ) {
return assemblyList . winRTMetadata LookupCache. GetOrAdd ( name . Name , LookupWinRTMetadata ) ;
return assemblyList . assembly LookupCache. GetOrAdd ( ( name . Name , true ) , LookupReferencedAssemblyInternal ) ;
} else {
} else {
return assemblyList . assemblyLookupCache . GetOrAdd ( name . FullName , LookupReferencedAssemblyInternal ) ;
return assemblyList . assemblyLookupCache . GetOrAdd ( ( name . FullName , false ) , LookupReferencedAssemblyInternal ) ;
}
}
}
}
public LoadedAssembly LookupReferencedAssembly ( string fullName )
{
return assemblyList . assemblyLookupCache . GetOrAdd ( fullName , LookupReferencedAssemblyInternal ) ;
}
class MyUniversalResolver : UniversalAssemblyResolver
class MyUniversalResolver : UniversalAssemblyResolver
{
{
@ -253,55 +235,52 @@ namespace ICSharpCode.ILSpy
}
}
}
}
LoadedAssembly LookupReferencedAssemblyInternal ( string fullName )
static Dictionary < string , LoadedAssembly > loadingAssemblies = new Dictionary < string , LoadedAssembly > ( ) ;
LoadedAssembly LookupReferencedAssemblyInternal ( ( string fullName , bool isWinRT ) data )
{
{
foreach ( LoadedAssembly asm in assemblyList . GetAssemblies ( ) ) {
string file ;
var asmDef = asm . GetAssemblyDefinitionAsync ( ) . Result ;
LoadedAssembly asm ;
if ( asmDef ! = null & & fullName . Equals ( asmDef . FullName , StringComparison . OrdinalIgnoreCase ) ) {
lock ( loadingAssemblies ) {
return asm ;
foreach ( LoadedAssembly loaded in assemblyList . GetAssemblies ( ) ) {
var asmDef = loaded . GetAssemblyDefinitionAsync ( ) . Result ;
if ( asmDef ! = null & & data . fullName . Equals ( data . isWinRT ? asmDef . Name . Name : asmDef . FullName , StringComparison . OrdinalIgnoreCase ) ) {
return loaded ;
}
}
}
}
if ( assemblyLoadDisableCount > 0 )
return null ;
if ( ! App . Current . Dispatcher . CheckAccess ( ) ) {
// Call this method on the GUI thread.
return ( LoadedAssembly ) App . Current . Dispatcher . Invoke ( DispatcherPriority . Normal , new Func < string , LoadedAssembly > ( LookupReferencedAssembly ) , fullName ) ;
}
var resolver = new MyUniversalResolver ( this ) { TargetFramework = GetTargetFrameworkIdAsync ( ) . Result } ;
if ( assemblyLoadDisableCount > 0 )
var name = AssemblyNameReference . Parse ( fullName ) ;
return null ;
var file = resolver . FindAssemblyFile ( name ) ;
if ( file ! = null ) {
if ( data . isWinRT ) {
loadedAssemblyReferences . AddMessage ( fullName , MessageKind . Info , "Success - Loading from: " + file ) ;
file = Path . Combine ( Environment . SystemDirectory , "WinMetadata" , data . fullName + ".winmd" ) ;
return assemblyList . OpenAssembly ( file , true ) ;
} else {
} else {
var resolver = new MyUniversalResolver ( this ) { TargetFramework = GetTargetFrameworkIdAsync ( ) . Result } ;
loadedAssemblyReferences . AddMessage ( fullName , MessageKind . Error , "Could not find reference: " + fullName ) ;
var name = AssemblyNameReference . Parse ( data . fullName ) ;
return null ;
file = resolver . FindAssemblyFile ( name ) ;
}
}
}
if ( loadingAssemblies . TryGetValue ( file , out asm ) )
LoadedAssembly LookupWinRTMetadata ( string name )
{
foreach ( LoadedAssembly asm in assemblyList . GetAssemblies ( ) ) {
var asmDef = asm . GetAssemblyDefinitionAsync ( ) . Result ;
if ( asmDef ! = null & & name . Equals ( asmDef . Name . Name , StringComparison . OrdinalIgnoreCase ) )
return asm ;
return asm ;
if ( file ! = null ) {
LoadedAssemblyReferencesInfo . AddMessage ( data . fullName , MessageKind . Info , "Success - Loading from: " + file ) ;
asm = new LoadedAssembly ( assemblyList , file ) { IsAutoLoaded = true } ;
} else {
LoadedAssemblyReferencesInfo . AddMessage ( data . fullName , MessageKind . Error , "Could not find reference: " + data . fullName ) ;
return null ;
}
loadingAssemblies . Add ( file , asm ) ;
}
}
if ( assemblyLoadDisableCount > 0 )
App . Current . Dispatcher . BeginInvoke ( ( Action ) delegate ( ) {
return null ;
lock ( assemblyList . assemblies ) {
if ( ! App . Current . Dispatcher . CheckAccess ( ) ) {
assemblyList . assemblies . Add ( asm ) ;
// Call this method on the GUI thread.
}
return ( LoadedAssembly ) App . Current . Dispatcher . Invoke ( DispatcherPriority . Normal , new Func < string , LoadedAssembly > ( LookupWinRTMetadata ) , name ) ;
lock ( loadingAssemblies ) {
}
loadingAssemblies . Remove ( file ) ;
}
string file = Path . Combine ( Environment . SystemDirectory , "WinMetadata" , name + ".winmd" ) ;
} ) ;
if ( File . Exists ( file ) ) {
return asm ;
return assemblyList . OpenAssembly ( file , true ) ;
} else {
return null ;
}
}
}
public Task ContinueWhenLoaded ( Action < Task < ModuleDefinition > > onAssemblyLoaded , TaskScheduler taskScheduler )
public Task ContinueWhenLoaded ( Action < Task < ModuleDefinition > > onAssemblyLoaded , TaskScheduler taskScheduler )