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,130 +1,165 @@
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 {
[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo);
[DllImport("user32.dll")] internal static class Program {
public static extern IntPtr GetForegroundWindow(); 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")]
public static extern bool SetForegroundWindow(IntPtr hWnd); private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo);
[DllImport("User32.dll", SetLastError = true)] [DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)] public static extern IntPtr GetForegroundWindow();
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); public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)] [DllImport("User32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [return: MarshalAs(UnmanagedType.Bool)]
private static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);
static Process[] games = Process.GetProcessesByName("Dundefgame"); [DllImport("user32.dll")]
static Process activeGame = games[0]; private static extern bool GetWindowRect(IntPtr handle, ref Rectangle rect);
static int defaultAffinity = 0b100000000000;
private static void AdjustAffinities() { [DllImport("user32.dll", SetLastError = true)]
int fullAffinity = 0b111111111111; private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
int i = 0;
foreach (Process game in games) {
if (game != activeGame) {
var processAffinty = defaultAffinity >> i;
fullAffinity = fullAffinity & ~processAffinty;
game.ProcessorAffinity = new IntPtr(processAffinty);
i++;
}
}
activeGame.ProcessorAffinity = new IntPtr(fullAffinity);
}
private static void AdjustPriorities() { public static Bitmap CaptureWindow(IntPtr handle) {
foreach (Process game in games) { if (screenshot == null)
game.PriorityClass = ProcessPriorityClass.Idle; screenshot = new Bitmap(rect.Width, rect.Height);
} graphics = Graphics.FromImage(screenshot);
activeGame.PriorityClass = ProcessPriorityClass.High; var hdc = graphics.GetHdc();
} PrintWindow(handle, hdc, 0);
graphics.ReleaseHdc(hdc);
return screenshot;
}
private static void NerfAll() { private static void AdjustAffinities() {
int i = 0; var fullAffinity = 0b111111111111;
foreach (Process game in games) { var i = 0;
game.ProcessorAffinity = new IntPtr(defaultAffinity >> i); foreach (var game in games)
game.PriorityClass = ProcessPriorityClass.Idle; if (game != activeGame) {
var processAffinty = defaultAffinity >> i;
fullAffinity = fullAffinity & ~processAffinty;
game.ProcessorAffinity = new IntPtr(processAffinty);
i++; i++;
} }
}
private static void SwitchToGame(int index) { activeGame.ProcessorAffinity = new IntPtr(fullAffinity);
SetForegroundWindow(games[index].MainWindowHandle); }
activeGame = games[index];
AdjustAffinities();
AdjustPriorities();
}
private static void SwitchMainGame() { private static void AdjustPriorities() {
IntPtr foregroundWindow = GetForegroundWindow(); foreach (var game in games) game.PriorityClass = ProcessPriorityClass.Idle;
Process foregroundGame = null; activeGame.PriorityClass = ProcessPriorityClass.High;
int foregroundGameIndex = -1; }
bool exists = false;
private static void NerfAll() {
foreach (Process game in games) { var i = 0;
if (foregroundWindow == game.MainWindowHandle) { foreach (var game in games) {
exists = true; game.ProcessorAffinity = new IntPtr(defaultAffinity >> i);
foregroundGame = game; game.PriorityClass = ProcessPriorityClass.Idle;
foregroundGameIndex = Array.IndexOf(games, game); i++;
}
}
private static void SwitchToGame(int index) {
SetForegroundWindow(games[index].MainWindowHandle);
activeGame = games[index];
AdjustAffinities();
AdjustPriorities();
}
private static void SwitchMainGame() {
var foregroundWindow = GetForegroundWindow();
Process foregroundGame = null;
var foregroundGameIndex = -1;
var exists = false;
foreach (var game in games)
if (foregroundWindow == game.MainWindowHandle) {
exists = true;
foregroundGame = game;
foregroundGameIndex = Array.IndexOf(games, game);
break;
}
if (exists) {
var tempGame = games[0];
games[0] = foregroundGame;
games[foregroundGameIndex] = tempGame;
}
}
[STAThread]
private static void Main() {
HotKeyManager.RegisterHotKey(Keys.D1, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.D2, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.D3, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.D4, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.D5, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.Q, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.W, KeyModifiers.Alt);
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) {
switch (e.Key) {
case Keys.D1:
SwitchToGame(0);
break; break;
} case Keys.D2:
} SwitchToGame(1);
break;
case Keys.D3:
SwitchToGame(2);
break;
case Keys.D4:
SwitchToGame(3);
break;
case Keys.D5:
SwitchMainGame();
break;
case Keys.Q:
NerfAll();
break;
case Keys.W:
if (paused) {
Console.Beep(1500, 500);
paused = false;
} else {
Console.Beep(500, 500);
paused = true;
}
if (exists) { break;
Process tempGame = games[0];
games[0] = foregroundGame;
games[foregroundGameIndex] = tempGame;
} }
} }
[STAThread] while (true) {
private static void Main() { while (!paused) {
HotKeyManager.RegisterHotKey(Keys.D1, KeyModifiers.Alt); screenshot = CaptureWindow(games[0].MainWindowHandle);
HotKeyManager.RegisterHotKey(Keys.D2, KeyModifiers.Alt); foreach (Pixel p in pixelList) {
HotKeyManager.RegisterHotKey(Keys.D3, KeyModifiers.Alt); if (p.ProcessBitmap(screenshot)) {
HotKeyManager.RegisterHotKey(Keys.D4, KeyModifiers.Alt); Console.Beep(1500, 850);
HotKeyManager.RegisterHotKey(Keys.D5, KeyModifiers.Alt); }
HotKeyManager.RegisterHotKey(Keys.Q, KeyModifiers.Alt);
HotKeyManager.HotKeyPressed += HotKeyManager_HotKeyPressed;
static void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e) {
switch (e.Key) {
case Keys.D1:
SwitchToGame(0);
break;
case Keys.D2:
SwitchToGame(1);
break;
case Keys.D3:
SwitchToGame(2);
break;
case Keys.D4:
SwitchToGame(3);
break;
case Keys.D5:
SwitchMainGame();
break;
case Keys.Q:
NerfAll();
break;
} }
Thread.Sleep(1000);
} }
Thread.Sleep(1000);
while (true) {
Thread.Sleep(2000);
}
} }
} }
} }