Skip to content

Commit bf8fdd0

Browse files
committed
Refactor code and update dependencies for v4.1
- Incremented version number in `version.json` from "4.0" to "4.1". - Updated project files to reference newer versions of dependencies, including `Microsoft.Extensions` and testing packages. - Moved dependency from `iTextSharp` to `iTextSharp.LGPLv2.Core`, which allowed `BouncyCastle` to be replaced with `BouncyCastle.Cryptography`, removing security warnings. - Refactored multiple files to utilize primary constructor syntax for improved readability. - Updated `Diagnostics.cs` to use C# 9.0 object creation syntax. - Corrected casing for PDF dictionary keys in `UsagePageGenerator.cs`, based on changes in iTextSharp. - Changed return types in `Devices.cs` for better type safety. - Under the hood, changed NuGet API Key.
1 parent 4177073 commit bf8fdd0

20 files changed

+118
-161
lines changed

HIDDevices.Generator/Diagnostics.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,72 +11,70 @@ namespace HIDDevices.Generator;
1111
public static class Diagnostics
1212
{
1313
// ReSharper disable ArrangeObjectCreationWhenTypeEvident - See https://github.com/dotnet/roslyn-analyzers/issues/5957
14-
#pragma warning disable IDE0090 // Use 'new(...)'
15-
public static readonly DiagnosticDescriptor Completed = new DiagnosticDescriptor(
14+
public static readonly DiagnosticDescriptor Completed = new(
1615
"HUT0001",
1716
"Generation succeeded",
1817
"Generation succeeded for version {0}. {1} usages in {2} usage pages in {3:g}.",
1918
"Generation",
2019
DiagnosticSeverity.Info,
2120
true);
2221

23-
public static readonly DiagnosticDescriptor Cancelled = new DiagnosticDescriptor(
22+
public static readonly DiagnosticDescriptor Cancelled = new(
2423
"HUT00FF",
2524
"Cancelled",
2625
"Generation was cancelled",
2726
"Generation",
2827
DiagnosticSeverity.Error,
2928
true);
3029

31-
public static readonly DiagnosticDescriptor CacheFolderCreationFailed = new DiagnosticDescriptor(
30+
public static readonly DiagnosticDescriptor CacheFolderCreationFailed = new(
3231
"HUT1001",
3332
"Could not find/create HID Usage Table Caching folder",
3433
"Folder {0} could not be created: {1}",
3534
"Caching",
3635
DiagnosticSeverity.Warning,
3736
true);
3837

39-
public static readonly DiagnosticDescriptor CachingDisabled = new DiagnosticDescriptor(
38+
public static readonly DiagnosticDescriptor CachingDisabled = new(
4039
"HUT1002",
4140
"Caching disabled",
4241
"Caching disabled as cache file locations could not be determined",
4342
"Caching",
4443
DiagnosticSeverity.Warning,
4544
true);
4645

47-
public static readonly DiagnosticDescriptor PdfNotFound = new DiagnosticDescriptor(
46+
public static readonly DiagnosticDescriptor PdfNotFound = new(
4847
"HUT1003",
4948
"PDF Not found",
5049
"Failed to find the PDF file {0}: {1}",
5150
"Caching",
5251
DiagnosticSeverity.Error,
5352
true);
5453

55-
public static readonly DiagnosticDescriptor JsonDeserializationFailed = new DiagnosticDescriptor(
54+
public static readonly DiagnosticDescriptor JsonDeserializationFailed = new(
5655
"HUT2001",
5756
"JSON Deserialization Failure",
5857
"Deserialization of the JSON HID USage Tables failed: {0}",
5958
"Deserialization",
6059
DiagnosticSeverity.Error,
6160
true);
6261

63-
public static readonly DiagnosticDescriptor JsonAttachmentNotFound = new DiagnosticDescriptor(
62+
public static readonly DiagnosticDescriptor JsonAttachmentNotFound = new(
6463
"HUT2002",
6564
"JSON attachment not found in PDF file",
6665
"Could not find the JSON attachment in '{0}'",
6766
"Deserialization",
6867
DiagnosticSeverity.Error,
6968
true);
7069

71-
public static readonly DiagnosticDescriptor JsonExtractionFailed = new DiagnosticDescriptor(
70+
public static readonly DiagnosticDescriptor JsonExtractionFailed = new(
7271
"HUT2003",
7372
"Error extracting JSON attachment from PDF file",
7473
"Extracting the JSON attachment from '{0}' failed: {1}",
7574
"Deserialization",
7675
DiagnosticSeverity.Error,
7776
true);
7877
// ReSharper restore ArrangeObjectCreationWhenTypeEvident
79-
#pragma warning restore IDE0090 // Use 'new(...)'
8078

8179
/// <summary>
8280
/// Adds a <see cref="Diagnostic" /> to the users compilation based on a <see cref="DiagnosticDescriptor" />.

HIDDevices.Generator/HIDDevices.Generator.csproj

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,10 @@
1515
</PropertyGroup>
1616

1717
<ItemGroup>
18-
<PackageReference Include="iTextSharp" Version="5.5.13.3" GeneratePathProperty="true" PrivateAssets="all">
19-
<NoWarn>NU1701</NoWarn>
20-
</PackageReference>
21-
<PackageReference Include="BouncyCastle" Version="1.8.9" GeneratePathProperty="true" PrivateAssets="all">
22-
<NoWarn>NU1701</NoWarn>
23-
</PackageReference>
24-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" PrivateAssets="all" />
25-
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
18+
<PackageReference Include="BouncyCastle.Cryptography" Version="2.5.0" GeneratePathProperty="true" PrivateAssets="all"/>
19+
<PackageReference Include="iTextSharp.LGPLv2.Core" Version="3.7.0" GeneratePathProperty="true" PrivateAssets="all"/>
20+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0" PrivateAssets="all" />
21+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0">
2622
<PrivateAssets>all</PrivateAssets>
2723
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2824
</PackageReference>
@@ -36,9 +32,9 @@
3632

3733
<Target Name="GetDependencyTargetPaths">
3834
<ItemGroup>
39-
<TargetPathWithTargetPlatformMoniker Include="$(PKGNewtonsoft_Json)\lib\netstandard2.0\Newtonsoft.Json.dll" IncludeRuntimeDependency="false"/>
40-
<TargetPathWithTargetPlatformMoniker Include="$(PKGiTextSharp)\lib\iTextSharp.dll" IncludeRuntimeDependency="false"/>
41-
<TargetPathWithTargetPlatformMoniker Include="$(PKGBouncyCastle)\lib\BouncyCastle.Crypto.dll" IncludeRuntimeDependency="false"/>
35+
<TargetPathWithTargetPlatformMoniker Include="$(PKGNewtonsoft_Json)\lib\netstandard2.0\Newtonsoft.Json.dll" IncludeRuntimeDependency="false" />
36+
<TargetPathWithTargetPlatformMoniker Include="$(PKGiTextSharp_LGPLv2_Core)\lib\netstandard2.0\iTextSharp.LGPLv2.Core.dll" IncludeRuntimeDependency="false" />
37+
<TargetPathWithTargetPlatformMoniker Include="$(PKGBouncyCastle_Cryptography)\lib\netstandard2.0\BouncyCastle.Cryptography.dll" IncludeRuntimeDependency="false" />
4238
</ItemGroup>
4339
</Target>
4440
</Project>

HIDDevices.Generator/HidUsageGenerator.cs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,56 +11,48 @@ namespace HIDDevices.Generator;
1111
/// This is used primarily by the Button/Ordinal pages, where there are 65535 UsageIds (all 'pre-defined').
1212
/// It would be silly (and wasteful) to pre-generate all of these, so rather it is done on demand.
1313
/// </summary>
14+
/// <remarks>
15+
/// Initializes a new instance of the <see cref="HidUsageGenerator" /> class.
16+
/// </remarks>
17+
/// <param name="namePrefix">Name of every generated Usage.</param>
18+
/// <param name="startUsageId">First valid UsageId.</param>
19+
/// <param name="endUsageId">Last valid UsageId.</param>
20+
/// <param name="kinds">Kinds to associate with generated UsageIds.</param>
1421
[JsonObject(MemberSerialization.OptIn)]
15-
public class HidUsageGenerator
22+
public class HidUsageGenerator(string namePrefix, ushort startUsageId, ushort endUsageId,
23+
IReadOnlyCollection<HidUsageKind> kinds)
1624
{
17-
/// <summary>
18-
/// Initializes a new instance of the <see cref="HidUsageGenerator" /> class.
19-
/// </summary>
20-
/// <param name="namePrefix">Name of every generated Usage.</param>
21-
/// <param name="startUsageId">First valid UsageId.</param>
22-
/// <param name="endUsageId">Last valid UsageId.</param>
23-
/// <param name="kinds">Kinds to associate with generated UsageIds.</param>
24-
public HidUsageGenerator(string namePrefix, ushort startUsageId, ushort endUsageId,
25-
IReadOnlyCollection<HidUsageKind> kinds)
26-
{
27-
NamePrefix = namePrefix;
28-
SafeNamePrefix = namePrefix.GetSafe();
29-
StartUsageId = startUsageId;
30-
EndUsageId = endUsageId;
31-
Kinds = kinds;
32-
}
3325

3426
/// <summary>
3527
/// Gets the Name all generated Usages shall have.
3628
/// </summary>
3729
[JsonProperty]
38-
public string NamePrefix { get; }
30+
public string NamePrefix { get; } = namePrefix;
3931

4032
/// <summary>
4133
/// Gets the Safe Name all generated Usages shall have.
4234
/// </summary>
43-
public string SafeNamePrefix { get; }
35+
public string SafeNamePrefix { get; } = namePrefix.GetSafe();
4436

4537
/// <summary>
4638
/// Gets the first valid UsageId for this generator. All IDs between <see cref="StartUsageId" /> and
4739
/// <see cref="EndUsageId" /> (inclusive) are valid.
4840
/// </summary>
4941
[JsonProperty]
50-
public ushort StartUsageId { get; }
42+
public ushort StartUsageId { get; } = startUsageId;
5143

5244
/// <summary>
5345
/// Gets the ast valid UsageId for this generator. All IDs between <see cref="StartUsageId" /> and
5446
/// <see cref="EndUsageId" /> (inclusive) are valid.
5547
/// </summary>
5648
[JsonProperty]
57-
public ushort EndUsageId { get; }
49+
public ushort EndUsageId { get; } = endUsageId;
5850

5951
/// <summary>
6052
/// Gets the Usage kinds as defined the HID Usage Table. Most UsageIds will only have a single kind.
6153
/// </summary>
6254
[JsonProperty]
63-
public IReadOnlyCollection<HidUsageKind> Kinds { get; }
55+
public IReadOnlyCollection<HidUsageKind> Kinds { get; } = kinds;
6456

6557
/// <inheritdoc />
6658
public override string ToString() => $"[{StartUsageId}-{EndUsageId}]";

HIDDevices.Generator/IndentStringBuilder.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace HIDDevices.Generator;
1010

11-
public class IndentStringBuilder
11+
public class IndentStringBuilder(string? defaultIndentStr = null, StringBuilder? builder = null)
1212
{
1313
/// <summary>
1414
/// The indent stack.
@@ -20,21 +20,15 @@ public class IndentStringBuilder
2020
/// </summary>
2121
private bool _pauseIndent;
2222

23-
public IndentStringBuilder(string? defaultIndentStr = null, StringBuilder? builder = null)
24-
{
25-
DefaultIndentStr = defaultIndentStr ?? new string(' ', 4);
26-
Builder = builder ?? new StringBuilder();
27-
}
28-
2923
/// <summary>
3024
/// The underlying <see cref="StringBuilder" />.
3125
/// </summary>
32-
public StringBuilder Builder { get; }
26+
public StringBuilder Builder { get; } = builder ?? new StringBuilder();
3327

3428
/// <summary>
3529
/// The default indent string.
3630
/// </summary>
37-
public string DefaultIndentStr { get; }
31+
public string DefaultIndentStr { get; } = defaultIndentStr ?? new string(' ', 4);
3832

3933
public bool IndentEmpty { get; set; } = false;
4034

HIDDevices.Generator/UsagePageGenerator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -573,10 +573,10 @@ private HidUsageTables LoadFromPdf(GeneratorExecutionContext context, Uri pdfUri
573573

574574
var reader = new PdfReader(pdfBytes);
575575
var catalog = reader.Catalog;
576-
if (PdfReader.GetPdfObject(catalog.Get(PdfName.NAMES)) is PdfDictionary documentNames &&
577-
PdfReader.GetPdfObject(documentNames.Get(PdfName.EMBEDDEDFILES)) is PdfDictionary embeddedFiles)
576+
if (PdfReader.GetPdfObject(catalog.Get(PdfName.Names)) is PdfDictionary documentNames &&
577+
PdfReader.GetPdfObject(documentNames.Get(PdfName.Embeddedfiles)) is PdfDictionary embeddedFiles)
578578
{
579-
var fileSpecs = embeddedFiles.GetAsArray(PdfName.NAMES);
579+
var fileSpecs = embeddedFiles.GetAsArray(PdfName.Names);
580580

581581
for (var i = 0; i < fileSpecs.Size; i++)
582582
{
@@ -607,7 +607,7 @@ private HidUsageTables LoadFromPdf(GeneratorExecutionContext context, Uri pdfUri
607607
return HidUsageTables.Empty;
608608
}
609609

610-
if (PdfReader.GetPdfObject(file.GetAsIndirectObject(key)) is not PRStream stream)
610+
if (PdfReader.GetPdfObject(file.GetAsIndirectObject(key)) is not PrStream stream)
611611
{
612612
context.Report(Diagnostics.JsonAttachmentNotFound, Location.None, pdfUri.AbsolutePath);
613613
return HidUsageTables.Empty;

HIDDevices.Sample/HIDDevices.Sample.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
14-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
13+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.2" />
14+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.2" />
1515
</ItemGroup>
1616

1717
<ItemGroup>
1818
<ProjectReference Include="..\HIDDevices.Usages\HIDDevices.Usages.csproj" />
19-
<ProjectReference Include="..\HIDDevices\HIDDevices.csproj"/>
19+
<ProjectReference Include="..\HIDDevices\HIDDevices.csproj" />
2020
</ItemGroup>
2121

2222
<ItemGroup>

HIDDevices.Sample/Samples/GameLoopSample.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected override void Execute()
4040
return;
4141
}
4242

43-
if (g.Name.ToLowerInvariant().Contains("xbox "))
43+
if (g.Name.Contains("xbox ", StringComparison.InvariantCultureIgnoreCase))
4444
{
4545
Logger.LogWarning(
4646
$"{g.Name} found! Unfortunately, it appears XInput-compatible HID device driver only transmits events from the HID device whilst the current process has a focussed window, so console applications/background services cannot detect button presses. Please try a different controller.");

HIDDevices.Sample/SimpleConsoleLogger.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,11 @@
77

88
namespace HIDDevices.Sample;
99

10-
public class SimpleConsoleLogger<T> : ILogger<T>
10+
public class SimpleConsoleLogger<T>(LogLevel logLevel, string? name = null) : ILogger<T>
1111
{
12-
public readonly string Name;
12+
public readonly string Name = name ?? typeof(T).Name;
1313

14-
public SimpleConsoleLogger(LogLevel logLevel, string? name = null)
15-
{
16-
Name = name ?? typeof(T).Name;
17-
LogLevel = logLevel;
18-
}
19-
20-
public LogLevel LogLevel { get; set; }
14+
public LogLevel LogLevel { get; set; } = logLevel;
2115

2216
/// <inheritdoc />
2317
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>net8.0</TargetFramework>
44
<IsPackable>false</IsPackable>
55
<Configurations>Debug;Release;GenerateFromCache;GenerateFromSource</Configurations>
66
</PropertyGroup>
77
<ItemGroup>
8-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
9-
<PackageReference Include="xunit" Version="2.7.0" />
10-
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
8+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
9+
<PackageReference Include="xunit" Version="2.9.3" />
10+
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
1111
<PrivateAssets>all</PrivateAssets>
1212
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1313
</PackageReference>
14-
<PackageReference Include="coverlet.collector" Version="6.0.1">
14+
<PackageReference Include="coverlet.collector" Version="6.0.4">
1515
<PrivateAssets>all</PrivateAssets>
1616
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1717
</PackageReference>
18-
<PackageReference Include="MartinCostello.Logging.XUnit" Version="0.3.0"/>
18+
<PackageReference Include="MartinCostello.Logging.XUnit" Version="0.5.1" />
1919
</ItemGroup>
2020
<ItemGroup>
2121
<ProjectReference Include="..\HIDDevices.Usages\HIDDevices.Usages.csproj" />
22-
<ProjectReference Include="..\HIDDevices\HIDDevices.csproj"/>
22+
<ProjectReference Include="..\HIDDevices\HIDDevices.csproj" />
2323
</ItemGroup>
2424
</Project>

HIDDevices.Usages/HIDDevices.Usages.csproj

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
</PropertyGroup>
5252
<ItemGroup>
5353
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
54-
<PackageReference Include="Nerdbank.GitVersioning" Version="3.6.133">
54+
<PackageReference Include="Nerdbank.GitVersioning" Version="3.7.115">
5555
<PrivateAssets>all</PrivateAssets>
5656
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
5757
</PackageReference>
@@ -68,24 +68,24 @@
6868
</ItemGroup>
6969
<!--Source Generation-->
7070
<ItemGroup Condition="'$(GenerateUsages)' == 'true'">
71-
<ProjectReference Include="$(MSBuildProjectDirectory)\..\HIDDevices.Generator\HIDDevices.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
71+
<ProjectReference Include="$(MSBuildProjectDirectory)\..\HIDDevices.Generator\HIDDevices.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
7272

7373
<!--Expose configuration parameters-->
74-
<CompilerVisibleProperty Include="RootNamespace"/>
75-
<CompilerVisibleProperty Include="HIDUsageTablesPDF"/>
76-
<CompilerVisibleProperty Include="HIDUsageTablesJSON"/>
77-
<CompilerVisibleProperty Include="HIDUsageTablesCacheFolder"/>
78-
<CompilerVisibleProperty Include="GenerateUsagesFromSource"/>
79-
<CompilerVisibleProperty Include="HIDUsagePagesMaxGenerated"/>
74+
<CompilerVisibleProperty Include="RootNamespace" />
75+
<CompilerVisibleProperty Include="HIDUsageTablesPDF" />
76+
<CompilerVisibleProperty Include="HIDUsageTablesJSON" />
77+
<CompilerVisibleProperty Include="HIDUsageTablesCacheFolder" />
78+
<CompilerVisibleProperty Include="GenerateUsagesFromSource" />
79+
<CompilerVisibleProperty Include="HIDUsagePagesMaxGenerated" />
8080

8181
<!--Exclude the output of source generators from the compilation, this will use the 'in-memory' generated files on compilation-->
82-
<Compile Remove="$(CompilerGeneratedFilesOutputPath)/**/*.cs"/>
83-
<Content Include="$(CompilerGeneratedFilesOutputPath)/**/*.cs"/>
82+
<Compile Remove="$(CompilerGeneratedFilesOutputPath)/**/*.cs" />
83+
<Content Include="$(CompilerGeneratedFilesOutputPath)/**/*.cs" />
8484
</ItemGroup>
8585
<Target Name="CleanSourceGeneratedFolders" Condition="'$(GenerateUsages)' == 'true'" BeforeTargets="BeforeBuild" DependsOnTargets="$(BeforeBuildDependsOn)">
8686
<!--Remove the auto-generated code prior to re-generation-->
87-
<RemoveDir Directories="$(CompilerGeneratedFilesOutputPath)\HIDDevices.Generator" ContinueOnError="true"/>
87+
<RemoveDir Directories="$(CompilerGeneratedFilesOutputPath)\HIDDevices.Generator" ContinueOnError="true" />
8888

89-
<Message Importance="High" Text="Generating Usage Tables!"/>
89+
<Message Importance="High" Text="Generating Usage Tables!" />
9090
</Target>
9191
</Project>

0 commit comments

Comments
 (0)