summaryrefslogtreecommitdiff
path: root/src/SMAPI.Toolkit/Framework/UpdateData/UpdateKey.cs
blob: 7c2cc73cc129b067f17f1b1871e7db3b12bb7978 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Diagnostics.CodeAnalysis;

namespace StardewModdingAPI.Toolkit.Framework.UpdateData
{
    /// <summary>A namespaced mod ID which uniquely identifies a mod within a mod repository.</summary>
    public class UpdateKey : IEquatable<UpdateKey>
    {
        /*********
        ** Accessors
        *********/
        /// <summary>The raw update key text.</summary>
        public string RawText { get; }

        /// <summary>The mod site containing the mod.</summary>
        public ModSiteKey Site { get; }

        /// <summary>The mod ID within the repository.</summary>
        public string? ID { get; }

        /// <summary>If specified, a substring in download names/descriptions to match.</summary>
        public string? Subkey { get; }

        /// <summary>Whether the update key seems to be valid.</summary>
#if NET5_0_OR_GREATER
        [MemberNotNullWhen(true, nameof(UpdateKey.ID))]
#endif
        public bool LooksValid { get; }


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="rawText">The raw update key text.</param>
        /// <param name="site">The mod site containing the mod.</param>
        /// <param name="id">The mod ID within the site.</param>
        /// <param name="subkey">If specified, a substring in download names/descriptions to match.</param>
        public UpdateKey(string? rawText, ModSiteKey site, string? id, string? subkey)
        {
            this.RawText = rawText?.Trim() ?? string.Empty;
            this.Site = site;
            this.ID = id?.Trim();
            this.Subkey = subkey?.Trim();
            this.LooksValid =
                site != ModSiteKey.Unknown
                && !string.IsNullOrWhiteSpace(id);
        }

        /// <summary>Construct an instance.</summary>
        /// <param name="site">The mod site containing the mod.</param>
        /// <param name="id">The mod ID within the site.</param>
        /// <param name="subkey">If specified, a substring in download names/descriptions to match.</param>
        public UpdateKey(ModSiteKey site, string? id, string? subkey)
            : this(UpdateKey.GetString(site, id, subkey), site, id, subkey) { }

        /// <summary>
        ///   Split a string into two at a delimiter.  If the delimiter does not appear in the string then the second
        ///   value of the returned tuple is null.  Both returned strings are trimmed of whitespace.
        /// </summary>
        /// <param name="s">The string to split.</param>
        /// <param name="delimiter">The character on which to split.</param>
        /// <param name="keepDelimiter">
        ///   If <c>true</c> then the second string returned will include the delimiter character
        ///   (provided that the string is not <c>null</c>)
        /// </param>
        /// <returns>
        ///   A pair containing the string consisting of all characters in <paramref name="s"/> before the first
        ///   occurrence of <paramref name="delimiter"/>, and a string consisting of all characters in <paramref name="s"/>
        ///   after the first occurrence of <paramref name="delimiter"/> or <c>null</c> if the delimiter does not
        ///   exist in s.  Both strings are trimmed of whitespace.
        /// </returns>
        private static (string, string?) Bifurcate(string s, char delimiter, bool keepDelimiter = false) {
            int pos = s.IndexOf(delimiter);
            if (pos < 0)
                return (s.Trim(), null);
            return (s.Substring(0, pos).Trim(), s.Substring(pos + (keepDelimiter ? 0 : 1)).Trim());
        }

        /// <summary>Parse a raw update key.</summary>
        /// <param name="raw">The raw update key to parse.</param>
        public static UpdateKey Parse(string? raw)
        {
            if (raw is null)
                return new UpdateKey(raw, ModSiteKey.Unknown, null, null);
            // extract site + ID
            (string rawSite, string? id) = Bifurcate(raw, ':');
            if (string.IsNullOrWhiteSpace(id))
                id = null;

            // extract subkey
            string? subkey = null;
            if (id != null)
                (id, subkey) = Bifurcate(id, '@', true);

            // parse
            if (!Enum.TryParse(rawSite, true, out ModSiteKey site))
                return new UpdateKey(raw, ModSiteKey.Unknown, id, subkey);
            if (id == null)
                return new UpdateKey(raw, site, null, subkey);

            return new UpdateKey(raw, site, id, subkey);
        }

        /// <summary>Parse a raw update key if it's valid.</summary>
        /// <param name="raw">The raw update key to parse.</param>
        /// <param name="parsed">The parsed update key, if valid.</param>
        /// <returns>Returns whether the update key was successfully parsed.</returns>
        public static bool TryParse(string raw, out UpdateKey parsed)
        {
            parsed = UpdateKey.Parse(raw);
            return parsed.LooksValid;
        }

        /// <summary>Get a string that represents the current object.</summary>
        public override string ToString()
        {
            return this.LooksValid
                ? UpdateKey.GetString(this.Site, this.ID, this.Subkey)
                : this.RawText;
        }

        /// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(UpdateKey? other)
        {
            if (!this.LooksValid)
            {
                return
                    other?.LooksValid == false
                    && this.RawText.Equals(other.RawText, StringComparison.OrdinalIgnoreCase);
            }

            return
                other != null
                && this.Site == other.Site
                && string.Equals(this.ID, other.ID, StringComparison.OrdinalIgnoreCase)
                && string.Equals(this.Subkey, other.Subkey, StringComparison.OrdinalIgnoreCase);
        }

        /// <summary>Determines whether the specified object is equal to the current object.</summary>
        /// <param name="obj">The object to compare with the current object.</param>
        public override bool Equals(object? obj)
        {
            return obj is UpdateKey other && this.Equals(other);
        }

        /// <summary>Serves as the default hash function. </summary>
        /// <returns>A hash code for the current object.</returns>
        public override int GetHashCode()
        {
            return this.ToString().ToLower().GetHashCode();
        }

        /// <summary>Get the string representation of an update key.</summary>
        /// <param name="site">The mod site containing the mod.</param>
        /// <param name="id">The mod ID within the repository.</param>
        /// <param name="subkey">If specified, a substring in download names/descriptions to match.</param>
        public static string GetString(ModSiteKey site, string? id, string? subkey = null)
        {
            return $"{site}:{id}{subkey}".Trim();
        }
    }
}