From 886258e4c3b1f40f4fcb4d2d57d834e7ec8ebac3 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sun, 25 May 2025 20:51:29 +0200 Subject: [PATCH] Refactor process tracking to handle null slots and compact array --- DD2Switcher/Program.cs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/DD2Switcher/Program.cs b/DD2Switcher/Program.cs index 23a87b3..6918f22 100644 --- a/DD2Switcher/Program.cs +++ b/DD2Switcher/Program.cs @@ -120,14 +120,32 @@ internal static class Program { } private static void Track(Process process) { + // First find the first null slot + int firstNullIndex = -1; for (int i = 0; i < NumProc; i++) { if (windows[i] == null) { - windows[i] = process; - Console.WriteLine( - $"Added {process.ProcessName} to tracked windows at index {i}"); - return; + firstNullIndex = i; + break; } } + + if (firstNullIndex == -1) { + Console.WriteLine("No slots available for tracking"); + return; + } + + // Compact the array by shifting non-null elements to the left + for (int i = firstNullIndex + 1; i < NumProc; i++) { + if (windows[i] != null) { + windows[firstNullIndex] = windows[i]; + windows[i] = null; + firstNullIndex++; + } + } + + // Add the new process at the first null slot + windows[firstNullIndex] = process; + Console.WriteLine($"Added {process.ProcessName} to tracked windows at index {firstNullIndex}"); } private static void TrackWows() {