Skip to content

Commit a042dc5

Browse files
committed
Fix some style things, add a better setup for loading from a known library
1 parent 50f4631 commit a042dc5

File tree

10 files changed

+88
-102
lines changed

10 files changed

+88
-102
lines changed

src/FRC-Utilities/CachedNativeString.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public unsafe struct CachedNativeString
1919
/// </summary>
2020
public UIntPtr Length;
2121

22-
private string m_string;
22+
private readonly string m_string;
2323

2424
internal CachedNativeString(string vStr)
2525
{

src/FRC-Utilities/FRC-Utilities.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<PropertyGroup>
2828
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
2929
<WarningsAsErrors />
30-
<Version>3.0.0</Version>
30+
<Version>3.0.4</Version>
3131
</PropertyGroup>
3232

3333
<ItemGroup>

src/FRC-Utilities/ILGeneration/InterfaceGenerator.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public InterfaceGenerator(IFunctionPointerLoader functionPointerLoader, IILGener
2121

2222
public T? GenerateImplementation()
2323
{
24-
Console.WriteLine("Generating Impl");
2524
AssemblyBuilder asmBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(typeof(T).Name + "Asm"), AssemblyBuilderAccess.Run);
2625
ModuleBuilder moduleBuilder = asmBuilder.DefineDynamicModule(typeof(T).Name + "Module");
2726
TypeBuilder typeBuilder = moduleBuilder.DefineType("Default" + typeof(T).Name);

src/FRC-Utilities/ManagedString.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public UIntPtr Length
2424
return (UIntPtr)(Buffer.Length - 1);
2525
}
2626
}
27-
private string m_string;
27+
private readonly string m_string;
2828

2929
/// <summary>
3030
/// Constructs a managed UTF8 string from a C# string

src/FRC-Utilities/NativeLibraryUtilities/EmbeddedLibraryLoader.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ void ILibraryLoader.UnloadLibrary()
4848
}
4949

5050
[DllImport("libdl.so.2")]
51+
#pragma warning disable IDE1006 // Naming Styles
5152
private static extern IntPtr dlopen(string fileName, int flags);
5253

5354
[DllImport("libdl.so.2")]
@@ -58,5 +59,6 @@ void ILibraryLoader.UnloadLibrary()
5859

5960
[DllImport("libdl.so.2")]
6061
private static extern int dlclose(IntPtr handle);
62+
#pragma warning restore IDE1006 // Naming Styles
6163
}
6264
}

src/FRC-Utilities/NativeLibraryUtilities/NativeLibraryLoader.cs

