Skip to content

Commit

Permalink
Merge pull request #69 from badcel/update-target-frameworks
Browse files Browse the repository at this point in the history
  • Loading branch information
badcel authored Nov 20, 2023
2 parents 9cd57b6 + 6f57910 commit 3ea703f
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 39 deletions.
14 changes: 9 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ env:
configuration: Release
publish_release: ${{ github.event.inputs.publish_release }}
version_suffix: ${{ github.event.inputs.version_suffix }}
tester_framework: net8.0

jobs:
build:
Expand All @@ -40,8 +41,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'
Expand All @@ -59,14 +63,14 @@ jobs:
if: matrix.os == 'ubuntu-latest'
run: |
sudo apt-get install libhidapi-hidraw0
dotnet run -- --requireOsPlatform LINUX --require64bit
dotnet run --framework ${{ env.tester_framework }} -- --requireOsPlatform LINUX --require64bit
working-directory: './src/HidApi.Net.Tester'

- name: Test OSX 64 bit
if: matrix.os == 'macos-latest'
run: |
brew install hidapi
dotnet run -- --requireOsPlatform OSX --require64bit
dotnet run --framework ${{ env.tester_framework }} -- --requireOsPlatform OSX --require64bit
working-directory: './src/HidApi.Net.Tester'

- name: Setup Windows 64 bit
Expand All @@ -80,7 +84,7 @@ jobs:
- name: Test Windows 64 bit
if: matrix.os == 'Windows-latest'
run: dotnet run -- --requireOsPlatform WINDOWS --require64bit
run: dotnet run --framework ${{ env.tester_framework }} -- --requireOsPlatform WINDOWS --require64bit
shell: msys2 {0}
working-directory: './src/HidApi.Net.Tester'

Expand Down
3 changes: 2 additions & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<Project>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AnalysisMode>Recommended</AnalysisMode>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace HidApi.Net.Tester;

public interface HidApiVerifier
public interface IHidApiVerifier
{
bool VerifyCharPointer();
bool VerifyWCharPointer();
Expand Down
6 changes: 3 additions & 3 deletions src/HidApi.Net.Tester/Tester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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()
Expand All @@ -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");
}
}
4 changes: 2 additions & 2 deletions src/HidApi.Net.Tester/Verifier/Linux.cs
Original file line number Diff line number Diff line change
@@ -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.");
Expand Down
4 changes: 2 additions & 2 deletions src/HidApi.Net.Tester/Verifier/Osx.cs
Original file line number Diff line number Diff line change
@@ -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.");
Expand Down
4 changes: 2 additions & 2 deletions src/HidApi.Net.Tester/Verifier/Windows.cs
Original file line number Diff line number Diff line change
@@ -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.");
Expand Down
4 changes: 4 additions & 0 deletions src/HidApi.Net.Tests/StringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -52,6 +55,7 @@ public void TestUtf32()
str.Should().Be(new string(new[] { '\u0100', '\u0100' }));
}
}
#pragma warning restore CA1861
}


Expand Down
24 changes: 22 additions & 2 deletions src/HidApi.Net/Internal/NullTerminatedString.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Text;

namespace HidApi;

internal readonly ref struct NullTerminatedString
Expand All @@ -11,9 +13,27 @@ public NullTerminatedString()
data = ReadOnlySpan<byte>.Empty;
}

internal NullTerminatedString(ref byte[] str)
private NullTerminatedString(ReadOnlySpan<byte> 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();
Expand Down
9 changes: 0 additions & 9 deletions src/HidApi.Net/Internal/Unicode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte> CreateBuffer(int size)
{
return new byte[size * sizeof(ushort)];
Expand Down
9 changes: 0 additions & 9 deletions src/HidApi.Net/Internal/Utf32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte> CreateBuffer(int size)
{
return new byte[size * sizeof(uint)];
Expand Down
6 changes: 3 additions & 3 deletions src/HidApi.Net/Internal/WCharT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public static ReadOnlySpan<byte> 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");
}
Expand Down

0 comments on commit 3ea703f

Please sign in to comment.