99// Restrict the set of directories where the native library is loaded from to safe directories.
1010[ assembly: DefaultDllImportSearchPaths ( DllImportSearchPath . AssemblyDirectory | DllImportSearchPath . ApplicationDirectory | DllImportSearchPath . SafeDirectories ) ]
1111
12- #pragma warning disable IDE1006 // Naming Styles
13-
14- // ReSharper disable InconsistentNaming
1512namespace LibGit2Sharp . Core
1613{
1714 internal static class NativeMethods
@@ -22,15 +19,13 @@ internal static class NativeMethods
2219 // An object tied to the lifecycle of the NativeMethods static class.
2320 // This will handle initialization and shutdown of the underlying
2421 // native library.
25- #pragma warning disable 0414
2622 private static NativeShutdownObject shutdownObject ;
27- #pragma warning restore 0414
2823
2924 static NativeMethods ( )
3025 {
3126 if ( Platform . IsRunningOnNetFramework ( ) || Platform . IsRunningOnNetCore ( ) )
3227 {
33- // Use .NET Core 3.0+ NativeLibrary when available.
28+ // Use NativeLibrary when available.
3429 if ( ! TryUseNativeLibrary ( ) )
3530 {
3631 // NativeLibrary is not available, fall back.
@@ -40,6 +35,7 @@ static NativeMethods()
4035 // If this call succeeds further DllImports will find the library loaded and not attempt to load it again.
4136 // If it fails the next DllImport will load the library from safe directories.
4237 string nativeLibraryPath = GetGlobalSettingsNativeLibraryPath ( ) ;
38+
4339 if ( nativeLibraryPath != null )
4440 {
4541 if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
@@ -61,89 +57,46 @@ static NativeMethods()
6157 private static string GetGlobalSettingsNativeLibraryPath ( )
6258 {
6359 string nativeLibraryDir = GlobalSettings . GetAndLockNativeLibraryPath ( ) ;
60+
6461 if ( nativeLibraryDir == null )
6562 {
6663 return null ;
6764 }
68- return Path . Combine ( nativeLibraryDir , libgit2 + Platform . GetNativeLibraryExtension ( ) ) ;
69- }
70-
71- private delegate bool TryLoadLibraryByNameDelegate ( string libraryName , Assembly assembly , DllImportSearchPath ? searchPath , out IntPtr handle ) ;
72- private delegate bool TryLoadLibraryByPathDelegate ( string libraryPath , out IntPtr handle ) ;
73-
74- static TryLoadLibraryByNameDelegate _tryLoadLibraryByName ;
75- static TryLoadLibraryByPathDelegate _tryLoadLibraryByPath ;
76-
77- static bool TryLoadLibrary ( string libraryName , Assembly assembly , DllImportSearchPath ? searchPath , out IntPtr handle )
78- {
79- if ( _tryLoadLibraryByName == null )
80- {
81- throw new NotSupportedException ( ) ;
82- }
83- return _tryLoadLibraryByName ( libraryName , assembly , searchPath , out handle ) ;
84- }
8565
86- static bool TryLoadLibrary ( string libraryPath , out IntPtr handle )
87- {
88- if ( _tryLoadLibraryByPath == null )
89- {
90- throw new NotSupportedException ( ) ;
91- }
92- return _tryLoadLibraryByPath ( libraryPath , out handle ) ;
66+ return Path . Combine ( nativeLibraryDir , libgit2 + Platform . GetNativeLibraryExtension ( ) ) ;
9367 }
9468
69+ #if NETSTANDARD
70+ private static bool TryUseNativeLibrary ( ) => false ;
71+ #else
9572 private static bool TryUseNativeLibrary ( )
9673 {
97- // NativeLibrary is available in .NET Core 3.0+.
98- // We use reflection to use NativeLibrary so this library can target 'netstandard2.0'.
99-
100- Type dllImportResolverType = Type . GetType ( "System.Runtime.InteropServices.DllImportResolver, System.Runtime.InteropServices" , throwOnError : false ) ;
101- Type nativeLibraryType = Type . GetType ( "System.Runtime.InteropServices.NativeLibrary, System.Runtime.InteropServices" , throwOnError : false ) ;
102- var tryLoadLibraryByName = ( TryLoadLibraryByNameDelegate ) nativeLibraryType ? . GetMethod ( "TryLoad" ,
103- new Type [ ] { typeof ( string ) , typeof ( Assembly ) , typeof ( DllImportSearchPath ? ) , typeof ( IntPtr ) . MakeByRefType ( ) } ) ? . CreateDelegate ( typeof ( TryLoadLibraryByNameDelegate ) ) ;
104- var tryLoadLibraryByPath = ( TryLoadLibraryByPathDelegate ) nativeLibraryType ? . GetMethod ( "TryLoad" ,
105- new Type [ ] { typeof ( string ) , typeof ( IntPtr ) . MakeByRefType ( ) } ) ? . CreateDelegate ( typeof ( TryLoadLibraryByPathDelegate ) ) ;
106- MethodInfo setDllImportResolver = nativeLibraryType ? . GetMethod ( "SetDllImportResolver" , new Type [ ] { typeof ( Assembly ) , dllImportResolverType } ) ;
107-
108- if ( dllImportResolverType == null ||
109- nativeLibraryType == null ||
110- tryLoadLibraryByName == null ||
111- tryLoadLibraryByPath == null ||
112- setDllImportResolver == null )
113- {
114- return false ;
115- }
116-
117- _tryLoadLibraryByPath = tryLoadLibraryByPath ;
118- _tryLoadLibraryByName = tryLoadLibraryByName ;
119-
120- // NativeMethods.SetDllImportResolver(typeof(NativeMethods).Assembly, ResolveDll);
121- object resolveDelegate = typeof ( NativeMethods ) . GetMethod ( nameof ( ResolveDll ) , BindingFlags . NonPublic | BindingFlags . Static ) . CreateDelegate ( dllImportResolverType ) ;
122- setDllImportResolver . Invoke ( null , new object [ ] { typeof ( NativeMethods ) . Assembly , resolveDelegate } ) ;
74+ NativeLibrary . SetDllImportResolver ( typeof ( NativeMethods ) . Assembly , ResolveDll ) ;
12375
12476 return true ;
12577 }
12678
12779 private static IntPtr ResolveDll ( string libraryName , Assembly assembly , DllImportSearchPath ? searchPath )
12880 {
12981 IntPtr handle = IntPtr . Zero ;
82+
13083 if ( libraryName == libgit2 )
13184 {
13285 // Use GlobalSettings.NativeLibraryPath when set.
13386 string nativeLibraryPath = GetGlobalSettingsNativeLibraryPath ( ) ;
134- if ( nativeLibraryPath != null &&
135- TryLoadLibrary ( nativeLibraryPath , out handle ) )
87+
88+ if ( nativeLibraryPath != null && NativeLibrary . TryLoad ( nativeLibraryPath , out handle ) )
13689 {
13790 return handle ;
13891 }
13992
14093 // Use Default DllImport resolution.
141- if ( TryLoadLibrary ( libraryName , assembly , searchPath , out handle ) )
94+ if ( NativeLibrary . TryLoad ( libraryName , assembly , searchPath , out handle ) )
14295 {
14396 return handle ;
14497 }
14598
146- // We cary a number of .so files for Linux which are linked against various
99+ // We carry a number of .so files for Linux which are linked against various
147100 // libc/OpenSSL libraries. Try them out.
148101 if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
149102 {
@@ -158,16 +111,19 @@ private static IntPtr ResolveDll(string libraryName, Assembly assembly, DllImpor
158111 foreach ( var runtimeFolder in Directory . GetDirectories ( runtimesDirectory , $ "*-{ processorArchitecture } ") )
159112 {
160113 string libPath = Path . Combine ( runtimeFolder , "native" , $ "lib{ libraryName } .so") ;
161- if ( TryLoadLibrary ( libPath , out handle ) )
114+
115+ if ( NativeLibrary . TryLoad ( libPath , out handle ) )
162116 {
163117 return handle ;
164118 }
165119 }
166120 }
167121 }
168122 }
123+
169124 return handle ;
170125 }
126+ #endif
171127
172128 public const int RTLD_NOW = 0x002 ;
173129
@@ -2110,4 +2066,3 @@ internal static extern unsafe int git_worktree_prune(
21102066 git_worktree_prune_options options ) ;
21112067 }
21122068}
2113- // ReSharper restore InconsistentNaming
0 commit comments