9
9
// Restrict the set of directories where the native library is loaded from to safe directories.
10
10
[ assembly: DefaultDllImportSearchPaths ( DllImportSearchPath . AssemblyDirectory | DllImportSearchPath . ApplicationDirectory | DllImportSearchPath . SafeDirectories ) ]
11
11
12
- #pragma warning disable IDE1006 // Naming Styles
13
-
14
- // ReSharper disable InconsistentNaming
15
12
namespace LibGit2Sharp . Core
16
13
{
17
14
internal static class NativeMethods
@@ -22,15 +19,13 @@ internal static class NativeMethods
22
19
// An object tied to the lifecycle of the NativeMethods static class.
23
20
// This will handle initialization and shutdown of the underlying
24
21
// native library.
25
- #pragma warning disable 0414
26
22
private static NativeShutdownObject shutdownObject ;
27
- #pragma warning restore 0414
28
23
29
24
static NativeMethods ( )
30
25
{
31
26
if ( Platform . IsRunningOnNetFramework ( ) || Platform . IsRunningOnNetCore ( ) )
32
27
{
33
- // Use .NET Core 3.0+ NativeLibrary when available.
28
+ // Use NativeLibrary when available.
34
29
if ( ! TryUseNativeLibrary ( ) )
35
30
{
36
31
// NativeLibrary is not available, fall back.
@@ -40,6 +35,7 @@ static NativeMethods()
40
35
// If this call succeeds further DllImports will find the library loaded and not attempt to load it again.
41
36
// If it fails the next DllImport will load the library from safe directories.
42
37
string nativeLibraryPath = GetGlobalSettingsNativeLibraryPath ( ) ;
38
+
43
39
if ( nativeLibraryPath != null )
44
40
{
45
41
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
@@ -61,89 +57,46 @@ static NativeMethods()
61
57
private static string GetGlobalSettingsNativeLibraryPath ( )
62
58
{
63
59
string nativeLibraryDir = GlobalSettings . GetAndLockNativeLibraryPath ( ) ;
60
+
64
61
if ( nativeLibraryDir == null )
65
62
{
66
63
return null ;
67
64
}
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
- }
85
65
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 ( ) ) ;
93
67
}
94
68
69
+ #if NETSTANDARD
70
+ private static bool TryUseNativeLibrary ( ) => false ;
71
+ #else
95
72
private static bool TryUseNativeLibrary ( )
96
73
{
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 ) ;
123
75
124
76
return true ;
125
77
}
126
78
127
79
private static IntPtr ResolveDll ( string libraryName , Assembly assembly , DllImportSearchPath ? searchPath )
128
80
{
129
81
IntPtr handle = IntPtr . Zero ;
82
+
130
83
if ( libraryName == libgit2 )
131
84
{
132
85
// Use GlobalSettings.NativeLibraryPath when set.
133
86
string nativeLibraryPath = GetGlobalSettingsNativeLibraryPath ( ) ;
134
- if ( nativeLibraryPath != null &&
135
- TryLoadLibrary ( nativeLibraryPath , out handle ) )
87
+
88
+ if ( nativeLibraryPath != null && NativeLibrary . TryLoad ( nativeLibraryPath , out handle ) )
136
89
{
137
90
return handle ;
138
91
}
139
92
140
93
// Use Default DllImport resolution.
141
- if ( TryLoadLibrary ( libraryName , assembly , searchPath , out handle ) )
94
+ if ( NativeLibrary . TryLoad ( libraryName , assembly , searchPath , out handle ) )
142
95
{
143
96
return handle ;
144
97
}
145
98
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
147
100
// libc/OpenSSL libraries. Try them out.
148
101
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
149
102
{
@@ -158,16 +111,19 @@ private static IntPtr ResolveDll(string libraryName, Assembly assembly, DllImpor
158
111
foreach ( var runtimeFolder in Directory . GetDirectories ( runtimesDirectory , $ "*-{ processorArchitecture } ") )
159
112
{
160
113
string libPath = Path . Combine ( runtimeFolder , "native" , $ "lib{ libraryName } .so") ;
161
- if ( TryLoadLibrary ( libPath , out handle ) )
114
+
115
+ if ( NativeLibrary . TryLoad ( libPath , out handle ) )
162
116
{
163
117
return handle ;
164
118
}
165
119
}
166
120
}
167
121
}
168
122
}
123
+
169
124
return handle ;
170
125
}
126
+ #endif
171
127
172
128
public const int RTLD_NOW = 0x002 ;
173
129
@@ -2110,4 +2066,3 @@ internal static extern unsafe int git_worktree_prune(
2110
2066
git_worktree_prune_options options ) ;
2111
2067
}
2112
2068
}
2113
- // ReSharper restore InconsistentNaming
0 commit comments