Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 70 additions & 52 deletions src/ConfigManager.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Concurrent;
using System.Reflection;

namespace BASpark
{
Expand Down Expand Up @@ -96,6 +95,12 @@ public static class ConfigManager

private static List<FilterProfile> _profiles = new List<FilterProfile>();

// 缓存属性元数据,避免 Save() 每次都反射查找
private static readonly ConcurrentDictionary<string, PropertyInfo?> _propertyCache = new();
Comment thread
ABA2396 marked this conversation as resolved.

// 保护并发 Save / _profiles 访问
private static readonly object _syncLock = new();

public static void Load()
{
try
Expand Down Expand Up @@ -229,20 +234,29 @@ public static void GetAnimationSpeedsForOverlay(out double trailSpeed, out doubl
}
}

public static List<FilterProfile> GetProfiles() => _profiles;
public static List<FilterProfile> GetProfiles()
{
lock (_syncLock) { return [.. _profiles]; }
}

public static FilterProfile? GetActiveProfile()
{
return _profiles.FirstOrDefault(p => p.Id == ActiveProfileId) ?? _profiles.FirstOrDefault();
lock (_syncLock)
{
return _profiles.FirstOrDefault(p => p.Id == ActiveProfileId) ?? _profiles.FirstOrDefault();
}
}

public static void SaveProfiles(List<FilterProfile> profiles, string activeId)
{
_profiles = profiles;
ActiveProfileId = activeId;
string json = System.Text.Json.JsonSerializer.Serialize(_profiles);
Save("FilterProfiles", json);
Save("ActiveProfileId", activeId);
lock (_syncLock)
{
_profiles = profiles;
ActiveProfileId = activeId;
string json = System.Text.Json.JsonSerializer.Serialize(_profiles);
Save("FilterProfiles", json);
Save("ActiveProfileId", activeId);
}
}

/// 将选中的视觉表现项恢复默认值并写入注册表
Expand Down Expand Up @@ -296,8 +310,9 @@ public static void Save(string name, object value)
{
try
{
using (RegistryKey key = Registry.CurrentUser.CreateSubKey(RegPath))
lock (_syncLock)
{
using RegistryKey key = Registry.CurrentUser.CreateSubKey(RegPath);
if (value is Enum enumValue)
{
key.SetValue(name, enumValue.ToString());
Expand All @@ -307,7 +322,7 @@ public static void Save(string name, object value)
key.SetValue(name, value);
}

var prop = typeof(ConfigManager).GetProperty(name);
var prop = _propertyCache.GetOrAdd(name, n => typeof(ConfigManager).GetProperty(n));
if (prop != null)
Comment thread
ABA2396 marked this conversation as resolved.
{
object propertyValue = value;
Expand Down Expand Up @@ -475,48 +490,51 @@ public static void ResetAndClear()
{
try
{
Registry.CurrentUser.DeleteSubKeyTree(RegPath, false);

string oldJson = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.json");
if (System.IO.File.Exists(oldJson))
lock (_syncLock)
{
System.IO.File.Delete(oldJson);
}
Registry.CurrentUser.DeleteSubKeyTree(RegPath, false);

ParticleColor = "45,175,255";
IsEffectEnabled = true;
AutoStart = false;
AgreedToPrivacy = false;
EnableTelemetry = false;
TotalClicks = 0;
LastNoticeContent = "";
EnableAlwaysTrailEffect = false;
StartSilent = false;
RunAsAdmin = false;
EffectScale = 1.5;
EffectOpacity = 1.0;
EffectSpeed = 1.0;
UseLinkedAnimationSpeed = true;
TrailAnimationSpeed = 1.0;
ClickAnimationSpeed = 1.0;
TrailRefreshRate = 40;
EnableEnvironmentFilter = false;
HideInFullscreen = true;
ShowEffectOnDesktop = true;
FilterProfiles = "";
ActiveProfileId = "";
_profiles.Clear();
IsTouchscreenMode = false;
ClickTriggerType = 0;
EnableMiddleClickTrigger = false;
ScreenshotCompatibilityMode = false;
EnabledScreenIds = "";
ScreenSelections = "";
UiLanguage = "";
NetworkRegion = NetworkRegionOption.Auto;
SidebarBackgroundImagePath = "";
TelemetryClientId = "";
LastTelemetrySentUtc = "";
string oldJson = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.json");
if (System.IO.File.Exists(oldJson))
{
System.IO.File.Delete(oldJson);
}

ParticleColor = "45,175,255";
IsEffectEnabled = true;
AutoStart = false;
AgreedToPrivacy = false;
EnableTelemetry = false;
TotalClicks = 0;
LastNoticeContent = "";
EnableAlwaysTrailEffect = false;
StartSilent = false;
RunAsAdmin = false;
EffectScale = 1.5;
EffectOpacity = 1.0;
EffectSpeed = 1.0;
UseLinkedAnimationSpeed = true;
TrailAnimationSpeed = 1.0;
ClickAnimationSpeed = 1.0;
TrailRefreshRate = 40;
EnableEnvironmentFilter = false;
HideInFullscreen = true;
ShowEffectOnDesktop = true;
FilterProfiles = "";
ActiveProfileId = "";
_profiles.Clear();
IsTouchscreenMode = false;
ClickTriggerType = 0;
EnableMiddleClickTrigger = false;
ScreenshotCompatibilityMode = false;
EnabledScreenIds = "";
ScreenSelections = "";
UiLanguage = "";
NetworkRegion = NetworkRegionOption.Auto;
SidebarBackgroundImagePath = "";
TelemetryClientId = "";
LastTelemetrySentUtc = "";
}
}
catch { }
}
Expand Down
Loading