Implement beeping on end of round

This commit is contained in:
PhatDave
2022-05-08 14:55:04 +02:00
parent 60ad8e4e08
commit cdba24d69b
3 changed files with 163 additions and 102 deletions

View File

@@ -54,6 +54,7 @@
<DependentUpon>Form1.cs</DependentUpon> <DependentUpon>Form1.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="HotKeyManager.cs" /> <Compile Include="HotKeyManager.cs" />
<Compile Include="Pixel.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Properties\Resources.resx"> <EmbeddedResource Include="Properties\Resources.resx">

25
DD2Switcher/Pixel.cs Normal file
View File

@@ -0,0 +1,25 @@
using System;
using System.Drawing;
namespace DD2Switcher;
public class Pixel {
private int x { get; set; }
private int y { get; set; }
private int R { get; set; }
private int G { get; set; }
private int B { get; set; }
public Pixel(int x, int y, int R, int G, int B) {
this.x = x;
this.y = y;
this.R = R;
this.G = G;
this.B = B;
}
public Boolean ProcessBitmap(Bitmap bmp) {
Color tempPixel = bmp.GetPixel(x, y);
return tempPixel.R >= R && tempPixel.B >= B && tempPixel.G >= G;
}
}

View File

@@ -1,14 +1,24 @@
using System; using System;
using System.Windows.Forms; using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Diagnostics; using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
using System.Windows.Forms;
namespace DD2Switcher;
namespace DD2Switcher {
internal static class Program { internal static class Program {
private static Rectangle rect = new(0, 0, 1920, 1080);
private static readonly Process[] games = Process.GetProcessesByName("Dundefgame");
private static Process activeGame = games[0];
private static Bitmap screenshot;
private static Graphics graphics;
private static readonly int defaultAffinity = 0b100000000000;
private static bool paused = true;
[DllImport("user32.dll")] [DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo); 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();
@@ -18,42 +28,46 @@ namespace DD2Switcher {
[DllImport("User32.dll", SetLastError = true)] [DllImport("User32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)] [return: MarshalAs(UnmanagedType.Bool)]
static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags); private static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);
[DllImport("user32.dll")] [DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr handle, ref Rectangle rect); private static extern bool GetWindowRect(IntPtr handle, ref Rectangle rect);
[DllImport("user32.dll", SetLastError = true)] [DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName); private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
static Process[] games = Process.GetProcessesByName("Dundefgame"); public static Bitmap CaptureWindow(IntPtr handle) {
static Process activeGame = games[0]; if (screenshot == null)
static int defaultAffinity = 0b100000000000; 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() {
int fullAffinity = 0b111111111111; var fullAffinity = 0b111111111111;
int i = 0; var i = 0;
foreach (Process game in games) { foreach (var game in games)
if (game != activeGame) { if (game != activeGame) {
var processAffinty = defaultAffinity >> i; var processAffinty = defaultAffinity >> i;
fullAffinity = fullAffinity & ~processAffinty; fullAffinity = fullAffinity & ~processAffinty;
game.ProcessorAffinity = new IntPtr(processAffinty); game.ProcessorAffinity = new IntPtr(processAffinty);
i++; i++;
} }
}
activeGame.ProcessorAffinity = new IntPtr(fullAffinity); activeGame.ProcessorAffinity = new IntPtr(fullAffinity);
} }
private static void AdjustPriorities() { private static void AdjustPriorities() {
foreach (Process game in games) { foreach (var game in games) game.PriorityClass = ProcessPriorityClass.Idle;
game.PriorityClass = ProcessPriorityClass.Idle;
}
activeGame.PriorityClass = ProcessPriorityClass.High; activeGame.PriorityClass = ProcessPriorityClass.High;
} }
private static void NerfAll() { private static void NerfAll() {
int i = 0; var i = 0;
foreach (Process game in games) { foreach (var game in games) {
game.ProcessorAffinity = new IntPtr(defaultAffinity >> i); game.ProcessorAffinity = new IntPtr(defaultAffinity >> i);
game.PriorityClass = ProcessPriorityClass.Idle; game.PriorityClass = ProcessPriorityClass.Idle;
i++; i++;
@@ -68,22 +82,21 @@ namespace DD2Switcher {
} }
private static void SwitchMainGame() { private static void SwitchMainGame() {
IntPtr foregroundWindow = GetForegroundWindow(); var foregroundWindow = GetForegroundWindow();
Process foregroundGame = null; Process foregroundGame = null;
int foregroundGameIndex = -1; var foregroundGameIndex = -1;
bool exists = false; var exists = false;
foreach (Process game in games) { foreach (var game in games)
if (foregroundWindow == game.MainWindowHandle) { if (foregroundWindow == game.MainWindowHandle) {
exists = true; exists = true;
foregroundGame = game; foregroundGame = game;
foregroundGameIndex = Array.IndexOf(games, game); foregroundGameIndex = Array.IndexOf(games, game);
break; break;
} }
}
if (exists) { if (exists) {
Process tempGame = games[0]; var tempGame = games[0];
games[0] = foregroundGame; games[0] = foregroundGame;
games[foregroundGameIndex] = tempGame; games[foregroundGameIndex] = tempGame;
} }
@@ -97,8 +110,12 @@ namespace DD2Switcher {
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.Q, KeyModifiers.Alt); HotKeyManager.RegisterHotKey(Keys.Q, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.W, KeyModifiers.Alt);
HotKeyManager.HotKeyPressed += HotKeyManager_HotKeyPressed; HotKeyManager.HotKeyPressed += HotKeyManager_HotKeyPressed;
List<Pixel> pixelList = new List<Pixel>();
pixelList.Add(new Pixel(1062, 885, 240, 240, 240));
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:
@@ -118,13 +135,31 @@ namespace DD2Switcher {
break; break;
case Keys.Q: case Keys.Q:
NerfAll(); NerfAll();
break;
case Keys.W:
if (paused) {
Console.Beep(1500, 500);
paused = false;
} else {
Console.Beep(500, 500);
paused = true;
}
break; break;
} }
} }
while (true) { while (true) {
Thread.Sleep(2000); while (!paused) {
} screenshot = CaptureWindow(games[0].MainWindowHandle);
foreach (Pixel p in pixelList) {
if (p.ProcessBitmap(screenshot)) {
Console.Beep(1500, 850);
}
}
Thread.Sleep(1000);
}
Thread.Sleep(1000);
} }
} }
} }