@@ -30,39 +30,6 @@ public enum TargetPlatform
30
30
Any = FreeBSD | Linux | OSX | Windows | Android | IOS | Tizen | ChromeOS | WebAssembly | Solaris | WatchOS | TVO
31
31
}
32
32
33
- public struct LibraryName
34
- {
35
- public string Name ;
36
- public TargetPlatform TargetPlatform ;
37
- }
38
-
39
- public struct LibraryExtension
40
- {
41
- public string Extension ;
42
- public TargetPlatform TargetPlatform ;
43
- }
44
-
45
- public struct LibraryLoaderHints
46
- {
47
- public LibraryName [ ] Names ;
48
- public LibraryExtension [ ] Extensions ;
49
- public string [ ] Paths ;
50
-
51
- public LibraryLoaderHints ( LibraryName [ ] names , LibraryExtension [ ] extensions , string [ ] paths )
52
- {
53
- Names = names ;
54
- Extensions = extensions ;
55
- Paths = paths ;
56
- }
57
-
58
- public LibraryLoaderHints ( LibraryName [ ] names , LibraryExtension [ ] extensions )
59
- {
60
- Names = names ;
61
- Extensions = extensions ;
62
- Paths = [ ] ;
63
- }
64
- }
65
-
66
33
public static class LibraryLoader
67
34
{
68
35
public static OSPlatform FreeBSD { get ; } = OSPlatform . Create ( "FREEBSD" ) ;
@@ -89,16 +56,8 @@ public static class LibraryLoader
89
56
90
57
public static OSPlatform TVOS { get ; } = OSPlatform . Create ( "TVOS" ) ;
91
58
92
- public static string GetExtension ( IEnumerable < NativeLibraryExtensionAttribute > extensionAttributes )
59
+ public static string GetExtension ( )
93
60
{
94
- foreach ( var extensionAttribute in extensionAttributes )
95
- {
96
- if ( RuntimeInformation . IsOSPlatform ( extensionAttribute . TargetPlatform . Convert ( ) ) )
97
- {
98
- return extensionAttribute . Extension ;
99
- }
100
- }
101
-
102
61
// Default extension based on platform
103
62
if ( RuntimeInformation . IsOSPlatform ( Windows ) )
104
63
{
@@ -153,36 +112,6 @@ public static string GetExtension(IEnumerable<NativeLibraryExtensionAttribute> e
153
112
return ".so" ;
154
113
}
155
114
156
- public static nint LoadLibrary ( )
157
- {
158
- var libraryAttributes = Assembly . GetCallingAssembly ( ) . GetCustomAttributes < NativeLibraryAttribute > ( ) ;
159
- var extensionAttributes = Assembly . GetCallingAssembly ( ) . GetCustomAttributes < NativeLibraryExtensionAttribute > ( ) ;
160
-
161
- var libraryName = GetLibraryName ( libraryAttributes ) ;
162
-
163
- var extension = GetExtension ( extensionAttributes ) ;
164
-
165
- if ( ! libraryName . EndsWith ( extension , StringComparison . OrdinalIgnoreCase ) )
166
- {
167
- libraryName += extension ;
168
- }
169
-
170
- var osPlatform = GetOSPlatform ( ) ;
171
- var architecture = GetArchitecture ( ) ;
172
- var libraryPath = GetNativeAssemblyPath ( osPlatform , architecture , libraryName ) ;
173
-
174
- nint handle ;
175
-
176
- handle = NativeLibrary . Load ( libraryPath ) ;
177
-
178
- if ( handle == IntPtr . Zero )
179
- {
180
- throw new DllNotFoundException ( $ "Unable to load library '{ libraryName } '.") ;
181
- }
182
-
183
- return handle ;
184
- }
185
-
186
115
public delegate string LibraryNameCallback ( ) ;
187
116
188
117
public delegate string LibraryExtensionCallback ( ) ;
@@ -191,7 +120,7 @@ public static nint LoadLibrary(LibraryNameCallback libraryNameCallback, LibraryE
191
120
{
192
121
var libraryName = libraryNameCallback ( ) ;
193
122
194
- var extension = libraryExtensionCallback != null ? libraryExtensionCallback ( ) : GetExtension ( [ ] ) ;
123
+ var extension = libraryExtensionCallback != null ? libraryExtensionCallback ( ) : GetExtension ( ) ;
195
124
196
125
if ( ! libraryName . EndsWith ( extension , StringComparison . OrdinalIgnoreCase ) )
197
126
{
@@ -311,110 +240,5 @@ private static string GetOSPlatform()
311
240
312
241
throw new ArgumentException ( "Unsupported OS platform." ) ;
313
242
}
314
-
315
- private static string GetLibraryName ( IEnumerable < NativeLibraryAttribute > nativeLibraries )
316
- {
317
- NativeLibraryAttribute ? nativeLibrary = null ;
318
- foreach ( NativeLibraryAttribute attri in nativeLibraries )
319
- {
320
- if ( attri . TargetPlatform == TargetPlatform . Any )
321
- {
322
- nativeLibrary = attri ; // Default
323
- continue ;
324
- }
325
-
326
- if ( RuntimeInformation . IsOSPlatform ( attri . TargetPlatform . Convert ( ) ) )
327
- {
328
- nativeLibrary = attri ;
329
- break ;
330
- }
331
- }
332
-
333
- if ( nativeLibrary == null )
334
- {
335
- throw new Exception ( "Dll not specified for this platform" ) ;
336
- }
337
-
338
- return nativeLibrary . LibraryName ;
339
- }
340
-
341
- private static OSPlatform Convert ( this TargetPlatform targetPlatform )
342
- {
343
- switch ( targetPlatform )
344
- {
345
- case TargetPlatform . FreeBSD :
346
- return FreeBSD ;
347
-
348
- case TargetPlatform . Linux :
349
- return Linux ;
350
-
351
- case TargetPlatform . OSX :
352
- return OSX ;
353
-
354
- case TargetPlatform . Windows :
355
- return Windows ;
356
-
357
- case TargetPlatform . Android :
358
- return Android ;
359
-
360
- case TargetPlatform . IOS :
361
- return IOS ;
362
-
363
- case TargetPlatform . Tizen :
364
- return Tizen ;
365
-
366
- case TargetPlatform . ChromeOS :
367
- return ChromeOS ;
368
-
369
- case TargetPlatform . WebAssembly :
370
- return WebAssembly ;
371
-
372
- case TargetPlatform . Solaris :
373
- return Solaris ;
374
-
375
- case TargetPlatform . WatchOS :
376
- return WatchOS ;
377
-
378
- case TargetPlatform . TVO :
379
- return TVOS ;
380
-
381
- default :
382
- throw new PlatformNotSupportedException ( ) ;
383
- }
384
- }
385
- }
386
-
387
- [ System . AttributeUsage ( AttributeTargets . Assembly , Inherited = false , AllowMultiple = true ) ]
388
- public sealed class NativeLibraryExtensionAttribute : Attribute
389
- {
390
- private readonly string extension ;
391
- private readonly TargetPlatform targetPlatform ;
392
-
393
- public NativeLibraryExtensionAttribute ( string extension , TargetPlatform targetPlatform = TargetPlatform . Any )
394
- {
395
- this . extension = extension ;
396
- this . targetPlatform = targetPlatform ;
397
- }
398
-
399
- public string Extension => extension ;
400
-
401
- public TargetPlatform TargetPlatform => targetPlatform ;
402
- }
403
-
404
- [ System . AttributeUsage ( AttributeTargets . Assembly , Inherited = false , AllowMultiple = true ) ]
405
- public sealed class NativeLibraryAttribute : Attribute
406
- {
407
- private readonly string libraryName ;
408
- private readonly TargetPlatform targetPlatform ;
409
-
410
- public NativeLibraryAttribute ( string libraryName , TargetPlatform targetPlatform = TargetPlatform . Any )
411
- {
412
- this . libraryName = libraryName ;
413
- this . targetPlatform = targetPlatform ;
414
- }
415
-
416
- public string LibraryName => libraryName ;
417
-
418
- public TargetPlatform TargetPlatform => targetPlatform ;
419
243
}
420
244
}
0 commit comments