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; _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)
{ {
+2 -3
View File
@@ -1,5 +1,4 @@
using ImGuiNET; using OpenCvSharp;
using OpenCvSharp;
using Spectrum.Input; using Spectrum.Input;
using System.Diagnostics; using System.Diagnostics;
@@ -141,7 +140,7 @@ 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;
-1
View File
@@ -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
View File
@@ -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);
-1
View File
@@ -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 = "";