21
21
using System . Text . RegularExpressions ;
22
22
using ILRepacking . Steps ;
23
23
using Mono . Cecil ;
24
+ using Mono . Cecil . Cil ;
24
25
using Mono . Cecil . PE ;
25
26
using Mono . Unix . Native ;
26
27
using ILRepacking . Mixins ;
@@ -112,9 +113,10 @@ private AssemblyDefinitionContainer ReadInputAssembly(string assembly, bool isPr
112
113
{
113
114
ReaderParameters rp = new ReaderParameters ( ReadingMode . Immediate ) { AssemblyResolver = GlobalAssemblyResolver } ;
114
115
// read PDB/MDB?
115
- if ( Options . DebugInfo && ( File . Exists ( Path . ChangeExtension ( assembly , "pdb" ) ) || File . Exists ( assembly + ".mdb" ) ) )
116
+ if ( Options . DebugInfo )
116
117
{
117
118
rp . ReadSymbols = true ;
119
+ rp . SymbolReaderProvider = new DefaultSymbolReaderProvider ( throwIfNoSymbol : false ) ;
118
120
}
119
121
AssemblyDefinition mergeAsm ;
120
122
try
@@ -144,6 +146,7 @@ private AssemblyDefinitionContainer ReadInputAssembly(string assembly, bool isPr
144
146
145
147
if ( ! Options . AllowZeroPeKind && ( mergeAsm . MainModule . Attributes & ModuleAttributes . ILOnly ) == 0 )
146
148
throw new ArgumentException ( "Failed to load assembly with Zero PeKind: " + assembly ) ;
149
+ GlobalAssemblyResolver . RegisterAssembly ( mergeAsm ) ;
147
150
148
151
return new AssemblyDefinitionContainer
149
152
{
@@ -264,7 +267,6 @@ public void Repack()
264
267
265
268
// Read input assemblies only after all properties are set.
266
269
ReadInputAssemblies ( ) ;
267
- GlobalAssemblyResolver . RegisterAssemblies ( MergedAssemblies ) ;
268
270
269
271
_platformFixer = new PlatformFixer ( this , PrimaryAssemblyMainModule . Runtime ) ;
270
272
_mappingHandler = new MappingHandler ( ) ;
@@ -309,7 +311,7 @@ public void Repack()
309
311
}
310
312
// set the main module attributes
311
313
TargetAssemblyMainModule . Attributes = PrimaryAssemblyMainModule . Attributes ;
312
- TargetAssemblyMainModule . Win32ResourceDirectory = MergeWin32Resources ( PrimaryAssemblyMainModule . Win32ResourceDirectory ) ;
314
+ TargetAssemblyMainModule . Win32ResourceDirectory = MergeWin32Resources ( ) ;
313
315
314
316
if ( Options . Version != null )
315
317
TargetAssemblyDefinition . Name . Version = Options . Version ;
@@ -337,10 +339,17 @@ public void Repack()
337
339
step . Perform ( ) ;
338
340
}
339
341
342
+ var anySymbolReader = MergedAssemblies
343
+ . Select ( m => m . MainModule . SymbolReader )
344
+ . Where ( r => r != null )
345
+ . FirstOrDefault ( ) ;
346
+ var symbolWriterProvider = anySymbolReader ? . GetWriterProvider ( ) ;
340
347
var parameters = new WriterParameters
341
348
{
342
349
StrongNameKeyPair = signingStep . KeyPair ,
343
- WriteSymbols = Options . DebugInfo
350
+ WriteSymbols = Options . DebugInfo && symbolWriterProvider != null ,
351
+ SymbolWriterProvider = symbolWriterProvider ,
352
+ DeterministicMvid = true
344
353
} ;
345
354
// create output directory if it does not exist
346
355
var outputDir = Path . GetDirectoryName ( Options . OutputFile ) ;
@@ -350,11 +359,19 @@ public void Repack()
350
359
Directory . CreateDirectory ( outputDir ) ;
351
360
}
352
361
362
+ Logger . Info ( "Writing output assembly to disk" ) ;
353
363
TargetAssemblyDefinition . Write ( Options . OutputFile , parameters ) ;
354
364
355
365
sourceServerDataStep . Write ( ) ;
356
366
357
- Logger . Info ( "Writing output assembly to disk" ) ;
367
+ foreach ( var assembly in MergedAssemblies )
368
+ {
369
+ assembly . Dispose ( ) ;
370
+ }
371
+
372
+ TargetAssemblyDefinition . Dispose ( ) ;
373
+ GlobalAssemblyResolver . Dispose ( ) ;
374
+
358
375
// If this is an executable and we are on linux/osx we should copy file permissions from
359
376
// the primary assembly
360
377
if ( isUnixEnvironment && ( kind == ModuleKind . Console || kind == ModuleKind . Windows ) )
@@ -405,13 +422,17 @@ private void ResolveSearchDirectories()
405
422
}
406
423
}
407
424
408
- private ResourceDirectory MergeWin32Resources ( ResourceDirectory primary )
425
+ private ResourceDirectory MergeWin32Resources ( )
409
426
{
410
- if ( primary == null )
411
- return null ;
427
+ var primary = PrimaryAssemblyMainModule . ReadWin32ResourceDirectory ( ) ?? new ResourceDirectory ( ) ;
428
+
412
429
foreach ( var ass in OtherAssemblies )
413
430
{
414
- MergeDirectory ( new List < ResourceEntry > ( ) , primary , ass , ass . MainModule . Win32ResourceDirectory ) ;
431
+ var directory = ass . MainModule . ReadWin32ResourceDirectory ( ) ;
432
+ if ( directory != null )
433
+ {
434
+ MergeDirectory ( new List < ResourceEntry > ( ) , primary , ass , directory ) ;
435
+ }
415
436
}
416
437
return primary ;
417
438
}
0 commit comments