Skip to content

Commit 801e31a

Browse files
committed
Fix publishing of the app
1 parent 2fe1a3d commit 801e31a

8 files changed

Lines changed: 150 additions & 17 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0-windows10.0.19041.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
8+
<SupportedOSPlatformVersion>10.0.19041.0</SupportedOSPlatformVersion>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<AdditionalFiles Include="NativeMethods.json" />
13+
<AdditionalFiles Include="NativeMethods.txt" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.287">
18+
<PrivateAssets>all</PrivateAssets>
19+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
20+
</PackageReference>
21+
</ItemGroup>
22+
23+
</Project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "https://aka.ms/CsWin32.schema.json",
3+
"public": true
4+
}

MarvinsAIRARefactored/NativeMethods.txt renamed to MarvinsAIRARefactored.Win32/NativeMethods.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ FindWindow
77
GetCursorPos
88
GetWindowLong
99
GetWindowThreadProcessId
10+
HDEVNOTIFY
1011
MapVirtualKey
1112
PostMessage
1213
PROCESS_INFORMATION_CLASS

MarvinsAIRARefactored.sln

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 18.4.11605.240
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MarvinsAIRARefactored", "MarvinsAIRARefactored\MarvinsAIRARefactored.csproj", "{47D4F509-459D-4DDE-9A42-86F944F2FF6C}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MarvinsAIRARefactored.Win32", "MarvinsAIRARefactored.Win32\MarvinsAIRARefactored.Win32.csproj", "{79333233-A7C6-4967-9719-99BADF904110}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,14 @@ Global
2123
{47D4F509-459D-4DDE-9A42-86F944F2FF6C}.Release|Any CPU.Build.0 = Release|Any CPU
2224
{47D4F509-459D-4DDE-9A42-86F944F2FF6C}.Release|x64.ActiveCfg = Release|x64
2325
{47D4F509-459D-4DDE-9A42-86F944F2FF6C}.Release|x64.Build.0 = Release|x64
26+
{79333233-A7C6-4967-9719-99BADF904110}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{79333233-A7C6-4967-9719-99BADF904110}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{79333233-A7C6-4967-9719-99BADF904110}.Debug|x64.ActiveCfg = Debug|Any CPU
29+
{79333233-A7C6-4967-9719-99BADF904110}.Debug|x64.Build.0 = Debug|Any CPU
30+
{79333233-A7C6-4967-9719-99BADF904110}.Release|Any CPU.ActiveCfg = Release|Any CPU
31+
{79333233-A7C6-4967-9719-99BADF904110}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{79333233-A7C6-4967-9719-99BADF904110}.Release|x64.ActiveCfg = Release|Any CPU
33+
{79333233-A7C6-4967-9719-99BADF904110}.Release|x64.Build.0 = Release|Any CPU
2434
EndGlobalSection
2535
GlobalSection(SolutionProperties) = preSolution
2636
HideSolutionNode = FALSE

MarvinsAIRARefactored/App.xaml.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private static string GetDevRootPath( [CallerFilePath] string callerFile = "" )
7171
public SpeechToText SpeechToText { get; private set; } = null!;
7272
public Wind Wind { get; private set; } = null!;
7373
public SeatBeltTensioner SeatBeltTensioner { get; private set; } = null!;
74-
public HidHotplugMonitor HidHotplugMonitor { get; private set; } = null!;
74+
public HidHotPlugMonitor HidHotPlugMonitor { get; private set; } = null!;
7575
public TradingPaints TradingPaints { get; private set; } = null!;
7676
public AppManager AppManager { get; private set; } = null!;
7777

@@ -135,7 +135,7 @@ private void InitializeRuntimeComponents()
135135
SpeechToText = new();
136136
Wind = new();
137137
SeatBeltTensioner = new();
138-
HidHotplugMonitor = new();
138+
HidHotPlugMonitor = new();
139139
TradingPaints = new();
140140
AppManager = new();
141141

@@ -298,7 +298,7 @@ void RunStartupStep( string statusKey, Action initializeAction )
298298
RunStartupStep( "InitializingTelemetry", Telemetry.Initialize );
299299
RunStartupStep( "InitializingWind", Wind.Initialize );
300300
RunStartupStep( "InitializingSeatBeltTensioner", SeatBeltTensioner.Initialize );
301-
RunStartupStep( "InitializingHidHotPlugMonitor", HidHotplugMonitor.Initialize );
301+
RunStartupStep( "InitializingHidHotPlugMonitor", HidHotPlugMonitor.Initialize );
302302
RunStartupStep( "InitializingTradingPaints", TradingPaints.Initialize );
303303
RunStartupStep( "InitializingSettingsFile", SettingsFile.Initialize );
304304

