diff --git a/ArchiSteamFarm/Core/NativeMethods.cs b/ArchiSteamFarm/Core/NativeMethods.cs new file mode 100644 index 000000000..2912b4645 --- /dev/null +++ b/ArchiSteamFarm/Core/NativeMethods.cs @@ -0,0 +1,84 @@ +// _ _ _ ____ _ _____ +// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ +// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ +// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | +// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| +// | +// Copyright 2015-2022 Ɓukasz "JustArchi" Domeradzki +// Contact: JustArchi@JustArchi.net +// | +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// | +// http://www.apache.org/licenses/LICENSE-2.0 +// | +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Runtime.InteropServices; +using System.Runtime.Versioning; + +namespace ArchiSteamFarm.Core; + +internal static partial class NativeMethods { + [SupportedOSPlatform("Windows")] + internal const EExecutionState AwakeExecutionState = EExecutionState.SystemRequired | EExecutionState.AwayModeRequired | EExecutionState.Continuous; + + [SupportedOSPlatform("Windows")] + internal const uint EnableQuickEditMode = 0x0040; + + [SupportedOSPlatform("Windows")] + internal const sbyte StandardInputHandle = -10; + +#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)] + [LibraryImport("libc", EntryPoint = "chmod", SetLastError = true, StringMarshalling = StringMarshalling.Utf8)] + [SupportedOSPlatform("FreeBSD")] + [SupportedOSPlatform("Linux")] + [SupportedOSPlatform("MacOS")] + internal static partial 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 + + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] + [LibraryImport("kernel32.dll")] + [SupportedOSPlatform("Windows")] + [return: MarshalAs(UnmanagedType.Bool)] + internal static partial bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode); + + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] + [LibraryImport("libc", EntryPoint = "geteuid", SetLastError = true)] + [SupportedOSPlatform("FreeBSD")] + [SupportedOSPlatform("Linux")] + [SupportedOSPlatform("MacOS")] + internal static partial uint GetEuid(); + + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] + [LibraryImport("kernel32.dll")] + [SupportedOSPlatform("Windows")] + internal static partial IntPtr GetStdHandle(int nStdHandle); + + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] + [LibraryImport("kernel32.dll")] + [SupportedOSPlatform("Windows")] + [return: MarshalAs(UnmanagedType.Bool)] + internal static partial bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode); + + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] + [LibraryImport("kernel32.dll")] + [SupportedOSPlatform("Windows")] + internal static partial EExecutionState SetThreadExecutionState(EExecutionState executionState); + + [Flags] + [SupportedOSPlatform("Windows")] + internal enum EExecutionState : uint { + None = 0, + SystemRequired = 0x00000001, + AwayModeRequired = 0x00000040, + Continuous = 0x80000000 + } +} diff --git a/ArchiSteamFarm/Core/OS.cs b/ArchiSteamFarm/Core/OS.cs index 63721ca56..2f9378eb1 100644 --- a/ArchiSteamFarm/Core/OS.cs +++ b/ArchiSteamFarm/Core/OS.cs @@ -148,7 +148,7 @@ internal static class OS { } if (OperatingSystem.IsFreeBSD() || OperatingSystem.IsLinux() || OperatingSystem.IsMacOS()) { - return NativeMethods.GetEUID() == 0; + return NativeMethods.GetEuid() == 0; } // We can't determine whether user is running as root or not, so fallback to that not happening @@ -344,60 +344,4 @@ internal static class OS { UserRead = 0x100, Combined777 = UserRead | UserWrite | UserExecute | GroupRead | GroupWrite | GroupExecute | OtherRead | OtherWrite | OtherExecute } - - private static class NativeMethods { - [SupportedOSPlatform("Windows")] - internal const EExecutionState AwakeExecutionState = EExecutionState.SystemRequired | EExecutionState.AwayModeRequired | EExecutionState.Continuous; - - [SupportedOSPlatform("Windows")] - internal const uint EnableQuickEditMode = 0x0040; - - [SupportedOSPlatform("Windows")] - internal const sbyte StandardInputHandle = -10; - -#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)] - [SupportedOSPlatform("FreeBSD")] - [SupportedOSPlatform("Linux")] - [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 - - [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] - [DllImport("kernel32.dll")] - [SupportedOSPlatform("Windows")] - internal static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode); - - [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] - [DllImport("libc", EntryPoint = "geteuid", SetLastError = true)] - [SupportedOSPlatform("FreeBSD")] - [SupportedOSPlatform("Linux")] - [SupportedOSPlatform("MacOS")] - internal static extern uint GetEUID(); - - [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] - [DllImport("kernel32.dll")] - [SupportedOSPlatform("Windows")] - internal static extern IntPtr GetStdHandle(int nStdHandle); - - [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] - [DllImport("kernel32.dll")] - [SupportedOSPlatform("Windows")] - internal static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode); - - [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] - [DllImport("kernel32.dll")] - [SupportedOSPlatform("Windows")] - internal static extern EExecutionState SetThreadExecutionState(EExecutionState executionState); - - [Flags] - [SupportedOSPlatform("Windows")] - internal enum EExecutionState : uint { - None = 0, - SystemRequired = 0x00000001, - AwayModeRequired = 0x00000040, - Continuous = 0x80000000 - } - } } diff --git a/Directory.Build.props b/Directory.Build.props index 8db77d912..6e712b830 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -4,6 +4,7 @@ + true AllEnabledByDefault ../resources/ASF.ico JustArchi @@ -70,7 +71,7 @@ none true - CS8002,IL2026,IL2057,IL2072,IL2075,IL2104,SYSLIB1045,SYSLIB1054 + CS8002,IL2026,IL2057,IL2072,IL2075,IL2104,SYSLIB1045