diff --git a/Spectrum/Config.cs b/Spectrum/Config.cs index b584b90..0ae637e 100644 --- a/Spectrum/Config.cs +++ b/Spectrum/Config.cs @@ -368,9 +368,13 @@ namespace Spectrum #region Trigger Settings public bool TriggerBot { get; set; } = false; public Keybind TriggerKeybind = new(Keys.XButton1, KeybindType.Hold); + public bool TriggerRandomDelay { get; set; } = false; public int TriggerDelay { get; set; } = 50; // milliseconds - public int TriggerFov { get; set; } = 15; // pixels + public bool TriggerSpray = false; // if true ignore duration + public bool TriggerRandomDuration { get; set; } = false; public int TriggerDuration { get; set; } = 100; // milliseconds + public bool TriggerInBoundsOnly { get; set; } = false; + public int TriggerFov { get; set; } = 15; // pixels public bool DrawTriggerFov { get; set; } = false; public Vector4 TriggerRadiusColor { get; set; } = new Vector4(1.0f, 0.0f, 0.0f, 1.0f); #endregion @@ -403,6 +407,7 @@ namespace Spectrum #region Misc public bool DebugMode { get; set; } = false; + public bool AntiCapture { get; set; } = false; #endregion } public class ColorData diff --git a/Spectrum/Detection/AutoLabeling.cs b/Spectrum/Detection/AutoLabeling.cs index b624320..dada3c5 100644 --- a/Spectrum/Detection/AutoLabeling.cs +++ b/Spectrum/Detection/AutoLabeling.cs @@ -206,6 +206,9 @@ namespace Spectrum.Detection if (cancellationTokenSource != null) return; LogManager.Log("Starting auto labeling...", LogLevel.Info); + int existingImages = Directory.Exists("bin/dataset/images") ? + Directory.GetFiles("bin/dataset/images", "image_*.jpg").Length : 0; + imageCount = existingImages; Started = true; cancellationTokenSource = new CancellationTokenSource(); backgroundTask = Task.Run(() => LabelingLoop(cancellationTokenSource.Token)); diff --git a/Spectrum/Detection/DetectionManager.cs b/Spectrum/Detection/DetectionManager.cs index d7777dd..9749098 100644 --- a/Spectrum/Detection/DetectionManager.cs +++ b/Spectrum/Detection/DetectionManager.cs @@ -8,7 +8,7 @@ namespace Spectrum.Detection { private readonly Stopwatch stopwatch = new Stopwatch(); private Rectangle bounds = new Rectangle(0, 0, 0, 0); - private readonly (int Width, int Height) screenSize = SystemHelper.GetPrimaryScreenSize(); + private readonly (int Width, int Height) screenSize = Win32.GetPrimaryScreenSize(); private readonly int minArea; private readonly ConfigManager mainConfig = Program.mainConfig; private readonly Renderer? renderer; @@ -206,7 +206,7 @@ namespace Spectrum.Detection bestRect.Width, bestRect.Height)); - if (config.EnableAim) + if (config.EnableAim && InputManager.IsKeyOrMouseDown(config.Keybind)) { InputManager.MoveMouse(); } @@ -214,7 +214,7 @@ namespace Spectrum.Detection { var _ = InputManager.ClickMouse(); } - if (renderer != null && config.DrawTriggerFov && config.TriggerBot) + if (renderer != null && config.DrawTriggerFov && config.TriggerBot && !config.TriggerInBoundsOnly) { renderer.AddCircle(new System.Numerics.Vector2(targetX, targetY), config.TriggerFov, config.TriggerRadiusColor, 100); } diff --git a/Spectrum/Input/InputManager.cs b/Spectrum/Input/InputManager.cs index 93cddab..1989fad 100644 --- a/Spectrum/Input/InputManager.cs +++ b/Spectrum/Input/InputManager.cs @@ -15,6 +15,7 @@ namespace Spectrum.Input private static Rectangle lastDetectionBox = new Rectangle(); private static long _lastMoveTicks = 0; private static readonly double _dtRef = 1.0 / 120.0; + private static bool isSpraying = false; private static double GetDeltaSeconds() { @@ -48,15 +49,15 @@ namespace Spectrum.Input public static void MoveMouse() { var config = mainConfig.Data; - Point reference = new Point(); + Point reference = new(); if (config.ClosestToMouse) { GetCursorPos(out reference); } else { - reference.X = SystemHelper.GetPrimaryScreenSize().Width / 2; - reference.Y = SystemHelper.GetPrimaryScreenSize().Height / 2; + reference.X = Win32.GetPrimaryScreenSize().Width / 2; + reference.Y = Win32.GetPrimaryScreenSize().Height / 2; } Point start = new Point(reference.X, reference.Y); @@ -133,58 +134,93 @@ namespace Spectrum.Input } else { - currentPosition.X = SystemHelper.GetPrimaryScreenSize().Width / 2; - currentPosition.Y = SystemHelper.GetPrimaryScreenSize().Height / 2; + currentPosition.X = Win32.GetPrimaryScreenSize().Width / 2; + currentPosition.Y = Win32.GetPrimaryScreenSize().Height / 2; } - if (config.TriggerDelay > 0) + if (config.TriggerDelay > 0 && !config.TriggerRandomDelay) await Task.Delay(config.TriggerDelay); + else if (config.TriggerRandomDelay) + await Task.Delay(Random.Shared.Next(10, config.TriggerDelay + 1)); int radius = config.TriggerFov; + bool shouldClick; + if (config.TriggerInBoundsOnly) + shouldClick = lastDetectionBox.Contains(currentPosition); + else + shouldClick = Math.Abs(currentPosition.X - lastDetection.X) < radius && Math.Abs(currentPosition.Y - lastDetection.Y) < radius; - if (Math.Abs(currentPosition.X - lastDetection.X) < radius && Math.Abs(currentPosition.Y - lastDetection.Y) < radius) + + if (shouldClick) { - switch (config.MovementMethod) + if (config.TriggerSpray && !isSpraying) { - case MovementMethod.MouseEvent: - MouseEvent.ClickDown(); - break; - case MovementMethod.Makcu: - if (EnsureMakcuReady() && MakcuMain.MakcuInstance != null) - MakcuMain.MakcuInstance.Press(MakcuMouseButton.Left); - else - MouseEvent.ClickDown(); - break; - case MovementMethod.Arduino: - ArduinoMain.ClickDown(); - break; - default: - MouseEvent.ClickDown(); - break; + isSpraying = true; + ClickDown(config.MovementMethod); } - await Task.Delay(config.TriggerDuration); - switch (config.MovementMethod) + else if (!config.TriggerSpray) { - case MovementMethod.MouseEvent: - MouseEvent.ClickUp(); - break; - case MovementMethod.Makcu: - if (EnsureMakcuReady() && MakcuMain.MakcuInstance != null) - MakcuMain.MakcuInstance.Release(MakcuMouseButton.Left); - else - MouseEvent.ClickUp(); - break; - case MovementMethod.Arduino: - ArduinoMain.ClickUp(); - break; - default: - MouseEvent.ClickUp(); - break; + ClickDown(config.MovementMethod); } + + if (!config.TriggerRandomDuration && !config.TriggerSpray) + await Task.Delay(config.TriggerDuration); + else if (!config.TriggerSpray) + await Task.Delay(Random.Shared.Next(20, config.TriggerDuration)); + + if (!config.TriggerSpray) + ClickUp(config.MovementMethod); } else { - var dist = Math.Sqrt(Math.Pow(currentPosition.X - lastDetection.X, 2) + Math.Pow(currentPosition.Y - lastDetection.Y, 2)); + if (isSpraying) + { + isSpraying = false; + ClickUp(config.MovementMethod); + } + } + } + + private static void ClickDown(MovementMethod movementMethod) + { + switch (movementMethod) + { + case MovementMethod.MouseEvent: + MouseEvent.ClickDown(); + break; + case MovementMethod.Makcu: + if (EnsureMakcuReady() && MakcuMain.MakcuInstance != null) + MakcuMain.MakcuInstance.Press(MakcuMouseButton.Left); + else + MouseEvent.ClickDown(); + break; + case MovementMethod.Arduino: + ArduinoMain.ClickDown(); + break; + default: + MouseEvent.ClickDown(); + break; + } + } + private static void ClickUp(MovementMethod movementMethod) + { + switch (movementMethod) + { + case MovementMethod.MouseEvent: + MouseEvent.ClickUp(); + break; + case MovementMethod.Makcu: + if (EnsureMakcuReady() && MakcuMain.MakcuInstance != null) + MakcuMain.MakcuInstance.Release(MakcuMouseButton.Left); + else + MouseEvent.ClickUp(); + break; + case MovementMethod.Arduino: + ArduinoMain.ClickUp(); + break; + default: + MouseEvent.ClickUp(); + break; } } diff --git a/Spectrum/Program.cs b/Spectrum/Program.cs index 8ce7b7d..ea878dc 100644 --- a/Spectrum/Program.cs +++ b/Spectrum/Program.cs @@ -71,34 +71,4 @@ namespace Spectrum detectionManager = new DetectionManager(renderer); } } - public static class SystemHelper - { - [DllImport("user32.dll")] - private static extern int GetSystemMetrics(int nIndex); - - [DllImport("user32.dll")] - public static extern short GetAsyncKeyState(int vKey); - - private const int SM_CXSCREEN = 0; - private const int SM_CYSCREEN = 1; - private const int SM_XVIRTUALSCREEN = 76; - private const int SM_YVIRTUALSCREEN = 77; - private const int SM_CXVIRTUALSCREEN = 78; - private const int SM_CYVIRTUALSCREEN = 79; - - public static (int Width, int Height) GetPrimaryScreenSize() - { - return (GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN)); - } - - public static (int X, int Y, int Width, int Height) GetVirtualScreenBounds() - { - return ( - GetSystemMetrics(SM_XVIRTUALSCREEN), - GetSystemMetrics(SM_YVIRTUALSCREEN), - GetSystemMetrics(SM_CXVIRTUALSCREEN), - GetSystemMetrics(SM_CYVIRTUALSCREEN) - ); - } - } } \ No newline at end of file diff --git a/Spectrum/Renderer.cs b/Spectrum/Renderer.cs index 00feef4..aa63573 100644 --- a/Spectrum/Renderer.cs +++ b/Spectrum/Renderer.cs @@ -13,7 +13,7 @@ namespace Spectrum { partial class Renderer : Overlay { - static (int width, int height) screenSize = SystemHelper.GetPrimaryScreenSize(); + static (int width, int height) screenSize = Win32.GetPrimaryScreenSize(); private List> detectionDrawCommands = new List>(); private List> activeDrawCommands = new List>(); private readonly object detectionDrawLock = new object(); @@ -191,17 +191,27 @@ namespace Spectrum if (triggerBot) { + bool BoundingBoxOnly = config.TriggerInBoundsOnly; + bool TriggerSpray = config.TriggerSpray; + int triggerDelay = config.TriggerDelay; if (ImGuiExtensions.SliderFill("Trigger Delay (ms)", ref triggerDelay, 1, 1000)) config.TriggerDelay = triggerDelay; - int triggerFov = config.TriggerFov; - if (ImGuiExtensions.SliderFill("Trigger FOV (px)", ref triggerFov, 1, 100)) - config.TriggerFov = triggerFov; - + if (TriggerSpray) + ImGui.BeginDisabled(); int triggerDuration = config.TriggerDuration; if (ImGuiExtensions.SliderFill("Trigger Duration (ms)", ref triggerDuration, 1, 1000)) config.TriggerDuration = triggerDuration; + if (TriggerSpray) + ImGui.EndDisabled(); + + if (BoundingBoxOnly) + ImGui.BeginDisabled(); + + int triggerFov = config.TriggerFov; + if (ImGuiExtensions.SliderFill("Trigger FOV (px)", ref triggerFov, 1, 100)) + config.TriggerFov = triggerFov; bool DrawTriggerFov = config.DrawTriggerFov; if (ImGui.Checkbox("##Draw Trigger FOV", ref DrawTriggerFov)) @@ -213,6 +223,31 @@ namespace Spectrum if (ImGui.ColorEdit4("Trigger Color", ref RadiusColor, ImGuiColorEditFlags.NoInputs | ImGuiColorEditFlags.NoLabel)) config.TriggerRadiusColor = RadiusColor; + if (BoundingBoxOnly) + ImGui.EndDisabled(); + + bool RandomDelay = config.TriggerRandomDelay; + if (ImGui.Checkbox(" Random Delay", ref RandomDelay)) + config.TriggerRandomDelay = RandomDelay; + if (ImGui.IsItemHovered(ImGuiHoveredFlags.None)) + ImGui.SetTooltip("Randomizes the trigger delay between 10 and the set Trigger Delay."); + + if (TriggerSpray) + ImGui.BeginDisabled(); + + bool RandomDuration = config.TriggerRandomDuration; + if (ImGui.Checkbox(" Random Duration", ref RandomDuration)) + config.TriggerRandomDuration = RandomDuration; + if (ImGui.IsItemHovered(ImGuiHoveredFlags.None)) + ImGui.SetTooltip("Randomizes the trigger duration between 20 and the set Trigger Duration."); + + if (TriggerSpray) + ImGui.EndDisabled(); + + if (ImGui.Checkbox(" Trigger Spray", ref TriggerSpray)) + config.TriggerSpray = TriggerSpray; + if (ImGui.Checkbox(" Bounding Box Only", ref BoundingBoxOnly)) + config.TriggerInBoundsOnly = BoundingBoxOnly; } ImGuiExtensions.EndPane(); } @@ -636,6 +671,16 @@ namespace Spectrum if (ImGuiExtensions.SliderFill("FPS Limit", ref _fpsLimit, 30, 480)) FPSLimit = _fpsLimit; + bool AntiCapture = config.AntiCapture; + if (ImGui.Checkbox("Anti-Capture", ref AntiCapture)) + { + config.AntiCapture = AntiCapture; + if (AntiCapture) + Win32.EnableAntiCapture(window.Handle); + else + Win32.DisableAntiCapture(window.Handle); + } + ImGui.TextUnformatted("Background Image Interval"); ImGui.SetNextItemWidth(-1); int BackgroundImageInterval = config.BackgroundImageInterval; diff --git a/Spectrum/Win32.cs b/Spectrum/Win32.cs new file mode 100644 index 0000000..57b0dc2 --- /dev/null +++ b/Spectrum/Win32.cs @@ -0,0 +1,51 @@ +using System.Runtime.InteropServices; + +namespace Spectrum +{ + class Win32 + { + [DllImport("user32.dll")] + private static extern int GetSystemMetrics(int nIndex); + + [DllImport("user32.dll")] + public static extern short GetAsyncKeyState(int vKey); + + private const int SM_CXSCREEN = 0; + private const int SM_CYSCREEN = 1; + private const int SM_XVIRTUALSCREEN = 76; + private const int SM_YVIRTUALSCREEN = 77; + private const int SM_CXVIRTUALSCREEN = 78; + private const int SM_CYVIRTUALSCREEN = 79; + + public static (int Width, int Height) GetPrimaryScreenSize() + { + return (GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN)); + } + + public static (int X, int Y, int Width, int Height) GetVirtualScreenBounds() + { + return ( + GetSystemMetrics(SM_XVIRTUALSCREEN), + GetSystemMetrics(SM_YVIRTUALSCREEN), + GetSystemMetrics(SM_CXVIRTUALSCREEN), + GetSystemMetrics(SM_CYVIRTUALSCREEN) + ); + } + [DllImport("user32.dll")] + private static extern bool SetWindowDisplayAffinity(IntPtr hwnd, uint dwAffinity); + + private const uint WDA_NONE = 0x00000000; + private const uint WDA_MONITOR = 0x00000001; + private const uint WDA_EXCLUDEFROMCAPTURE = 0x00000011; + + public static bool EnableAntiCapture(IntPtr windowHandle) + { + return SetWindowDisplayAffinity(windowHandle, WDA_EXCLUDEFROMCAPTURE); + } + + public static bool DisableAntiCapture(IntPtr windowHandle) + { + return SetWindowDisplayAffinity(windowHandle, WDA_NONE); + } + } +}