-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
LoadedPackage.cs
358 lines (318 loc) · 10.6 KB
/
LoadedPackage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.Metadata;
namespace ICSharpCode.ILSpyX
{
/// <summary>
/// NuGet package or .NET bundle:
/// </summary>
public class LoadedPackage
{
public enum PackageKind
{
Zip,
Bundle,
}
/// <summary>
/// Gets the LoadedAssembly instance representing this bundle.
/// </summary>
internal LoadedAssembly? LoadedAssembly { get; set; }
public PackageKind Kind { get; }
public SingleFileBundle.Header BundleHeader { get; set; }
/// <summary>
/// List of all entries, including those in sub-directories within the package.
/// </summary>
public IReadOnlyList<PackageEntry> Entries { get; }
public PackageFolder RootFolder { get; }
public LoadedPackage(PackageKind kind, IEnumerable<PackageEntry> entries)
{
this.Kind = kind;
this.Entries = entries.ToArray();
var topLevelEntries = new List<PackageEntry>();
var folders = new Dictionary<string, PackageFolder>();
var rootFolder = new PackageFolder(this, null, "");
folders.Add("", rootFolder);
foreach (var entry in this.Entries)
{
var (dirname, filename) = SplitName(entry.Name);
if (!string.IsNullOrEmpty(filename))
{
GetFolder(dirname).Entries.Add(new FolderEntry(filename, entry));
}
}
this.RootFolder = rootFolder;
static (string, string) SplitName(string filename)
{
int pos = filename.LastIndexOfAny(new char[] { '/', '\\' });
if (pos == -1)
return ("", filename); // file in root
else
return (filename.Substring(0, pos), filename.Substring(pos + 1));
}
PackageFolder GetFolder(string name)
{
if (folders.TryGetValue(name, out var result))
return result;
var (dirname, basename) = SplitName(name);
PackageFolder parent = GetFolder(dirname);
result = new PackageFolder(this, parent, basename);
parent.Folders.Add(result);
folders.Add(name, result);
return result;
}
}
public static LoadedPackage FromZipFile(string file)
{
Debug.WriteLine($"LoadedPackage.FromZipFile({file})");
using var archive = ZipFile.OpenRead(file);
return new LoadedPackage(PackageKind.Zip,
archive.Entries.Select(entry => new ZipFileEntry(file, entry)));
}
/// <summary>
/// Load a .NET single-file bundle.
/// </summary>
public static LoadedPackage? FromBundle(string fileName)
{
using var memoryMappedFile = MemoryMappedFile.CreateFromFile(fileName, FileMode.Open, null, 0, MemoryMappedFileAccess.Read);
var view = memoryMappedFile.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read);
try
{
if (!SingleFileBundle.IsBundle(view, out long bundleHeaderOffset))
return null;
var manifest = SingleFileBundle.ReadManifest(view, bundleHeaderOffset);
var entries = manifest.Entries.Select(e => new BundleEntry(fileName, view, e)).ToList();
var result = new LoadedPackage(PackageKind.Bundle, entries);
result.BundleHeader = manifest;
view = null; // don't dispose the view, we're still using it in the bundle entries
return result;
}
catch (InvalidDataException)
{
return null;
}
finally
{
view?.Dispose();
}
}
/// <summary>
/// Entry inside a package folder. Effectively renames the entry.
/// </summary>
sealed class FolderEntry : PackageEntry
{
readonly PackageEntry originalEntry;
public override string Name { get; }
public FolderEntry(string name, PackageEntry originalEntry)
{
this.Name = name;
this.originalEntry = originalEntry;
}
public override ManifestResourceAttributes Attributes => originalEntry.Attributes;
public override string FullName => originalEntry.FullName;
public override ResourceType ResourceType => originalEntry.ResourceType;
public override Stream? TryOpenStream() => originalEntry.TryOpenStream();
public override long? TryGetLength() => originalEntry.TryGetLength();
}
sealed class ZipFileEntry : PackageEntry
{
readonly string zipFile;
public override string Name { get; }
public override string FullName => $"zip://{zipFile};{Name}";
public ZipFileEntry(string zipFile, ZipArchiveEntry entry)
{
this.zipFile = zipFile;
this.Name = entry.FullName;
}
public override Stream? TryOpenStream()
{
Debug.WriteLine("Decompress " + Name);
using var archive = ZipFile.OpenRead(zipFile);
var entry = archive.GetEntry(Name);
if (entry == null)
return null;
var memoryStream = new MemoryStream();
using (var s = entry.Open())
{
s.CopyTo(memoryStream);
}
memoryStream.Position = 0;
return memoryStream;
}
public override long? TryGetLength()
{
Debug.WriteLine("TryGetLength " + Name);
using var archive = ZipFile.OpenRead(zipFile);
var entry = archive.GetEntry(Name);
if (entry == null)
return null;
return entry.Length;
}
}
sealed class BundleEntry : PackageEntry
{
readonly string bundleFile;
readonly MemoryMappedViewAccessor view;
readonly SingleFileBundle.Entry entry;
public BundleEntry(string bundleFile, MemoryMappedViewAccessor view, SingleFileBundle.Entry entry)
{
this.bundleFile = bundleFile;
this.view = view;
this.entry = entry;
}
public override string Name => entry.RelativePath;
public override string FullName => $"bundle://{bundleFile};{Name}";
public override Stream TryOpenStream()
{
Debug.WriteLine("Open bundle member " + Name);
if (entry.CompressedSize == 0)
{
return new UnmanagedMemoryStream(view.SafeMemoryMappedViewHandle, entry.Offset, entry.Size);
}
else
{
Stream compressedStream = new UnmanagedMemoryStream(view.SafeMemoryMappedViewHandle, entry.Offset, entry.CompressedSize);
using var deflateStream = new DeflateStream(compressedStream, CompressionMode.Decompress);
Stream decompressedStream = new MemoryStream((int)entry.Size);
deflateStream.CopyTo(decompressedStream);
if (decompressedStream.Length != entry.Size)
{
throw new InvalidDataException($"Corrupted single-file entry '{entry.RelativePath}'. Declared decompressed size '{entry.Size}' is not the same as actual decompressed size '{decompressedStream.Length}'.");
}
decompressedStream.Seek(0, SeekOrigin.Begin);
return decompressedStream;
}
}
public override long? TryGetLength()
{
return entry.Size;
}
}
}
public abstract class PackageEntry : Resource
{
/// <summary>
/// Gets the file name of the entry (may include path components, relative to the package root).
/// </summary>
public abstract override string Name { get; }
/// <summary>
/// Gets the full file name for the entry.
/// </summary>
public abstract string FullName { get; }
}
public sealed class PackageFolder : IAssemblyResolver
{
/// <summary>
/// Gets the short name of the folder.
/// </summary>
public string Name { get; }
readonly LoadedPackage package;
readonly PackageFolder? parent;
internal PackageFolder(LoadedPackage package, PackageFolder? parent, string name)
{
this.package = package;
this.parent = parent;
this.Name = name;
}
public List<PackageFolder> Folders { get; } = new List<PackageFolder>();
public List<PackageEntry> Entries { get; } = new List<PackageEntry>();
public MetadataFile? Resolve(IAssemblyReference reference)
{
var asm = ResolveFileName(reference.Name + ".dll");
if (asm != null)
{
return asm.GetMetadataFileOrNull();
}
return parent?.Resolve(reference);
}
public Task<MetadataFile?> ResolveAsync(IAssemblyReference reference)
{
var asm = ResolveFileName(reference.Name + ".dll");
if (asm != null)
{
return asm.GetMetadataFileOrNullAsync();
}
if (parent != null)
{
return parent.ResolveAsync(reference);
}
return Task.FromResult<MetadataFile?>(null);
}
public MetadataFile? ResolveModule(MetadataFile mainModule, string moduleName)
{
var asm = ResolveFileName(moduleName + ".dll");
if (asm != null)
{
return asm.GetMetadataFileOrNull();
}
return parent?.ResolveModule(mainModule, moduleName);
}
public Task<MetadataFile?> ResolveModuleAsync(MetadataFile mainModule, string moduleName)
{
var asm = ResolveFileName(moduleName + ".dll");
if (asm != null)
{
return asm.GetMetadataFileOrNullAsync();
}
if (parent != null)
{
return parent.ResolveModuleAsync(mainModule, moduleName);
}
return Task.FromResult<MetadataFile?>(null);
}
readonly Dictionary<string, LoadedAssembly?> assemblies = new Dictionary<string, LoadedAssembly?>(StringComparer.OrdinalIgnoreCase);
public LoadedAssembly? ResolveFileName(string name)
{
if (package.LoadedAssembly == null)
return null;
lock (assemblies)
{
if (assemblies.TryGetValue(name, out var asm))
return asm;
var entry = Entries.FirstOrDefault(e => string.Equals(name, e.Name, StringComparison.OrdinalIgnoreCase));
if (entry != null)
{
asm = new LoadedAssembly(
package.LoadedAssembly, entry.Name,
fileLoaders: package.LoadedAssembly.AssemblyList.LoaderRegistry,
assemblyResolver: this,
stream: Task.Run(entry.TryOpenStream),
applyWinRTProjections: package.LoadedAssembly.AssemblyList.ApplyWinRTProjections,
useDebugSymbols: package.LoadedAssembly.AssemblyList.UseDebugSymbols
);
}
else
{
asm = null;
}
assemblies.Add(name, asm);
return asm;
}
}
}
}