Compare commits
2 Commits
3872c5181f
...
197f098f67
Author | SHA1 | Date | |
---|---|---|---|
197f098f67 | |||
bd24acde46 |
@@ -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];
|
||||||
|
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) {
|
||||||
if (process != activeProcess) {
|
|
||||||
try {
|
try {
|
||||||
process.ProcessorAffinity = defaultAffinity;
|
window.ProcessorAffinity = defaultAffinity;
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
fuckedProcesses.Add(process);
|
windows[i] = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var active = windows[ActiveIndex];
|
||||||
|
if (active != null) {
|
||||||
try {
|
try {
|
||||||
activeProcess.ProcessorAffinity = fullAffinity;
|
active.ProcessorAffinity = fullAffinity;
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
fuckedProcesses.Add(activeProcess);
|
windows[ActiveIndex] = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var fucked in fuckedProcesses)
|
|
||||||
processes.Remove(fucked);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
try {
|
||||||
process.PriorityClass = ProcessPriorityClass.Idle;
|
window.PriorityClass = ProcessPriorityClass.Idle;
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
fuckedProcesses.Add(process);
|
windows[i] = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var active = windows[ActiveIndex];
|
||||||
|
if (active != null) {
|
||||||
try {
|
try {
|
||||||
activeProcess.PriorityClass = ProcessPriorityClass.High;
|
active.PriorityClass = ProcessPriorityClass.High;
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
fuckedProcesses.Add(activeProcess);
|
windows[ActiveIndex] = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var fucked in fuckedProcesses)
|
private static Process GetForegroundProcess() {
|
||||||
processes.Remove(fucked);
|
var foregroundWindow = GetForegroundWindow();
|
||||||
|
var process = Process.GetProcesses();
|
||||||
|
Process foregroundProcess = null;
|
||||||
|
foreach (var p in process)
|
||||||
|
if (foregroundWindow == p.MainWindowHandle) {
|
||||||
|
foregroundProcess = p;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void SwitchToProcess(int index) {
|
if (foregroundProcess == null) return null;
|
||||||
Console.WriteLine("Switching to process at index " + index);
|
return foregroundProcess;
|
||||||
if (index >= processes.Count) return;
|
}
|
||||||
var targetWindowHandle = processes[processes.Count - 1 - index].MainWindowHandle;
|
|
||||||
if (targetWindowHandle == IntPtr.Zero) {
|
private static void TrackWindow() {
|
||||||
processes.RemoveAt(processes.Count - 1 - index);
|
Console.WriteLine("Toggling foreground window as tracked...");
|
||||||
|
var foregroundProcess = GetForegroundProcess();
|
||||||
|
if (foregroundProcess == null) return;
|
||||||
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
SetForegroundWindow(targetWindowHandle);
|
|
||||||
activeProcess = processes[processes.Count - 1 - index];
|
|
||||||
AdjustAffinities();
|
|
||||||
AdjustPriorities();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void SwitchMainGame() {
|
for (int i = 0; i < 9; i++) {
|
||||||
var foregroundWindow = GetForegroundWindow();
|
var window = windows[i];
|
||||||
Process foregroundGame = null;
|
if (window == null) {
|
||||||
var foregroundGameIndex = -1;
|
Console.WriteLine($"Adding foreground window to tracked at index {i}...");
|
||||||
var exists = false;
|
windows[i] = foregroundProcess;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
foreach (var process in processes)
|
private static void Swap(int index) {
|
||||||
if (foregroundWindow == process.MainWindowHandle) {
|
Console.WriteLine($"Swapping window at index {index}");
|
||||||
exists = true;
|
var process = GetForegroundProcess();
|
||||||
foregroundGame = process;
|
if (process == null) return;
|
||||||
foregroundGameIndex = processes.IndexOf(process);
|
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;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (exists) {
|
if (!found) {
|
||||||
var tempGame = processes[0];
|
for (int i = 0; i < 9; i++) {
|
||||||
processes[0] = foregroundGame;
|
var window = windows[i];
|
||||||
processes[foregroundGameIndex] = tempGame;
|
if (window == null) {
|
||||||
|
Console.WriteLine($"Adding foreground window to tracked at index {i}...");
|
||||||
|
windows[i] = process;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ToggleGame() {
|
for (int i = 0; i < 9; i++) {
|
||||||
Console.WriteLine("Toggling foreground window as tracked...");
|
var window = windows[i];
|
||||||
var foregroundWindow = GetForegroundWindow();
|
if (window != null && window.Id == process.Id) {
|
||||||
var systemProcesses = Process.GetProcesses();
|
windows[i] = windows[index];
|
||||||
Process foregroundProcess = null;
|
windows[index] = window;
|
||||||
|
Console.WriteLine($"Swapped window at index {i} to {index}");
|
||||||
foreach (var process in systemProcesses)
|
return;
|
||||||
if (foregroundWindow == process.MainWindowHandle) {
|
}
|
||||||
foregroundProcess = process;
|
}
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (foregroundProcess == null) return;
|
private static void TabTo(int index) {
|
||||||
Console.WriteLine("Foreground process: " + foregroundProcess.ProcessName);
|
if (index >= NumProc) return;
|
||||||
var existingProcess = processes.Find(process => process.Id == foregroundProcess.Id);
|
Console.WriteLine($"Tab to window at index {index}");
|
||||||
if (existingProcess != null) {
|
|
||||||
Console.WriteLine("Removing foreground process from tracked...");
|
var window = windows[index];
|
||||||
processes.Remove(existingProcess);
|
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user