This commit is contained in:
2025-04-25 16:28:48 +02:00
parent be2c6f7829
commit 6a766a5441

View File

@@ -1,255 +1,262 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Windows.Forms; using System.Windows.Forms;
namespace DD2Switcher; namespace DD2Switcher;
internal static class Program { internal static class Program {
private static int NumProc = 19; private static int NumProc = 19;
private static Process[] windows = new Process[NumProc]; private static Process[] windows = new Process[NumProc];
private static int ActiveIndex = -1; private static int ActiveIndex = -1;
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);
[DllImport("user32.dll")] [DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow(); public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")] [DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd); public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("kernel32.dll", SetLastError = true)] [DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)] [return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole(); static extern bool AllocConsole();
private static void CleanWindows() { private static void CleanWindows() {
for (int i = 0; i < NumProc; i++) { for (int i = 0; i < NumProc; i++) {
var window = windows[i]; var window = windows[i];
if (window == null) continue; if (window == null) continue;
if (window.MainWindowHandle == IntPtr.Zero) { if (window.MainWindowHandle == IntPtr.Zero) {
Console.WriteLine($"Window at index {i} has no main window, removing from tracked windows"); Console.WriteLine($"Window at index {i} has no main window, removing from tracked windows");
windows[i] = null; windows[i] = null;
} }
} }
} }
private static void AdjustAffinities() { private static void AdjustAffinities() {
CleanWindows(); CleanWindows();
for (int i = 0; i < NumProc; i++) { for (int i = 0; i < NumProc; i++) {
var window = windows[i]; var window = windows[i];
if (window == null) continue; if (window == null) continue;
if (i != ActiveIndex) { if (i != ActiveIndex) {
try { try {
window.ProcessorAffinity = defaultAffinity; window.ProcessorAffinity = defaultAffinity;
} }
catch (Exception e) { catch (Exception e) {
windows[i] = null; windows[i] = null;
} }
} }
} }
var active = windows[ActiveIndex]; var active = windows[ActiveIndex];
if (active != null) { if (active != null) {
try { try {
active.ProcessorAffinity = fullAffinity; active.ProcessorAffinity = fullAffinity;
} }
catch (Exception e) { catch (Exception e) {
windows[ActiveIndex] = null; windows[ActiveIndex] = null;
} }
} }
} }
private static void AdjustPriorities() { private static void AdjustPriorities() {
CleanWindows(); CleanWindows();
for (int i = 0; i < NumProc; i++) { for (int i = 0; i < NumProc; i++) {
var window = windows[i]; var window = windows[i];
if (window == null) continue; if (window == null) continue;
if (i != ActiveIndex) { if (i != ActiveIndex) {
try { try {
window.PriorityClass = ProcessPriorityClass.Idle; window.PriorityClass = ProcessPriorityClass.Idle;
} }
catch (Exception e) { catch (Exception e) {
windows[i] = null; windows[i] = null;
} }
} }
} }
var active = windows[ActiveIndex]; var active = windows[ActiveIndex];
if (active != null) { if (active != null) {
try { try {
active.PriorityClass = ProcessPriorityClass.High; active.PriorityClass = ProcessPriorityClass.High;
} }
catch (Exception e) { catch (Exception e) {
windows[ActiveIndex] = null; windows[ActiveIndex] = null;
} }
} }
} }
private static Process GetForegroundProcess() { private static Process GetForegroundProcess() {
var foregroundWindow = GetForegroundWindow(); var foregroundWindow = GetForegroundWindow();
var process = Process.GetProcesses(); var process = Process.GetProcesses();
Process foregroundProcess = null; Process foregroundProcess = null;
foreach (var p in process) foreach (var p in process)
if (foregroundWindow == p.MainWindowHandle) { if (foregroundWindow == p.MainWindowHandle) {
foregroundProcess = p; foregroundProcess = p;
break; break;
} }
if (foregroundProcess == null) return null; if (foregroundProcess == null) return null;
return foregroundProcess; return foregroundProcess;
} }
private static Boolean ProcessTracked(int id) { private static Boolean ProcessTracked(int id) {
for (int i = 0; i < NumProc; i++) { for (int i = 0; i < NumProc; i++) {
if (windows[i] != null && windows[i].Id == id) { if (windows[i] != null && windows[i].Id == id) {
return true; return true;
} }
} }
return false; return false;
} }
private static void Track(Process process) { private static void Track(Process process) {
for (int i = 0; i < NumProc; i++) { for (int i = 0; i < NumProc; i++) {
if (windows[i] == null) { if (windows[i] == null) {
windows[i] = process; windows[i] = process;
Console.WriteLine($"Added {process.ProcessName} to tracked windows at index {i}"); Console.WriteLine($"Added {process.ProcessName} to tracked windows at index {i}");
return; return;
} }
} }
} }
private static void TrackWows() { private static void TrackWows() {
CleanWindows(); CleanWindows();
var processes = Process.GetProcesses(); var processes = Process.GetProcesses();
foreach (var process in processes) { foreach (var process in processes) {
if (process.ProcessName == "UWow-64") { if (process.ProcessName == "UWow-64") {
Console.WriteLine($"Found UWow-64 at pid {process.Id}"); Console.WriteLine($"Found UWow-64 at pid {process.Id}");
if (ProcessTracked(process.Id)) if (ProcessTracked(process.Id))
continue; continue;
Track(process); Track(process);
} }
} }
} }
private static void Swap(int index) { private static void Swap(int index) {
index = (index - 1) % NumProc; index = (index - 1) % NumProc;
if (index < 0) index = NumProc - 1; if (index < 0) index = NumProc - 1;
if (index >= NumProc) return; if (index >= NumProc) return;
CleanWindows(); CleanWindows();
Console.WriteLine($"Swapping window at index {index}"); Console.WriteLine($"Swapping window at index {index}");
var process = GetForegroundProcess(); var process = GetForegroundProcess();
if (process == null) return; if (process == null) return;
Console.WriteLine($"Foreground process: {process}"); Console.WriteLine($"Foreground process: {process}");
bool found = false; bool found = false;
for (int i = 0; i < NumProc; i++) { for (int i = 0; i < NumProc; i++) {
var window = windows[i]; var window = windows[i];
if (window != null && window.Id == process.Id) { if (window != null && window.Id == process.Id) {
found = true; found = true;
break; break;
} }
} }
if (!found) { if (!found) {
for (int i = 0; i < NumProc; i++) { for (int i = 0; i < NumProc; i++) {
var window = windows[i]; var window = windows[i];
if (window == null) { if (window == null) {
Console.WriteLine($"Adding foreground window to tracked at index {i}..."); Console.WriteLine($"Adding foreground window to tracked at index {i}...");
windows[i] = process; windows[i] = process;
} }
} }
} }
for (int i = 0; i < NumProc; i++) { for (int i = 0; i < NumProc; i++) {
var window = windows[i]; var window = windows[i];
if (window != null && window.Id == process.Id) { if (window != null && window.Id == process.Id) {
windows[i] = windows[index]; windows[i] = windows[index];
windows[index] = window; windows[index] = window;
Console.WriteLine($"Swapped window at index {i} to {index}"); Console.WriteLine($"Swapped window at index {i} to {index}");
return; return;
} }
} }
} }
private static void TabTo(int index) { private static void TabTo(int index) {
index = (index - 1) % NumProc; index = (index - 1) % NumProc;
if (index < 0) index = NumProc - 1; if (index < 0) index = NumProc - 1;
if (index >= NumProc) return; if (index >= NumProc) return;
CleanWindows(); CleanWindows();
Console.WriteLine($"Tab to window at index {index}"); Console.WriteLine($"Tab to window at index {index}");
var window = windows[index]; var window = windows[index];
if (window == null || window.MainWindowHandle == IntPtr.Zero) { if (window == null || window.MainWindowHandle == IntPtr.Zero) {
Console.WriteLine($"Window at index {index} does not exist, removing from tracked windows"); Console.WriteLine($"Window at index {index} does not exist, removing from tracked windows");
windows[index] = null; windows[index] = null;
} }
else { else {
SetForegroundWindow(window.MainWindowHandle); SetForegroundWindow(window.MainWindowHandle);
ActiveIndex = index; ActiveIndex = index;
AdjustAffinities(); AdjustAffinities();
AdjustPriorities(); 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();
foreach (var process in processes) foreach (var process in processes)
if (process.Id != currentProcess.Id && process.ProcessName == currentProcess.ProcessName) { if (process.Id != currentProcess.Id && process.ProcessName == currentProcess.ProcessName) {
process.Kill(); process.Kill();
Process.GetCurrentProcess().Kill(); Process.GetCurrentProcess().Kill();
} }
// Register main number keys (0-9) long delLast = 0;
for (int i = 0; i < 10; i++) { HotKeyManager.RegisterHotKey(Keys.Decimal, KeyModifiers.NoRepeat);
HotKeyManager.RegisterHotKey(Keys.D0 + i, KeyModifiers.Alt); // Register main number keys (0-9)
HotKeyManager.RegisterHotKey(Keys.D0 + i, KeyModifiers.Alt | KeyModifiers.Shift); for (int i = 0; i < 10; i++)
} HotKeyManager.RegisterHotKey(Keys.D0 + i, KeyModifiers.Alt);
// Register numpad keys (1-9) // Register numpad keys (1-9)
for (int i = 0; i < 9; i++) { for (int i = 0; i < 9; i++)
HotKeyManager.RegisterHotKey(Keys.NumPad1 + i, KeyModifiers.Alt); HotKeyManager.RegisterHotKey(Keys.NumPad1 + i, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.NumPad1 + i, KeyModifiers.Alt | KeyModifiers.Shift);
}
HotKeyManager.RegisterHotKey(Keys.Oemtilde, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.Oemtilde, KeyModifiers.Alt); HotKeyManager.HotKeyPressed += HotKeyManager_HotKeyPressed;
HotKeyManager.HotKeyPressed += HotKeyManager_HotKeyPressed;
void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e) {
static void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e) { if (e.Key == Keys.Oemtilde && e.Modifiers == KeyModifiers.Alt) {
if (e.Key == Keys.Oemtilde && e.Modifiers == KeyModifiers.Alt) { TrackWows();
TrackWows(); return;
return; }
}
long now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
int index; if (e.Key == Keys.Decimal)
if (e.Key >= Keys.D0 && e.Key <= Keys.D9) { delLast = now;
index = e.Key - Keys.D0;
} int index;
else if (e.Key >= Keys.NumPad1 && e.Key <= Keys.NumPad9) { if (e.Key >= Keys.D0 && e.Key <= Keys.D9) {
index = 10 + (e.Key - Keys.NumPad1); index = e.Key - Keys.D0;
} }
else { else if (e.Key >= Keys.NumPad1 && e.Key <= Keys.NumPad9) {
return; index = 10 + (e.Key - Keys.NumPad1);
} }
else {
if (e.Modifiers == KeyModifiers.Alt) { return;
TabTo(index); }
}
else if (e.Modifiers == (KeyModifiers.Alt | KeyModifiers.Shift)) { long cocksucker = DateTime.Now.Second;
Swap(index); Console.WriteLine(cocksucker);
} if (e.Modifiers == KeyModifiers.Alt) {
} if (now - delLast <= 1) {
Swap(index);
Console.CancelKeyPress += (sender, e) => { Process.GetCurrentProcess().Kill(); }; delLast = 0;
while (true) } else {
System.Threading.Thread.Sleep(100000); TabTo(index);
} }
} }
}
Console.CancelKeyPress += (sender, e) => { Process.GetCurrentProcess().Kill(); };
while (true)
System.Threading.Thread.Sleep(100000);
}
}