MarvinsAIRARefactored/Components/DirectInput.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void Initialize()
7070

7171
EnumerateDevices();
7272

73-
app.HidHotplugMonitor.DeviceListMightHaveChanged += OnDeviceListMightHaveChanged;
73+
app.HidHotPlugMonitor.DeviceListMightHaveChanged += OnDeviceListMightHaveChanged;
7474

7575
app.Logger.WriteLine( "[DirectInput] <<< Initialize" );
7676
}

MarvinsAIRARefactored/Components/HidHotPlugMonitor.cs

Lines changed: 103 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-

1+
using System.ComponentModel;
22
using System.Runtime.InteropServices;
33
using System.Windows.Interop;
44

@@ -8,7 +8,7 @@
88

99
namespace MarvinsAIRARefactored.Components;
1010

11-
public sealed class HidHotplugMonitor : IDisposable
11+
public sealed class HidHotPlugMonitor : IDisposable
1212
{
1313
private HwndSource? _hwndSource;
1414
private HDEVNOTIFY _deviceNotifyHandle;
@@ -22,10 +22,14 @@ public void Initialize()
2222
{
2323
var app = App.Instance!;
2424

25+
app.Logger.WriteLine( "[HidHotPlugMonitor] Initialize >>>" );
26+
2527
app.MainWindow.SourceInitialized += ( _, __ ) =>
2628
{
2729
var hwnd = new WindowInteropHelper( app.MainWindow ).Handle;
2830

31+
app.Logger.WriteLine( $"[HidHotPlugMonitor] SourceInitialized hwnd=0x{hwnd.ToInt64():X}" );
32+
2933
SetupForHwnd( hwnd );
3034
};
3135

@@ -37,30 +41,62 @@ public void Initialize()
3741
{
3842
var hwnd = new WindowInteropHelper( app.MainWindow ).EnsureHandle();
3943

44+
app.Logger.WriteLine( $"[HidHotPlugMonitor] EnsureHandle hwnd=0x{hwnd.ToInt64():X}" );
45+
4046
SetupForHwnd( hwnd );
4147
} );
4248

4349
app.MainWindow.Closed += ( _, __ ) => Dispose();
50+
51+
app.Logger.WriteLine( "[HidHotPlugMonitor] <<< Initialize" );
4452
}
4553

4654
public void Dispose()
4755
{
56+
var app = App.Instance!;
57+
58+
app.Logger.WriteLine( "[HidHotPlugMonitor] Dispose >>>" );
59+
4860
if ( _deviceNotifyHandle != HDEVNOTIFY.Null )
4961
{
50-
_ = PInvoke.UnregisterDeviceNotification( _deviceNotifyHandle );
62+
var unregisterSucceeded = PInvoke.UnregisterDeviceNotification( _deviceNotifyHandle );
63+
64+
if ( unregisterSucceeded )
65+
{
66+
app.Logger.WriteLine( "[HidHotPlugMonitor] UnregisterDeviceNotification succeeded." );
67+
}
68+
else
69+
{
70+
var lastError = Marshal.GetLastPInvokeError();
71+
var lastErrorMessage = new Win32Exception( lastError ).Message;
72+
73+
app.Logger.WriteLine( $"[HidHotPlugMonitor] UnregisterDeviceNotification failed. Error={lastError} ({lastErrorMessage})" );
74+
}
5175

5276
_deviceNotifyHandle = HDEVNOTIFY.Null;
5377
}
78+
else
79+
{
80+
app.Logger.WriteLine( "[HidHotPlugMonitor] No device notification handle to unregister." );
81+
}
5482

5583
_hwndSource?.RemoveHook( WndProc );
5684

5785
_hwndSource = null;
5886

5987
_debounceTimer?.Dispose();
88+
_debounceTimer = null;
89+
90+
app.Logger.WriteLine( "[HidHotPlugMonitor] <<< Dispose" );
6091
}
6192

