@@ -8,25 +8,23 @@ internal static partial class NativeLibrary
88{
99 private static readonly Assembly s_assembly = typeof ( NativeLibrary ) . Assembly ;
1010
11- private static bool s_isAgentServer ;
12- private static readonly List < string > s_searchPath = [ ] ;
13- private static readonly Dictionary < string , nint > s_libraryHandles = [ ] ;
14-
1511#pragma warning disable CA2255 // 不应在库中使用 “ModuleInitializer” 属性
16- [ ModuleInitializer ]
17- internal static void SetNativeAssemblyResolver ( ) => SetDllImportResolver ( s_assembly , NativeAssemblyResolver ) ;
18- #pragma warning restore CA2255 // 不应在库中使用 “ModuleInitializer” 属性
12+ #pragma warning disable S2223 // Non-constant static fields should not be visible
1913
20- public static void Init ( bool isAgentServer , params string [ ] paths )
21- {
22- if ( s_libraryHandles . Count != 0 )
23- throw new InvalidOperationException ( "NativeLibrary is already loaded." ) ;
14+ internal static bool IsLoaded ;
15+ internal static string LoadedDirectory = string . Empty ;
16+ internal static readonly Dictionary < string , nint > LoadedLibraryHandles = [ ] ;
2417
25- s_isAgentServer = isAgentServer ;
26- s_searchPath . AddRange ( paths ) ;
27- }
18+ internal static ApiInfoFlags ApiInfo ;
19+ internal static readonly List < string > SearchPath = [ ] ;
20+
21+ [ ModuleInitializer ]
22+ internal static void SetNativeLibraryResolver ( ) => SetDllImportResolver ( s_assembly , NativeLibraryResolver ) ;
23+
24+ #pragma warning restore S2223 // Non-constant static fields should not be visible
25+ #pragma warning restore CA2255 // 不应在库中使用 “ModuleInitializer” 属性
2826
29- public static IntPtr NativeAssemblyResolver ( string libraryName , Assembly assembly , DllImportSearchPath ? searchPath ) => libraryName switch
27+ public static nint NativeLibraryResolver ( string libraryName , Assembly assembly , DllImportSearchPath ? searchPath ) => libraryName switch
3028 {
3129 "MaaFramework" or "MaaToolkit"
3230 or "MaaAgentServer" or "MaaAgentClient" => GetLibraryHandle ( libraryName ) ,
@@ -36,32 +34,36 @@ public static void Init(bool isAgentServer, params string[] paths)
3634
3735 private static nint GetLibraryHandle ( string libraryName )
3836 {
39- if ( s_libraryHandles . TryGetValue ( libraryName , out var libraryHandle ) )
37+ if ( LoadedLibraryHandles . TryGetValue ( libraryName , out var libraryHandle ) )
4038 return libraryHandle ;
4139
42- if ( ! TryGetRuntimesPath ( libraryName , out var dllPath ) || ! TryLoad ( dllPath , out libraryHandle ) )
43- {
44- s_libraryHandles . Add ( libraryName , nint . Zero ) ;
45- NativeBindingInfo . NativeAssemblyDirectory = null ;
46- NativeBindingInfo . IsStatelessMode = false ;
47- NativeBindingInfo . ApiInfo = "Using default dll resolver." ;
48- return nint . Zero ;
49- }
40+ IsLoaded = true ;
41+ var resolver = TryGetRuntimesPath ( libraryName , out var dllPath ) && TryLoad ( dllPath , out libraryHandle )
42+ ? ApiInfoFlags . UseBindingResolver
43+ : ApiInfoFlags . UseDefaultResolver ;
44+ SetBindingContext (
45+ resolver ,
46+ dllDir : Path . GetDirectoryName ( dllPath ) ?? string . Empty ,
47+ isFirst : LoadedLibraryHandles . Count == 0 ) ;
48+
49+ LoadedLibraryHandles . Add ( libraryName , libraryHandle ) ;
50+ return libraryHandle ;
51+ }
5052
51- s_libraryHandles . Add ( libraryName , libraryHandle ) ;
53+ private static void SetBindingContext ( ApiInfoFlags resolver , string dllDir , bool isFirst )
54+ {
55+ if ( ApiInfo . HasFlag_ResolverExcept ( resolver ) )
56+ throw new InvalidOperationException ( $ "The resolver '{ ApiInfo } ' was attempted to switch to '{ resolver } '.") ;
5257
53- var dllDir = Path . GetDirectoryName ( dllPath ) ;
54- if ( s_libraryHandles . Count == 1 )
55- {
56- NativeBindingInfo . NativeAssemblyDirectory ??= dllDir ;
57- NativeBindingInfo . IsStatelessMode = s_isAgentServer ;
58- NativeBindingInfo . ApiInfo = s_isAgentServer ? "In MaaAgentServer context." : "In MaaFramework context." ;
59- }
58+ if ( isFirst )
59+ LoadedDirectory = dllDir ;
6060
61- if ( NativeBindingInfo . NativeAssemblyDirectory != dllDir )
62- throw new InvalidOperationException ( $ "The native assembly directory '{ NativeBindingInfo . NativeAssemblyDirectory } ' was switched to '{ dllDir } '.") ;
61+ if ( LoadedDirectory != dllDir )
62+ throw new InvalidOperationException ( $ "The native library directory '{ LoadedDirectory } ' was attempted to switch to '{ dllDir } '.") ;
6363
64- return libraryHandle ;
64+ ApiInfo |= resolver ;
65+ if ( ! ApiInfo . HasFlag_Context ( ) )
66+ ApiInfo |= ApiInfoFlags . InFrameworkContext ;
6567 }
6668
6769 private static bool TryGetRuntimesPath ( string libraryName , out string dllPath )
@@ -73,7 +75,7 @@ private static bool TryGetRuntimesPath(string libraryName, out string dllPath)
7375
7476 private static IEnumerable < string > GetRuntimesPaths ( string libraryFullName )
7577 {
76- var searchPaths = s_searchPath . Concat (
78+ var searchPaths = SearchPath . Concat (
7779 [
7880 Environment . GetEnvironmentVariable ( "MAAFW_BINARY_PATH" ) ,
7981 Path . GetDirectoryName ( s_assembly . Location ) ,
@@ -82,8 +84,8 @@ private static IEnumerable<string> GetRuntimesPaths(string libraryFullName)
8284
8385 var runtimePaths = new [ ]
8486 {
87+ "./" ,
8588 $ "./runtimes/{ GetArchitectureName ( ) } /native/",
86- "./"
8789 } ;
8890
8991 return from searchPath in searchPaths
@@ -109,7 +111,7 @@ select Path.GetFullPath(
109111
110112 private static string GetFullLibraryName ( string libraryName )
111113 {
112- if ( s_isAgentServer && libraryName == "MaaFramework" )
114+ if ( libraryName == "MaaFramework" && ApiInfo . HasFlag ( ApiInfoFlags . InAgentServerContext ) )
113115 libraryName = "MaaAgentServer" ;
114116
115117 if ( IsWindows )
0 commit comments