From 6bb49b90b7970cd9065ffc642c24d7711201fc2f Mon Sep 17 00:00:00 2001 From: whoswhip Date: Mon, 16 Jun 2025 20:59:13 -0400 Subject: [PATCH] Add another mouse movement path, make the config file more readable, added config file watching (update config on file save), more console key commands --- Spectrum/Config.cs | 175 +++++++++++++++++++++++++++++++-------- Spectrum/InputManager.cs | 40 ++++++++- Spectrum/Program.cs | 129 +++++++++++++++++++++++++++-- Spectrum/Spectrum.sln | 24 ++++++ 4 files changed, 325 insertions(+), 43 deletions(-) create mode 100644 Spectrum/Spectrum.sln diff --git a/Spectrum/Config.cs b/Spectrum/Config.cs index c8b54c0..1420965 100644 --- a/Spectrum/Config.cs +++ b/Spectrum/Config.cs @@ -6,23 +6,39 @@ namespace Spectrum { public static class Config { + private static FileSystemWatcher? _configWatcher; #region Variables + + public enum MovementType + { + Linear, + CubicBezier, + Adaptive + } + // Image settings public static int ImageWidth { get; set; } = 640; public static int ImageHeight { get; set; } = 640; + // Offset settings public static double YOffsetPercent { get; set; } = 0.8; public static double XOffsetPercent { get; set; } = 0.5; + + // Aim settings public static bool EnableAim { get; set; } = true; public static bool ClosestToMouse { get; set; } = true; // if false it will use the center of the screen public static int Keybind { get; set; } = 0x06; // first side button on mouse public static double Sensitivity { get; set; } = 0.5; + public static MovementType AimMovementType { get; set; } = MovementType.CubicBezier; + // Display settings public static bool ShowDetectionWindow { get; set; } = true; + // Data collection settings public static bool CollectData { get; set; } = false; public static bool AutoLabel { get; set; } = false; // collect data needs to be enabled public static int BackgroundImageInterval { get; set; } = 10; // after 10 loops with no detection, save a background image + // Color settings public static Scalar UpperHSV { get; set; } = new Scalar(150, 255, 229); public static Scalar LowerHSV { get; set; } = new Scalar(150, 255, 229); #endregion @@ -31,28 +47,44 @@ namespace Spectrum { if (File.Exists("config.json")) { - string config_content = File.ReadAllText("config.json"); + string config_content = File.ReadAllText("config.json"); try { var config = JObject.Parse(config_content); - ImageWidth = config["ImageWidth"]?.Value() ?? ImageWidth; - ImageHeight = config["ImageHeight"]?.Value() ?? ImageHeight; + bool error = false; + + var imageSettings = config["ImageSettings"]; + ImageWidth = imageSettings?["ImageWidth"]?.Value() ?? config["ImageWidth"]?.Value() ?? ImageWidth; + ImageHeight = imageSettings?["ImageHeight"]?.Value() ?? config["ImageHeight"]?.Value() ?? ImageHeight; - YOffsetPercent = config["YOffsetPercent"]?.Value() ?? YOffsetPercent; - XOffsetPercent = config["XOffsetPercent"]?.Value() ?? XOffsetPercent; - EnableAim = config["EnableAim"]?.Value() ?? EnableAim; - ClosestToMouse = config["ClosestToMouse"]?.Value() ?? ClosestToMouse; - Keybind = config["Keybind"]?.Value() ?? Keybind; - Sensitivity = config["Sensitivity"]?.Value() ?? Sensitivity; + var offsetSettings = config["OffsetSettings"]; + YOffsetPercent = offsetSettings?["YOffsetPercent"]?.Value() ?? config["YOffsetPercent"]?.Value() ?? YOffsetPercent; + XOffsetPercent = offsetSettings?["XOffsetPercent"]?.Value() ?? config["XOffsetPercent"]?.Value() ?? XOffsetPercent; + + var aimSettings = config["AimSettings"]; + EnableAim = aimSettings?["EnableAim"]?.Value() ?? config["EnableAim"]?.Value() ?? EnableAim; + ClosestToMouse = aimSettings?["ClosestToMouse"]?.Value() ?? config["ClosestToMouse"]?.Value() ?? ClosestToMouse; + Keybind = aimSettings?["Keybind"]?.Value() ?? config["Keybind"]?.Value() ?? Keybind; + Sensitivity = aimSettings?["Sensitivity"]?.Value() ?? config["Sensitivity"]?.Value() ?? Sensitivity; + AimMovementType = aimSettings?["AimMovementType"]?.Value() switch + { + "Linear" => MovementType.Linear, + "CubicBezier" => MovementType.CubicBezier, + "Adaptive" => MovementType.Adaptive, + _ => AimMovementType + }; - ShowDetectionWindow = config["ShowDetectionWindow"]?.Value() ?? ShowDetectionWindow; + var displaySettings = config["DisplaySettings"]; + ShowDetectionWindow = displaySettings?["ShowDetectionWindow"]?.Value() ?? config["ShowDetectionWindow"]?.Value() ?? ShowDetectionWindow; - CollectData = config["CollectData"]?.Value() ?? CollectData; - AutoLabel = config["AutoLabel"]?.Value() ?? AutoLabel; - BackgroundImageInterval = config["BackgroundImageInterval"]?.Value() ?? BackgroundImageInterval; + var dataSettings = config["DataCollectionSettings"]; + CollectData = dataSettings?["CollectData"]?.Value() ?? config["CollectData"]?.Value() ?? CollectData; + AutoLabel = dataSettings?["AutoLabel"]?.Value() ?? config["AutoLabel"]?.Value() ?? AutoLabel; + BackgroundImageInterval = dataSettings?["BackgroundImageInterval"]?.Value() ?? config["BackgroundImageInterval"]?.Value() ?? BackgroundImageInterval; - UpperHSV = config["UpperHSV"]?.ToObject() ?? UpperHSV; - LowerHSV = config["LowerHSV"]?.ToObject() ?? LowerHSV; + var colorSettings = config["ColorSettings"]; + UpperHSV = colorSettings?["UpperHSV"]?.ToObject() ?? config["UpperHSV"]?.ToObject() ?? UpperHSV; + LowerHSV = colorSettings?["LowerHSV"]?.ToObject() ?? config["LowerHSV"]?.ToObject() ?? LowerHSV; if (ImageWidth <= 0 || ImageHeight <= 0) { @@ -61,6 +93,7 @@ namespace Spectrum Console.ResetColor(); ImageWidth = 640; ImageHeight = 640; + error = true; } if (YOffsetPercent < 0 || YOffsetPercent > 1 || XOffsetPercent < 0 || XOffsetPercent > 1) { @@ -69,6 +102,7 @@ namespace Spectrum Console.ResetColor(); YOffsetPercent = 0.8; XOffsetPercent = 0.5; + error = true; } if (Keybind < 0 || Keybind > 255) { @@ -76,13 +110,15 @@ namespace Spectrum Console.WriteLine("[ERROR] Invalid keybind in config.json, using default value."); Console.ResetColor(); Keybind = 0x06; + error = true; } - if (Sensitivity < 0 || Sensitivity > 1) + if (Sensitivity < 0 || Sensitivity > 2) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("[ERROR] Invalid sensitivity in config.json, using default value."); Console.ResetColor(); Sensitivity = 0.5; + error = true; } if (AutoLabel && !CollectData ) { @@ -90,15 +126,18 @@ namespace Spectrum Console.WriteLine("[WARNING] AutoLabel is enabled but CollectData is not. Disabling AutoLabel."); Console.ResetColor(); AutoLabel = false; + error = true; } if (BackgroundImageInterval <= 0) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("[ERROR] Invalid BackgroundImageInterval in config.json, using default value."); Console.ResetColor(); + error = true; } + if (error) + SaveConfig(); - SaveConfig(); if (!silent) { Console.ForegroundColor = ConsoleColor.Green; @@ -121,27 +160,95 @@ namespace Spectrum Console.ResetColor(); SaveConfig(); } - } - public static void SaveConfig() + } public static void SaveConfig() { var config = new JObject { - ["ImageWidth"] = ImageWidth, - ["ImageHeight"] = ImageHeight, - ["YOffsetPercent"] = YOffsetPercent, - ["XOffsetPercent"] = XOffsetPercent, - ["EnableAim"] = EnableAim, - ["ClosestToMouse"] = ClosestToMouse, - ["Keybind"] = Keybind, - ["Sensitivity"] = Sensitivity, - ["ShowDetectionWindow"] = ShowDetectionWindow, - ["CollectData"] = CollectData, - ["AutoLabel"] = AutoLabel, - ["BackgroundImageInterval"] = BackgroundImageInterval, - ["UpperHSV"] = JToken.FromObject(UpperHSV), - ["LowerHSV"] = JToken.FromObject(LowerHSV) + ["ImageSettings"] = new JObject + { + ["ImageWidth"] = ImageWidth, + ["ImageHeight"] = ImageHeight + }, + ["OffsetSettings"] = new JObject + { + ["YOffsetPercent"] = YOffsetPercent, + ["XOffsetPercent"] = XOffsetPercent + }, + ["AimSettings"] = new JObject + { + ["EnableAim"] = EnableAim, + ["ClosestToMouse"] = ClosestToMouse, + ["Keybind"] = Keybind, + ["Sensitivity"] = Sensitivity + }, + ["DisplaySettings"] = new JObject + { + ["ShowDetectionWindow"] = ShowDetectionWindow + }, + ["DataCollectionSettings"] = new JObject + { + ["CollectData"] = CollectData, + ["AutoLabel"] = AutoLabel, + ["BackgroundImageInterval"] = BackgroundImageInterval + }, + ["ColorSettings"] = new JObject + { + ["UpperHSV"] = JToken.FromObject(UpperHSV), + ["LowerHSV"] = JToken.FromObject(LowerHSV) + } + }; File.WriteAllText("config.json", config.ToString(Formatting.Indented)); + } + + public static void StartFileWatcher() + { + if (_configWatcher != null) + return; + + _configWatcher = new FileSystemWatcher(Directory.GetCurrentDirectory(), "config.json") + { + NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size }; - File.WriteAllText("config.json", config.ToString(Formatting.Indented)); + + _configWatcher.Changed += OnConfigFileChanged; + _configWatcher.EnableRaisingEvents = true; + + Console.ForegroundColor = ConsoleColor.Cyan; + Console.WriteLine("[INFO] File watcher started for config.json"); + Console.ResetColor(); + } + + public static void StopFileWatcher() + { + if (_configWatcher != null) + { + _configWatcher.EnableRaisingEvents = false; + _configWatcher.Changed -= OnConfigFileChanged; + _configWatcher.Dispose(); + _configWatcher = null; + + Console.ForegroundColor = ConsoleColor.Cyan; + Console.WriteLine("[INFO] File watcher stopped for config.json"); + Console.ResetColor(); + } + } + + private static void OnConfigFileChanged(object sender, FileSystemEventArgs e) + { + Thread.Sleep(100); + + try + { + LoadConfig(true); + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("[INFO] Configuration automatically reloaded due to file change."); + Console.ResetColor(); + } + catch (Exception ex) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"[ERROR] Failed to reload configuration: {ex.Message}"); + Console.ResetColor(); + } } } diff --git a/Spectrum/InputManager.cs b/Spectrum/InputManager.cs index 176a498..5d0b10e 100644 --- a/Spectrum/InputManager.cs +++ b/Spectrum/InputManager.cs @@ -16,7 +16,7 @@ namespace Spectrum public int X; public int Y; } - // AIMMY V2 CODE - https://github.com/babyhamsta/Aimmy/blob/Aimmy-V2/Aimmy2/InputLogic/MouseManager.cs Line 34 - 50 + // AIMMY V2 CODE - https://github.com/Babyhamsta/Aimmy/blob/2e906552673e359ded90383c5e97d79c8f38a2f2/Aimmy2/InputLogic/MouseManager.cs#L34-L50 private static Point CubicBezier(Point start, Point end, Point control1, Point control2, double t) { double u = 1 - t; @@ -28,6 +28,34 @@ namespace Spectrum return new Point((int)x, (int)y); } + private static Point CubicBezierMovement(Point start, Point end, double t) + { + Point control1 = new Point(start.X + (end.X - start.X) / 3, start.Y + (end.Y - start.Y) / 3); + Point control2 = new Point(start.X + 2 * (end.X - start.X) / 3, start.Y + 2 * (end.Y - start.Y) / 3); + return CubicBezier(start, end, control1, control2, t); + } + + private static Point LinearInterpolation(Point start, Point end, double t) + { + int x = (int)(start.X + t * (end.X - start.X)); + int y = (int)(start.Y + t * (end.Y - start.Y)); + return new Point(x, y); + } + + private static Point AdaptiveMovement(Point start, Point end, double sensitivity) + { + // Calculate the distance between the start and end points + double distance = Math.Sqrt(Math.Pow(end.X - start.X, 2) + Math.Pow(end.Y - start.Y, 2)); + // Calculate the step size based on sensitivity + double stepSize = distance * sensitivity; + // Calculate the direction vector + double directionX = (end.X - start.X) / distance; + double directionY = (end.Y - start.Y) / distance; + // Calculate the new position + int newX = (int)(start.X + directionX * stepSize); + int newY = (int)(start.Y + directionY * stepSize); + return new Point(newX, newY); + } public static void MoveMouse(Point target) { @@ -47,9 +75,13 @@ namespace Spectrum Point start = new Point(reference.X, reference.Y); Point end = new Point(target.X, target.Y); - Point control1 = new(start.X + (end.X - start.X) / 3, start.Y + (end.Y - start.Y) / 3); - Point control2 = new(start.X + 2 * (end.X - start.X) / 3, start.Y + 2 * (end.Y - start.Y) / 3); - Point newPosition = CubicBezier(start, end, control1, control2, Config.Sensitivity); + Point newPosition = Config.AimMovementType switch + { + Config.MovementType.CubicBezier => CubicBezierMovement(start, end, Config.Sensitivity), + Config.MovementType.Linear => LinearInterpolation(start, end, Config.Sensitivity), + Config.MovementType.Adaptive => AdaptiveMovement(start, end, Config.Sensitivity), + _ => end + }; int deltaX = newPosition.X - reference.X; int deltaY = newPosition.Y - reference.Y; diff --git a/Spectrum/Program.cs b/Spectrum/Program.cs index 5a9bd56..d69783c 100644 --- a/Spectrum/Program.cs +++ b/Spectrum/Program.cs @@ -12,6 +12,9 @@ namespace Spectrum static void Main() { Config.LoadConfig(); + Config.StartFileWatcher(); + + var screenSize = SystemHelper.GetPrimaryScreenSize(); bounds = new Rectangle( (screenSize.Width - Config.ImageWidth) / 2, @@ -45,12 +48,125 @@ namespace Spectrum { Cv2.DestroyAllWindows(); } + Config.StopFileWatcher(); + Environment.Exit(0); } - else if (key == ConsoleKey.F5 || key == ConsoleKey.R) + else if (key == ConsoleKey.O) { - Config.LoadConfig(true); - Console.ForegroundColor = ConsoleColor.Green; - Console.WriteLine("[INFO] Configuration reloaded."); + try + { + string configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.json"); + System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo + { + FileName = configFilePath, + UseShellExecute = true, + CreateNoWindow = true + }); + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("[INFO] Opened config file in default application."); + Console.ResetColor(); + } + catch (Exception ex) + { + Console.WriteLine($"Failed to open config file: {ex.Message}"); + } + } + else if (key == ConsoleKey.F1) + { + Config.ShowDetectionWindow = !Config.ShowDetectionWindow; + if (Config.ShowDetectionWindow) + { + Cv2.NamedWindow("Spectrum Detection", WindowFlags.AutoSize); + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("[INFO] Enabled detection window."); + } + else + { + Cv2.DestroyAllWindows(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("[INFO] Disabled detection window."); + } + Console.ResetColor(); + } + else if (key == ConsoleKey.F2) + { + Config.AutoLabel = !Config.AutoLabel; + if (Config.AutoLabel) + { + Config.CollectData = true; + AutoLabeling.StartLabeling(); + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("[INFO] Enabled auto-labeling."); + } + else + { + AutoLabeling.StopLabeling(); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("[INFO] Disabled auto-labeling."); + } + Console.ResetColor(); + } + else if (key == ConsoleKey.F3) + { + Config.CollectData = !Config.CollectData; + if (Config.CollectData) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("[INFO] Enabled data collection."); + } + else + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("[INFO] Disabled data collection."); + } + Console.ResetColor(); + } + else if (key == ConsoleKey.F4) + { + Config.EnableAim = !Config.EnableAim; + if (Config.EnableAim) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("[INFO] Enabled aiming."); + } + else + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("[INFO] Disabled aiming."); + } + Console.ResetColor(); + } + else if (key == ConsoleKey.F5) + { + Config.ClosestToMouse = !Config.ClosestToMouse; + if (Config.ClosestToMouse) + { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("[INFO] Enabled closest to mouse aiming."); + } + else + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("[INFO] Disabled closest to mouse aiming."); + } + Console.ResetColor(); + } + else if (key == ConsoleKey.H || key == ConsoleKey.Help) + { + Console.WriteLine("Spectrum Keybinds:"); + Console.WriteLine("F1: Toggle detection window"); + Console.WriteLine("F2: Toggle auto-labeling"); + Console.WriteLine("F3: Toggle data collection"); + Console.WriteLine("F4: Toggle aiming"); + Console.WriteLine("F5: Toggle closest to mouse aiming"); + Console.WriteLine("O: Open config file in default application"); + Console.WriteLine("ESC: Exit the application"); + Console.WriteLine("H: Show this help message"); + } + else + { + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine("[WARNING] Unrecognized key pressed. Press H for help."); Console.ResetColor(); } } @@ -117,6 +233,10 @@ namespace Spectrum AutoLabeling.AddToQueue(drawing, bounds, filteredContours); AutoLabeling.AddBackgroundImage(drawing, true); } + if (!Config.AutoLabel && Config.CollectData) + { + AutoLabeling.AddBackgroundImage(drawing, false); + } if (Config.EnableAim) { @@ -125,7 +245,6 @@ namespace Spectrum if (Config.ShowDetectionWindow) { - Cv2.DrawContours(drawing, filteredContours, -1, Scalar.Red, 2); Cv2.Rectangle(drawing, new OpenCvSharp.Point(bestMinX, bestMinY), new OpenCvSharp.Point(bestMaxX, bestMaxY), Scalar.Blue, 2); Cv2.ImShow("Spectrum Detection", drawing); Cv2.WaitKey(1); diff --git a/Spectrum/Spectrum.sln b/Spectrum/Spectrum.sln new file mode 100644 index 0000000..6665c7d --- /dev/null +++ b/Spectrum/Spectrum.sln @@ -0,0 +1,24 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.2.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spectrum", "Spectrum.csproj", "{C33459B7-8D67-9FB2-0CDB-25002B06AE60}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C33459B7-8D67-9FB2-0CDB-25002B06AE60}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C33459B7-8D67-9FB2-0CDB-25002B06AE60}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C33459B7-8D67-9FB2-0CDB-25002B06AE60}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C33459B7-8D67-9FB2-0CDB-25002B06AE60}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {44E9592E-6BC3-403A-9503-398EC3EFC57C} + EndGlobalSection +EndGlobal