예제 #1
0
 public LoadResult(Exception peFileLoadException, LoadedPackage package)
 {
     this.PEFileLoadException = peFileLoadException ?? throw new ArgumentNullException(nameof(peFileLoadException));
     this.Package             = package ?? throw new ArgumentNullException(nameof(package));
 }
예제 #2
0
        async Task <LoadResult> LoadAsync(Task <Stream?>?streamTask)
        {
            // runs on background thread
            var stream = streamTask != null ? await streamTask.ConfigureAwait(false) : null;

            if (stream != null)
            {
                // Read the module from a precrafted stream
                if (!stream.CanSeek)
                {
                    var memoryStream = new MemoryStream();
                    stream.CopyTo(memoryStream);
                    stream.Close();
                    memoryStream.Position = 0;
                    stream = memoryStream;
                }
                var streamOptions = stream is MemoryStream ? PEStreamOptions.PrefetchEntireImage : PEStreamOptions.Default;
                return(LoadAssembly(stream, streamOptions, applyWinRTProjections));
            }
            // Read the module from disk
            Exception loadAssemblyException;

            try
            {
                using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    return(LoadAssembly(fileStream, PEStreamOptions.PrefetchEntireImage, applyWinRTProjections));
                }
            }
            catch (PEFileNotSupportedException ex)
            {
                loadAssemblyException = ex;
            }
            catch (BadImageFormatException ex)
            {
                loadAssemblyException = ex;
            }
            // Maybe its a compressed Xamarin/Mono assembly, see https://github.com/xamarin/xamarin-android/pull/4686
            try
            {
                return(LoadCompressedAssembly(fileName));
            }
            catch (InvalidDataException)
            {
                // Not a compressed module, try other options below
            }
            // If it's not a .NET module, maybe it's a single-file bundle
            var bundle = LoadedPackage.FromBundle(fileName);

            if (bundle != null)
            {
                bundle.LoadedAssembly = this;
                return(new LoadResult(loadAssemblyException, bundle));
            }
            // If it's not a .NET module, maybe it's a zip archive (e.g. .nupkg)
            try
            {
                var zip = LoadedPackage.FromZipFile(fileName);
                zip.LoadedAssembly = this;
                return(new LoadResult(loadAssemblyException, zip));
            }
            catch (InvalidDataException)
            {
                throw loadAssemblyException;
            }
        }