summaryrefslogtreecommitdiff
path: root/src/SMAPI.Toolkit/Utilities/PathLookups/MinimalFileLookup.cs
blob: 414b569be42a86f0ee6cb4d76c35689f75e39cb1 (plain)
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
using System.Collections.Generic;
using System.IO;

namespace StardewModdingAPI.Toolkit.Utilities.PathLookups
{
    /// <summary>An API for file lookups within a root directory with minimal preprocessing.</summary>
    internal class MinimalFileLookup : IFileLookup
    {
        /*********
        ** Accessors
        *********/
        /// <summary>The file lookups by root path.</summary>
        private static readonly Dictionary<string, MinimalFileLookup> CachedRoots = new();

        /// <summary>The root directory path for relative paths.</summary>
        private readonly string RootPath;


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="rootPath">The root directory path for relative paths.</param>
        public MinimalFileLookup(string rootPath)
        {
            this.RootPath = rootPath;
        }

        /// <inheritdoc />
        public FileInfo GetFile(string relativePath)
        {
            return new(
                Path.Combine(this.RootPath, PathUtilities.NormalizePath(relativePath))
            );
        }

        /// <inheritdoc />
        public void Add(string relativePath) { }

        /// <summary>Get a cached dictionary of relative paths within a root path, for case-insensitive file lookups.</summary>
        /// <param name="rootPath">The root path to scan.</param>
        public static MinimalFileLookup GetCachedFor(string rootPath)
        {
            rootPath = PathUtilities.NormalizePath(rootPath);

            if (!MinimalFileLookup.CachedRoots.TryGetValue(rootPath, out MinimalFileLookup? lookup))
                MinimalFileLookup.CachedRoots[rootPath] = lookup = new MinimalFileLookup(rootPath);

            return lookup;
        }
    }
}