From aa7fce300d1057f75cbe544e804204e56fc7432d Mon Sep 17 00:00:00 2001 From: Archi Date: Thu, 5 Aug 2021 21:17:42 +0200 Subject: [PATCH] Use new OperatingSystem API for OS detection Supporting netf gets harder every day... --- ArchiSteamFarm/Core/ASF.cs | 4 +-- ArchiSteamFarm/Core/OS.cs | 35 ++++++++++++++++--- ArchiSteamFarm/Helpers/ArchiCryptoHelper.cs | 12 ++++++- .../Helpers/CrossProcessFileBasedSemaphore.cs | 16 +++++++-- 4 files changed, 57 insertions(+), 10 deletions(-) diff --git a/ArchiSteamFarm/Core/ASF.cs b/ArchiSteamFarm/Core/ASF.cs index 07689adbc..4089d4e18 100644 --- a/ArchiSteamFarm/Core/ASF.cs +++ b/ArchiSteamFarm/Core/ASF.cs @@ -20,6 +20,7 @@ // limitations under the License. #if NETFRAMEWORK +using System.Runtime.InteropServices; using ArchiSteamFarm.Compatibility; using File = System.IO.File; using Path = System.IO.Path; @@ -34,7 +35,6 @@ using System.IO; using System.IO.Compression; using System.Linq; using System.Reflection; -using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Text; using System.Threading; @@ -317,7 +317,7 @@ namespace ArchiSteamFarm.Core { #if NETFRAMEWORK if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { #else - if (RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { + if (OperatingSystem.IsFreeBSD() || OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) { #endif string executable = Path.Combine(SharedInfo.HomeDirectory, SharedInfo.AssemblyName); diff --git a/ArchiSteamFarm/Core/OS.cs b/ArchiSteamFarm/Core/OS.cs index 32678e10d..1b1ba014e 100644 --- a/ArchiSteamFarm/Core/OS.cs +++ b/ArchiSteamFarm/Core/OS.cs @@ -22,6 +22,8 @@ #if NETFRAMEWORK using ArchiSteamFarm.Compatibility; using File = System.IO.File; +#else +using System.Runtime.Versioning; #endif using System; using System.Diagnostics; @@ -81,19 +83,25 @@ namespace ArchiSteamFarm.Core { private static Mutex? SingleInstance; internal static void CoreInit(bool systemRequired) { +#if NETFRAMEWORK if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { +#else + if (OperatingSystem.IsWindows()) { +#endif if (systemRequired) { WindowsKeepSystemActive(); } if (!Console.IsOutputRedirected) { - // Normally we should use UTF-8 encoding as it's the most correct one for our case, and we already use it on other OSes such as Linux + // Normally we should use UTF-8 console encoding as it's the most correct one for our case, and we already use it on other OSes such as Linux // However, older Windows versions, mainly 7/8.1 can't into UTF-8 without appropriate console font, and expecting from users to change it manually is unwanted // As irrational as it can sound, those versions actually can work with unicode encoding instead, as they magically map it into proper chars despite of incorrect font - // We could in theory conditionally use UTF-8 for Windows 10+ and unicode otherwise, but Windows version detection is simply not worth the hassle in this case - // Therefore, until we can drop support for Windows < 10, we'll stick with Unicode for all Windows boxes, unless there will be valid reasoning for conditional switch // See https://github.com/JustArchiNET/ArchiSteamFarm/issues/1289 for more details +#if NETFRAMEWORK Console.OutputEncoding = Encoding.Unicode; +#else + Console.OutputEncoding = OperatingSystem.IsWindowsVersionAtLeast(10) ? Encoding.UTF8 : Encoding.Unicode; +#endif // Quick edit mode will freeze when user start selecting something on the console until the selection is cancelled // Users are very often doing it accidentally without any real purpose, and we want to avoid this common issue which causes the whole process to hang @@ -170,6 +178,11 @@ namespace ArchiSteamFarm.Core { return true; } +#if !NETFRAMEWORK + [SupportedOSPlatform("FreeBSD")] + [SupportedOSPlatform("Linux")] + [SupportedOSPlatform("OSX")] +#endif internal static void UnixSetFileAccess(string path, EUnixPermission permission) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); @@ -178,7 +191,7 @@ namespace ArchiSteamFarm.Core { #if NETFRAMEWORK if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && !RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { #else - if (!RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD) && !RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && !RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { + if (!OperatingSystem.IsFreeBSD() && !OperatingSystem.IsLinux() && !OperatingSystem.IsMacOS()) { #endif throw new PlatformNotSupportedException(); } @@ -237,8 +250,15 @@ namespace ArchiSteamFarm.Core { #endif } +#if !NETFRAMEWORK + [SupportedOSPlatform("Windows")] +#endif private static void WindowsDisableQuickEditMode() { +#if NETFRAMEWORK if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { +#else + if (!OperatingSystem.IsWindows()) { +#endif throw new PlatformNotSupportedException(); } @@ -257,8 +277,15 @@ namespace ArchiSteamFarm.Core { } } +#if !NETFRAMEWORK + [SupportedOSPlatform("Windows")] +#endif private static void WindowsKeepSystemActive() { +#if NETFRAMEWORK if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { +#else + if (!OperatingSystem.IsWindows()) { +#endif throw new PlatformNotSupportedException(); } diff --git a/ArchiSteamFarm/Helpers/ArchiCryptoHelper.cs b/ArchiSteamFarm/Helpers/ArchiCryptoHelper.cs index 113f6b915..71cc7cfa0 100644 --- a/ArchiSteamFarm/Helpers/ArchiCryptoHelper.cs +++ b/ArchiSteamFarm/Helpers/ArchiCryptoHelper.cs @@ -19,11 +19,13 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if NETFRAMEWORK +using System.Runtime.InteropServices; +#endif using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; -using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Text; using ArchiSteamFarm.Core; @@ -187,7 +189,11 @@ namespace ArchiSteamFarm.Helpers { throw new ArgumentNullException(nameof(encryptedString)); } +#if NETFRAMEWORK if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { +#else + if (!OperatingSystem.IsWindows()) { +#endif return null; } @@ -234,7 +240,11 @@ namespace ArchiSteamFarm.Helpers { throw new ArgumentNullException(nameof(decryptedString)); } +#if NETFRAMEWORK if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { +#else + if (!OperatingSystem.IsWindows()) { +#endif return null; } diff --git a/ArchiSteamFarm/Helpers/CrossProcessFileBasedSemaphore.cs b/ArchiSteamFarm/Helpers/CrossProcessFileBasedSemaphore.cs index bd43f4396..65d6e1f1b 100644 --- a/ArchiSteamFarm/Helpers/CrossProcessFileBasedSemaphore.cs +++ b/ArchiSteamFarm/Helpers/CrossProcessFileBasedSemaphore.cs @@ -19,10 +19,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if NETFRAMEWORK +using System.Runtime.InteropServices; +#endif using System; using System.Diagnostics; using System.IO; -using System.Runtime.InteropServices; using System.Security.AccessControl; using System.Threading; using System.Threading.Tasks; @@ -163,7 +165,11 @@ namespace ArchiSteamFarm.Helpers { if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); +#if NETFRAMEWORK if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { +#else + if (OperatingSystem.IsWindows()) { +#endif DirectoryInfo directoryInfo = new(directoryPath); try { @@ -177,7 +183,7 @@ namespace ArchiSteamFarm.Helpers { #if NETFRAMEWORK } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { #else - } else if (RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { + } else if (OperatingSystem.IsFreeBSD() || OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) { #endif OS.UnixSetFileAccess(directoryPath, OS.EUnixPermission.Combined777); } @@ -186,7 +192,11 @@ namespace ArchiSteamFarm.Helpers { try { new FileStream(FilePath, FileMode.CreateNew).Dispose(); +#if NETFRAMEWORK if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { +#else + if (OperatingSystem.IsWindows()) { +#endif FileInfo fileInfo = new(FilePath); try { @@ -200,7 +210,7 @@ namespace ArchiSteamFarm.Helpers { #if NETFRAMEWORK } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { #else - } else if (RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { + } else if (OperatingSystem.IsFreeBSD() || OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) { #endif OS.UnixSetFileAccess(FilePath, OS.EUnixPermission.Combined777); }