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