Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/BASpark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
<UseVisualBasic>true</UseVisualBasic>
<ApplicationIcon>app.ico</ApplicationIcon>

<Version>1.2.0</Version>
Expand Down
112 changes: 76 additions & 36 deletions src/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ public enum ProcessFilterModeOption
Whitelist
}

public class FilterProfile
{
public string Id { get; set; } = Guid.NewGuid().ToString();
public string Name { get; set; } = "新配置组";
public ProcessFilterModeOption Mode { get; set; } = ProcessFilterModeOption.Blacklist;
public List<string> Processes { get; set; } = new List<string>();
}

public static class ConfigManager
{
private const string RegPath = @"Software\BASpark";
Expand All @@ -32,10 +40,13 @@ public static class ConfigManager
public static int TrailRefreshRate { get; set; } = 40;
public static bool EnableEnvironmentFilter { get; set; } = false;
public static bool HideInFullscreen { get; set; } = true;
public static ProcessFilterModeOption ProcessFilterMode { get; set; } = ProcessFilterModeOption.Disabled;
public static string ProcessFilterList { get; set; } = "";
public static bool ShowEffectOnDesktop { get; set; } = true;
public static string FilterProfiles { get; set; } = "";
public static string ActiveProfileId { get; set; } = "";
public static bool IsTouchscreenMode { get; set; } = false;

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

public static void Load()
{
try
Expand All @@ -61,23 +72,73 @@ public static void Load()
TrailRefreshRate = Math.Clamp(Convert.ToInt32(key.GetValue("TrailRefreshRate", 40)), 10, 240);
EnableEnvironmentFilter = Convert.ToBoolean(key.GetValue("EnableEnvironmentFilter", false));
HideInFullscreen = Convert.ToBoolean(key.GetValue("HideInFullscreen", true));
ShowEffectOnDesktop = Convert.ToBoolean(key.GetValue("ShowEffectOnDesktop", true));
IsTouchscreenMode = Convert.ToBoolean(key.GetValue("IsTouchscreenMode", false));

string processFilterModeRaw = key.GetValue("ProcessFilterMode", ProcessFilterModeOption.Disabled.ToString())?.ToString()
?? ProcessFilterModeOption.Disabled.ToString();
if (!Enum.TryParse(processFilterModeRaw, true, out ProcessFilterModeOption processFilterMode))
FilterProfiles = key.GetValue("FilterProfiles", "")?.ToString() ?? "";
ActiveProfileId = key.GetValue("ActiveProfileId", "")?.ToString() ?? "";

if (!string.IsNullOrEmpty(FilterProfiles))
{
try
{
_profiles = System.Text.Json.JsonSerializer.Deserialize<List<FilterProfile>>(FilterProfiles) ?? new List<FilterProfile>();
}
catch { _profiles = new List<FilterProfile>(); }
}

// 向后兼容处理
if (_profiles.Count == 0)
{
processFilterMode = ProcessFilterModeOption.Disabled;
string processFilterModeRaw = key.GetValue("ProcessFilterMode", "Disabled")?.ToString() ?? "Disabled";
ProcessFilterModeOption oldMode;
if (!Enum.TryParse(processFilterModeRaw, true, out oldMode))
{
oldMode = ProcessFilterModeOption.Disabled;
}
string oldListRaw = key.GetValue("ProcessFilterList", "")?.ToString() ?? "";
var oldList = oldListRaw
.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Select(s => s.ToLowerInvariant())
.Distinct()
.ToList();

var defaultProfile = new FilterProfile
{
Name = "默认配置",
Mode = oldMode == ProcessFilterModeOption.Disabled ? ProcessFilterModeOption.Blacklist : oldMode,
Processes = oldList
};
_profiles.Add(defaultProfile);
ActiveProfileId = defaultProfile.Id;
}

ProcessFilterMode = processFilterMode;
ProcessFilterList = NormalizeProcessFilterList(key.GetValue("ProcessFilterList", "")?.ToString() ?? "");
if (string.IsNullOrEmpty(ActiveProfileId) && _profiles.Count > 0)
{
ActiveProfileId = _profiles[0].Id;
}
}
}
}
catch { }
}

public static List<FilterProfile> GetProfiles() => _profiles;

public static FilterProfile? GetActiveProfile()
{
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);
}

public static void Save(string name, object value)
{
try
Expand Down Expand Up @@ -116,34 +177,12 @@ public static void Save(string name, object value)
catch { }
}

public static string NormalizeProcessFilterList(string rawValue)
{
if (string.IsNullOrWhiteSpace(rawValue))
{
return string.Empty;
}

var normalizedLines = rawValue
.Replace("\r\n", "\n")
.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Select(line => line.Trim().ToLowerInvariant())
.Where(line => !string.IsNullOrWhiteSpace(line))
.Distinct(StringComparer.OrdinalIgnoreCase);

return string.Join(Environment.NewLine, normalizedLines);
}

public static IReadOnlySet<string> GetProcessFilterEntries()
{
string normalized = NormalizeProcessFilterList(ProcessFilterList);
if (string.IsNullOrEmpty(normalized))
{
return new HashSet<string>(StringComparer.OrdinalIgnoreCase);
}
var profile = GetActiveProfile();
if (profile == null) return new HashSet<string>(StringComparer.OrdinalIgnoreCase);

return normalized
.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.ToHashSet(StringComparer.OrdinalIgnoreCase);
return profile.Processes.ToHashSet(StringComparer.OrdinalIgnoreCase);
}

public static void ResetAndClear()
Expand All @@ -152,7 +191,6 @@ public static void ResetAndClear()
{
Registry.CurrentUser.DeleteSubKeyTree(RegPath, false);

// 适配 264100 版本之前的配置存储逻辑
string oldJson = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.json");
if (System.IO.File.Exists(oldJson))
{
Expand All @@ -175,8 +213,10 @@ public static void ResetAndClear()
TrailRefreshRate = 40;
EnableEnvironmentFilter = false;
HideInFullscreen = true;
ProcessFilterMode = ProcessFilterModeOption.Disabled;
ProcessFilterList = "";
ShowEffectOnDesktop = true;
FilterProfiles = "";
ActiveProfileId = "";
_profiles.Clear();
IsTouchscreenMode = false;
}
catch { }
Expand Down
Loading
Loading