From a1a42e5aac1bf636254e33ee01c336b17549284f Mon Sep 17 00:00:00 2001 From: Luna <34936608+Plootie@users.noreply.github.com> Date: Tue, 23 Sep 2025 22:12:49 +0100 Subject: [PATCH 1/3] Implement error catching for hotspots --- LockIn/ViewModels/MainWindowViewModel.cs | 71 ++++++++++++++++-------- 1 file changed, 49 insertions(+), 22 deletions(-) diff --git a/LockIn/ViewModels/MainWindowViewModel.cs b/LockIn/ViewModels/MainWindowViewModel.cs index 36a59db..05b2a5d 100644 --- a/LockIn/ViewModels/MainWindowViewModel.cs +++ b/LockIn/ViewModels/MainWindowViewModel.cs @@ -39,36 +39,56 @@ public MainWindowViewModel() private async Task GetHelldiversProcessAsync(CancellationToken ct = default) { - Process[] processes; - while ((processes = Process.GetProcessesByName(HELLDIVER_PROCESS_NAME)).Length < 1) + try { - await Task.Delay(1000, ct).ConfigureAwait(false); + Process[] processes; + while ((processes = Process.GetProcessesByName(HELLDIVER_PROCESS_NAME)).Length < 1) + { + await Task.Delay(1000, ct).ConfigureAwait(false); + } + + return processes[0]; + } + catch (Exception e) + { + MessageBox.Show( + $"The code has undemocratically crashed!\nPlease report this violation to your democracy officer:\n\n{e}", + "Error", MessageBoxButton.OK, MessageBoxImage.Error); + throw; } - - return processes[0]; } private async Task LockIn(Process proc, CancellationToken ct = default) { - IntPtr helldiverWindowHandle = proc.MainWindowHandle; //Technically this gets cached anyway but meh - while (!ct.IsCancellationRequested && !proc.HasExited) + try { - IntPtr foregroundWindowHandle = Natives.GetForegroundWindow(); - if (foregroundWindowHandle == helldiverWindowHandle && proc.Responding) - { - if (!Natives.GetWindowRect(foregroundWindowHandle, out RECT windowBounds)) break; - Natives.ClipCursor(ref windowBounds); - } - else + IntPtr helldiverWindowHandle = proc.MainWindowHandle; //Technically this gets cached anyway but meh + while (!ct.IsCancellationRequested && !proc.HasExited) { - Natives.ClipCursor(IntPtr.Zero); - } + IntPtr foregroundWindowHandle = Natives.GetForegroundWindow(); + if (foregroundWindowHandle == helldiverWindowHandle && proc.Responding) + { + if (!Natives.GetWindowRect(foregroundWindowHandle, out RECT windowBounds)) break; + Natives.ClipCursor(ref windowBounds); + } + else + { + Natives.ClipCursor(IntPtr.Zero); + } - try{ await Task.Delay(100, ct).ConfigureAwait(false); } - catch { break; } - } + try{ await Task.Delay(100, ct).ConfigureAwait(false); } + catch { break; } + } - Natives.ClipCursor(IntPtr.Zero); + Natives.ClipCursor(IntPtr.Zero); + } + catch (Exception e) + { + MessageBox.Show( + $"The code has undemocratically crashed!\nPlease report this violation to your democracy officer:\n\n{e}", + "Error", MessageBoxButton.OK, MessageBoxImage.Error); + throw; + } } private void QuitApplication(object sender, ExitEventArgs e) @@ -93,11 +113,17 @@ public void ClipGame() StatusText = _statusTextDictionary[StatusEnum.LookingForGame]; CancellationTokenSource linkedTokenSource = + CancellationTokenSource.CreateLinkedTokenSource(_applicationExitTokenSource.Token, _clipCTS.Token); _ = GetHelldiversProcessAsync(linkedTokenSource.Token).ContinueWith(async t => { - //TODO: Report this error - if (t.Status != TaskStatus.RanToCompletion) return; + if (t.Status != TaskStatus.RanToCompletion) + { + MessageBox.Show( + $"The code has undemocratically crashed!\nPlease report this violation to your democracy officer:\n\n{t.Exception}", + "Error", MessageBoxButton.OK, MessageBoxImage.Error); + return; + } Process helldiversProc = t.Result; while (helldiversProc.MainWindowHandle == IntPtr.Zero) { @@ -108,6 +134,7 @@ public void ClipGame() StatusText = _statusTextDictionary[StatusEnum.Enabled]; _ = LockIn(helldiversProc, linkedTokenSource.Token).ContinueWith(x => { + if(x.Status != TaskStatus.RanToCompletion) return; if(IsLockEnabled) ClipGame(); }); }, linkedTokenSource.Token); From 274684c1a397011af469db00e0ad13271672e66c Mon Sep 17 00:00:00 2001 From: Luna <34936608+Plootie@users.noreply.github.com> Date: Tue, 23 Sep 2025 22:18:19 +0100 Subject: [PATCH 2/3] Fix error reporting catching task cancellation --- LockIn/ViewModels/MainWindowViewModel.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/LockIn/ViewModels/MainWindowViewModel.cs b/LockIn/ViewModels/MainWindowViewModel.cs index 05b2a5d..38cec34 100644 --- a/LockIn/ViewModels/MainWindowViewModel.cs +++ b/LockIn/ViewModels/MainWindowViewModel.cs @@ -51,9 +51,12 @@ private async Task GetHelldiversProcessAsync(CancellationToken ct = def } catch (Exception e) { - MessageBox.Show( - $"The code has undemocratically crashed!\nPlease report this violation to your democracy officer:\n\n{e}", - "Error", MessageBoxButton.OK, MessageBoxImage.Error); + if (e is not OperationCanceledException) + { + MessageBox.Show( + $"The code has undemocratically crashed!\nPlease report this violation to your democracy officer:\n\n{e}", + "Error", MessageBoxButton.OK, MessageBoxImage.Error); + } throw; } } From 50663af884e08c498de8d877196c9c07b2ade9a1 Mon Sep 17 00:00:00 2001 From: Luna <34936608+Plootie@users.noreply.github.com> Date: Thu, 25 Sep 2025 18:52:04 +0100 Subject: [PATCH 3/3] Add reporting for missing window handle --- LockIn/ViewModels/MainWindowViewModel.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/LockIn/ViewModels/MainWindowViewModel.cs b/LockIn/ViewModels/MainWindowViewModel.cs index 38cec34..309ed5b 100644 --- a/LockIn/ViewModels/MainWindowViewModel.cs +++ b/LockIn/ViewModels/MainWindowViewModel.cs @@ -130,7 +130,9 @@ public void ClipGame() Process helldiversProc = t.Result; while (helldiversProc.MainWindowHandle == IntPtr.Zero) { + MessageBox.Show("Proc found but now window handle. Waiting..."); await Task.Delay(250, linkedTokenSource.Token).ConfigureAwait(false); + helldiversProc.Refresh(); } helldiversProc.Refresh();