6293
private unsafe void RegisterForHidNotifications( IntPtr hwnd )
6394
{
95+
var app = App.Instance!;
96+
97+
app.Logger.WriteLine( "[HidHotPlugMonitor] RegisterForHidNotifications >>>" );
98+
app.Logger.WriteLine( $"[HidHotPlugMonitor] Registering HID notifications for hwnd=0x{hwnd.ToInt64():X}, interfaceGuid={_hidInterfaceGuid}" );
99+
64100
var deviceBroadcastInterface = new DEV_BROADCAST_DEVICEINTERFACE_W
65101
{
66102
dbcc_size = (uint) sizeof( DEV_BROADCAST_DEVICEINTERFACE_W ),
@@ -72,46 +108,102 @@ private unsafe void RegisterForHidNotifications( IntPtr hwnd )
72108
new HANDLE( hwnd ),
73109
&deviceBroadcastInterface,
74110
REGISTER_NOTIFICATION_FLAGS.DEVICE_NOTIFY_WINDOW_HANDLE );
111+
112+
if ( _deviceNotifyHandle == HDEVNOTIFY.Null )
113+
{
114+
var lastError = Marshal.GetLastPInvokeError();
115+
var lastErrorMessage = new Win32Exception( lastError ).Message;
116+
117+
app.Logger.WriteLine( $"[HidHotPlugMonitor] RegisterDeviceNotification failed. Error={lastError} ({lastErrorMessage})" );
118+
}
119+
else
120+
{
121+
app.Logger.WriteLine( "[HidHotPlugMonitor] RegisterDeviceNotification succeeded." );
122+
}
123+
124+
app.Logger.WriteLine( "[HidHotPlugMonitor] <<< RegisterForHidNotifications" );
75125
}
76126

77127
private void SetupForHwnd( IntPtr hwnd )
78128
{
79-
if ( ( hwnd == IntPtr.Zero ) || ( _hwndSource is not null ) )
129+
var app = App.Instance!;
130+
131+
app.Logger.WriteLine( "[HidHotPlugMonitor] SetupForHwnd >>>" );
132+
133+
if ( hwnd == IntPtr.Zero )
80134
{
135+
app.Logger.WriteLine( "[HidHotPlugMonitor] SetupForHwnd skipped because hwnd is zero." );
136+
app.Logger.WriteLine( "[HidHotPlugMonitor] <<< SetupForHwnd" );
137+
138+
return;
139+
}
140+
141+
if ( _hwndSource is not null )
142+
{
143+
app.Logger.WriteLine( $"[HidHotPlugMonitor] SetupForHwnd skipped because HwndSource already exists. hwnd=0x{hwnd.ToInt64():X}" );
144+
app.Logger.WriteLine( "[HidHotPlugMonitor] <<< SetupForHwnd" );
145+
81146
return;
82147
}
83148

84149
_hwndSource = HwndSource.FromHwnd( hwnd );
85-
_hwndSource?.AddHook( WndProc );
150+
151+
if ( _hwndSource is null )
152+
{
153+
app.Logger.WriteLine( $"[HidHotPlugMonitor] HwndSource.FromHwnd returned null. hwnd=0x{hwnd.ToInt64():X}" );
154+
app.Logger.WriteLine( "[HidHotPlugMonitor] <<< SetupForHwnd" );
155+
156+
return;
157+
}
158+
159+
_hwndSource.AddHook( WndProc );
160+
161+
app.Logger.WriteLine( $"[HidHotPlugMonitor] WndProc hook added. hwnd=0x{hwnd.ToInt64():X}" );
86162

87163
RegisterForHidNotifications( hwnd );
88164

89165
if ( _debounceTimer is null )
90166
{
91167
_debounceTimer = new System.Timers.Timer( 2000 ) { AutoReset = false };
92168

93-
_debounceTimer.Elapsed += ( _, __ ) => DeviceListMightHaveChanged?.Invoke( this, EventArgs.Empty );
169+
_debounceTimer.Elapsed += ( _, __ ) =>
170+
{
171+
app.Logger.WriteLine( "[HidHotPlugMonitor] Device change debounce elapsed. Raising DeviceListMightHaveChanged." );
172+
173+
DeviceListMightHaveChanged?.Invoke( this, EventArgs.Empty );
174+
};
175+
176+
app.Logger.WriteLine( "[HidHotPlugMonitor] Device change debounce timer created." );
94177
}
178+
179+
app.Logger.WriteLine( "[HidHotPlugMonitor] <<< SetupForHwnd" );
95180
}
96181

