Compare commits

..

2 Commits

Author SHA1 Message Date
197f098f67 Fully implement the tabbing and switching 2024-11-03 14:56:47 +01:00
bd24acde46 Rework almost everything 2024-11-03 14:54:03 +01:00

View File

@@ -7,9 +7,10 @@ using System.Windows.Forms;
namespace DD2Switcher; namespace DD2Switcher;
internal static class Program { internal static class Program {
private static List<Process> 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 defaultAffinity = new(0xFF000000);
private static readonly IntPtr fullAffinity = new(0xFFFFFFFF); private static readonly IntPtr fullAffinity = new(0xFFFFFFFF);
@@ -24,116 +25,162 @@ internal static class Program {
static extern bool AllocConsole(); static extern bool AllocConsole();
private static void AdjustAffinities() { private static void AdjustAffinities() {
List<Process> fuckedProcesses = new(); for (int i = 0; i < NumProc; i++) {
var window = windows[i];
foreach (var process in processes) if (window == null) continue;
if (process != activeProcess) { if (window.MainWindowHandle == IntPtr.Zero) {
try { Console.WriteLine($"Window at index {i} has no main window, removing from tracked windows");
process.ProcessorAffinity = defaultAffinity; windows[i] = null;
} continue;
catch (Exception e) {
fuckedProcesses.Add(process);
}
} }
try { if (i != ActiveIndex) {
activeProcess.ProcessorAffinity = fullAffinity; try {
} window.ProcessorAffinity = defaultAffinity;
catch (Exception e) { }
fuckedProcesses.Add(activeProcess); catch (Exception e) {
windows[i] = null;
}
}
} }
foreach (var fucked in fuckedProcesses) var active = windows[ActiveIndex];
processes.Remove(fucked); if (active != null) {
try {
active.ProcessorAffinity = fullAffinity;
}
catch (Exception e) {
windows[ActiveIndex] = null;
}
}
} }
private static void AdjustPriorities() { private static void AdjustPriorities() {
List<Process> fuckedProcesses = new(); for (int i = 0; i < NumProc; i++) {
var window = windows[i];
if (window == null) continue;
if (window.MainWindowHandle == IntPtr.Zero) {
Console.WriteLine($"Window at index {i} has no main window, removing from tracked windows");
windows[i] = null;
continue;
}
foreach (var process in processes) { if (i != ActiveIndex) {
try {
window.PriorityClass = ProcessPriorityClass.Idle;
}
catch (Exception e) {
windows[i] = null;
}
}
}
var active = windows[ActiveIndex];
if (active != null) {
try { try {
process.PriorityClass = ProcessPriorityClass.Idle; active.PriorityClass = ProcessPriorityClass.High;
} }
catch (Exception e) { 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) { private static Process GetForegroundProcess() {
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() {
var foregroundWindow = GetForegroundWindow(); var foregroundWindow = GetForegroundWindow();
Process foregroundGame = null; var process = Process.GetProcesses();
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();
Process foregroundProcess = null; Process foregroundProcess = null;
foreach (var p in process)
foreach (var process in systemProcesses) if (foregroundWindow == p.MainWindowHandle) {
if (foregroundWindow == process.MainWindowHandle) { foregroundProcess = p;
foregroundProcess = process;
break; 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; if (foregroundProcess == null) return;
Console.WriteLine("Foreground process: " + foregroundProcess.ProcessName); Console.WriteLine($"Foreground process: {foregroundProcess}");
var existingProcess = processes.Find(process => process.Id == foregroundProcess.Id);
if (existingProcess != null) { for (int i = 0; i < 9; i++) {
Console.WriteLine("Removing foreground process from tracked..."); var window = windows[i];
processes.Remove(existingProcess); 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 { else {
Console.WriteLine("Adding foreground process to tracked..."); SetForegroundWindow(window.MainWindowHandle);
processes.Add(foregroundProcess); ActiveIndex = index;
AdjustAffinities();
AdjustPriorities();
} }
} }
[STAThread] [STAThread]
private static void Main() { private static void Main() {
// AllocConsole(); //AllocConsole();
var processes = Process.GetProcesses(); var processes = Process.GetProcesses();
var currentProcess = Process.GetCurrentProcess(); var currentProcess = Process.GetCurrentProcess();
@@ -144,70 +191,26 @@ internal static class Program {
Process.GetCurrentProcess().Kill(); Process.GetCurrentProcess().Kill();
} }
for (int i = 0; i < 9; i++) {
HotKeyManager.RegisterHotKey(Keys.D1, KeyModifiers.Alt); windows[i] = null;
HotKeyManager.RegisterHotKey(Keys.D2, KeyModifiers.Alt); HotKeyManager.RegisterHotKey(Keys.D1 + i, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.D3, KeyModifiers.Alt); HotKeyManager.RegisterHotKey(Keys.D1 + i, KeyModifiers.Alt | KeyModifiers.Shift);
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);
HotKeyManager.RegisterHotKey(Keys.Oemtilde, KeyModifiers.Alt); HotKeyManager.RegisterHotKey(Keys.Oemtilde, KeyModifiers.Alt);
// HotKeyManager.RegisterHotKey(Keys.Q, KeyModifiers.Alt);
// HotKeyManager.RegisterHotKey(Keys.W, KeyModifiers.Alt);
// HotKeyManager.RegisterHotKey(Keys.R, KeyModifiers.Alt);
HotKeyManager.HotKeyPressed += HotKeyManager_HotKeyPressed; HotKeyManager.HotKeyPressed += HotKeyManager_HotKeyPressed;
var pixelList = new System.Collections.Generic.List<Pixel>();
// 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) { static void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e) {
switch (e.Key) { if (e.Key == Keys.Oemtilde && e.Modifiers == KeyModifiers.Alt) {
case Keys.D1: TrackWindow();
SwitchToProcess(0); return;
break; }
case Keys.D2:
SwitchToProcess(1); if (e.Modifiers == KeyModifiers.Alt) {
break; TabTo(e.Key - Keys.D1);
case Keys.D3: }
SwitchToProcess(2); else if (e.Modifiers == (KeyModifiers.Alt | KeyModifiers.Shift)) {
break; Swap(e.Key - Keys.D1);
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;
} }
} }