summaryrefslogtreecommitdiff
path: root/src/SMAPI.Toolkit/Framework/ModScanning/ModFolder.cs
blob: 106e3622cd271fe8f331795d8e50eb54e29c84e8 (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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using StardewModdingAPI.Toolkit.Serialization.Models;
using StardewModdingAPI.Toolkit.Utilities;

namespace StardewModdingAPI.Toolkit.Framework.ModScanning
{
    /// <summary>The info about a mod read from its folder.</summary>
    public class ModFolder
    {
        /*********
        ** Fields
        *********/
        /// <summary>The backing field for <see cref="Directory"/>.</summary>
        private DirectoryInfo? DirectoryImpl;


        /*********
        ** Accessors
        *********/
        /// <summary>A suggested display name for the mod folder.</summary>
        public string DisplayName { get; }

        /// <summary>The folder path containing the mod's manifest.json.</summary>
        public string DirectoryPath { get; }

        /// <summary>The folder containing the mod's manifest.json.</summary>
        [JsonIgnore]
        public DirectoryInfo Directory => this.DirectoryImpl ??= new DirectoryInfo(this.DirectoryPath);

        /// <summary>The mod type.</summary>
        public ModType Type { get; }

        /// <summary>The mod manifest.</summary>
        public Manifest? Manifest { get; }

        /// <summary>The error which occurred parsing the manifest, if any.</summary>
        public ModParseError ManifestParseError { get; set; }

        /// <summary>A human-readable message for the <see cref="ManifestParseError"/>, if any.</summary>
        public string? ManifestParseErrorText { get; set; }


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="root">The root folder containing mods.</param>
        /// <param name="directory">The folder containing the mod's manifest.json.</param>
        /// <param name="type">The mod type.</param>
        /// <param name="manifest">The mod manifest.</param>
        public ModFolder(DirectoryInfo root, DirectoryInfo directory, ModType type, Manifest manifest)
            : this(root, directory, type, manifest, ModParseError.None, null) { }

        /// <summary>Construct an instance.</summary>
        /// <param name="root">The root folder containing mods.</param>
        /// <param name="directory">The folder containing the mod's manifest.json.</param>
        /// <param name="type">The mod type.</param>
        /// <param name="manifest">The mod manifest.</param>
        /// <param name="manifestParseError">The error which occurred parsing the manifest, if any.</param>
        /// <param name="manifestParseErrorText">A human-readable message for the <paramref name="manifestParseError"/>, if any.</param>
        public ModFolder(DirectoryInfo root, DirectoryInfo directory, ModType type, Manifest? manifest, ModParseError manifestParseError, string? manifestParseErrorText)
        {
            // save info
            this.DirectoryImpl = directory;
            this.DirectoryPath = directory.FullName;
            this.Type = type;
            this.Manifest = manifest;
            this.ManifestParseError = manifestParseError;
            this.ManifestParseErrorText = manifestParseErrorText;

            // set display name
            this.DisplayName = !string.IsNullOrWhiteSpace(manifest?.Name)
                ? manifest.Name
                : PathUtilities.GetRelativePath(root.FullName, directory.FullName);
        }

        /// <summary>Construct an instance.</summary>
        /// <param name="displayName">A suggested display name for the mod folder.</param>
        /// <param name="directoryPath">The folder path containing the mod's manifest.json.</param>
        /// <param name="type">The mod type.</param>
        /// <param name="manifest">The mod manifest.</param>
        /// <param name="manifestParseError">The error which occurred parsing the manifest, if any.</param>
        /// <param name="manifestParseErrorText">A human-readable message for the <paramref name="manifestParseError"/>, if any.</param>
        [JsonConstructor]
        public ModFolder(string displayName, string directoryPath, ModType type, Manifest? manifest, ModParseError manifestParseError, string? manifestParseErrorText)
        {
            this.DisplayName = displayName;
            this.DirectoryPath = directoryPath;
            this.Type = type;
            this.Manifest = manifest;
            this.ManifestParseError = manifestParseError;
            this.ManifestParseErrorText = manifestParseErrorText;
        }

        /// <summary>Get the update keys for a mod.</summary>
        /// <param name="manifest">The mod manifest.</param>
        public IEnumerable<string> GetUpdateKeys(Manifest manifest)
        {
            return
                manifest.UpdateKeys
                .Where(p => !string.IsNullOrWhiteSpace(p))
                .ToArray();
        }
    }
}