11using System . Collections . Generic ;
2+ using System . Diagnostics . CodeAnalysis ;
23using System . IO ;
34using System . Linq ;
45using System . Reflection ;
@@ -8,12 +9,13 @@ namespace NetcodePatcher.Tools.Common;
89
910class PatcherLoadContext : AssemblyLoadContext
1011{
12+ private static readonly HashSet < string > SpecificAssemblyNames = [ "NetcodePatcher" , "Unity.Netcode.Runtime" ] ;
1113 private static readonly HashSet < string > SharedDependencyAssemblyNames = [ "Serilog" ] ;
12- private readonly AssemblyDependencyResolver _resolver ;
14+ private readonly PatcherConfiguration _configuration ;
1315
14- public PatcherLoadContext ( string name , string pluginPath ) : base ( name )
16+ public PatcherLoadContext ( string name , PatcherConfiguration configuration ) : base ( name )
1517 {
16- _resolver = new AssemblyDependencyResolver ( pluginPath ) ;
18+ _configuration = configuration ;
1719 }
1820
1921 protected override Assembly ? Load ( AssemblyName assemblyName )
@@ -22,13 +24,49 @@ public PatcherLoadContext(string name, string pluginPath) : base(name)
2224 . FirstOrDefault ( assembly => assembly . GetName ( ) == assemblyName ) ;
2325 if ( loadedAssembly is not null ) return loadedAssembly ;
2426
25- string ? assemblyPath = _resolver . ResolveAssemblyToPath ( assemblyName ) ;
27+ if ( assemblyName . Name is null ) return null ;
28+
29+ if ( SharedDependencyAssemblyNames . Contains ( assemblyName . Name ) )
30+ return Default . LoadFromAssemblyName ( assemblyName ) ;
31+
32+ string ? assemblyPath = ResolveAssemblyToPath ( assemblyName ) ;
2633 if ( assemblyPath is null ) return null ;
34+ return LoadFromAssemblyPath ( assemblyPath ) ;
35+ }
2736
28- if ( SharedDependencyAssemblyNames . Contains ( Path . GetFileNameWithoutExtension ( assemblyPath ) ) ) {
29- return Default . LoadFromAssemblyPath ( assemblyPath ) ;
37+ private string ? ResolveAssemblyToPath ( AssemblyName assemblyName )
38+ {
39+ if ( assemblyName . Name is null ) return null ;
40+
41+ if ( SpecificAssemblyNames . Contains ( assemblyName . Name ) )
42+ {
43+ if ( TryResolveSpecificAssemblyToPath ( assemblyName , out var specificPath ) )
44+ return specificPath ;
3045 }
3146
32- return LoadFromAssemblyPath ( assemblyPath ) ;
47+ if ( TryResolveCommonAssemblyToPath ( assemblyName , out var commonPath ) )
48+ return commonPath ;
49+
50+ return null ;
51+
52+ bool TryResolveCommonAssemblyToPath ( AssemblyName assemblyName , [ MaybeNullWhen ( false ) ] out string path )
53+ {
54+ path = Path . Combine ( _configuration . PatcherCommonAssemblyDir , $ "{ assemblyName . Name } .dll") ;
55+ if ( File . Exists ( path ) )
56+ return true ;
57+
58+ path = null ;
59+ return false ;
60+ }
61+
62+ bool TryResolveSpecificAssemblyToPath ( AssemblyName assemblyName , [ MaybeNullWhen ( false ) ] out string path )
63+ {
64+ path = Path . Combine ( _configuration . PatcherNetcodeSpecificAssemblyDir , $ "{ assemblyName . Name } .dll") ;
65+ if ( File . Exists ( path ) )
66+ return true ;
67+
68+ path = null ;
69+ return false ;
70+ }
3371 }
3472}
0 commit comments