Lines changed: 74 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,27 @@ public static bool CheckIsRoboRio()
3333
return File.Exists("/usr/local/frc/bin/frcRunRobot.sh");
3434
}
3535

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+
3657
/// <summary>
3758
/// Add a file location to be used when automatically searching for a library to load
3859
/// </summary>
@@ -117,7 +138,7 @@ public void LoadNativeLibrary<T>(string location, bool directLoad = false, strin
117138
case OsType.MacOs64:
118139
LibraryLoader = new MacOsLibraryLoader();
119140
break;
120-
case OsType.LinuxArmhf:
141+
case OsType.LinuxAarch64:
121142
case OsType.LinuxRaspbian:
122143
case OsType.roboRIO:
123144
LibraryLoader = new EmbeddedLibraryLoader();
@@ -158,7 +179,7 @@ public void LoadNativeLibrary<T>(bool directLoad = false, string? extractLocatio
158179
case OsType.MacOs64:
159180
LibraryLoader = new MacOsLibraryLoader();
160181
break;
161-
case OsType.LinuxArmhf:
182+
case OsType.LinuxAarch64:
162183
case OsType.LinuxRaspbian:
163184
case OsType.roboRIO:
164185
LibraryLoader = new EmbeddedLibraryLoader();
@@ -169,11 +190,11 @@ public void LoadNativeLibrary<T>(bool directLoad = false, string? extractLocatio
169190
}
170191

171192
/// <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
173194
/// </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>
175196
/// <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)
177198
{
178199
if (localLoadOnRio && CheckIsRoboRio())
179200
{
@@ -185,17 +206,6 @@ public void LoadNativeLibraryFromReflectedAssembly(string assemblyName, bool loc
185206
return;
186207
}
187208

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-
199209
if (OsType == OsType.None)
200210
throw new InvalidOperationException(
201211
"OS type is unknown. Must use the overload to manually load the file");
@@ -217,7 +227,7 @@ public void LoadNativeLibraryFromReflectedAssembly(string assemblyName, bool loc
217227
case OsType.MacOs64:
218228
LibraryLoader = new MacOsLibraryLoader();
219229
break;
220-
case OsType.LinuxArmhf:
230+
case OsType.LinuxAarch64:
221231
case OsType.LinuxRaspbian:
222232
case OsType.roboRIO:
223233
LibraryLoader = new EmbeddedLibraryLoader();
@@ -230,11 +240,42 @@ public void LoadNativeLibraryFromReflectedAssembly(string assemblyName, bool loc
230240
string extractLocation = Path.GetTempFileName();
231241
UsingTempFile = true;
232242

233-
ExtractNativeLibrary(m_nativeLibraryName[OsType], extractLocation, asm);
243+
ExtractNativeLibrary(m_nativeLibraryName[OsType], extractLocation, assembly);
234244
LibraryLoader.LoadLibrary(extractLocation);
235245
LibraryLocation = extractLocation;
236246
}
237247

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+
238279
/// <summary>
239280
/// Load a native interface
240281
/// </summary>
@@ -256,26 +297,16 @@ public void LoadNativeLibraryFromReflectedAssembly(string assemblyName, bool loc
256297

257298
private void ExtractNativeLibrary(string resourceLocation, string extractLocation, Assembly asm)
258299
{
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);
270305
}
271306

272307
private void ExtractNativeLibrary(string resourceLocation, string extractLocation, Type type)
273308
{
274-
#if !NETSTANDARD
275-
ExtractNativeLibrary(resourceLocation, extractLocation, type.Assembly);
276-
#else
277309
ExtractNativeLibrary(resourceLocation, extractLocation, type.GetTypeInfo().Assembly);
278-
#endif
279310
}
280311

281312
private static bool Is64BitOs()
@@ -285,11 +316,7 @@ private static bool Is64BitOs()
285316

286317
private static bool IsWindows()
287318
{
288-
#if NETSTANDARD
289319
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
290-
#else
291-
return Path.DirectorySeparatorChar == '\\';
292-
#endif
293320
}
294321

295322
/// <summary>
@@ -308,9 +335,17 @@ public static OsType GetOsType()
308335
{
309336
return OsType.roboRIO;
310337
}
311-
#if NETSTANDARD
312-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
338+
else if (CheckIsRaspbian())
313339
{
340+
return OsType.LinuxRaspbian;
341+
}
342+
// else if (CheckIsAarch64())
343+
// {
344+
// return OsType.LinuxAarch64;
345+
// }
346+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
347+
{
348+
314349
if (Is64BitOs()) return OsType.Linux64;
315350
else return OsType.Linux32;
316351
}
@@ -323,60 +358,6 @@ public static OsType GetOsType()
323358
{
324359
return OsType.None;
325360
}
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
380361
}
381362
}
382363
}

src/FRC-Utilities/NativeLibraryUtilities/OsType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public enum OsType
4141
/// <summary>
4242
/// Linux Arm Hard Float (Not Raspbian)
4343
/// </summary>
44-
LinuxArmhf,
44+
LinuxAarch64,
4545
/// <summary>
4646
/// Linux Raspbian
4747
/// </summary>

src/FRC-Utilities/NativeLibraryUtilities/Uname.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ public override string ToString()
6666
}
6767

6868
[StructLayout(LayoutKind.Sequential)]
69+
#pragma warning disable IDE1006 // Naming Styles
6970
internal struct _Utsname
71+
#pragma warning restore IDE1006 // Naming Styles
7072
{
7173
public IntPtr sysname;
7274
public IntPtr nodename;
@@ -101,6 +103,7 @@ private static void CopyUtsname(out Utsname to, ref _Utsname from)
101103

102104
internal const string MPH = "MonoPosixHelper";
103105
internal const string LIBC = "libc";
106+
#pragma warning disable IDE1006 // Naming Styles
104107

105108
[DllImport(LIBC, CallingConvention = CallingConvention.Cdecl)]
106109
public static extern void free(IntPtr ptr);
@@ -109,10 +112,11 @@ private static void CopyUtsname(out Utsname to, ref _Utsname from)
109112
EntryPoint = "Mono_Posix_Syscall_uname")]
110113
private static extern int sys_uname(out _Utsname buf);
111114

115+
112116
public static int uname(out Utsname? buf)
117+
#pragma warning restore IDE1006 // Naming Styles
113118
{
114-
_Utsname _buf;
115-
int r = sys_uname(out _buf);
119+
int r = sys_uname(out _Utsname _buf);
116120
buf = null;
117121
if (r == 0)
118122
{

src/FRC-Utilities/UTF8String.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static class UTF8String
1212
{
1313
private static readonly ConcurrentDictionary<string, CachedNativeString> s_stringCache = new ConcurrentDictionary<string, CachedNativeString>();
1414

15-
private static Func<string, CachedNativeString> CacheCreateFunc = (s) => new CachedNativeString(s);
15+
private static readonly Func<string, CachedNativeString> CacheCreateFunc = (s) => new CachedNativeString(s);
1616

1717
/// <summary>
1818
/// Creates a UTF8 string that will be cached, using the already cached string is already created

test/FRC-Utilities.Test/ILGeneration/InterfaceGeneratorTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public interface I1Func
1717

1818
private class MockFPLoader : IFunctionPointerLoader
1919
{
20-
private Dictionary<string, (Delegate, IntPtr)> fpMap = new Dictionary<string, (Delegate, IntPtr)>();
20+
private readonly Dictionary<string, (Delegate, IntPtr)> fpMap = new Dictionary<string, (Delegate, IntPtr)>();
2121

2222
public void AddDelegate<T>(string name, T del) where T : Delegate
2323
{

0 commit comments

Comments
 (0)