From 0964cdac96e2c6ef3d0bcde10283f10685289243 Mon Sep 17 00:00:00 2001 From: Archi Date: Wed, 10 Nov 2021 19:03:05 +0100 Subject: [PATCH] Minimize define hell Skipping a 20 KB stub in OS-specific non-windows builds and omitting a few very fast if checks isn't worth the code quality degradation that involves all of the ifdef options. ifdefs should be reserved for stuff that either doesn't compile whatsoever in some specific configurations (NETFRAMEWORK), or is required to make logical decisions based on the compiler input (e.g. DEBUG for detecting debugging builds or ASF_VARIANT_* for hardcoding the platform identifier to use for auto-updates) In all other situations, we should use OperatingSystem if condition, even if it's equal to hitting them on the platforms that are unlikely to hit them. And I say unlikely, because nothing stops me from downloading a win-x64 build and running it like a generic one on windows, what you gonna do? --- ArchiSteamFarm/ArchiSteamFarm.csproj | 2 +- ArchiSteamFarm/Core/ASF.cs | 2 -- ArchiSteamFarm/Core/OS.cs | 24 ------------------- ArchiSteamFarm/Helpers/ArchiCryptoHelper.cs | 8 ------- .../Helpers/CrossProcessFileBasedSemaphore.cs | 20 +++------------- ArchiSteamFarm/IPC/WebUtilities.cs | 1 + ArchiSteamFarm/Program.cs | 10 ++++---- Directory.Build.props | 10 -------- Directory.Packages.props | 2 +- 9 files changed, 11 insertions(+), 68 deletions(-) diff --git a/ArchiSteamFarm/ArchiSteamFarm.csproj b/ArchiSteamFarm/ArchiSteamFarm.csproj index 6b8fbf9b6..feb1fb306 100644 --- a/ArchiSteamFarm/ArchiSteamFarm.csproj +++ b/ArchiSteamFarm/ArchiSteamFarm.csproj @@ -25,7 +25,7 @@ - + diff --git a/ArchiSteamFarm/Core/ASF.cs b/ArchiSteamFarm/Core/ASF.cs index 4cdcce963..059abc281 100644 --- a/ArchiSteamFarm/Core/ASF.cs +++ b/ArchiSteamFarm/Core/ASF.cs @@ -308,7 +308,6 @@ namespace ArchiSteamFarm.Core { return null; } -#if TARGET_GENERIC || !TARGET_WINDOWS if (OperatingSystem.IsFreeBSD() || OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) { string executable = Path.Combine(SharedInfo.HomeDirectory, SharedInfo.AssemblyName); @@ -316,7 +315,6 @@ namespace ArchiSteamFarm.Core { OS.UnixSetFileAccess(executable, OS.EUnixPermission.Combined755); } } -#endif ArchiLogger.LogGenericInfo(Strings.UpdateFinished); diff --git a/ArchiSteamFarm/Core/OS.cs b/ArchiSteamFarm/Core/OS.cs index daf93aace..74cc466ef 100644 --- a/ArchiSteamFarm/Core/OS.cs +++ b/ArchiSteamFarm/Core/OS.cs @@ -90,7 +90,6 @@ namespace ArchiSteamFarm.Core { private static string? BackingVersion; private static Mutex? SingleInstance; -#if TARGET_GENERIC || TARGET_WINDOWS internal static void CoreInit(bool systemRequired) { if (OperatingSystem.IsWindows()) { if (systemRequired) { @@ -111,9 +110,6 @@ namespace ArchiSteamFarm.Core { } } } -#else - internal static void CoreInit(bool _) { } -#endif internal static string GetOsResourceName(string objectName) { if (string.IsNullOrEmpty(objectName)) { @@ -143,19 +139,15 @@ namespace ArchiSteamFarm.Core { } internal static bool IsRunningAsRoot() { -#if TARGET_GENERIC || TARGET_WINDOWS if (OperatingSystem.IsWindows()) { using WindowsIdentity identity = WindowsIdentity.GetCurrent(); return new WindowsPrincipal(identity).IsInRole(WindowsBuiltInRole.Administrator); } -#endif -#if TARGET_GENERIC || !TARGET_WINDOWS if (OperatingSystem.IsFreeBSD() || OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) { return NativeMethods.GetEUID() == 0; } -#endif // We can't determine whether user is running as root or not, so fallback to that not happening return false; @@ -201,7 +193,6 @@ namespace ArchiSteamFarm.Core { return true; } -#if TARGET_GENERIC || !TARGET_WINDOWS [SupportedOSPlatform("FreeBSD")] [SupportedOSPlatform("Linux")] [SupportedOSPlatform("MacOS")] @@ -225,7 +216,6 @@ namespace ArchiSteamFarm.Core { ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, Marshal.GetLastWin32Error())); } } -#endif internal static void UnregisterProcess() { if (SingleInstance == null) { @@ -269,7 +259,6 @@ namespace ArchiSteamFarm.Core { #endif } -#if TARGET_GENERIC || TARGET_WINDOWS [SupportedOSPlatform("Windows")] private static void WindowsDisableQuickEditMode() { if (!OperatingSystem.IsWindows()) { @@ -307,9 +296,7 @@ namespace ArchiSteamFarm.Core { ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.WarningFailedWithError, result)); } } -#endif -#if TARGET_GENERIC || !TARGET_WINDOWS [Flags] [SupportedOSPlatform("FreeBSD")] [SupportedOSPlatform("Linux")] @@ -327,10 +314,8 @@ namespace ArchiSteamFarm.Core { Combined755 = UserRead | UserWrite | UserExecute | GroupRead | GroupExecute | OtherRead | OtherExecute, Combined777 = UserRead | UserWrite | UserExecute | GroupRead | GroupWrite | GroupExecute | OtherRead | OtherWrite | OtherExecute } -#endif private static class NativeMethods { -#if TARGET_GENERIC || TARGET_WINDOWS [SupportedOSPlatform("Windows")] internal const EExecutionState AwakeExecutionState = EExecutionState.SystemRequired | EExecutionState.AwayModeRequired | EExecutionState.Continuous; @@ -339,9 +324,7 @@ namespace ArchiSteamFarm.Core { [SupportedOSPlatform("Windows")] internal const sbyte StandardInputHandle = -10; -#endif -#if TARGET_GENERIC || !TARGET_WINDOWS #pragma warning disable CA2101 // False positive, we can't use unicode charset on Unix, and it uses UTF-8 by default anyway [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [DllImport("libc", EntryPoint = "chmod", SetLastError = true)] @@ -350,25 +333,19 @@ namespace ArchiSteamFarm.Core { [SupportedOSPlatform("MacOS")] internal static extern int Chmod(string path, int mode); #pragma warning restore CA2101 // False positive, we can't use unicode charset on Unix, and it uses UTF-8 by default anyway -#endif -#if TARGET_GENERIC || TARGET_WINDOWS [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [DllImport("kernel32.dll")] [SupportedOSPlatform("Windows")] internal static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode); -#endif -#if TARGET_GENERIC || !TARGET_WINDOWS [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [DllImport("libc", EntryPoint = "geteuid", SetLastError = true)] [SupportedOSPlatform("FreeBSD")] [SupportedOSPlatform("Linux")] [SupportedOSPlatform("MacOS")] internal static extern uint GetEUID(); -#endif -#if TARGET_GENERIC || TARGET_WINDOWS [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] [DllImport("kernel32.dll")] [SupportedOSPlatform("Windows")] @@ -392,7 +369,6 @@ namespace ArchiSteamFarm.Core { AwayModeRequired = 0x00000040, Continuous = 0x80000000 } -#endif } } } diff --git a/ArchiSteamFarm/Helpers/ArchiCryptoHelper.cs b/ArchiSteamFarm/Helpers/ArchiCryptoHelper.cs index 1473f93ca..3b6d7b932 100644 --- a/ArchiSteamFarm/Helpers/ArchiCryptoHelper.cs +++ b/ArchiSteamFarm/Helpers/ArchiCryptoHelper.cs @@ -217,7 +217,6 @@ namespace ArchiSteamFarm.Helpers { throw new ArgumentNullException(nameof(encryptedString)); } -#if TARGET_GENERIC || TARGET_WINDOWS if (!OperatingSystem.IsWindows()) { return null; } @@ -235,9 +234,6 @@ namespace ArchiSteamFarm.Helpers { return null; } -#else - return null; -#endif } private static string? EncryptAES(string decryptedString) { @@ -268,7 +264,6 @@ namespace ArchiSteamFarm.Helpers { throw new ArgumentNullException(nameof(decryptedString)); } -#if TARGET_GENERIC || TARGET_WINDOWS if (!OperatingSystem.IsWindows()) { return null; } @@ -286,9 +281,6 @@ namespace ArchiSteamFarm.Helpers { return null; } -#else - return null; -#endif } public enum ECryptoMethod : byte { diff --git a/ArchiSteamFarm/Helpers/CrossProcessFileBasedSemaphore.cs b/ArchiSteamFarm/Helpers/CrossProcessFileBasedSemaphore.cs index 4f4783641..f6788be04 100644 --- a/ArchiSteamFarm/Helpers/CrossProcessFileBasedSemaphore.cs +++ b/ArchiSteamFarm/Helpers/CrossProcessFileBasedSemaphore.cs @@ -19,12 +19,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -#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; @@ -167,7 +165,6 @@ namespace ArchiSteamFarm.Helpers { if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); -#if TARGET_GENERIC || TARGET_WINDOWS if (OperatingSystem.IsWindows()) { DirectoryInfo directoryInfo = new(directoryPath); @@ -179,20 +176,14 @@ namespace ArchiSteamFarm.Helpers { // Non-critical, user might have no rights to manage the resource ASF.ArchiLogger.LogGenericDebuggingException(e); } - } -#endif - -#if TARGET_GENERIC || !TARGET_WINDOWS - if (OperatingSystem.IsFreeBSD() || OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) { + } else 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); @@ -204,14 +195,9 @@ namespace ArchiSteamFarm.Helpers { // Non-critical, user might have no rights to manage the resource ASF.ArchiLogger.LogGenericDebuggingException(e); } - } -#endif - -#if TARGET_GENERIC || !TARGET_WINDOWS - if (OperatingSystem.IsFreeBSD() || OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) { + } else 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/IPC/WebUtilities.cs b/ArchiSteamFarm/IPC/WebUtilities.cs index e76244605..c33af3c83 100644 --- a/ArchiSteamFarm/IPC/WebUtilities.cs +++ b/ArchiSteamFarm/IPC/WebUtilities.cs @@ -50,6 +50,7 @@ namespace ArchiSteamFarm.IPC { return services.Configure(action); } #endif + internal static string? GetUnifiedName(this Type type) { if (type == null) { throw new ArgumentNullException(nameof(type)); diff --git a/ArchiSteamFarm/Program.cs b/ArchiSteamFarm/Program.cs index cf9a7e295..b3627dd51 100644 --- a/ArchiSteamFarm/Program.cs +++ b/ArchiSteamFarm/Program.cs @@ -54,13 +54,13 @@ namespace ArchiSteamFarm { internal static bool Service { get; private set; } internal static bool ShutdownSequenceInitialized { get; private set; } -#if !NETFRAMEWORK && (TARGET_GENERIC || !TARGET_WINDOWS) +#if !NETFRAMEWORK private static readonly Dictionary RegisteredPosixSignals = new(); #endif private static readonly TaskCompletionSource ShutdownResetEvent = new(); -#if !NETFRAMEWORK && (TARGET_GENERIC || !TARGET_WINDOWS) +#if !NETFRAMEWORK private static readonly ImmutableHashSet SupportedPosixSignals = ImmutableHashSet.Create(PosixSignal.SIGINT, PosixSignal.SIGTERM); #endif @@ -135,7 +135,7 @@ namespace ArchiSteamFarm { AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; TaskScheduler.UnobservedTaskException += OnUnobservedTaskException; -#if !NETFRAMEWORK && (TARGET_GENERIC || !TARGET_WINDOWS) +#if !NETFRAMEWORK if (OperatingSystem.IsFreeBSD() || OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) { foreach (PosixSignal signal in SupportedPosixSignals) { RegisteredPosixSignals[signal] = PosixSignalRegistration.Create(signal, OnPosixSignal); @@ -382,7 +382,7 @@ namespace ArchiSteamFarm { ShutdownSequenceInitialized = true; -#if !NETFRAMEWORK && (TARGET_GENERIC || !TARGET_WINDOWS) +#if !NETFRAMEWORK if (OperatingSystem.IsFreeBSD() || OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) { // Unregister from registered signals foreach (PosixSignalRegistration registration in RegisteredPosixSignals.Values) { @@ -429,7 +429,7 @@ namespace ArchiSteamFarm { private static async void OnCancelKeyPress(object? sender, ConsoleCancelEventArgs e) => await Exit(130).ConfigureAwait(false); -#if !NETFRAMEWORK && (TARGET_GENERIC || !TARGET_WINDOWS) +#if !NETFRAMEWORK private static async void OnPosixSignal(PosixSignalContext signal) { if (signal == null) { throw new ArgumentNullException(nameof(signal)); diff --git a/Directory.Build.props b/Directory.Build.props index 9848dc954..122539b26 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -44,16 +44,6 @@ $(DefineConstants);ASF_VARIANT_$(ASFVariant.Replace('-', '_').ToUpperInvariant()) - - $(DefineConstants);TARGET_GENERIC - true - - - - $(DefineConstants);TARGET_WINDOWS - true - - AllDisabledByDefault diff --git a/Directory.Packages.props b/Directory.Packages.props index 335a9cf44..c92f5e9b9 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -22,7 +22,7 @@ - +