refactor: simplify code

This commit is contained in:
2025-10-15 21:08:42 -04:00
parent 5832f92edc
commit c14427bf81
5 changed files with 16 additions and 32 deletions
+4 -13
View File
@@ -25,7 +25,7 @@ namespace Spectrum
_configDirectory = configDirectory;
_currentConfigName = Path.GetFileNameWithoutExtension(fileName);
_data = new T();
_availableConfigs = new List<string>();
_availableConfigs = [];
if (!Directory.Exists(_configDirectory))
{
@@ -37,7 +37,7 @@ namespace Spectrum
LoadConfig();
}
public void LoadConfig(bool silent = false, string filename = "")
public void LoadConfig(string filename = "")
{
if (string.IsNullOrEmpty(filename))
filename = _defaultFilename;
@@ -51,7 +51,6 @@ namespace Spectrum
var json = File.ReadAllText(filename);
_data = JsonConvert.DeserializeObject<T>(json) ?? new T();
_currentConfigName = Path.GetFileNameWithoutExtension(filename);
if (!silent) LogManager.Log($"Loaded config {filename}.", LogLevel.Info);
}
catch
{
@@ -83,7 +82,6 @@ namespace Spectrum
{
File.WriteAllText(filename, JsonConvert.SerializeObject(_data, Formatting.Indented));
_currentConfigName = Path.GetFileNameWithoutExtension(filename);
LogManager.Log($"Saved config to {filename}.", LogLevel.Info);
}
catch (Exception ex)
{
@@ -119,7 +117,7 @@ namespace Spectrum
return false;
}
LoadConfig(false, filePath);
LoadConfig(filePath);
return true;
}
@@ -165,7 +163,6 @@ namespace Spectrum
_directoryWatcher.Changed += OnDirectoryChanged;
_directoryWatcher.EnableRaisingEvents = true;
LogManager.Log($"Directory watcher started for {_configDirectory}.", LogLevel.Info);
}
catch (Exception ex)
{
@@ -185,7 +182,6 @@ namespace Spectrum
_directoryWatcher.Dispose();
_directoryWatcher = null;
LogManager.Log($"Directory watcher stopped for {_configDirectory}.", LogLevel.Info);
}
private void OnDirectoryChanged(object sender, FileSystemEventArgs e)
@@ -213,7 +209,6 @@ namespace Spectrum
try
{
File.Delete(filePath);
LogManager.Log($"Deleted config {configName}.", LogLevel.Info);
return true;
}
catch (Exception ex)
@@ -251,7 +246,6 @@ namespace Spectrum
File.Move(oldPath, newPath);
if (_currentConfigName == oldName)
_currentConfigName = newName;
LogManager.Log($"Renamed config from {oldName} to {newName}.", LogLevel.Info);
return true;
}
catch (Exception ex)
@@ -271,7 +265,6 @@ namespace Spectrum
};
_fileWatcher.Changed += OnConfigFileChanged;
_fileWatcher.EnableRaisingEvents = true;
LogManager.Log($"File watcher started for {_defaultFilename}.", LogLevel.Info);
}
public void StopFileWatcher()
@@ -281,7 +274,6 @@ namespace Spectrum
_fileWatcher.Changed -= OnConfigFileChanged;
_fileWatcher.Dispose();
_fileWatcher = null;
LogManager.Log($"File watcher stopped for {_defaultFilename}.", LogLevel.Info);
}
private void OnConfigFileChanged(object sender, FileSystemEventArgs e)
@@ -289,8 +281,7 @@ namespace Spectrum
Thread.Sleep(100);
try
{
LoadConfig(true);
LogManager.Log($"Configuration reloaded successfully for {_defaultFilename}.", LogLevel.Info);
LoadConfig();
}
catch (Exception ex)
{
+10 -11
View File
@@ -1,5 +1,4 @@
using ImGuiNET;
using OpenCvSharp;
using OpenCvSharp;
using Spectrum.Input;
using System.Diagnostics;
@@ -70,12 +69,12 @@ namespace Spectrum.Detection
Thread.Sleep(2);
continue;
}
Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(screenshot);
screenshot.Dispose();
Mat? drawing = (config.AutoLabel || config.CollectData) ? mat.Clone() : null;
Cv2.CvtColor(mat, mat, ColorConversionCodes.BGR2HSV);
Cv2.InRange(mat, config.LowerHSV, config.UpperHSV, mat);
Cv2.Dilate(mat, mat, null, iterations: 2);
@@ -95,7 +94,7 @@ namespace Spectrum.Detection
double minDistSquared = double.MaxValue;
OpenCvSharp.Point[]? closestContour = null;
Rect bestRect = default;
bool shouldDrawDetections = config.DrawDetections && renderer != null;
foreach (var contour in processedContours)
@@ -107,8 +106,8 @@ namespace Spectrum.Detection
int absCenterX = centerX + bounds.X;
int absCenterY = centerY + bounds.Y;
double distSquared = (absCenterX - refX) * (absCenterX - refX) +
double distSquared = (absCenterX - refX) * (absCenterX - refX) +
(absCenterY - refY) * (absCenterY - refY);
if (distSquared < minDistSquared)
@@ -141,14 +140,14 @@ namespace Spectrum.Detection
bestRect.Width,
bestRect.Height
);
renderer!.AddRect(_rect, config.TargetColor, 2.0f);
renderer!.AddRect(_rect, config.TargetColor, 1.0f);
}
int groupX;
int groupY;
if (config.XPixelOffset)
groupX = bestRect.X + config.XOffsetPixels;
else
else
groupX = (int)(bestRect.X + bestRect.Width * config.XOffsetPercent);
if (config.YPixelOffset)
groupY = bestRect.Y + config.YOffsetPixels;
@@ -269,7 +268,7 @@ namespace Spectrum.Detection
var rect = Cv2.BoundingRect(contours[i]);
contourRects.Add((rect, new List<int> { i }));
}
bool merged;
do
{
-1
View File
@@ -88,7 +88,6 @@ namespace Spectrum
public static void ClearLog()
{
_logEntries.Clear();
Log("Log cleared.", LogLevel.Info);
}
public static void OpenLogFolder()
+2 -6
View File
@@ -28,7 +28,6 @@ namespace Spectrum
mainConfig.Data.CaptureMethod = CaptureMethod.GDI;
}
renderer.Run();
LogManager.Log("Renderer started successfully.", LogManager.LogLevel.Info);
}
catch (Exception ex)
{
@@ -41,10 +40,7 @@ namespace Spectrum
foreach (var dir in dirs)
{
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
LogManager.Log($"Created directory: {dir}", LogManager.LogLevel.Info);
}
}
mainConfig.LoadConfig();
@@ -60,8 +56,8 @@ namespace Spectrum
colorConfig.Data.Colors.Add(new ColorInfo("Arsenal [Magenta]", new Scalar(179, 255, 255), new Scalar(150, 255, 255)));
colorConfig.Data.Colors.Add(new ColorInfo("Combat Surf [Red]", new Scalar(0, 255, 255), new Scalar(0, 255, 255)));
colorConfig.SaveConfig();
colorConfig.LoadConfig(true);
mainConfig.LoadConfig(true);
colorConfig.LoadConfig();
mainConfig.LoadConfig();
}
Thread.Sleep(1000);
-1
View File
@@ -216,7 +216,6 @@ namespace Spectrum
else
{
Program.colorConfig.Data.Colors.Add(new ColorInfo(_colorName, _upperHSV, _lowerHSV));
LogManager.Log($"Created new color: {_colorName}", LogManager.LogLevel.Info);
}
Program.colorConfig.SaveConfig();
_colorName = "";