summaryrefslogtreecommitdiff
path: root/src/SMAPI.Installer/Framework/InstallerContext.cs
blob: abc7adde246bc241ec333f7d6215778d3c360038 (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
using System.IO;
using StardewModdingAPI.Toolkit;
using StardewModdingAPI.Toolkit.Framework.GameScanning;
using StardewModdingAPI.Toolkit.Utilities;

namespace StardewModdingAPI.Installer.Framework
{
    /// <summary>The installer context.</summary>
    internal class InstallerContext
    {
        /*********
        ** Fields
        *********/
        /// <summary>The underlying toolkit game scanner.</summary>
        private readonly GameScanner GameScanner = new();


        /*********
        ** Accessors
        *********/
        /// <summary>The current OS.</summary>
        public Platform Platform { get; }

        /// <summary>The human-readable OS name and version.</summary>
        public string PlatformName { get; }

        /// <summary>Whether the installer is running on Windows.</summary>
        public bool IsWindows => this.Platform == Platform.Windows;

        /// <summary>Whether the installer is running on a Unix OS (including Linux or macOS).</summary>
        public bool IsUnix => !this.IsWindows;


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        public InstallerContext()
        {
            this.Platform = EnvironmentUtility.DetectPlatform();
            this.PlatformName = EnvironmentUtility.GetFriendlyPlatformName(this.Platform);
        }

        /// <summary>Get the installer's version number.</summary>
        public ISemanticVersion GetInstallerVersion()
        {
            var raw = this.GetType().Assembly.GetName().Version;
            return new SemanticVersion(raw);
        }

        /// <summary>Get whether a folder seems to contain the game files.</summary>
        /// <param name="dir">The folder to check.</param>
        public bool LooksLikeGameFolder(DirectoryInfo dir)
        {
            return this.GameScanner.LooksLikeGameFolder(dir);
        }

        /// <summary>Get whether a folder seems to contain the game, and which version it contains if so.</summary>
        /// <param name="dir">The folder to check.</param>
        public GameFolderType GetGameFolderType(DirectoryInfo dir)
        {
            return this.GameScanner.GetGameFolderType(dir);
        }
    }
}