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

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>

View File

@@ -1,88 +1,88 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')"/>
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{2AC26899-8E27-4B96-85A9-C387186EAD27}</ProjectGuid>
<OutputType>WinExe</OutputType>
<RootNamespace>DD2Switcher</RootNamespace>
<AssemblyName>DD2Switcher</AssemblyName>
<TargetFrameworkVersion>v4.8.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
<LangVersion>10</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>x64</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32bit>false</Prefer32bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>x64</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32bit>false</Prefer32bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System"/>
<Reference Include="System.Core"/>
<Reference Include="System.Xml.Linq"/>
<Reference Include="System.Data.DataSetExtensions"/>
<Reference Include="Microsoft.CSharp"/>
<Reference Include="System.Data"/>
<Reference Include="System.Deployment"/>
<Reference Include="System.Drawing"/>
<Reference Include="System.Net.Http"/>
<Reference Include="System.Windows.Forms"/>
<Reference Include="System.Xml"/>
</ItemGroup>
<ItemGroup>
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="HotKeyManager.cs"/>
<Compile Include="Pixel.cs"/>
<Compile Include="Program.cs"/>
<Compile Include="Properties\AssemblyInfo.cs"/>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="App.config"/>
</ItemGroup>
<ItemGroup>
<Content Include="beep.wav"/>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')"/>
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{2AC26899-8E27-4B96-85A9-C387186EAD27}</ProjectGuid>
<OutputType>WinExe</OutputType>
<RootNamespace>DD2Switcher</RootNamespace>
<AssemblyName>DD2Switcher</AssemblyName>
<TargetFrameworkVersion>v4.8.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
<LangVersion>10</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>x64</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32bit>false</Prefer32bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>x64</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32bit>false</Prefer32bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System"/>
<Reference Include="System.Core"/>
<Reference Include="System.Xml.Linq"/>
<Reference Include="System.Data.DataSetExtensions"/>
<Reference Include="Microsoft.CSharp"/>
<Reference Include="System.Data"/>
<Reference Include="System.Deployment"/>
<Reference Include="System.Drawing"/>
<Reference Include="System.Net.Http"/>
<Reference Include="System.Windows.Forms"/>
<Reference Include="System.Xml"/>
</ItemGroup>
<ItemGroup>
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="HotKeyManager.cs"/>
<Compile Include="Pixel.cs"/>
<Compile Include="Program.cs"/>
<Compile Include="Properties\AssemblyInfo.cs"/>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="App.config"/>
</ItemGroup>
<ItemGroup>
<Content Include="beep.wav"/>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/>
</Project>

View File

@@ -1,40 +1,40 @@
namespace DD2Switcher
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Text = "Form1";
}
#endregion
}
namespace DD2Switcher
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Text = "Form1";
}
#endregion
}
}

View File

@@ -1,9 +1,9 @@
using System.Windows.Forms;
namespace DD2Switcher;
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
using System.Windows.Forms;
namespace DD2Switcher;
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
}

View File

@@ -1,105 +1,105 @@
using System;
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(false);
private static int _id;
static HotKeyManager() {
var messageLoop = new Thread(delegate() { Application.Run(new MessageWindow()); });
messageLoop.Name = "MessageLoopThread";
messageLoop.IsBackground = true;
messageLoop.Start();
}
public static event EventHandler<HotKeyEventArgs> 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);
}
}
}
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
using System;
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(false);
private static int _id;
static HotKeyManager() {
var messageLoop = new Thread(delegate() { Application.Run(new MessageWindow()); });
messageLoop.Name = "MessageLoopThread";
messageLoop.IsBackground = true;
messageLoop.Start();
}
public static event EventHandler<HotKeyEventArgs> 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);
}
}
}
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
}

View File

@@ -1,25 +1,25 @@
using System.Diagnostics;
using System.Drawing;
namespace DD2Switcher;
public class Pixel {
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;
}
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;
}
using System.Diagnostics;
using System.Drawing;
namespace DD2Switcher;
public class Pixel {
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;
}
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;
}
}

