summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/GameVersion.cs
blob: c347ffba93d003d94b4c617d28f9e4ca9ca15f27 (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
using System;
using System.Collections.Generic;

namespace StardewModdingAPI.Framework
{
    /// <summary>An implementation of <see cref="ISemanticVersion"/> that correctly handles the non-semantic versions used by older Stardew Valley releases.</summary>
    internal class GameVersion : SemanticVersion
    {
        /*********
        ** Private methods
        *********/
        /// <summary>A mapping of game to semantic versions.</summary>
        private static readonly IDictionary<string, string> VersionMap = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase)
        {
            ["1.01"] = "1.0.1",
            ["1.02"] = "1.0.2",
            ["1.03"] = "1.0.3",
            ["1.04"] = "1.0.4",
            ["1.05"] = "1.0.5",
            ["1.051"] = "1.0.6-prerelease1", // not a very good mapping, but good enough for SMAPI's purposes.
            ["1.051b"] = "1.0.6-prelease2",
            ["1.06"] = "1.0.6",
            ["1.07"] = "1.0.7",
            ["1.07a"] = "1.0.8-prerelease1",
            ["1.08"] = "1.0.8",
            ["1.11"] = "1.1.1",
            ["1.3.0.1"] = "1.13-alpha.1",
            ["1.3.0.2"] = "1.13-alpha.2"
        };


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="version">The game version string.</param>
        public GameVersion(string version)
            : base(GameVersion.GetSemanticVersionString(version)) { }

        /// <summary>Get a string representation of the version.</summary>
        public override string ToString()
        {
            return GameVersion.GetGameVersionString(base.ToString());
        }


        /*********
        ** Private methods
        *********/
        /// <summary>Convert a game version string to a semantic version string.</summary>
        /// <param name="gameVersion">The game version string.</param>
        private static string GetSemanticVersionString(string gameVersion)
        {
            return GameVersion.VersionMap.TryGetValue(gameVersion, out string semanticVersion)
                ? semanticVersion
                : gameVersion;
        }

        /// <summary>Convert a game version string to a semantic version string.</summary>
        /// <param name="gameVersion">The game version string.</param>
        private static string GetGameVersionString(string gameVersion)
        {
            foreach (var mapping in GameVersion.VersionMap)
            {
                if (mapping.Value.Equals(gameVersion, StringComparison.InvariantCultureIgnoreCase))
                    return mapping.Key;
            }
            return gameVersion;
        }
    }
}