diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 51e984f..d5a7300 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,8 +40,11 @@ jobs: - name: Prepare .NET uses: actions/setup-dotnet@v3 with: - dotnet-version: '6.0.x' - + dotnet-version: | + 6.0.x + 7.0.x + 8.0.x + - name: Build solution run: dotnet build --nologo -c ${{ env.configuration }} working-directory: './src' diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 3ec396f..82b7c34 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -1,8 +1,9 @@ - net6.0 + net6.0;net7.0;net8.0 enable enable true + Recommended \ No newline at end of file diff --git a/src/HidApi.Net.Tester/HidApiVerifier.cs b/src/HidApi.Net.Tester/IHidApiVerifier.cs similarity index 74% rename from src/HidApi.Net.Tester/HidApiVerifier.cs rename to src/HidApi.Net.Tester/IHidApiVerifier.cs index aa54a1e..8592b05 100644 --- a/src/HidApi.Net.Tester/HidApiVerifier.cs +++ b/src/HidApi.Net.Tester/IHidApiVerifier.cs @@ -1,6 +1,6 @@ namespace HidApi.Net.Tester; -public interface HidApiVerifier +public interface IHidApiVerifier { bool VerifyCharPointer(); bool VerifyWCharPointer(); diff --git a/src/HidApi.Net.Tester/Tester.cs b/src/HidApi.Net.Tester/Tester.cs index 96d4fa8..d340817 100644 --- a/src/HidApi.Net.Tester/Tester.cs +++ b/src/HidApi.Net.Tester/Tester.cs @@ -73,7 +73,7 @@ private static bool Check64BitOs(bool? require64Bit) return false; } - private static HidApiVerifier GetVerifier() + private static IHidApiVerifier GetVerifier() { if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) return new Linux(); @@ -84,7 +84,7 @@ private static HidApiVerifier GetVerifier() if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) return new Osx(); - throw new Exception("No verifier available for os platform."); + throw new NotSupportedException("No verifier available for os platform."); } private static OSPlatform GetOsPlatform() @@ -101,6 +101,6 @@ private static OSPlatform GetOsPlatform() if (RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD)) return OSPlatform.FreeBSD; - throw new Exception("Unknown OsPlatform"); + throw new NotSupportedException("Unknown OsPlatform"); } } diff --git a/src/HidApi.Net.Tester/Verifier/Linux.cs b/src/HidApi.Net.Tester/Verifier/Linux.cs index 72ec44c..33ac414 100644 --- a/src/HidApi.Net.Tester/Verifier/Linux.cs +++ b/src/HidApi.Net.Tester/Verifier/Linux.cs @@ -1,11 +1,11 @@ namespace HidApi.Net.Tester; -public class Linux : HidApiVerifier +public class Linux : IHidApiVerifier { public bool VerifyCharPointer() { var version = Hid.VersionString(); - if (version.Split('.').Length == 3 && version.StartsWith("0.")) + if (version.Split('.').Length == 3 && version.StartsWith("0.", StringComparison.InvariantCulture)) return true; Console.WriteLine("Error: Could not verify HIDAPI. String seems to have incorrect format."); diff --git a/src/HidApi.Net.Tester/Verifier/Osx.cs b/src/HidApi.Net.Tester/Verifier/Osx.cs index e31c27d..c98e549 100644 --- a/src/HidApi.Net.Tester/Verifier/Osx.cs +++ b/src/HidApi.Net.Tester/Verifier/Osx.cs @@ -1,11 +1,11 @@ namespace HidApi.Net.Tester; -public class Osx : HidApiVerifier +public class Osx : IHidApiVerifier { public bool VerifyCharPointer() { var version = Hid.VersionString(); - if (version.Split('.').Length == 3 && version.StartsWith("0.")) + if (version.Split('.').Length == 3 && version.StartsWith("0.", StringComparison.InvariantCulture)) return true; Console.WriteLine("Error: Could not verify HIDAPI. String seems to have incorrect format."); diff --git a/src/HidApi.Net.Tester/Verifier/Windows.cs b/src/HidApi.Net.Tester/Verifier/Windows.cs index 4f247f9..f74d7e3 100644 --- a/src/HidApi.Net.Tester/Verifier/Windows.cs +++ b/src/HidApi.Net.Tester/Verifier/Windows.cs @@ -1,11 +1,11 @@ namespace HidApi.Net.Tester; -public class Windows : HidApiVerifier +public class Windows : IHidApiVerifier { public bool VerifyCharPointer() { var version = Hid.VersionString(); - if (version.Split('.').Length == 3 && version.StartsWith("0.")) + if (version.Split('.').Length == 3 && version.StartsWith("0.", StringComparison.InvariantCulture)) return true; Console.WriteLine("Error: Could not verify HIDAPI. String seems to have incorrect format."); diff --git a/src/HidApi.Net.Tests/StringTests.cs b/src/HidApi.Net.Tests/StringTests.cs index 7bf096a..eb43c65 100644 --- a/src/HidApi.Net.Tests/StringTests.cs +++ b/src/HidApi.Net.Tests/StringTests.cs @@ -9,6 +9,7 @@ public class StringTests [TestMethod] public void TestUnicode() { +#pragma warning disable CA1861 var data = new byte[6]; data[0] = 0; data[1] = 1; @@ -25,11 +26,13 @@ public void TestUnicode() str.Should().Be(new string(new[] { '\u0100', '\u0100' })); } } +#pragma warning restore CA1861 } [TestMethod] public void TestUtf32() { +#pragma warning disable CA1861 var data = new byte[12]; data[0] = 0; data[1] = 1; @@ -52,6 +55,7 @@ public void TestUtf32() str.Should().Be(new string(new[] { '\u0100', '\u0100' })); } } +#pragma warning restore CA1861 } diff --git a/src/HidApi.Net/Internal/NullTerminatedString.cs b/src/HidApi.Net/Internal/NullTerminatedString.cs index 2b5dd50..374af2d 100644 --- a/src/HidApi.Net/Internal/NullTerminatedString.cs +++ b/src/HidApi.Net/Internal/NullTerminatedString.cs @@ -1,3 +1,5 @@ +using System.Text; + namespace HidApi; internal readonly ref struct NullTerminatedString @@ -11,9 +13,27 @@ public NullTerminatedString() data = ReadOnlySpan.Empty; } - internal NullTerminatedString(ref byte[] str) + private NullTerminatedString(ReadOnlySpan data) { - data = str; + this.data = data; + } + + internal static NullTerminatedString WithUnicode(string str) + { + var src = Encoding.Unicode.GetBytes(str); + var dest = new byte[src.Length + sizeof(ushort)]; + Array.Copy(src, dest, src.Length); + + return new NullTerminatedString(dest); + } + + internal static NullTerminatedString WithUtf32(string str) + { + var src = Encoding.UTF32.GetBytes(str); + var dest = new byte[src.Length + sizeof(uint)]; + Array.Copy(src, dest, src.Length); + + return new NullTerminatedString(dest); } public ref readonly byte GetPinnableReference() => ref data.GetPinnableReference(); diff --git a/src/HidApi.Net/Internal/Unicode.cs b/src/HidApi.Net/Internal/Unicode.cs index e4f8db1..a2f0ba7 100644 --- a/src/HidApi.Net/Internal/Unicode.cs +++ b/src/HidApi.Net/Internal/Unicode.cs @@ -4,15 +4,6 @@ namespace HidApi; internal static class Unicode { - public static NullTerminatedString CreateNullTerminatedString(string str) - { - var src = Encoding.Unicode.GetBytes(str); - var dest = new byte[src.Length + sizeof(ushort)]; - Array.Copy(src, dest, src.Length); - - return new NullTerminatedString(ref dest); - } - public static ReadOnlySpan CreateBuffer(int size) { return new byte[size * sizeof(ushort)]; diff --git a/src/HidApi.Net/Internal/Utf32.cs b/src/HidApi.Net/Internal/Utf32.cs index 7a03487..23b4846 100644 --- a/src/HidApi.Net/Internal/Utf32.cs +++ b/src/HidApi.Net/Internal/Utf32.cs @@ -4,15 +4,6 @@ namespace HidApi; internal static class Utf32 { - public static NullTerminatedString CreateNullTerminatedString(string str) - { - var src = Encoding.UTF32.GetBytes(str); - var dest = new byte[src.Length + sizeof(uint)]; - Array.Copy(src, dest, src.Length); - - return new NullTerminatedString(ref dest); - } - public static ReadOnlySpan CreateBuffer(int size) { return new byte[size * sizeof(uint)]; diff --git a/src/HidApi.Net/Internal/WCharT.cs b/src/HidApi.Net/Internal/WCharT.cs index 11afe84..499e948 100644 --- a/src/HidApi.Net/Internal/WCharT.cs +++ b/src/HidApi.Net/Internal/WCharT.cs @@ -50,13 +50,13 @@ public static ReadOnlySpan CreateBuffer(int size) public static NullTerminatedString CreateNullTerminatedString(string str) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - return Utf32.CreateNullTerminatedString(str); + return NullTerminatedString.WithUtf32(str); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - return Unicode.CreateNullTerminatedString(str); + return NullTerminatedString.WithUnicode(str); if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - return Utf32.CreateNullTerminatedString(str); + return NullTerminatedString.WithUtf32(str); throw new NotSupportedException("Unsupported platform to create a null terminated string"); }