diff --git a/DD2Switcher/DD2Switcher.csproj b/DD2Switcher/DD2Switcher.csproj index a4a34ac..3db0316 100644 --- a/DD2Switcher/DD2Switcher.csproj +++ b/DD2Switcher/DD2Switcher.csproj @@ -1,6 +1,6 @@  - + Debug AnyCPU @@ -8,14 +8,14 @@ WinExe DD2Switcher DD2Switcher - v4.0 + v4.8.1 512 true true - latest + 10 - AnyCPU + x64 true full false @@ -23,28 +23,30 @@ DEBUG;TRACE prompt 4 + false - AnyCPU + x64 pdbonly true bin\Release\ TRACE prompt 4 + false - - - - - - - - - - - + + + + + + + + + + + @@ -53,10 +55,10 @@ Form1.cs - - - - + + + + ResXFileCodeGenerator Resources.Designer.cs @@ -77,7 +79,10 @@ - + - + + + + \ No newline at end of file diff --git a/DD2Switcher/Form1.cs b/DD2Switcher/Form1.cs index f53ed01..4cd1087 100644 --- a/DD2Switcher/Form1.cs +++ b/DD2Switcher/Form1.cs @@ -1,12 +1,9 @@ using System.Windows.Forms; -namespace DD2Switcher -{ - public partial class Form1 : Form - { - public Form1() - { - InitializeComponent(); - } +namespace DD2Switcher; + +public partial class Form1 : Form { + public Form1() { + InitializeComponent(); } } \ No newline at end of file diff --git a/DD2Switcher/HotKeyManager.cs b/DD2Switcher/HotKeyManager.cs index b56d733..ddf71d1 100644 --- a/DD2Switcher/HotKeyManager.cs +++ b/DD2Switcher/HotKeyManager.cs @@ -3,104 +3,103 @@ using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; -namespace DD2Switcher { - public static class HotKeyManager { - private static volatile MessageWindow _wnd; - private static volatile IntPtr _hwnd; - private static readonly ManualResetEvent _windowReadyEvent = new ManualResetEvent(false); +namespace DD2Switcher; - private static int _id; +public static class HotKeyManager { + private static volatile MessageWindow _wnd; + private static volatile IntPtr _hwnd; + private static readonly ManualResetEvent _windowReadyEvent = new(false); - static HotKeyManager() { - var messageLoop = new Thread(delegate() { Application.Run(new MessageWindow()); }); - messageLoop.Name = "MessageLoopThread"; - messageLoop.IsBackground = true; - messageLoop.Start(); - } + private static int _id; - public static event EventHandler HotKeyPressed; - - public static int RegisterHotKey(Keys key, KeyModifiers modifiers) { - _windowReadyEvent.WaitOne(); - var id = Interlocked.Increment(ref _id); - _wnd.Invoke(new RegisterHotKeyDelegate(RegisterHotKeyInternal), _hwnd, id, (uint)modifiers, (uint)key); - return id; - } - - public static void UnregisterHotKey(int id) { - _wnd.Invoke(new UnRegisterHotKeyDelegate(UnRegisterHotKeyInternal), _hwnd, id); - } - - private static void RegisterHotKeyInternal(IntPtr hwnd, int id, uint modifiers, uint key) { - RegisterHotKey(hwnd, id, modifiers, key); - } - - private static void UnRegisterHotKeyInternal(IntPtr hwnd, int id) { - UnregisterHotKey(_hwnd, id); - } - - private static void OnHotKeyPressed(HotKeyEventArgs e) { - if (HotKeyPressed != null) HotKeyPressed(null, e); - } - - [DllImport("user32", SetLastError = true)] - private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk); - - [DllImport("user32", SetLastError = true)] - private static extern bool UnregisterHotKey(IntPtr hWnd, int id); - - private delegate void RegisterHotKeyDelegate(IntPtr hwnd, int id, uint modifiers, uint key); - - private delegate void UnRegisterHotKeyDelegate(IntPtr hwnd, int id); - - private class MessageWindow : Form { - private const int WM_HOTKEY = 0x312; - - public MessageWindow() { - _wnd = this; - _hwnd = Handle; - _windowReadyEvent.Set(); - } - - protected override void WndProc(ref Message m) { - if (m.Msg == WM_HOTKEY) { - var e = new HotKeyEventArgs(m.LParam); - OnHotKeyPressed(e); - } - - base.WndProc(ref m); - } - - protected override void SetVisibleCore(bool value) { - // Ensure the window never becomes visible - base.SetVisibleCore(false); - } - } + static HotKeyManager() { + var messageLoop = new Thread(delegate() { Application.Run(new MessageWindow()); }); + messageLoop.Name = "MessageLoopThread"; + messageLoop.IsBackground = true; + messageLoop.Start(); } + public static event EventHandler HotKeyPressed; - public class HotKeyEventArgs : EventArgs { - public readonly Keys Key; - public readonly KeyModifiers Modifiers; - - public HotKeyEventArgs(Keys key, KeyModifiers modifiers) { - Key = key; - Modifiers = modifiers; - } - - public HotKeyEventArgs(IntPtr hotKeyParam) { - var param = (uint)hotKeyParam.ToInt64(); - Key = (Keys)((param & 0xffff0000) >> 16); - Modifiers = (KeyModifiers)(param & 0x0000ffff); - } + public static int RegisterHotKey(Keys key, KeyModifiers modifiers) { + _windowReadyEvent.WaitOne(); + var id = Interlocked.Increment(ref _id); + _wnd.Invoke(new RegisterHotKeyDelegate(RegisterHotKeyInternal), _hwnd, id, (uint)modifiers, (uint)key); + return id; } - [Flags] - public enum KeyModifiers { - Alt = 1, - Control = 2, - Shift = 4, - Windows = 8, - NoRepeat = 0x4000 + public static void UnregisterHotKey(int id) { + _wnd.Invoke(new UnRegisterHotKeyDelegate(UnRegisterHotKeyInternal), _hwnd, id); } + + private static void RegisterHotKeyInternal(IntPtr hwnd, int id, uint modifiers, uint key) { + RegisterHotKey(hwnd, id, modifiers, key); + } + + private static void UnRegisterHotKeyInternal(IntPtr hwnd, int id) { + UnregisterHotKey(_hwnd, id); + } + + private static void OnHotKeyPressed(HotKeyEventArgs e) { + if (HotKeyPressed != null) HotKeyPressed(null, e); + } + + [DllImport("user32", SetLastError = true)] + private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk); + + [DllImport("user32", SetLastError = true)] + private static extern bool UnregisterHotKey(IntPtr hWnd, int id); + + private delegate void RegisterHotKeyDelegate(IntPtr hwnd, int id, uint modifiers, uint key); + + private delegate void UnRegisterHotKeyDelegate(IntPtr hwnd, int id); + + private class MessageWindow : Form { + private const int WM_HOTKEY = 0x312; + + public MessageWindow() { + _wnd = this; + _hwnd = Handle; + _windowReadyEvent.Set(); + } + + protected override void WndProc(ref Message m) { + if (m.Msg == WM_HOTKEY) { + var e = new HotKeyEventArgs(m.LParam); + OnHotKeyPressed(e); + } + + base.WndProc(ref m); + } + + protected override void SetVisibleCore(bool value) { + // Ensure the window never becomes visible + base.SetVisibleCore(false); + } + } +} + +public class HotKeyEventArgs : EventArgs { + public readonly Keys Key; + public readonly KeyModifiers Modifiers; + + public HotKeyEventArgs(Keys key, KeyModifiers modifiers) { + Key = key; + Modifiers = modifiers; + } + + public HotKeyEventArgs(IntPtr hotKeyParam) { + var param = (uint)hotKeyParam.ToInt64(); + Key = (Keys)((param & 0xffff0000) >> 16); + Modifiers = (KeyModifiers)(param & 0x0000ffff); + } +} + +[Flags] +public enum KeyModifiers { + Alt = 1, + Control = 2, + Shift = 4, + Windows = 8, + NoRepeat = 0x4000 } \ No newline at end of file diff --git a/DD2Switcher/Pixel.cs b/DD2Switcher/Pixel.cs index 7ad41ac..1cf917f 100644 --- a/DD2Switcher/Pixel.cs +++ b/DD2Switcher/Pixel.cs @@ -1,15 +1,9 @@ -using System; +using System.Diagnostics; using System.Drawing; -namespace DD2Switcher; +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; @@ -18,8 +12,14 @@ public class Pixel { this.B = B; } - public Boolean ProcessBitmap(Bitmap bmp) { - Color tempPixel = bmp.GetPixel(x, y); - return tempPixel.R == R && tempPixel.B == B && tempPixel.G == G; + private int x { get; } + private int y { get; } + private int R { get; } + private int G { get; } + private int B { get; } + + public bool ProcessBitmap(Bitmap bmp) { + var tempPixel = bmp.GetPixel(x, y); + return tempPixel.R >= R && tempPixel.B >= B && tempPixel.G >= G; } } \ No newline at end of file diff --git a/DD2Switcher/Program.cs b/DD2Switcher/Program.cs index 88fc8c9..aa04bc2 100644 --- a/DD2Switcher/Program.cs +++ b/DD2Switcher/Program.cs @@ -2,20 +2,29 @@ using System.Collections.Generic; using System.Diagnostics; using System.Drawing; +using System.Media; using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; -namespace DD2Switcher; +namespace DD2Switcher; internal static class Program { - private static Rectangle rect = new(0, 0, 1920, 1080); + private static Rectangle rect = new(0, 0, 2560, 1440); private static readonly Process[] games = Process.GetProcessesByName("Dundefgame"); + + private static readonly SoundPlayer beeper = + 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 int defaultAffinity = 0b100000000000; + private static readonly IntPtr defaultAffinity = new(0xFF000000); + private static readonly IntPtr fullAffinity = new(0xFFFFFFFF); private static bool paused = true; + + private static List relevantPoints = new List(); + private static List pointsToRemove = new List(); [DllImport("user32.dll")] private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo); @@ -47,18 +56,10 @@ internal static class Program { } private static void AdjustAffinities() { - var fullAffinity = 0b111111111111; - var i = 0; foreach (var game in games) - if (game != activeGame) { - // var processAffinty = defaultAffinity >> i; - var processAffinty = defaultAffinity; - fullAffinity = fullAffinity & ~processAffinty; - game.ProcessorAffinity = new IntPtr(processAffinty); - i++; - } - - activeGame.ProcessorAffinity = new IntPtr(fullAffinity); + if (game != activeGame) + game.ProcessorAffinity = defaultAffinity; + activeGame.ProcessorAffinity = fullAffinity; } private static void AdjustPriorities() { @@ -67,11 +68,16 @@ internal static class Program { } private static void NerfAll() { - var i = 0; foreach (var game in games) { - game.ProcessorAffinity = new IntPtr(defaultAffinity); + game.ProcessorAffinity = defaultAffinity; game.PriorityClass = ProcessPriorityClass.Idle; - i++; + } + } + + private static void BuffAll() { + foreach (var game in games) { + game.ProcessorAffinity = fullAffinity; + game.PriorityClass = ProcessPriorityClass.Normal; } } @@ -106,6 +112,13 @@ internal static class Program { [STAThread] private static void Main() { + var processes = Process.GetProcesses(); + var currentProcess = Process.GetCurrentProcess(); + + foreach (var process in processes) + if (process.Id != currentProcess.Id && process.ProcessName == currentProcess.ProcessName) + process.Kill(); + HotKeyManager.RegisterHotKey(Keys.D1, KeyModifiers.Alt); HotKeyManager.RegisterHotKey(Keys.D2, KeyModifiers.Alt); HotKeyManager.RegisterHotKey(Keys.D3, KeyModifiers.Alt); @@ -114,11 +127,12 @@ internal static class Program { HotKeyManager.RegisterHotKey(Keys.D6, KeyModifiers.Alt); HotKeyManager.RegisterHotKey(Keys.Q, KeyModifiers.Alt); HotKeyManager.RegisterHotKey(Keys.W, KeyModifiers.Alt); + HotKeyManager.RegisterHotKey(Keys.R, KeyModifiers.Alt); HotKeyManager.HotKeyPressed += HotKeyManager_HotKeyPressed; - - List pixelList = new List(); - pixelList.Add(new Pixel(108, 108, 0, 0, 0)); - pixelList.Add(new Pixel(1062, 885, 255, 255, 255)); + + var pixelList = new System.Collections.Generic.List(); + // 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) { switch (e.Key) { @@ -140,15 +154,15 @@ internal static class Program { case Keys.D6: Environment.Exit(0); break; - case Keys.Q: - NerfAll(); - break; case Keys.W: if (paused) { - Console.Beep(1500, 500); + beeper.Play(); paused = false; - } else { - Console.Beep(500, 500); + } + else { + beeper.Play(); + Thread.Sleep(150); + beeper.Play(); paused = true; } @@ -157,16 +171,51 @@ internal static class Program { } while (true) { + bool runOnce = false; + bool AAA = false; + relevantPoints.Clear(); while (!paused) { screenshot = CaptureWindow(games[0].MainWindowHandle); - foreach (Pixel p in pixelList) { + // screenshot.Save("SS.png"); + + // if (!runOnce) { + // runOnce = true; + // for (var y = 0; y < screenshot.Height; y++) + // for (var x = 0; x < screenshot.Width; x++) { + // var pixelColor = screenshot.GetPixel(x, y); + // 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)) { - Console.Beep(1500, 850); + 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); } } diff --git a/DD2Switcher/beep.wav b/DD2Switcher/beep.wav new file mode 100644 index 0000000..b8c1994 Binary files /dev/null and b/DD2Switcher/beep.wav differ diff --git a/DD2Switcher/obj/Release/.NETFramework,Version=v4.8.AssemblyAttributes.cs b/DD2Switcher/obj/Release/.NETFramework,Version=v4.8.AssemblyAttributes.cs new file mode 100644 index 0000000..15efebf --- /dev/null +++ b/DD2Switcher/obj/Release/.NETFramework,Version=v4.8.AssemblyAttributes.cs @@ -0,0 +1,4 @@ +// +using System; +using System.Reflection; +[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]