View File

@@ -1,222 +1,211 @@
using System;
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;
internal static class Program {
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 IntPtr defaultAffinity = new(0xFF000000);
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")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("User32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);
[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() {
foreach (var game in games)
if (game != activeGame)
game.ProcessorAffinity = defaultAffinity;
activeGame.ProcessorAffinity = fullAffinity;
}
private static void AdjustPriorities() {
foreach (var game in games) game.PriorityClass = ProcessPriorityClass.Idle;
activeGame.PriorityClass = ProcessPriorityClass.High;
}
private static void NerfAll() {
foreach (var game in games) {
game.ProcessorAffinity = defaultAffinity;
game.PriorityClass = ProcessPriorityClass.Idle;
}
}
private static void BuffAll() {
foreach (var game in games) {
game.ProcessorAffinity = fullAffinity;
game.PriorityClass = ProcessPriorityClass.Normal;
}
}
private static void SwitchToGame(int index) {
if (index >= games.Length) return;
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() {
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);
HotKeyManager.RegisterHotKey(Keys.D4, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.D5, KeyModifiers.Alt);
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;
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) {
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.D6:
Environment.Exit(0);
break;
case Keys.W:
if (paused) {
beeper.Play();
paused = false;
}
else {
beeper.Play();
Thread.Sleep(150);
beeper.Play();
paused = true;
}
break;
}
}
while (true) {
bool runOnce = false;
bool AAA = false;
relevantPoints.Clear();
while (!paused) {
screenshot = CaptureWindow(games[0].MainWindowHandle);
// 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)) {
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);
}
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace DD2Switcher;
internal static class Program {
private static List<Process> processes = new();
private static Process activeProcess;
private static readonly IntPtr defaultAffinity = new(0xFF000000);
private static readonly IntPtr fullAffinity = new(0xFFFFFFFF);
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();
private static void AdjustAffinities() {
List<Process> fuckedProcesses = new();
foreach (var process in processes)
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() {
List<Process> fuckedProcesses = new();
foreach (var process in processes) {
try {
process.PriorityClass = ProcessPriorityClass.Idle;
}
catch (Exception e) {
fuckedProcesses.Add(process);
}
}
try {
activeProcess.PriorityClass = ProcessPriorityClass.High;
}
catch (Exception e) {
fuckedProcesses.Add(activeProcess);
}
foreach (var fucked in fuckedProcesses)
processes.Remove(fucked);
}
private static void SwitchToProcess(int index) {
Console.WriteLine("Switching to process at index " + index);
if (index >= processes.Count) return;
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();
AdjustPriorities();
}
private static void SwitchMainGame() {
var foregroundWindow = GetForegroundWindow();
Process foregroundGame = null;
var foregroundGameIndex = -1;
var exists = false;
foreach (var process in processes)
if (foregroundWindow == process.MainWindowHandle) {
exists = true;
foregroundGame = process;
foregroundGameIndex = processes.IndexOf(process);
break;
}
if (exists) {
var tempGame = processes[0];
processes[0] = foregroundGame;
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]
private static void Main() {
// AllocConsole();
var processes = Process.GetProcesses();
var currentProcess = Process.GetCurrentProcess();
foreach (var process in processes)
if (process.Id != currentProcess.Id && process.ProcessName == currentProcess.ProcessName) {
process.Kill();
Process.GetCurrentProcess().Kill();
}
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.D6, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.D7, KeyModifiers.Alt);
HotKeyManager.RegisterHotKey(Keys.D8, 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;
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) {
switch (e.Key) {
case Keys.D1:
SwitchToProcess(0);
break;
case Keys.D2:
SwitchToProcess(1);
break;
case Keys.D3:
SwitchToProcess(2);
break;
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;
}
}
Console.CancelKeyPress += (sender, e) => {
Process.GetCurrentProcess().Kill();
};
while (true)
System.Threading.Thread.Sleep(100000);
}
}