summaryrefslogtreecommitdiff
path: root/src/SMAPI.Internal/EnvironmentUtility.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2019-06-17 14:49:05 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2019-09-14 18:05:10 -0400
commit46a0dd6236a39175e85c11eac44a710db1c07847 (patch)
tree0a24917954de718d980417ac3487cec4469cda7c /src/SMAPI.Internal/EnvironmentUtility.cs
parent660e8087a1885fcf48b7d34b4ec726eda0e732d1 (diff)
downloadSMAPI-46a0dd6236a39175e85c11eac44a710db1c07847.tar.gz
SMAPI-46a0dd6236a39175e85c11eac44a710db1c07847.tar.bz2
SMAPI-46a0dd6236a39175e85c11eac44a710db1c07847.zip
move environment utility into toolkit for reuse
Diffstat (limited to 'src/SMAPI.Internal/EnvironmentUtility.cs')
-rw-r--r--src/SMAPI.Internal/EnvironmentUtility.cs112
1 files changed, 0 insertions, 112 deletions
diff --git a/src/SMAPI.Internal/EnvironmentUtility.cs b/src/SMAPI.Internal/EnvironmentUtility.cs
deleted file mode 100644
index c4e4678a..00000000
--- a/src/SMAPI.Internal/EnvironmentUtility.cs
+++ /dev/null
@@ -1,112 +0,0 @@
-using System;
-using System.Diagnostics.CodeAnalysis;
-using System.Linq;
-#if SMAPI_FOR_WINDOWS
-using System.Management;
-#endif
-using System.Runtime.InteropServices;
-
-namespace StardewModdingAPI.Internal
-{
- /// <summary>Provides methods for fetching environment information.</summary>
- internal static class EnvironmentUtility
- {
- /*********
- ** Fields
- *********/
- /// <summary>Get the OS name from the system uname command.</summary>
- /// <param name="buffer">The buffer to fill with the resulting string.</param>
- [DllImport("libc")]
- static extern int uname(IntPtr buffer);
-
-
- /*********
- ** Public methods
- *********/
- /// <summary>Detect the current OS.</summary>
- public static Platform DetectPlatform()
- {
- switch (Environment.OSVersion.Platform)
- {
- case PlatformID.MacOSX:
- return Platform.Mac;
-
- case PlatformID.Unix:
- return EnvironmentUtility.IsRunningMac()
- ? Platform.Mac
- : Platform.Linux;
-
- default:
- return Platform.Windows;
- }
- }
-
-
- /// <summary>Get the human-readable OS name and version.</summary>
- /// <param name="platform">The current platform.</param>
- [SuppressMessage("ReSharper", "EmptyGeneralCatchClause", Justification = "Error suppressed deliberately to fallback to default behaviour.")]
- public static string GetFriendlyPlatformName(Platform platform)
- {
-#if SMAPI_FOR_WINDOWS
- try
- {
- return new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem")
- .Get()
- .Cast<ManagementObject>()
- .Select(entry => entry.GetPropertyValue("Caption").ToString())
- .FirstOrDefault();
- }
- catch { }
-#endif
- return (platform == Platform.Mac ? "MacOS " : "") + Environment.OSVersion;
- }
-
- /// <summary>Get the name of the Stardew Valley executable.</summary>
- /// <param name="platform">The current platform.</param>
- public static string GetExecutableName(Platform platform)
- {
- return platform == Platform.Windows
- ? "Stardew Valley.exe"
- : "StardewValley.exe";
- }
-
- /// <summary>Get whether the platform uses Mono.</summary>
- /// <param name="platform">The current platform.</param>
- public static bool IsMono(this Platform platform)
- {
- return platform == Platform.Linux || platform == Platform.Mac;
- }
-
- /*********
- ** Private methods
- *********/
- /// <summary>Detect whether the code is running on Mac.</summary>
- /// <remarks>
- /// This code is derived from the Mono project (see System.Windows.Forms/System.Windows.Forms/XplatUI.cs). It detects Mac by calling the
- /// <c>uname</c> system command and checking the response, which is always 'Darwin' for MacOS.
- /// </remarks>
- private static bool IsRunningMac()
- {
- IntPtr buffer = IntPtr.Zero;
- try
- {
- buffer = Marshal.AllocHGlobal(8192);
- if (EnvironmentUtility.uname(buffer) == 0)
- {
- string os = Marshal.PtrToStringAnsi(buffer);
- return os == "Darwin";
- }
- return false;
- }
- catch
- {
- return false; // default to Linux
- }
- finally
- {
- if (buffer != IntPtr.Zero)
- Marshal.FreeHGlobal(buffer);
- }
- }
- }
-}