File size: 2,817 Bytes
a268ccb
 
 
 
 
 
2a2618d
a268ccb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a2618d
 
a268ccb
 
 
 
 
 
 
 
 
2a2618d
 
0e209a3
 
 
 
 
 
 
 
a268ccb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using K4os.Compression.LZ4;

namespace FastSeekWpf.Core;

public static class CacheManager
{
    private static readonly string CachePath = Path.Combine(
        Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
        "FastSeek", "cache.dat");

    private static readonly JsonSerializerOptions JsonOptions = new()
    {
        PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
        DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
        IncludeFields = true
    };

    private static string ConfigPath => Path.Combine(
        Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
        "FastSeek", "config.txt");

    static CacheManager()
    {
        Directory.CreateDirectory(Path.GetDirectoryName(CachePath)!);
    }

    public static void SaveCache(IndexStore store)
    {
        var cache = store.ToCache();
        string json = JsonSerializer.Serialize(cache, JsonOptions);
        byte[] bytes = Encoding.UTF8.GetBytes(json);
        byte[] compressed = LZ4Pickler.Pickle(bytes, LZ4Level.L12_MAX);
        File.WriteAllBytes(CachePath, compressed);
    }

    public static CacheData? LoadCache()
    {
        if (!File.Exists(CachePath)) return null;

        try
        {
            byte[] compressed = File.ReadAllBytes(CachePath);
            byte[] bytes = LZ4Pickler.Unpickle(compressed);
            string json = Encoding.UTF8.GetString(bytes);
            var cache = JsonSerializer.Deserialize<CacheData>(json, JsonOptions);
            // Back-compat: single DriveRoot → DriveRoots list
            if (cache != null && cache.DriveRoots == null || cache!.DriveRoots.Count == 0)
            {
                // This is the old format — can't use it, need full rescan
                return null;
            }
            return cache;
        }
        catch
        {
            return null;
        }
    }

    public static void ClearCache()
    {
        if (File.Exists(CachePath))
            File.Delete(CachePath);
    }

    public static List<string> LoadExclusions()
    {
        if (!File.Exists(ConfigPath)) return new List<string>();

        var result = new List<string>();
        foreach (var line in File.ReadAllLines(ConfigPath))
        {
            string trimmed = line.Trim().ToLowerInvariant();
            if (!string.IsNullOrEmpty(trimmed))
                result.Add(trimmed);
        }
        return result;
    }

    public static void SaveExclusions(List<string> exclusions)
    {
        Directory.CreateDirectory(Path.GetDirectoryName(ConfigPath)!);
        File.WriteAllText(ConfigPath, string.Join("\n", exclusions));
    }
}