From bb513921f13be8ef20763ca54f92293627c7dd17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Domeradzki?= Date: Fri, 13 Aug 2021 23:07:17 +0200 Subject: [PATCH] Include ASF's windows-only parts only in generic and windows builds (#2404) * Include ASF's windows-only parts only in generic and windows builds * Apply Abry's note --- ArchiSteamFarm/ArchiSteamFarm.csproj | 2 +- ArchiSteamFarm/Core/AprilFools.cs | 2 ++ ArchiSteamFarm/Helpers/ArchiCryptoHelper.cs | 8 ++++++++ .../Helpers/CrossProcessFileBasedSemaphore.cs | 20 ++++++++++++++++--- ArchiSteamFarm/Program.cs | 6 +++++- Directory.Build.props | 10 ++++++++++ Directory.Packages.props | 2 +- 7 files changed, 44 insertions(+), 6 deletions(-) diff --git a/ArchiSteamFarm/ArchiSteamFarm.csproj b/ArchiSteamFarm/ArchiSteamFarm.csproj index fb9ca8e65..3a1bd59d4 100644 --- a/ArchiSteamFarm/ArchiSteamFarm.csproj +++ b/ArchiSteamFarm/ArchiSteamFarm.csproj @@ -29,7 +29,7 @@ - + diff --git a/ArchiSteamFarm/Core/AprilFools.cs b/ArchiSteamFarm/Core/AprilFools.cs index 60f04f1a7..8ae697cd3 100644 --- a/ArchiSteamFarm/Core/AprilFools.cs +++ b/ArchiSteamFarm/Core/AprilFools.cs @@ -74,10 +74,12 @@ namespace ArchiSteamFarm.Core { } } +#if TARGET_GENERIC || TARGET_WINDOWS internal static void OnTimeChanged(object? sender, EventArgs e) { lock (LockObject) { Timer.Change(TimeSpan.Zero, Timeout.InfiniteTimeSpan); } } +#endif } } diff --git a/ArchiSteamFarm/Helpers/ArchiCryptoHelper.cs b/ArchiSteamFarm/Helpers/ArchiCryptoHelper.cs index bfb1ada89..8ecfe264b 100644 --- a/ArchiSteamFarm/Helpers/ArchiCryptoHelper.cs +++ b/ArchiSteamFarm/Helpers/ArchiCryptoHelper.cs @@ -193,6 +193,7 @@ namespace ArchiSteamFarm.Helpers { return null; } +#if TARGET_GENERIC || TARGET_WINDOWS try { byte[] decryptedData = ProtectedData.Unprotect( Convert.FromBase64String(encryptedString), @@ -206,6 +207,9 @@ namespace ArchiSteamFarm.Helpers { return null; } +#else + throw new InvalidOperationException(); +#endif } private static string? EncryptAES(string decryptedString) { @@ -240,6 +244,7 @@ namespace ArchiSteamFarm.Helpers { return null; } +#if TARGET_GENERIC || TARGET_WINDOWS try { byte[] encryptedData = ProtectedData.Protect( Encoding.UTF8.GetBytes(decryptedString), @@ -253,6 +258,9 @@ namespace ArchiSteamFarm.Helpers { return null; } +#else + throw new InvalidOperationException(); +#endif } public enum ECryptoMethod : byte { diff --git a/ArchiSteamFarm/Helpers/CrossProcessFileBasedSemaphore.cs b/ArchiSteamFarm/Helpers/CrossProcessFileBasedSemaphore.cs index 2b4b07fb2..99a85dde7 100644 --- a/ArchiSteamFarm/Helpers/CrossProcessFileBasedSemaphore.cs +++ b/ArchiSteamFarm/Helpers/CrossProcessFileBasedSemaphore.cs @@ -22,10 +22,12 @@ #if NETFRAMEWORK using OperatingSystem = JustArchiNET.Madness.OperatingSystemMadness.OperatingSystem; #endif +#if TARGET_GENERIC || TARGET_WINDOWS +using System.Security.AccessControl; +#endif using System; using System.Diagnostics; using System.IO; -using System.Security.AccessControl; using System.Threading; using System.Threading.Tasks; using ArchiSteamFarm.Core; @@ -165,6 +167,7 @@ namespace ArchiSteamFarm.Helpers { if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); +#if TARGET_GENERIC || TARGET_WINDOWS if (OperatingSystem.IsWindows()) { DirectoryInfo directoryInfo = new(directoryPath); @@ -176,14 +179,20 @@ namespace ArchiSteamFarm.Helpers { // Non-critical, user might have no rights to manage the resource ASF.ArchiLogger.LogGenericDebuggingException(e); } - } else if (OperatingSystem.IsFreeBSD() || OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) { + } +#endif + +#if TARGET_GENERIC || !TARGET_WINDOWS + if (OperatingSystem.IsFreeBSD() || OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) { OS.UnixSetFileAccess(directoryPath, OS.EUnixPermission.Combined777); } +#endif } try { new FileStream(FilePath, FileMode.CreateNew).Dispose(); +#if TARGET_GENERIC || TARGET_WINDOWS if (OperatingSystem.IsWindows()) { FileInfo fileInfo = new(FilePath); @@ -195,9 +204,14 @@ namespace ArchiSteamFarm.Helpers { // Non-critical, user might have no rights to manage the resource ASF.ArchiLogger.LogGenericDebuggingException(e); } - } else if (OperatingSystem.IsFreeBSD() || OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) { + } +#endif + +#if TARGET_GENERIC || !TARGET_WINDOWS + if (OperatingSystem.IsFreeBSD() || OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) { OS.UnixSetFileAccess(FilePath, OS.EUnixPermission.Combined777); } +#endif } catch (IOException) { // Ignored, if the file was already created in the meantime by another instance, this is fine } diff --git a/ArchiSteamFarm/Program.cs b/ArchiSteamFarm/Program.cs index d7a265a74..b9c703916 100644 --- a/ArchiSteamFarm/Program.cs +++ b/ArchiSteamFarm/Program.cs @@ -22,6 +22,9 @@ #if NETFRAMEWORK using OperatingSystem = JustArchiNET.Madness.OperatingSystemMadness.OperatingSystem; #endif +#if TARGET_GENERIC || TARGET_WINDOWS +using Microsoft.Win32; +#endif using System; using System.Collections; using System.Collections.Generic; @@ -42,7 +45,6 @@ using ArchiSteamFarm.NLog.Targets; using ArchiSteamFarm.Steam; using ArchiSteamFarm.Storage; using ArchiSteamFarm.Web; -using Microsoft.Win32; using Newtonsoft.Json; using NLog; using NLog.Targets; @@ -274,9 +276,11 @@ namespace ArchiSteamFarm { // April Fools easter egg logic AprilFools.Init(); +#if TARGET_GENERIC || TARGET_WINDOWS if (OperatingSystem.IsWindows()) { SystemEvents.TimeChanged += AprilFools.OnTimeChanged; } +#endif } if (!string.IsNullOrEmpty(latestJson)) { diff --git a/Directory.Build.props b/Directory.Build.props index fb59c950b..b352b0025 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -34,6 +34,16 @@ $(DefineConstants);ASF_VARIANT_$(ASFVariant.Replace('-', '_').ToUpperInvariant()) + + $(DefineConstants);TARGET_GENERIC + true + + + + $(DefineConstants);TARGET_WINDOWS + true + + ../resources/ArchiSteamFarm.snk.pub diff --git a/Directory.Packages.props b/Directory.Packages.props index adc945742..68f22527b 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -22,7 +22,7 @@ - +