@@ -21,9 +21,9 @@ namespace MonkeyLoader
2121 /// </summary>
2222 public sealed class AssemblyPool : IAssemblyResolver
2323 {
24- private readonly Dictionary < AssemblyName , AssemblyEntry > _assemblies = new ( ) ;
24+ private readonly Dictionary < AssemblyName , AssemblyEntry > _assemblies = [ ] ;
2525 private readonly HashSet < string > _directories = new ( StringComparer . OrdinalIgnoreCase ) ;
26- private readonly HashSet < AssemblyPool > _fallbackPools = new ( ) ;
26+ private readonly HashSet < AssemblyPool > _fallbackPools = [ ] ;
2727 private readonly Func < string ? > ? _getPatchedAssemblyPath ;
2828 private readonly Logger _logger ;
2929
@@ -39,6 +39,8 @@ public sealed class AssemblyPool : IAssemblyResolver
3939 /// <summary>
4040 /// Creates a new <see cref="AssemblyPool"/> instance, loading assemblies when asked to resolve them if desired.
4141 /// </summary>
42+ /// <param name="loader">The mod loader that this pool belongs to.</param>
43+ /// <param name="poolName">The name of this pool.</param>
4244 /// <param name="getPatchedAssemblyPath">Provides the path where to save patched assemblies. Return <c>null</c> to disable.</param>
4345 /// <param name="loadForResolve">Whether to load assemblies when asked to resolve them.</param>
4446 public AssemblyPool ( MonkeyLoader loader , string poolName = "AssemblyPool" , Func < string ? > ? getPatchedAssemblyPath = null , bool loadForResolve = true )
@@ -51,16 +53,14 @@ public AssemblyPool(MonkeyLoader loader, string poolName = "AssemblyPool", Func<
5153 AppDomain . CurrentDomain . AssemblyResolve += ResolveAssembly ;
5254 }
5355
54- public bool AddFallbackPool ( AssemblyPool pool ) => _fallbackPools . Add ( pool ) ;
56+ public bool AddFallbackPool ( AssemblyPool pool )
57+ => _fallbackPools . Add ( pool ) ;
5558
5659 public void AddSearchDirectory ( string directory )
57- {
58- _directories . Add ( directory ) ;
59- }
60+ => _directories . Add ( directory ) ;
6061
6162 public void Dispose ( )
62- {
63- }
63+ { }
6464
6565 public IEnumerable < ILoadedNuGetPackage > GetAllAsLoadedPackages ( string optionalPrefix )
6666 {
@@ -131,18 +131,6 @@ public void LoadAll(string path)
131131 /// <returns>The loaded <see cref="Assembly"/>.</returns>
132132 /// <exception cref="KeyNotFoundException">When the <paramref name="name"/> doesn't exist in this pool.</exception>
133133 public Assembly LoadAssembly ( AssemblyName name ) => GetEntry ( name ) . LoadAssembly ( _logger , PatchedAssemblyPath ) ;
134-
135- public bool TryResolveAssembly ( AssemblyName name , [ NotNullWhen ( true ) ] out Assembly ? assembly )
136- {
137- if ( TryGetEntry ( name , out var entry ) )
138- {
139- assembly = entry . LoadAssembly ( _logger , PatchedAssemblyPath ) ;
140- return true ;
141- }
142-
143- assembly = null ;
144- return false ;
145- }
146134
147135 public AssemblyDefinition LoadDefinition ( string path , ReaderParameters ? readerParameters = null )
148136 {
@@ -231,6 +219,18 @@ public bool TryResolve(AssemblyName name, [NotNullWhen(true)] out AssemblyDefini
231219 return false ;
232220 }
233221
222+ public bool TryResolveAssembly ( AssemblyName name , [ NotNullWhen ( true ) ] out Assembly ? assembly )
223+ {
224+ if ( TryGetEntry ( name , out var entry ) )
225+ {
226+ assembly = entry . LoadAssembly ( _logger , PatchedAssemblyPath ) ;
227+ return true ;
228+ }
229+
230+ assembly = null ;
231+ return false ;
232+ }
233+
234234 /// <summary>
235235 /// Tries to wait until nothing else is modifying the <see cref="AssemblyDefinition"/> of an entry anymore,
236236 /// before making a snapshot and returning it. The definition has to be returned using
@@ -271,7 +271,7 @@ private AssemblyEntry GetEntry(AssemblyName name)
271271 return assemblyDefinition ;
272272 }
273273
274- private Assembly ? ResolveAssembly ( object sender , ResolveEventArgs args )
274+ private Assembly ? ResolveAssembly ( object ? sender , ResolveEventArgs args )
275275 {
276276 var name = new AssemblyName ( AssemblyNameReference . Parse ( args . Name ) . Name ) ;
277277
@@ -287,7 +287,7 @@ private AssemblyEntry GetEntry(AssemblyName name)
287287 private AssemblyDefinition ? SearchDirectory ( AssemblyNameReference name , ReaderParameters parameters )
288288 {
289289 parameters . AssemblyResolver ??= this ;
290- var extensions = name . IsWindowsRuntime ? new [ ] { ".winmd" , ".dll" } : new [ ] { ".exe" , ".dll" } ;
290+ string [ ] extensions = name . IsWindowsRuntime ? [ ".winmd" , ".dll" ] : [ ".exe" , ".dll" ] ;
291291
292292 foreach ( var directory in _directories )
293293 {
@@ -310,15 +310,15 @@ private AssemblyEntry GetEntry(AssemblyName name)
310310 return null ;
311311 }
312312
313- private bool TryGetEntry ( AssemblyName name , out AssemblyEntry entry )
313+ private bool TryGetEntry ( AssemblyName name , [ NotNullWhen ( true ) ] out AssemblyEntry ? entry )
314314 => _assemblies . TryGetValue ( name , out entry ) ;
315315
316316 private sealed class AssemblyEntry : IDisposable
317317 {
318318 public readonly FileInfo ? AssemblyFile ;
319319 public readonly AssemblyName Name ;
320- private AssemblyDefinition _definition ;
321320 private readonly IAssemblyLoadStrategy _assemblyLoadStrategy ;
321+ private AssemblyDefinition _definition ;
322322 private AutoResetEvent ? _definitionLock ;
323323 private MemoryStream ? _definitionSnapshot ;
324324 private bool _disposedValue = false ;
@@ -361,7 +361,7 @@ public IEnumerable<AssemblyName> GetDependencies(HashSet<AssemblyName> alreadyLo
361361 : _definition . GetAssemblyReferences ( )
362362 . Select ( reference => AssemblyNameReference . Parse ( reference . Name ) . Name ) ;
363363
364- return fullNames . Select ( name => new AssemblyName ( name ) )
364+ return fullNames . Select ( name => new AssemblyName ( name ! ) )
365365 . Where ( name => ! alreadyLoaded . Contains ( name ) ) ;
366366 }
367367
@@ -387,7 +387,7 @@ public Assembly LoadAssembly(Logger logger, string? patchedAssemblyPath)
387387 // so let's always load assemblies from files for now
388388 if ( saveAssemblies )
389389 {
390- var targetPath = Path . Combine ( patchedAssemblyPath , $ "{ Name } .dll") ;
390+ var targetPath = Path . Combine ( patchedAssemblyPath ! , $ "{ Name } .dll") ;
391391
392392 try
393393 {
@@ -398,7 +398,7 @@ public Assembly LoadAssembly(Logger logger, string? patchedAssemblyPath)
398398 {
399399 logger . Warn ( ex . LogFormat ( $ "Exception while trying to save assembly to { targetPath } ") ) ;
400400 }
401-
401+
402402 _loadedAssembly = _assemblyLoadStrategy . LoadFile ( Path . GetFullPath ( targetPath ) ) ;
403403 logger . Trace ( ( ) => $ "Loaded changed assembly definition [{ Name } ]") ;
404404 }
0 commit comments