summaryrefslogtreecommitdiff
path: root/src/SMAPI.Installer/Framework/InstallerContext.cs
blob: 88e57760a00c9bac8f54a3beb6c7de4a66e45939 (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
using System;
using System.IO;
using Microsoft.Win32;
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 <see cref="Environment.OSVersion"/> value that represents Windows 7.</summary>
        private readonly Version Windows7Version = new Version(6, 1);

        /// <summary>The underlying toolkit game scanner.</summary>
        private readonly GameScanner GameScanner = new GameScanner();


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

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

        /// <summary>The name of the Stardew Valley executable.</summary>
        public string ExecutableName { 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);
            this.ExecutableName = EnvironmentUtility.GetExecutableName(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 the current system has .NET Framework 4.5 or later installed. This only applies on Windows.</summary>
        /// <exception cref="NotSupportedException">The current platform is not Windows.</exception>
        public bool HasNetFramework45()
        {
            switch (this.Platform)
            {
                case Platform.Windows:
                    using (RegistryKey versionKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full"))
                        return versionKey?.GetValue("Release") != null; // .NET Framework 4.5+

                default:
                    throw new NotSupportedException("The installed .NET Framework version can only be checked on Windows.");
            }
        }

        /// <summary>Get whether the current system has XNA Framework installed. This only applies on Windows.</summary>
        /// <exception cref="NotSupportedException">The current platform is not Windows.</exception>
        public bool HasXna()
        {
            switch (this.Platform)
            {
                case Platform.Windows:
                    using (RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\XNA\Framework"))
                        return key != null; // XNA Framework 4.0+

                default:
                    throw new NotSupportedException("The installed XNA Framework version can only be checked on Windows.");
            }
        }

        /// <summary>Whether the current OS supports newer versions of .NET Framework.</summary>
        public bool CanInstallLatestNetFramework()
        {
            return Environment.OSVersion.Version >= this.Windows7Version; // Windows 7+
        }

        /// <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);
        }
    }
}