@@ -33,6 +33,27 @@ public static bool CheckIsRoboRio()
33
33
return File . Exists ( "/usr/local/frc/bin/frcRunRobot.sh" ) ;
34
34
}
35
35
36
+ /// <summary>
37
+ /// Checks if the current system is Raspbian
38
+ /// </summary>
39
+ /// <returns></returns>
40
+ public static bool CheckIsRaspbian ( )
41
+ {
42
+ try
43
+ {
44
+ string text = File . ReadAllText ( "/etc/os-release" ) ;
45
+ if ( text . Contains ( "ID=raspbian" ) )
46
+ {
47
+ return true ;
48
+ }
49
+ }
50
+ catch
51
+ {
52
+
53
+ }
54
+ return false ;
55
+ }
56
+
36
57
/// <summary>
37
58
/// Add a file location to be used when automatically searching for a library to load
38
59
/// </summary>
@@ -117,7 +138,7 @@ public void LoadNativeLibrary<T>(string location, bool directLoad = false, strin
117
138
case OsType . MacOs64 :
118
139
LibraryLoader = new MacOsLibraryLoader ( ) ;
119
140
break ;
120
- case OsType . LinuxArmhf :
141
+ case OsType . LinuxAarch64 :
121
142
case OsType . LinuxRaspbian :
122
143
case OsType . roboRIO :
123
144
LibraryLoader = new EmbeddedLibraryLoader ( ) ;
@@ -158,7 +179,7 @@ public void LoadNativeLibrary<T>(bool directLoad = false, string? extractLocatio
158
179
case OsType . MacOs64 :
159
180
LibraryLoader = new MacOsLibraryLoader ( ) ;
160
181
break ;
161
- case OsType . LinuxArmhf :
182
+ case OsType . LinuxAarch64 :
162
183
case OsType . LinuxRaspbian :
163
184
case OsType . roboRIO :
164
185
LibraryLoader = new EmbeddedLibraryLoader ( ) ;
@@ -169,11 +190,11 @@ public void LoadNativeLibrary<T>(bool directLoad = false, string? extractLocatio
169
190
}
170
191
171
192
/// <summary>
172
- /// Loads a native library with a reflected assembly holding the native libraries
193
+ /// Loads a native library with an assembly holding the native libraries
173
194
/// </summary>
174
- /// <param name="assemblyName ">The name of the assembly to reflect into and load from</param>
195
+ /// <param name="assembly ">The assembly to load from</param>
175
196
/// <param name="localLoadOnRio">True to force a local load on the RoboRIO</param>
176
- public void LoadNativeLibraryFromReflectedAssembly ( string assemblyName , bool localLoadOnRio = true )
197
+ public void LoadNativeLibraryFromAssembly ( Assembly assembly , bool localLoadOnRio = true )
177
198
{
178
199
if ( localLoadOnRio && CheckIsRoboRio ( ) )
179
200
{
@@ -185,17 +206,6 @@ public void LoadNativeLibraryFromReflectedAssembly(string assemblyName, bool loc
185
206
return ;
186
207
}
187
208
188
- AssemblyName name = new AssemblyName ( assemblyName ) ;
189
- Assembly asm ;
190
- try
191
- {
192
- asm = Assembly . Load ( name ) ;
193
- }
194
- catch ( Exception e )
195
- {
196
- throw new InvalidOperationException ( $ "Failed to load desktop libraries. Please ensure that the { assemblyName } is installed and referenced by your project", e ) ;
197
- }
198
-
199
209
if ( OsType == OsType . None )
200
210
throw new InvalidOperationException (
201
211
"OS type is unknown. Must use the overload to manually load the file" ) ;
@@ -217,7 +227,7 @@ public void LoadNativeLibraryFromReflectedAssembly(string assemblyName, bool loc
217
227
case OsType . MacOs64 :
218
228
LibraryLoader = new MacOsLibraryLoader ( ) ;
219
229
break ;
220
- case OsType . LinuxArmhf :
230
+ case OsType . LinuxAarch64 :
221
231
case OsType . LinuxRaspbian :
222
232
case OsType . roboRIO :
223
233
LibraryLoader = new EmbeddedLibraryLoader ( ) ;
@@ -230,11 +240,42 @@ public void LoadNativeLibraryFromReflectedAssembly(string assemblyName, bool loc
230
240
string extractLocation = Path . GetTempFileName ( ) ;
231
241
UsingTempFile = true ;
232
242
233
- ExtractNativeLibrary ( m_nativeLibraryName [ OsType ] , extractLocation , asm ) ;
243
+ ExtractNativeLibrary ( m_nativeLibraryName [ OsType ] , extractLocation , assembly ) ;
234
244
LibraryLoader . LoadLibrary ( extractLocation ) ;
235
245
LibraryLocation = extractLocation ;
236
246
}
237
247
248
+ /// <summary>
249
+ /// Loads a native library with a reflected assembly holding the native libraries
250
+ /// </summary>
251
+ /// <param name="assemblyName">The name of the assembly to reflect into and load from</param>
252
+ /// <param name="localLoadOnRio">True to force a local load on the RoboRIO</param>
253
+ public void LoadNativeLibraryFromReflectedAssembly ( string assemblyName , bool localLoadOnRio = true )
254
+ {
255
+ if ( localLoadOnRio && CheckIsRoboRio ( ) )
256
+ {
257
+ ILibraryLoader loader = new EmbeddedLibraryLoader ( ) ;
258
+ LibraryLoader = loader ;
259
+ var location = m_nativeLibraryName [ OsType . roboRIO ] ;
260
+ loader . LoadLibrary ( location ) ;
261
+ LibraryLocation = location ;
262
+ return ;
263
+ }
264
+
265
+ AssemblyName name = new AssemblyName ( assemblyName ) ;
266
+ Assembly asm ;
267
+ try
268
+ {
269
+ asm = Assembly . Load ( name ) ;
270
+ }
271
+ catch ( Exception e )
272
+ {
273
+ throw new InvalidOperationException ( $ "Failed to load desktop libraries. Please ensure that the { assemblyName } is installed and referenced by your project", e ) ;
274
+ }
275
+
276
+ LoadNativeLibraryFromAssembly ( asm , localLoadOnRio ) ;
277
+ }
278
+
238
279
/// <summary>
239
280
/// Load a native interface
240
281
/// </summary>
@@ -256,26 +297,16 @@ public void LoadNativeLibraryFromReflectedAssembly(string assemblyName, bool loc
256
297
257
298
private void ExtractNativeLibrary ( string resourceLocation , string extractLocation , Assembly asm )
258
299
{
259
- byte [ ] bytes ;
260
- //Load our resource file into memory
261
- using ( Stream s = asm . GetManifestResourceStream ( resourceLocation ) )
262
- {
263
- if ( s == null || s . Length == 0 )
264
- throw new InvalidOperationException ( "File to extract cannot be null or empty" ) ;
265
- bytes = new byte [ ( int ) s . Length ] ;
266
- s . Read ( bytes , 0 , ( int ) s . Length ) ;
267
- }
268
- File . WriteAllBytes ( extractLocation , bytes ) ;
269
- GC . Collect ( ) ;
300
+ using Stream s = asm . GetManifestResourceStream ( resourceLocation ) ;
301
+ if ( s == null || s . Length == 0 )
302
+ throw new InvalidOperationException ( $ "File { resourceLocation } was not found in Assembly { asm . GetName ( ) } ") ;
303
+ using Stream outputStream = new FileStream ( extractLocation , FileMode . Create ) ;
304
+ s . CopyTo ( outputStream ) ;
270
305
}
271
306
272
307
private void ExtractNativeLibrary ( string resourceLocation , string extractLocation , Type type )
273
308
{
274
- #if ! NETSTANDARD
275
- ExtractNativeLibrary ( resourceLocation , extractLocation , type . Assembly ) ;
276
- #else
277
309
ExtractNativeLibrary ( resourceLocation , extractLocation , type . GetTypeInfo ( ) . Assembly ) ;
278
- #endif
279
310
}
280
311
281
312
private static bool Is64BitOs ( )
@@ -285,11 +316,7 @@ private static bool Is64BitOs()
285
316
286
317
private static bool IsWindows ( )
287
318
{
288
- #if NETSTANDARD
289
319
return RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ;
290
- #else
291
- return Path . DirectorySeparatorChar == '\\ ' ;
292
- #endif
293
320
}
294
321
295
322
/// <summary>
@@ -308,9 +335,17 @@ public static OsType GetOsType()
308
335
{
309
336
return OsType . roboRIO ;
310
337
}
311
- #if NETSTANDARD
312
- if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
338
+ else if ( CheckIsRaspbian ( ) )
313
339
{
340
+ return OsType . LinuxRaspbian ;
341
+ }
342
+ // else if (CheckIsAarch64())
343
+ // {
344
+ // return OsType.LinuxAarch64;
345
+ // }
346
+ else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
347
+ {
348
+
314
349
if ( Is64BitOs ( ) ) return OsType . Linux64 ;
315
350
else return OsType . Linux32 ;
316
351
}
@@ -323,60 +358,6 @@ public static OsType GetOsType()
323
358
{
324
359
return OsType . None ;
325
360
}
326
- #else
327
- Utsname uname ;
328
- try
329
- {
330
- Uname . uname ( out uname ) ;
331
- }
332
- catch ( Exception e )
333
- {
334
- Console . WriteLine ( e ) ;
335
- return OsType . None ;
336
- }
337
-
338
- bool mac = uname . sysname == "Darwin" ;
339
- bool armv7 = uname . machine . ToLower ( ) . Contains ( "armv7" ) ;
340
- bool armv6 = uname . machine . ToLower ( ) . Contains ( "armv6" ) ;
341
-
342
- if ( armv7 )
343
- {
344
- try
345
- {
346
- string text = File . ReadAllText ( "/etc/os-release" ) ;
347
- if ( text . Contains ( "ID=raspbian" ) )
348
- {
349
- return OsType . LinuxRaspbian ;
350
- }
351
- else
352
- {
353
- return OsType . LinuxArmhf ;
354
- }
355
- }
356
- catch
357
- {
358
- return OsType . LinuxArmhf ;
359
- }
360
- }
361
- if ( armv6 )
362
- {
363
- throw new PlatformNotSupportedException ( "Arm v6 Devices (most likely a Pi 1 or a Pi Zero) are not supported" ) ;
364
- }
365
-
366
- //Check for Bitness
367
- if ( Is64BitOs ( ) )
368
- {
369
- //We are 64 bit.
370
- if ( mac ) return OsType . MacOs64 ;
371
- return OsType . Linux64 ;
372
- }
373
- else
374
- {
375
- //We are 64 32 bit process.
376
- if ( mac ) return OsType . MacOs32 ;
377
- return OsType . Linux32 ;
378
- }
379
- #endif
380
361
}
381
362
}
382
363
}
0 commit comments