diff --git a/DD2Switcher/Program.cs b/DD2Switcher/Program.cs index 75552bf..c1c0428 100644 --- a/DD2Switcher/Program.cs +++ b/DD2Switcher/Program.cs @@ -7,9 +7,10 @@ using System.Windows.Forms; namespace DD2Switcher; internal static class Program { - private static List processes = new(); + private static int NumProc = 9; + private static Process[] windows = new Process[NumProc]; + private static int ActiveIndex = -1; - private static Process activeProcess; private static readonly IntPtr defaultAffinity = new(0xFF000000); private static readonly IntPtr fullAffinity = new(0xFFFFFFFF); @@ -24,116 +25,158 @@ internal static class Program { static extern bool AllocConsole(); private static void AdjustAffinities() { - List fuckedProcesses = new(); - - foreach (var process in processes) - if (process != activeProcess) { + for (int i = 0; i < NumProc; i++) { + var window = windows[i]; + if (window.MainWindowHandle == IntPtr.Zero) { + Console.WriteLine($"Window at index {i} has no main window, removing from tracked windows"); + windows[i] = null; + continue; + } + if (window != null && i != ActiveIndex) { try { - process.ProcessorAffinity = defaultAffinity; + window.ProcessorAffinity = defaultAffinity; } catch (Exception e) { - fuckedProcesses.Add(process); + windows[i] = null; } } - - try { - activeProcess.ProcessorAffinity = fullAffinity; - } - catch (Exception e) { - fuckedProcesses.Add(activeProcess); } - foreach (var fucked in fuckedProcesses) - processes.Remove(fucked); + var active = windows[ActiveIndex]; + if (active != null) { + try { + active.ProcessorAffinity = fullAffinity; + } + catch (Exception e) { + windows[ActiveIndex] = null; + } + } } private static void AdjustPriorities() { - List fuckedProcesses = new(); - - foreach (var process in processes) { + for (int i = 0; i < NumProc; i++) { + var window = windows[i]; + if (window.MainWindowHandle == IntPtr.Zero) { + Console.WriteLine($"Window at index {i} has no main window, removing from tracked windows"); + windows[i] = null; + continue; + } + if (window != null && i != ActiveIndex) { + try { + window.PriorityClass = ProcessPriorityClass.Idle; + } + catch (Exception e) { + windows[i] = null; + } + } + } + + var active = windows[ActiveIndex]; + if (active != null) { try { - process.PriorityClass = ProcessPriorityClass.Idle; + active.PriorityClass = ProcessPriorityClass.High; } catch (Exception e) { - fuckedProcesses.Add(process); + windows[ActiveIndex] = null; } } - - try { - activeProcess.PriorityClass = ProcessPriorityClass.High; - } - catch (Exception e) { - fuckedProcesses.Add(activeProcess); - } - - foreach (var fucked in fuckedProcesses) - processes.Remove(fucked); } - private static void SwitchToProcess(int index) { - Console.WriteLine("Switching to process at index " + index); - if (index >= processes.Count) return; - var targetWindowHandle = processes[processes.Count - 1 - index].MainWindowHandle; - if (targetWindowHandle == IntPtr.Zero) { - processes.RemoveAt(processes.Count - 1 - index); - return; - } - - SetForegroundWindow(targetWindowHandle); - activeProcess = processes[processes.Count - 1 - index]; - AdjustAffinities(); - AdjustPriorities(); - } - - private static void SwitchMainGame() { + private static Process GetForegroundProcess() { var foregroundWindow = GetForegroundWindow(); - Process foregroundGame = null; - var foregroundGameIndex = -1; - var exists = false; - - foreach (var process in processes) - if (foregroundWindow == process.MainWindowHandle) { - exists = true; - foregroundGame = process; - foregroundGameIndex = processes.IndexOf(process); - break; - } - - if (exists) { - var tempGame = processes[0]; - processes[0] = foregroundGame; - processes[foregroundGameIndex] = tempGame; - } - } - - private static void ToggleGame() { - Console.WriteLine("Toggling foreground window as tracked..."); - var foregroundWindow = GetForegroundWindow(); - var systemProcesses = Process.GetProcesses(); + var process = Process.GetProcesses(); Process foregroundProcess = null; - - foreach (var process in systemProcesses) - if (foregroundWindow == process.MainWindowHandle) { - foregroundProcess = process; + foreach (var p in process) + if (foregroundWindow == p.MainWindowHandle) { + foregroundProcess = p; break; } + if (foregroundProcess == null) return null; + return foregroundProcess; + } + + private static void TrackWindow() { + Console.WriteLine("Toggling foreground window as tracked..."); + var foregroundProcess = GetForegroundProcess(); if (foregroundProcess == null) return; - Console.WriteLine("Foreground process: " + foregroundProcess.ProcessName); - var existingProcess = processes.Find(process => process.Id == foregroundProcess.Id); - if (existingProcess != null) { - Console.WriteLine("Removing foreground process from tracked..."); - processes.Remove(existingProcess); + Console.WriteLine($"Foreground process: {foregroundProcess}"); + + for (int i = 0; i < 9; i++) { + var window = windows[i]; + if (window != null && window.Id == foregroundProcess.Id) { + Console.WriteLine($"Removing foreground window from tracked at index {i}..."); + windows[i] = null; + return; + } + } + + for (int i = 0; i < 9; i++) { + var window = windows[i]; + if (window == null) { + Console.WriteLine($"Adding foreground window to tracked at index {i}..."); + windows[i] = foregroundProcess; + return; + } + } + } + + private static void Swap(int index) { + Console.WriteLine($"Swapping window at index {index}"); + var process = GetForegroundProcess(); + if (process == null) return; + Console.WriteLine($"Foreground process: {process}"); + bool found = false; + + for (int i = 0; i < 9; i++) { + var window = windows[i]; + if (window != null && window.Id == process.Id) { + found = true; + break; + } + } + + if (!found) { + for (int i = 0; i < 9; i++) { + var window = windows[i]; + if (window == null) { + Console.WriteLine($"Adding foreground window to tracked at index {i}..."); + windows[i] = process; + } + } + } + + for (int i = 0; i < 9; i++) { + var window = windows[i]; + if (window != null && window.Id == process.Id) { + windows[i] = windows[index]; + windows[index] = window; + Console.WriteLine($"Swapped window at index {i} to {index}"); + return; + } + } + } + + private static void TabTo(int index) { + if (index >= NumProc) return; + Console.WriteLine($"Tab to window at index {index}"); + + var window = windows[index]; + if (window == null || window.MainWindowHandle == IntPtr.Zero) { + Console.WriteLine($"Window at index {index} does not exist, removing from tracked windows"); + windows[index] = null; } else { - Console.WriteLine("Adding foreground process to tracked..."); - processes.Add(foregroundProcess); + SetForegroundWindow(window.MainWindowHandle); + ActiveIndex = index; + AdjustAffinities(); + AdjustPriorities(); } } - + [STAThread] private static void Main() { - // AllocConsole(); + AllocConsole(); var processes = Process.GetProcesses(); var currentProcess = Process.GetCurrentProcess(); @@ -144,26 +187,11 @@ internal static class Program { Process.GetCurrentProcess().Kill(); } - - HotKeyManager.RegisterHotKey(Keys.D1, KeyModifiers.Alt); - HotKeyManager.RegisterHotKey(Keys.D2, KeyModifiers.Alt); - HotKeyManager.RegisterHotKey(Keys.D3, KeyModifiers.Alt); - HotKeyManager.RegisterHotKey(Keys.D4, KeyModifiers.Alt); - HotKeyManager.RegisterHotKey(Keys.D5, KeyModifiers.Alt); - HotKeyManager.RegisterHotKey(Keys.D6, KeyModifiers.Alt); - HotKeyManager.RegisterHotKey(Keys.D7, KeyModifiers.Alt); - HotKeyManager.RegisterHotKey(Keys.D8, KeyModifiers.Alt); - HotKeyManager.RegisterHotKey(Keys.D9, KeyModifiers.Alt); - - HotKeyManager.RegisterHotKey(Keys.D1, KeyModifiers.Alt | KeyModifiers.Shift); - HotKeyManager.RegisterHotKey(Keys.D2, KeyModifiers.Alt | KeyModifiers.Shift); - HotKeyManager.RegisterHotKey(Keys.D3, KeyModifiers.Alt | KeyModifiers.Shift); - HotKeyManager.RegisterHotKey(Keys.D4, KeyModifiers.Alt | KeyModifiers.Shift); - HotKeyManager.RegisterHotKey(Keys.D5, KeyModifiers.Alt | KeyModifiers.Shift); - HotKeyManager.RegisterHotKey(Keys.D6, KeyModifiers.Alt | KeyModifiers.Shift); - HotKeyManager.RegisterHotKey(Keys.D7, KeyModifiers.Alt | KeyModifiers.Shift); - HotKeyManager.RegisterHotKey(Keys.D8, KeyModifiers.Alt | KeyModifiers.Shift); - HotKeyManager.RegisterHotKey(Keys.D9, KeyModifiers.Alt | KeyModifiers.Shift); + for (int i = 0; i < 9; i++) { + windows.Add(null); + HotKeyManager.RegisterHotKey(Keys.D1 + i, KeyModifiers.Alt); + HotKeyManager.RegisterHotKey(Keys.D1 + i, KeyModifiers.Alt | KeyModifiers.Shift); + } HotKeyManager.RegisterHotKey(Keys.Oemtilde, KeyModifiers.Alt); @@ -172,42 +200,17 @@ internal static class Program { // HotKeyManager.RegisterHotKey(Keys.R, KeyModifiers.Alt); HotKeyManager.HotKeyPressed += HotKeyManager_HotKeyPressed; - var pixelList = new System.Collections.Generic.List(); - // pixelList.Add(new Pixel(1401, 1234, 224, 224, 224)); - pixelList.Add(new Pixel(1359, 1235, 220, 220, 220)); - static void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e) { - switch (e.Key) { - case Keys.D1: - SwitchToProcess(0); - break; - case Keys.D2: - SwitchToProcess(1); - break; - case Keys.D3: - SwitchToProcess(2); - break; - case Keys.D4: - SwitchToProcess(3); - break; - case Keys.D5: - SwitchToProcess(4); - break; - case Keys.D6: - SwitchToProcess(5); - break; - case Keys.D7: - SwitchToProcess(6); - break; - case Keys.D8: - SwitchToProcess(7); - break; - case Keys.D9: - SwitchToProcess(8); - break; - case Keys.Oemtilde: - ToggleGame(); - break; + if (e.Key == Keys.Oemtilde && e.Modifiers == KeyModifiers.Alt) { + TrackWindow(); + return; + } + + if (e.Modifiers == KeyModifiers.Alt) { + TabTo(e.Key - Keys.D1); + } + else if (e.Modifiers == (KeyModifiers.Alt | KeyModifiers.Shift)) { + Swap(e.Key - Keys.D1); } }