Skip to content

Commit d4a4be9

Browse files
Add IFileLoader API
1 parent 02eb6b7 commit d4a4be9

14 files changed

+421
-155
lines changed

ICSharpCode.ILSpyX/AssemblyList.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
using System.Xml.Linq;
3131

3232
using ICSharpCode.ILSpyX.Extensions;
33+
using ICSharpCode.ILSpyX.FileLoaders;
3334

3435
namespace ICSharpCode.ILSpyX
3536
{
@@ -128,6 +129,7 @@ public event NotifyCollectionChangedEventHandler CollectionChanged {
128129

129130
public bool ApplyWinRTProjections { get; set; }
130131
public bool UseDebugSymbols { get; set; }
132+
public FileLoaderRegistry LoaderRegistry => this.manager.LoaderRegistry;
131133

132134
/// <summary>
133135
/// Gets the loaded assemblies. This method is thread-safe.
@@ -279,7 +281,7 @@ public LoadedAssembly OpenAssembly(string file, bool isAutoLoaded = false)
279281
{
280282
file = Path.GetFullPath(file);
281283
return OpenAssembly(file, () => {
282-
var newAsm = new LoadedAssembly(this, file, applyWinRTProjections: ApplyWinRTProjections, useDebugSymbols: UseDebugSymbols);
284+
var newAsm = new LoadedAssembly(this, file, fileLoaders: manager.LoaderRegistry, applyWinRTProjections: ApplyWinRTProjections, useDebugSymbols: UseDebugSymbols);
283285
newAsm.IsAutoLoaded = isAutoLoaded;
284286
return newAsm;
285287
});
@@ -293,6 +295,7 @@ public LoadedAssembly OpenAssembly(string file, Stream? stream, bool isAutoLoade
293295
file = Path.GetFullPath(file);
294296
return OpenAssembly(file, () => {
295297
var newAsm = new LoadedAssembly(this, file, stream: Task.FromResult(stream),
298+
fileLoaders: manager.LoaderRegistry,
296299
applyWinRTProjections: ApplyWinRTProjections, useDebugSymbols: UseDebugSymbols);
297300
newAsm.IsAutoLoaded = isAutoLoaded;
298301
return newAsm;
@@ -345,6 +348,7 @@ LoadedAssembly OpenAssembly(string file, Func<LoadedAssembly> load)
345348
return null;
346349

347350
var newAsm = new LoadedAssembly(this, file, stream: Task.FromResult<Stream?>(stream),
351+
fileLoaders: manager.LoaderRegistry,
348352
applyWinRTProjections: ApplyWinRTProjections, useDebugSymbols: UseDebugSymbols);
349353
newAsm.IsAutoLoaded = target.IsAutoLoaded;
350354

@@ -374,6 +378,7 @@ LoadedAssembly OpenAssembly(string file, Func<LoadedAssembly> load)
374378
if (index < 0)
375379
return null;
376380
var newAsm = new LoadedAssembly(this, target.FileName, pdbFileName: target.PdbFileName,
381+
fileLoaders: manager.LoaderRegistry,
377382
applyWinRTProjections: ApplyWinRTProjections, useDebugSymbols: UseDebugSymbols);
378383
newAsm.IsAutoLoaded = target.IsAutoLoaded;
379384
lock (lockObj)

ICSharpCode.ILSpyX/AssemblyListManager.cs

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using System.Xml.Linq;
2424

2525
using ICSharpCode.Decompiler.Metadata;
26+
using ICSharpCode.ILSpyX.FileLoaders;
2627
using ICSharpCode.ILSpyX.Settings;
2728

2829
namespace ICSharpCode.ILSpyX
@@ -59,6 +60,8 @@ public AssemblyListManager(ISettingsProvider settingsProvider)
5960

6061
public ObservableCollection<string> AssemblyLists { get; } = new ObservableCollection<string>();
6162

63+
public FileLoaderRegistry LoaderRegistry { get; } = new FileLoaderRegistry();
64+
6265
/// <summary>
6366
/// Loads an assembly list from the ILSpySettings.
6467
/// If no list with the specified name is found, the default list is loaded instead.

ICSharpCode.ILSpyX/AssemblyListSnapshot.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
using ICSharpCode.Decompiler.Metadata;
2828
using ICSharpCode.Decompiler.Util;
2929
using ICSharpCode.ILSpyX.Extensions;
30+
using ICSharpCode.ILSpyX.FileLoaders;
3031

3132
namespace ICSharpCode.ILSpyX
3233
{
@@ -156,7 +157,7 @@ public async Task<IList<LoadedAssembly>> GetAllAssembliesAsync()
156157

157158
foreach (var asm in assemblies)
158159
{
159-
LoadedAssembly.LoadResult result;
160+
LoadResult result;
160161
try
161162
{
162163
result = await asm.GetLoadResultAsync().ConfigureAwait(false);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2024 Siegfried Pammer
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4+
// software and associated documentation files (the "Software"), to deal in the Software
5+
// without restriction, including without limitation the rights to use, copy, modify, merge,
6+
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7+
// to whom the Software is furnished to do so, subject to the following conditions:
8+
//
9+
// The above copyright notice and this permission notice shall be included in all copies or
10+
// substantial portions of the Software.
11+
//
12+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14+
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15+
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17+
// DEALINGS IN THE SOFTWARE.
18+
19+
using System.IO;
20+
21+
namespace ICSharpCode.ILSpyX.FileLoaders
22+
{
23+
public sealed class ArchiveFileLoader : IFileLoader
24+
{
25+
public LoadResult? Load(string fileName, Stream stream, FileLoadSettings settings)
26+
{
27+
try
28+
{
29+
var zip = LoadedPackage.FromZipFile(fileName);
30+
if (zip != null)
31+
{
32+
return new LoadResult { Package = zip };
33+
}
34+
return null;
35+
}
36+
catch (InvalidDataException)
37+
{
38+
return null;
39+
}
40+
}
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2024 Siegfried Pammer
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4+
// software and associated documentation files (the "Software"), to deal in the Software
5+
// without restriction, including without limitation the rights to use, copy, modify, merge,
6+
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7+
// to whom the Software is furnished to do so, subject to the following conditions:
8+
//
9+
// The above copyright notice and this permission notice shall be included in all copies or
10+
// substantial portions of the Software.
11+
//
12+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14+
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15+
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17+
// DEALINGS IN THE SOFTWARE.
18+
19+
using System.IO;
20+
21+
namespace ICSharpCode.ILSpyX.FileLoaders
22+
{
23+
public sealed class BundleFileLoader : IFileLoader
24+
{
25+
public LoadResult? Load(string fileName, Stream stream, FileLoadSettings settings)
26+
{
27+
var bundle = LoadedPackage.FromBundle(fileName);
28+
if (bundle != null)
29+
{
30+
return new LoadResult { Package = bundle };
31+
}
32+
return null;
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) 2024 Siegfried Pammer
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4+
// software and associated documentation files (the "Software"), to deal in the Software
5+
// without restriction, including without limitation the rights to use, copy, modify, merge,
6+
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7+
// to whom the Software is furnished to do so, subject to the following conditions:
8+
//
9+
// The above copyright notice and this permission notice shall be included in all copies or
10+
// substantial portions of the Software.
11+
//
12+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14+
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15+
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17+
// DEALINGS IN THE SOFTWARE.
18+
19+
using System;
20+
using System.Collections.Generic;
21+
22+
namespace ICSharpCode.ILSpyX.FileLoaders
23+
{
24+
public sealed class FileLoaderRegistry
25+
{
26+
readonly List<IFileLoader> registeredLoaders = new List<IFileLoader>();
27+
28+
public IReadOnlyList<IFileLoader> RegisteredLoaders => registeredLoaders;
29+
30+
public void Register(IFileLoader loader)
31+
{
32+
if (loader is null)
33+
{
34+
throw new ArgumentNullException(nameof(loader));
35+
}
36+
37+
registeredLoaders.Add(loader);
38+
}
39+
40+
public FileLoaderRegistry()
41+
{
42+
Register(new XamarinCompressedFileLoader());
43+
Register(new WebCilFileLoader());
44+
Register(new MetadataFileLoader());
45+
Register(new BundleFileLoader());
46+
Register(new ArchiveFileLoader());
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2024 Siegfried Pammer
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4+
// software and associated documentation files (the "Software"), to deal in the Software
5+
// without restriction, including without limitation the rights to use, copy, modify, merge,
6+
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7+
// to whom the Software is furnished to do so, subject to the following conditions:
8+
//
9+
// The above copyright notice and this permission notice shall be included in all copies or
10+
// substantial portions of the Software.
11+
//
12+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14+
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15+
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17+
// DEALINGS IN THE SOFTWARE.
18+
19+
using System;
20+
using System.IO;
21+
22+
using ICSharpCode.Decompiler.Metadata;
23+
24+
namespace ICSharpCode.ILSpyX.FileLoaders
25+
{
26+
public sealed class LoadResult
27+
{
28+
public MetadataFile? MetadataFile { get; init; }
29+
public Exception? FileLoadException { get; init; }
30+
public LoadedPackage? Package { get; init; }
31+
}
32+
33+
public record FileLoadSettings(bool ApplyWinRTProjections);
34+
35+
public interface IFileLoader
36+
{
37+
LoadResult? Load(string fileName, Stream stream, FileLoadSettings settings);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) 2024 Siegfried Pammer
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4+
// software and associated documentation files (the "Software"), to deal in the Software
5+
// without restriction, including without limitation the rights to use, copy, modify, merge,
6+
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7+
// to whom the Software is furnished to do so, subject to the following conditions:
8+
//
9+
// The above copyright notice and this permission notice shall be included in all copies or
10+
// substantial portions of the Software.
11+
//
12+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14+
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15+
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17+
// DEALINGS IN THE SOFTWARE.
18+
19+
using System;
20+
using System.IO;
21+
using System.Reflection.Metadata;
22+
23+
using ICSharpCode.Decompiler.Metadata;
24+
25+
using static ICSharpCode.Decompiler.Metadata.MetadataFile;
26+
27+
namespace ICSharpCode.ILSpyX.FileLoaders
28+
{
29+
public sealed class MetadataFileLoader : IFileLoader
30+
{
31+
public LoadResult? Load(string fileName, Stream stream, FileLoadSettings settings)
32+
{
33+
try
34+
{
35+
var kind = Path.GetExtension(fileName).Equals(".pdb", StringComparison.OrdinalIgnoreCase)
36+
? MetadataFileKind.ProgramDebugDatabase : MetadataFileKind.Metadata;
37+
var metadata = MetadataReaderProvider.FromMetadataStream(stream, MetadataStreamOptions.PrefetchMetadata | MetadataStreamOptions.LeaveOpen);
38+
var metadataFile = new MetadataFile(kind, fileName, metadata);
39+
return new LoadResult { MetadataFile = metadataFile };
40+
}
41+
catch (BadImageFormatException)
42+
{
43+
return null;
44+
}
45+
}
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2024 Siegfried Pammer
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4+
// software and associated documentation files (the "Software"), to deal in the Software
5+
// without restriction, including without limitation the rights to use, copy, modify, merge,
6+
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7+
// to whom the Software is furnished to do so, subject to the following conditions:
8+
//
9+
// The above copyright notice and this permission notice shall be included in all copies or
10+
// substantial portions of the Software.
11+
//
12+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14+
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15+
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17+
// DEALINGS IN THE SOFTWARE.
18+
19+
using System.IO;
20+
using System.Reflection.Metadata;
21+
22+
using ICSharpCode.Decompiler.Metadata;
23+
24+
namespace ICSharpCode.ILSpyX.FileLoaders
25+
{
26+
public sealed class WebCilFileLoader : IFileLoader
27+
{
28+
public LoadResult? Load(string fileName, Stream stream, FileLoadSettings settings)
29+
{
30+
MetadataReaderOptions options = settings.ApplyWinRTProjections
31+
? MetadataReaderOptions.ApplyWindowsRuntimeProjections
32+
: MetadataReaderOptions.None;
33+
34+
var wasm = WebCilFile.FromStream(fileName, options);
35+
if (wasm != null)
36+
{
37+
return new LoadResult { MetadataFile = wasm };
38+
}
39+
return null;
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)