Add another mouse movement path, make the config file more readable, added config file watching (update config on file save), more console key commands

This commit is contained in:
whoswhip
2025-06-16 20:59:13 -04:00
parent 2adfa5d8f8
commit 6bb49b90b7
4 changed files with 325 additions and 43 deletions
+141 -34
View File
@@ -6,23 +6,39 @@ namespace Spectrum
{ {
public static class Config public static class Config
{ {
private static FileSystemWatcher? _configWatcher;
#region Variables #region Variables
public enum MovementType
{
Linear,
CubicBezier,
Adaptive
}
// Image settings
public static int ImageWidth { get; set; } = 640; public static int ImageWidth { get; set; } = 640;
public static int ImageHeight { get; set; } = 640; public static int ImageHeight { get; set; } = 640;
// Offset settings
public static double YOffsetPercent { get; set; } = 0.8; public static double YOffsetPercent { get; set; } = 0.8;
public static double XOffsetPercent { get; set; } = 0.5; public static double XOffsetPercent { get; set; } = 0.5;
// Aim settings
public static bool EnableAim { get; set; } = true; 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 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 int Keybind { get; set; } = 0x06; // first side button on mouse
public static double Sensitivity { get; set; } = 0.5; 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; public static bool ShowDetectionWindow { get; set; } = true;
// Data collection settings
public static bool CollectData { get; set; } = false; public static bool CollectData { get; set; } = false;
public static bool AutoLabel { get; set; } = false; // collect data needs to be enabled 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 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 UpperHSV { get; set; } = new Scalar(150, 255, 229);
public static Scalar LowerHSV { get; set; } = new Scalar(150, 255, 229); public static Scalar LowerHSV { get; set; } = new Scalar(150, 255, 229);
#endregion #endregion
@@ -31,28 +47,44 @@ namespace Spectrum
{ {
if (File.Exists("config.json")) if (File.Exists("config.json"))
{ {
string config_content = File.ReadAllText("config.json"); string config_content = File.ReadAllText("config.json");
try try
{ {
var config = JObject.Parse(config_content); var config = JObject.Parse(config_content);
ImageWidth = config["ImageWidth"]?.Value<int>() ?? ImageWidth; bool error = false;
ImageHeight = config["ImageHeight"]?.Value<int>() ?? ImageHeight;
var imageSettings = config["ImageSettings"];
ImageWidth = imageSettings?["ImageWidth"]?.Value<int>() ?? config["ImageWidth"]?.Value<int>() ?? ImageWidth;
ImageHeight = imageSettings?["ImageHeight"]?.Value<int>() ?? config["ImageHeight"]?.Value<int>() ?? ImageHeight;
YOffsetPercent = config["YOffsetPercent"]?.Value<double>() ?? YOffsetPercent; var offsetSettings = config["OffsetSettings"];
XOffsetPercent = config["XOffsetPercent"]?.Value<double>() ?? XOffsetPercent; YOffsetPercent = offsetSettings?["YOffsetPercent"]?.Value<double>() ?? config["YOffsetPercent"]?.Value<double>() ?? YOffsetPercent;
EnableAim = config["EnableAim"]?.Value<bool>() ?? EnableAim; XOffsetPercent = offsetSettings?["XOffsetPercent"]?.Value<double>() ?? config["XOffsetPercent"]?.Value<double>() ?? XOffsetPercent;
ClosestToMouse = config["ClosestToMouse"]?.Value<bool>() ?? ClosestToMouse;
Keybind = config["Keybind"]?.Value<int>() ?? Keybind; var aimSettings = config["AimSettings"];
Sensitivity = config["Sensitivity"]?.Value<double>() ?? Sensitivity; EnableAim = aimSettings?["EnableAim"]?.Value<bool>() ?? config["EnableAim"]?.Value<bool>() ?? EnableAim;
ClosestToMouse = aimSettings?["ClosestToMouse"]?.Value<bool>() ?? config["ClosestToMouse"]?.Value<bool>() ?? ClosestToMouse;
Keybind = aimSettings?["Keybind"]?.Value<int>() ?? config["Keybind"]?.Value<int>() ?? Keybind;
Sensitivity = aimSettings?["Sensitivity"]?.Value<double>() ?? config["Sensitivity"]?.Value<double>() ?? Sensitivity;
AimMovementType = aimSettings?["AimMovementType"]?.Value<string>() switch
{
"Linear" => MovementType.Linear,
"CubicBezier" => MovementType.CubicBezier,
"Adaptive" => MovementType.Adaptive,
_ => AimMovementType
};
ShowDetectionWindow = config["ShowDetectionWindow"]?.Value<bool>() ?? ShowDetectionWindow; var displaySettings = config["DisplaySettings"];
ShowDetectionWindow = displaySettings?["ShowDetectionWindow"]?.Value<bool>() ?? config["ShowDetectionWindow"]?.Value<bool>() ?? ShowDetectionWindow;
CollectData = config["CollectData"]?.Value<bool>() ?? CollectData; var dataSettings = config["DataCollectionSettings"];
AutoLabel = config["AutoLabel"]?.Value<bool>() ?? AutoLabel; CollectData = dataSettings?["CollectData"]?.Value<bool>() ?? config["CollectData"]?.Value<bool>() ?? CollectData;
BackgroundImageInterval = config["BackgroundImageInterval"]?.Value<int>() ?? BackgroundImageInterval; AutoLabel = dataSettings?["AutoLabel"]?.Value<bool>() ?? config["AutoLabel"]?.Value<bool>() ?? AutoLabel;
BackgroundImageInterval = dataSettings?["BackgroundImageInterval"]?.Value<int>() ?? config["BackgroundImageInterval"]?.Value<int>() ?? BackgroundImageInterval;
UpperHSV = config["UpperHSV"]?.ToObject<Scalar>() ?? UpperHSV; var colorSettings = config["ColorSettings"];
LowerHSV = config["LowerHSV"]?.ToObject<Scalar>() ?? LowerHSV; UpperHSV = colorSettings?["UpperHSV"]?.ToObject<Scalar>() ?? config["UpperHSV"]?.ToObject<Scalar>() ?? UpperHSV;
LowerHSV = colorSettings?["LowerHSV"]?.ToObject<Scalar>() ?? config["LowerHSV"]?.ToObject<Scalar>() ?? LowerHSV;
if (ImageWidth <= 0 || ImageHeight <= 0) if (ImageWidth <= 0 || ImageHeight <= 0)
{ {
@@ -61,6 +93,7 @@ namespace Spectrum
Console.ResetColor(); Console.ResetColor();
ImageWidth = 640; ImageWidth = 640;
ImageHeight = 640; ImageHeight = 640;
error = true;
} }
if (YOffsetPercent < 0 || YOffsetPercent > 1 || XOffsetPercent < 0 || XOffsetPercent > 1) if (YOffsetPercent < 0 || YOffsetPercent > 1 || XOffsetPercent < 0 || XOffsetPercent > 1)
{ {
@@ -69,6 +102,7 @@ namespace Spectrum
Console.ResetColor(); Console.ResetColor();
YOffsetPercent = 0.8; YOffsetPercent = 0.8;
XOffsetPercent = 0.5; XOffsetPercent = 0.5;
error = true;
} }
if (Keybind < 0 || Keybind > 255) if (Keybind < 0 || Keybind > 255)
{ {
@@ -76,13 +110,15 @@ namespace Spectrum
Console.WriteLine("[ERROR] Invalid keybind in config.json, using default value."); Console.WriteLine("[ERROR] Invalid keybind in config.json, using default value.");
Console.ResetColor(); Console.ResetColor();
Keybind = 0x06; Keybind = 0x06;
error = true;
} }
if (Sensitivity < 0 || Sensitivity > 1) if (Sensitivity < 0 || Sensitivity > 2)
{ {
Console.ForegroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[ERROR] Invalid sensitivity in config.json, using default value."); Console.WriteLine("[ERROR] Invalid sensitivity in config.json, using default value.");
Console.ResetColor(); Console.ResetColor();
Sensitivity = 0.5; Sensitivity = 0.5;
error = true;
} }
if (AutoLabel && !CollectData ) if (AutoLabel && !CollectData )
{ {
@@ -90,15 +126,18 @@ namespace Spectrum
Console.WriteLine("[WARNING] AutoLabel is enabled but CollectData is not. Disabling AutoLabel."); Console.WriteLine("[WARNING] AutoLabel is enabled but CollectData is not. Disabling AutoLabel.");
Console.ResetColor(); Console.ResetColor();
AutoLabel = false; AutoLabel = false;
error = true;
} }
if (BackgroundImageInterval <= 0) if (BackgroundImageInterval <= 0)
{ {
Console.ForegroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[ERROR] Invalid BackgroundImageInterval in config.json, using default value."); Console.WriteLine("[ERROR] Invalid BackgroundImageInterval in config.json, using default value.");
Console.ResetColor(); Console.ResetColor();
error = true;
} }
if (error)
SaveConfig();
SaveConfig();
if (!silent) if (!silent)
{ {
Console.ForegroundColor = ConsoleColor.Green; Console.ForegroundColor = ConsoleColor.Green;
@@ -121,27 +160,95 @@ namespace Spectrum
Console.ResetColor(); Console.ResetColor();
SaveConfig(); SaveConfig();
} }
} } public static void SaveConfig()
public static void SaveConfig()
{ {
var config = new JObject var config = new JObject
{ {
["ImageWidth"] = ImageWidth, ["ImageSettings"] = new JObject
["ImageHeight"] = ImageHeight, {
["YOffsetPercent"] = YOffsetPercent, ["ImageWidth"] = ImageWidth,
["XOffsetPercent"] = XOffsetPercent, ["ImageHeight"] = ImageHeight
["EnableAim"] = EnableAim, },
["ClosestToMouse"] = ClosestToMouse, ["OffsetSettings"] = new JObject
["Keybind"] = Keybind, {
["Sensitivity"] = Sensitivity, ["YOffsetPercent"] = YOffsetPercent,
["ShowDetectionWindow"] = ShowDetectionWindow, ["XOffsetPercent"] = XOffsetPercent
["CollectData"] = CollectData, },
["AutoLabel"] = AutoLabel, ["AimSettings"] = new JObject
["BackgroundImageInterval"] = BackgroundImageInterval, {
["UpperHSV"] = JToken.FromObject(UpperHSV), ["EnableAim"] = EnableAim,
["LowerHSV"] = JToken.FromObject(LowerHSV) ["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();
}
} }
} }
+36 -4
View File
@@ -16,7 +16,7 @@ namespace Spectrum
public int X; public int X;
public int Y; 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) private static Point CubicBezier(Point start, Point end, Point control1, Point control2, double t)
{ {
double u = 1 - t; double u = 1 - t;
@@ -28,6 +28,34 @@ namespace Spectrum
return new Point((int)x, (int)y); 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) public static void MoveMouse(Point target)
{ {
@@ -47,9 +75,13 @@ namespace Spectrum
Point start = new Point(reference.X, reference.Y); Point start = new Point(reference.X, reference.Y);
Point end = new Point(target.X, target.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 newPosition = Config.AimMovementType switch
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); 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 deltaX = newPosition.X - reference.X;
int deltaY = newPosition.Y - reference.Y; int deltaY = newPosition.Y - reference.Y;
+124 -5
View File
@@ -12,6 +12,9 @@ namespace Spectrum
static void Main() static void Main()
{ {
Config.LoadConfig(); Config.LoadConfig();
Config.StartFileWatcher();
var screenSize = SystemHelper.GetPrimaryScreenSize(); var screenSize = SystemHelper.GetPrimaryScreenSize();
bounds = new Rectangle( bounds = new Rectangle(
(screenSize.Width - Config.ImageWidth) / 2, (screenSize.Width - Config.ImageWidth) / 2,
@@ -45,12 +48,125 @@ namespace Spectrum
{ {
Cv2.DestroyAllWindows(); Cv2.DestroyAllWindows();
} }
Config.StopFileWatcher();
Environment.Exit(0);
} }
else if (key == ConsoleKey.F5 || key == ConsoleKey.R) else if (key == ConsoleKey.O)
{ {
Config.LoadConfig(true); try
Console.ForegroundColor = ConsoleColor.Green; {
Console.WriteLine("[INFO] Configuration reloaded."); 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(); Console.ResetColor();
} }
} }
@@ -117,6 +233,10 @@ namespace Spectrum
AutoLabeling.AddToQueue(drawing, bounds, filteredContours); AutoLabeling.AddToQueue(drawing, bounds, filteredContours);
AutoLabeling.AddBackgroundImage(drawing, true); AutoLabeling.AddBackgroundImage(drawing, true);
} }
if (!Config.AutoLabel && Config.CollectData)
{
AutoLabeling.AddBackgroundImage(drawing, false);
}
if (Config.EnableAim) if (Config.EnableAim)
{ {
@@ -125,7 +245,6 @@ namespace Spectrum
if (Config.ShowDetectionWindow) 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.Rectangle(drawing, new OpenCvSharp.Point(bestMinX, bestMinY), new OpenCvSharp.Point(bestMaxX, bestMaxY), Scalar.Blue, 2);
Cv2.ImShow("Spectrum Detection", drawing); Cv2.ImShow("Spectrum Detection", drawing);
Cv2.WaitKey(1); Cv2.WaitKey(1);
+24
View File
@@ -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