using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.Json; namespace LDUninstaller.Core.Junk { public class ExclusionItem { public string Path { get; set; } = string.Empty; public bool IsRegex { get; set; } public DateTime AddedAt { get; set; } = DateTime.Now; public string Reason { get; set; } = string.Empty; } public static class ExclusionManager { private static readonly object _lock = new(); private static readonly HashSet _exactExclusions = new(StringComparer.OrdinalIgnoreCase); private static readonly List _items = new(); private static bool _loaded = false; public static string ExclusionFilePath => Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "LDUninstaller", "exclusions.json"); public static void EnsureLoaded() { if (_loaded) return; lock (_lock) { if (_loaded) return; LoadInternal(); _loaded = true; } } private static void LoadInternal() { _exactExclusions.Clear(); _items.Clear(); try { string path = ExclusionFilePath; if (File.Exists(path)) { string json = File.ReadAllText(path); var list = JsonSerializer.Deserialize>(json); if (list != null) { foreach (var item in list) { if (!string.IsNullOrWhiteSpace(item.Path)) { string norm = NormalizePath(item.Path); _exactExclusions.Add(norm); _items.Add(item); } } } } } catch { } } public static void Save() { lock (_lock) { try { string dir = Path.GetDirectoryName(ExclusionFilePath) ?? ""; if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) { Directory.CreateDirectory(dir); } string json = JsonSerializer.Serialize(_items, new JsonSerializerOptions { WriteIndented = true }); File.WriteAllText(ExclusionFilePath, json); } catch { } } } public static bool IsExcluded(string targetPath) { if (string.IsNullOrWhiteSpace(targetPath)) return false; EnsureLoaded(); string norm = NormalizePath(targetPath); lock (_lock) { if (_exactExclusions.Contains(norm)) return true; foreach (var item in _items) { if (item.IsRegex) { try { if (System.Text.RegularExpressions.Regex.IsMatch(norm, item.Path, System.Text.RegularExpressions.RegexOptions.IgnoreCase)) return true; } catch { } } else { string itemNorm = NormalizePath(item.Path); if (norm.Equals(itemNorm, StringComparison.OrdinalIgnoreCase)) return true; if (norm.StartsWith(itemNorm + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase)) return true; } } } return false; } public static bool AddExclusion(string path, string reason = "Kullanıcı Koruması", bool isRegex = false) { if (string.IsNullOrWhiteSpace(path)) return false; EnsureLoaded(); lock (_lock) { string norm = NormalizePath(path); if (_exactExclusions.Contains(norm)) return false; var item = new ExclusionItem { Path = path.Trim(), IsRegex = isRegex, AddedAt = DateTime.Now, Reason = reason }; _items.Add(item); _exactExclusions.Add(norm); Save(); return true; } } public static bool RemoveExclusion(string path) { if (string.IsNullOrWhiteSpace(path)) return false; EnsureLoaded(); lock (_lock) { string norm = NormalizePath(path); int removed = _items.RemoveAll(x => NormalizePath(x.Path).Equals(norm, StringComparison.OrdinalIgnoreCase)); _exactExclusions.Remove(norm); if (removed > 0) { Save(); return true; } } return false; } public static List GetAllExclusions() { EnsureLoaded(); lock (_lock) { return new List(_items); } } private static string NormalizePath(string p) { if (string.IsNullOrWhiteSpace(p)) return string.Empty; return p.Replace('/', '\\').Trim().TrimEnd('\\'); } } }