97182
private IntPtr WndProc( IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled )
98183
{
99184
if ( msg == PInvoke.WM_DEVICECHANGE )
100185
{
186+
var app = App.Instance!;
101187
var eventType = wParam.ToInt32();
102188

189+
app.Logger.WriteLine( $"[HidHotPlugMonitor] WM_DEVICECHANGE eventType=0x{eventType:X}, lParam=0x{lParam.ToInt64():X}" );
190+
103191
if ( eventType == PInvoke.DBT_DEVICEARRIVAL || eventType == PInvoke.DBT_DEVICEREMOVECOMPLETE )
104192
{
105193
_debounceTimer?.Stop();
106194
_debounceTimer?.Start();
107195

108196
if ( lParam == IntPtr.Zero )
109197
{
198+
app.Logger.WriteLine( "[HidHotPlugMonitor] Device change lParam is zero; no device path available." );
199+
110200
return IntPtr.Zero;
111201
}
112202

113203
var broadcastHeader = Marshal.PtrToStructure<DEV_BROADCAST_HDR>( lParam );
114204

205+
app.Logger.WriteLine( $"[HidHotPlugMonitor] Device change broadcast type={broadcastHeader.dbch_devicetype}" );
206+
115207
if ( broadcastHeader.dbch_devicetype == DEV_BROADCAST_HDR_DEVICE_TYPE.DBT_DEVTYP_DEVICEINTERFACE )
116208
{
117209
var nameOffset = Marshal.OffsetOf<DEV_BROADCAST_DEVICEINTERFACE_W>(
@@ -120,8 +212,6 @@ private IntPtr WndProc( IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref
120212
var namePtr = IntPtr.Add( lParam, nameOffset );
121213
var devicePath = Marshal.PtrToStringUni( namePtr ) ?? string.Empty;
122214

123-
var app = App.Instance!;
124-
125215
if ( eventType == PInvoke.DBT_DEVICEREMOVECOMPLETE )
126216
{
127217
app.Logger.WriteLine( $"[HidHotPlugMonitor] Device {devicePath} was removed!" );
@@ -131,9 +221,13 @@ private IntPtr WndProc( IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref
131221
app.Logger.WriteLine( $"[HidHotPlugMonitor] Device {devicePath} was added!" );
132222
}
133223
}
224+
else
225+
{
226+
app.Logger.WriteLine( $"[HidHotPlugMonitor] Ignoring non-device-interface broadcast type={broadcastHeader.dbch_devicetype}" );
227+
}
134228
}
135229
}
136230

137231
return IntPtr.Zero;
138232
}
139-
}
233+
}

MarvinsAIRARefactored/MarvinsAIRARefactored.csproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<ApplicationHighDpiMode>PerMonitorV2</ApplicationHighDpiMode>
1414
<SupportedOSPlatformVersion>10.0.19041.0</SupportedOSPlatformVersion>
1515
<EnableDefaultEmbeddedResourceItems>false</EnableDefaultEmbeddedResourceItems>
16+
<IncludePackageReferencesDuringMarkupCompilation>true</IncludePackageReferencesDuringMarkupCompilation>
1617
</PropertyGroup>
1718
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" />
1819
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'" />
@@ -197,10 +198,6 @@
197198
<PackageReference Include="CsvHelper" Version="33.1.0" />
198199
<PackageReference Include="IRSDKSharper" Version="1.1.8" />
199200
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.4022.49" />
200-
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.287">
201-
<PrivateAssets>all</PrivateAssets>
202-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
203-
</PackageReference>
204201
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
205202
<PackageReference Include="OpenMacroBoard.SDK" Version="6.1.0" />
206203
<PackageReference Include="SharpDX.DirectInput" Version="4.2.0" />
@@ -213,6 +210,10 @@
213210
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
214211
</ItemGroup>
215212

213+
<ItemGroup>
214+
<ProjectReference Include="..\MarvinsAIRARefactored.Win32\MarvinsAIRARefactored.Win32.csproj" />
215+
</ItemGroup>
216+
216217
<ItemGroup>
217218
<Reference Include="SimagicHPR">
218219
<HintPath>SimagicHPR.dll</HintPath>

0 commit comments

Comments
 (0)