Update... idk what happened...

This commit is contained in:
2024-11-02 23:11:46 +01:00
parent bf81aad59d
commit bc79315ca2
10 changed files with 503 additions and 509 deletions

2
.gitignore vendored
View File

@@ -1,3 +1,5 @@
.idea .idea
**/Debug **/Debug
**/Properties **/Properties
obj
bin

View File

@@ -1,2 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/PencilsConfiguration/ActualSeverity/@EntryValue">INFO</s:String></wpf:ResourceDictionary> <s:String x:Key="/Default/CodeInspection/PencilsConfiguration/ActualSeverity/@EntryValue">INFO</s:String>
<s:String x:Key="/Default/Environment/Hierarchy/Build/BuildTool/CustomBuildToolPath/@EntryValue">C:\Users\Administrator\AppData\Local\JetBrains\Toolbox\apps\Rider\ch-0\231.8109.212\tools\MSBuild\Current\Bin\amd64\MSBuild.exe</s:String>
</wpf:ResourceDictionary>

View File

@@ -1,33 +1,17 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Drawing;
using System.Media;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms; using System.Windows.Forms;
namespace DD2Switcher; namespace DD2Switcher;
internal static class Program { internal static class Program {
private static Rectangle rect = new(0, 0, 2560, 1440); private static List<Process> processes = new();
private static readonly Process[] games = Process.GetProcessesByName("Dundefgame");
private static readonly SoundPlayer beeper = private static Process activeProcess;
new(@"C:\Users\Administrator\RiderProjects\DD2Switcher\DD2Switcher\beep.wav");
private static Process activeGame = games[0];
private static Bitmap screenshot;
private static Graphics graphics;
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);
private static bool paused = true;
private static List<Point> relevantPoints = new List<Point>();
private static List<Point> pointsToRemove = new List<Point>();
[DllImport("user32.dll")]
private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo);
[DllImport("user32.dll")] [DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow(); public static extern IntPtr GetForegroundWindow();
@@ -35,56 +19,67 @@ internal static class Program {
[DllImport("user32.dll")] [DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd); public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("User32.dll", SetLastError = true)] [DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)] [return: MarshalAs(UnmanagedType.Bool)]
private static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags); static extern bool AllocConsole();
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr handle, ref Rectangle rect);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
public static Bitmap CaptureWindow(IntPtr handle) {
if (screenshot == null)
screenshot = new Bitmap(rect.Width, rect.Height);
graphics = Graphics.FromImage(screenshot);
var hdc = graphics.GetHdc();
PrintWindow(handle, hdc, 0);
graphics.ReleaseHdc(hdc);
return screenshot;
}
private static void AdjustAffinities() { private static void AdjustAffinities() {
foreach (var game in games) List<Process> fuckedProcesses = new();
if (game != activeGame)
game.ProcessorAffinity = defaultAffinity; foreach (var process in processes)
activeGame.ProcessorAffinity = fullAffinity; if (process != activeProcess) {
try {
process.ProcessorAffinity = defaultAffinity;
}
catch (Exception e) {
fuckedProcesses.Add(process);
}
}
try {
activeProcess.ProcessorAffinity = fullAffinity;
}
catch (Exception e) {
fuckedProcesses.Add(activeProcess);
}
foreach (var fucked in fuckedProcesses)
processes.Remove(fucked);
} }
private static void AdjustPriorities() { private static void AdjustPriorities() {
foreach (var game in games) game.PriorityClass = ProcessPriorityClass.Idle; List<Process> fuckedProcesses = new();
activeGame.PriorityClass = ProcessPriorityClass.High;
}
private static void NerfAll() { foreach (var process in processes) {
foreach (var game in games) { try {
game.ProcessorAffinity = defaultAffinity; process.PriorityClass = ProcessPriorityClass.Idle;
game.PriorityClass = ProcessPriorityClass.Idle; }
catch (Exception e) {
fuckedProcesses.Add(process);
}
} }
}
private static void BuffAll() { try {
foreach (var game in games) { activeProcess.PriorityClass = ProcessPriorityClass.High;
game.ProcessorAffinity = fullAffinity;
game.PriorityClass = ProcessPriorityClass.Normal;
} }
catch (Exception e) {
fuckedProcesses.Add(activeProcess);
}
foreach (var fucked in fuckedProcesses)
processes.Remove(fucked);
} }
private static void SwitchToGame(int index) { private static void SwitchToProcess(int index) {
if (index >= games.Length) return; Console.WriteLine("Switching to process at index " + index);
SetForegroundWindow(games[index].MainWindowHandle); if (index >= processes.Count) return;
activeGame = games[index]; 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(); AdjustAffinities();
AdjustPriorities(); AdjustPriorities();
} }
@@ -95,29 +90,59 @@ internal static class Program {
var foregroundGameIndex = -1; var foregroundGameIndex = -1;
var exists = false; var exists = false;
foreach (var game in games) foreach (var process in processes)
if (foregroundWindow == game.MainWindowHandle) { if (foregroundWindow == process.MainWindowHandle) {
exists = true; exists = true;
foregroundGame = game; foregroundGame = process;
foregroundGameIndex = Array.IndexOf(games, game); foregroundGameIndex = processes.IndexOf(process);
break; break;
} }
if (exists) { if (exists) {
var tempGame = games[0]; var tempGame = processes[0];
games[0] = foregroundGame; processes[0] = foregroundGame;
games[foregroundGameIndex] = tempGame; processes[foregroundGameIndex] = tempGame;
}
}
private static void ToggleGame() {
Console.WriteLine("Toggling foreground window as tracked...");
var foregroundWindow = GetForegroundWindow();
var systemProcesses = Process.GetProcesses();
Process foregroundProcess = null;
foreach (var process in systemProcesses)
if (foregroundWindow == process.MainWindowHandle) {
foregroundProcess = process;
break;
}
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);
}
else {
Console.WriteLine("Adding foreground process to tracked...");
processes.Add(foregroundProcess);
} }
} }
[STAThread] [STAThread]
private static void Main() { private static void Main() {
// 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();
}
HotKeyManager.RegisterHotKey(Keys.D1, KeyModifiers.Alt); HotKeyManager.RegisterHotKey(Keys.D1, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.D2, KeyModifiers.Alt); HotKeyManager.RegisterHotKey(Keys.D2, KeyModifiers.Alt);
@@ -125,9 +150,15 @@ internal static class Program {
HotKeyManager.RegisterHotKey(Keys.D4, KeyModifiers.Alt); HotKeyManager.RegisterHotKey(Keys.D4, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.D5, KeyModifiers.Alt); HotKeyManager.RegisterHotKey(Keys.D5, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.D6, KeyModifiers.Alt); HotKeyManager.RegisterHotKey(Keys.D6, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.Q, KeyModifiers.Alt); HotKeyManager.RegisterHotKey(Keys.D7, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.W, KeyModifiers.Alt); HotKeyManager.RegisterHotKey(Keys.D8, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.R, KeyModifiers.Alt); HotKeyManager.RegisterHotKey(Keys.D9, 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>(); var pixelList = new System.Collections.Generic.List<Pixel>();
@@ -137,86 +168,44 @@ internal static class Program {
static void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e) { static void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e) {
switch (e.Key) { switch (e.Key) {
case Keys.D1: case Keys.D1:
SwitchToGame(0); SwitchToProcess(0);
break; break;
case Keys.D2: case Keys.D2:
SwitchToGame(1); SwitchToProcess(1);
break; break;
case Keys.D3: case Keys.D3:
SwitchToGame(2); SwitchToProcess(2);
break; break;
case Keys.D4: case Keys.D4:
SwitchToGame(3); SwitchToProcess(3);
break; break;
case Keys.D5: case Keys.D5:
SwitchMainGame(); SwitchToProcess(4);
break; break;
case Keys.D6: case Keys.D6:
Environment.Exit(0); SwitchToProcess(5);
break; break;
case Keys.W: case Keys.D7:
if (paused) { SwitchToProcess(6);
beeper.Play(); break;
paused = false; case Keys.D8:
} SwitchToProcess(7);
else { break;
beeper.Play(); case Keys.D9:
Thread.Sleep(150); SwitchToProcess(8);
beeper.Play(); break;
paused = true; case Keys.Oemtilde:
} ToggleGame();
break; break;
} }
} }
while (true) {
bool runOnce = false;
bool AAA = false;
relevantPoints.Clear();
while (!paused) {
screenshot = CaptureWindow(games[0].MainWindowHandle);
// screenshot.Save("SS.png");
// if (!runOnce) { Console.CancelKeyPress += (sender, e) => {
// runOnce = true; Process.GetCurrentProcess().Kill();
// for (var y = 0; y < screenshot.Height; y++) };
// for (var x = 0; x < screenshot.Width; x++) { while (true)
// var pixelColor = screenshot.GetPixel(x, y); System.Threading.Thread.Sleep(100000);
// if (pixelColor.R > 220 && pixelColor.G > 220 && pixelColor.B > 220)
// relevantPoints.Add(new Point(x, y));
// }
// }
//
// pointsToRemove.Clear();
// foreach (var relevantPoint in relevantPoints) {
// var pixel = screenshot.GetPixel(relevantPoint.X, relevantPoint.Y);
// if (!(pixel.R > 220) || !(pixel.G > 220) || !(pixel.B > 220))
// pointsToRemove.Add(relevantPoint);
// }
//
// foreach (var point in pointsToRemove) {
// relevantPoints.Remove(point);
// }
//
// Debug.WriteLine(relevantPoints.Count);
foreach (var p in pixelList)
if (p.ProcessBitmap(screenshot)) {
beeper.Play();
break;
}
Thread.Sleep(250);
}
// System.IO.TextWriter tw = new System.IO.StreamWriter("SavedList.txt");
// foreach (var point in relevantPoints) {
// tw.WriteLine(point.ToString());
// }
// tw.Close();
Thread.Sleep(250);
}
} }
} }