summaryrefslogtreecommitdiff
path: root/src/SMAPI.Toolkit/Framework/ModData/ModDataField.cs
blob: b3954693122039a7a6737f9c17a20f59bc806eb8 (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
using System.Linq;

namespace StardewModdingAPI.Toolkit.Framework.ModData
{
    /// <summary>A versioned mod metadata field.</summary>
    public class ModDataField
    {
        /*********
        ** Accessors
        *********/
        /// <summary>The field key.</summary>
        public ModDataFieldKey Key { get; }

        /// <summary>The field value.</summary>
        public string Value { get; }

        /// <summary>Whether this field should only be applied if it's not already set.</summary>
        public bool IsDefault { get; }

        /// <summary>The lowest version in the range, or <c>null</c> for all past versions.</summary>
        public ISemanticVersion LowerVersion { get; }

        /// <summary>The highest version in the range, or <c>null</c> for all future versions.</summary>
        public ISemanticVersion UpperVersion { get; }


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="key">The field key.</param>
        /// <param name="value">The field value.</param>
        /// <param name="isDefault">Whether this field should only be applied if it's not already set.</param>
        /// <param name="lowerVersion">The lowest version in the range, or <c>null</c> for all past versions.</param>
        /// <param name="upperVersion">The highest version in the range, or <c>null</c> for all future versions.</param>
        public ModDataField(ModDataFieldKey key, string value, bool isDefault, ISemanticVersion lowerVersion, ISemanticVersion upperVersion)
        {
            this.Key = key;
            this.Value = value;
            this.IsDefault = isDefault;
            this.LowerVersion = lowerVersion;
            this.UpperVersion = upperVersion;
        }

        /// <summary>Get whether this data field applies for the given manifest.</summary>
        /// <param name="manifest">The mod manifest.</param>
        public bool IsMatch(IManifest manifest)
        {
            return
                manifest?.Version != null // ignore invalid manifest
                && (!this.IsDefault || !this.HasFieldValue(manifest, this.Key))
                && (this.LowerVersion == null || !manifest.Version.IsOlderThan(this.LowerVersion))
                && (this.UpperVersion == null || !manifest.Version.IsNewerThan(this.UpperVersion));
        }


        /*********
        ** Private methods
        *********/
        /// <summary>Get whether a manifest field has a meaningful value for the purposes of enforcing <see cref="IsDefault"/>.</summary>
        /// <param name="manifest">The mod manifest.</param>
        /// <param name="key">The field key matching <see cref="ModDataFieldKey"/>.</param>
        private bool HasFieldValue(IManifest manifest, ModDataFieldKey key)
        {
            switch (key)
            {
                // update key
                case ModDataFieldKey.UpdateKey:
                    return manifest.UpdateKeys != null && manifest.UpdateKeys.Any(p => !string.IsNullOrWhiteSpace(p));

                // non-manifest fields
                case ModDataFieldKey.AlternativeUrl:
                case ModDataFieldKey.StatusReasonPhrase:
                case ModDataFieldKey.Status:
                    return false;

                default:
                    return false;
            }
        }
    }
}