2017-11-18 17:27:06 +01:00
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
//
// Copyright 2015-2017 Ł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.
2017-02-05 13:52:00 +01:00
using System ;
2017-08-02 20:47:53 +02:00
using System.IO ;
2017-09-26 05:59:41 +02:00
using System.Net.WebSockets ;
2017-02-05 13:52:00 +01:00
using System.Runtime.InteropServices ;
using ArchiSteamFarm.Localization ;
namespace ArchiSteamFarm {
internal static class OS {
2017-12-17 11:53:36 +01:00
internal static void Init ( bool service ) {
2017-07-13 06:08:52 +02:00
switch ( Environment . OSVersion . Platform ) {
2017-02-05 13:52:00 +01:00
case PlatformID . Win32NT :
case PlatformID . Win32S :
case PlatformID . Win32Windows :
case PlatformID . WinCE :
2017-03-05 17:41:57 +01:00
DisableQuickEditMode ( ) ;
2017-03-07 03:47:56 +01:00
2017-12-17 11:53:36 +01:00
if ( service ) {
2017-03-07 03:47:56 +01:00
KeepWindowsSystemActive ( ) ;
}
2017-02-05 13:52:00 +01:00
break ;
}
}
2017-09-26 05:59:41 +02:00
// TODO: We should really get rid of this once https://github.com/SteamRE/SteamKit/issues/455 or https://github.com/dotnet/corefx/issues/9503 is solved
internal static bool SupportsWebSockets ( ) {
try {
using ( new ClientWebSocket ( ) ) {
return true ;
}
} catch ( PlatformNotSupportedException ) {
return false ;
}
}
2017-08-02 20:47:53 +02:00
internal static void UnixSetFileAccessExecutable ( string path ) {
2017-09-24 13:05:53 +02:00
if ( string . IsNullOrEmpty ( path ) | | ! File . Exists ( path ) ) {
ASF . ArchiLogger . LogNullError ( nameof ( path ) ) ;
2017-08-02 20:47:53 +02:00
return ;
}
2017-08-31 07:30:09 +02:00
// Chmod() returns 0 on success, -1 on failure
if ( NativeMethods . Chmod ( path , ( int ) NativeMethods . UnixExecutePermission ) ! = 0 ) {
ASF . ArchiLogger . LogGenericError ( string . Format ( Strings . WarningFailedWithError , Marshal . GetLastWin32Error ( ) ) ) ;
2017-08-02 20:47:53 +02:00
}
}
2017-03-05 17:41:57 +01:00
private static void DisableQuickEditMode ( ) {
2017-09-24 13:05:53 +02:00
if ( Console . IsOutputRedirected ) {
return ;
}
2017-03-05 17:41:57 +01:00
// http://stackoverflow.com/questions/30418886/how-and-why-does-quickedit-mode-in-command-prompt-freeze-applications
IntPtr consoleHandle = NativeMethods . GetStdHandle ( NativeMethods . StandardInputHandle ) ;
2017-03-14 08:41:33 -03:00
if ( ! NativeMethods . GetConsoleMode ( consoleHandle , out uint consoleMode ) ) {
2017-03-05 17:41:57 +01:00
ASF . ArchiLogger . LogGenericError ( Strings . WarningFailed ) ;
return ;
}
consoleMode & = ~ NativeMethods . EnableQuickEditMode ;
if ( ! NativeMethods . SetConsoleMode ( consoleHandle , consoleMode ) ) {
ASF . ArchiLogger . LogGenericError ( Strings . WarningFailed ) ;
}
}
2017-02-05 13:52:00 +01:00
private static void KeepWindowsSystemActive ( ) {
// This function calls unmanaged API in order to tell Windows OS that it should not enter sleep state while the program is running
// If user wishes to enter sleep mode, then he should use ShutdownOnFarmingFinished or manage ASF process with third-party tool or script
// More info: https://msdn.microsoft.com/library/windows/desktop/aa373208(v=vs.85).aspx
2017-08-31 07:30:09 +02:00
NativeMethods . EExecutionState result = NativeMethods . SetThreadExecutionState ( NativeMethods . AwakeExecutionState ) ;
2017-02-05 13:52:00 +01:00
// SetThreadExecutionState() returns NULL on failure, which is mapped to 0 (EExecutionState.Error) in our case
2017-02-27 20:05:02 +01:00
if ( result = = NativeMethods . EExecutionState . Error ) {
2017-02-05 13:52:00 +01:00
ASF . ArchiLogger . LogGenericError ( string . Format ( Strings . WarningFailedWithError , result ) ) ;
}
}
2017-02-27 20:05:02 +01:00
private static class NativeMethods {
2017-08-31 07:30:09 +02:00
internal const EExecutionState AwakeExecutionState = EExecutionState . SystemRequired | EExecutionState . AwayModeRequired | EExecutionState . Continuous ;
2017-03-05 17:41:57 +01:00
internal const uint EnableQuickEditMode = 0x0040 ;
2017-07-13 06:08:52 +02:00
internal const sbyte StandardInputHandle = - 10 ;
2017-08-31 07:30:09 +02:00
internal const EUnixPermission UnixExecutePermission = EUnixPermission . UserRead | EUnixPermission . UserWrite | EUnixPermission . UserExecute | EUnixPermission . GroupRead | EUnixPermission . GroupExecute | EUnixPermission . OtherRead | EUnixPermission . OtherExecute ;
2017-03-05 17:41:57 +01:00
2017-08-31 07:30:09 +02:00
[DllImport("libc", EntryPoint = "chmod", SetLastError = true)]
internal static extern int Chmod ( string path , int mode ) ;
[DllImport("kernel32.dll")]
2017-03-05 17:41:57 +01:00
internal static extern bool GetConsoleMode ( IntPtr hConsoleHandle , out uint lpMode ) ;
2017-08-31 07:30:09 +02:00
[DllImport("kernel32.dll")]
2017-03-05 17:41:57 +01:00
internal static extern IntPtr GetStdHandle ( int nStdHandle ) ;
2017-08-31 07:30:09 +02:00
[DllImport("kernel32.dll")]
2017-03-05 17:41:57 +01:00
internal static extern bool SetConsoleMode ( IntPtr hConsoleHandle , uint dwMode ) ;
2017-08-31 07:30:09 +02:00
[DllImport("kernel32.dll")]
2017-02-27 20:05:02 +01:00
internal static extern EExecutionState SetThreadExecutionState ( EExecutionState executionState ) ;
2017-02-05 13:52:00 +01:00
2017-02-27 20:05:02 +01:00
[Flags]
internal enum EExecutionState : uint {
Error = 0 ,
SystemRequired = 0x00000001 ,
AwayModeRequired = 0x00000040 ,
Continuous = 0x80000000
}
2017-08-31 07:30:09 +02:00
[Flags]
internal enum EUnixPermission {
OtherExecute = 0x1 ,
OtherRead = 0x4 ,
GroupExecute = 0x8 ,
GroupRead = 0x20 ,
UserExecute = 0x40 ,
UserWrite = 0x80 ,
UserRead = 0x100
/ *
OtherWrite = 0x2
GroupWrite = 0x10
* /
}
2017-02-05 13:52:00 +01:00
}
}
}