From 2af76f746d835763119a6804ab3ac4d501517a02 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Sun, 5 Feb 2017 13:52:00 +0100 Subject: [PATCH] Closes #457 --- ArchiSteamFarm/ArchiSteamFarm.csproj | 1 + ArchiSteamFarm/OS.cs | 69 ++++++++++++++++++++++++++++ ArchiSteamFarm/Program.cs | 3 +- GUI/GUI.csproj | 3 ++ 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 ArchiSteamFarm/OS.cs diff --git a/ArchiSteamFarm/ArchiSteamFarm.csproj b/ArchiSteamFarm/ArchiSteamFarm.csproj index 1c5801500..c8c077220 100644 --- a/ArchiSteamFarm/ArchiSteamFarm.csproj +++ b/ArchiSteamFarm/ArchiSteamFarm.csproj @@ -138,6 +138,7 @@ + diff --git a/ArchiSteamFarm/OS.cs b/ArchiSteamFarm/OS.cs new file mode 100644 index 000000000..9125810b5 --- /dev/null +++ b/ArchiSteamFarm/OS.cs @@ -0,0 +1,69 @@ +/* + _ _ _ ____ _ _____ + / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ + / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ + / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | +/_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| + + 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. + +*/ + +using System; +using System.Runtime.InteropServices; +using ArchiSteamFarm.Localization; + +namespace ArchiSteamFarm { + internal static class OS { + private static readonly PlatformID PlatformID = Environment.OSVersion.Platform; + + internal static void Init() { + switch (PlatformID) { + case PlatformID.Win32NT: + case PlatformID.Win32S: + case PlatformID.Win32Windows: + case PlatformID.WinCE: + KeepWindowsSystemActive(); + break; + } + } + + 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 + EExecutionState result = SetThreadExecutionState(EExecutionState.Continuous | EExecutionState.AwayModeRequired | EExecutionState.SystemRequired); + + // SetThreadExecutionState() returns NULL on failure, which is mapped to 0 (EExecutionState.Error) in our case + if (result == EExecutionState.Error) { + ASF.ArchiLogger.LogGenericError(string.Format(Strings.WarningFailedWithError, result)); + } + } + + [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] + private static extern EExecutionState SetThreadExecutionState(EExecutionState executionState); + + [Flags] + private enum EExecutionState : uint { + Error = 0, + SystemRequired = 0x00000001, +// DisplayRequired = 0x00000002, +// UserPresent = 0x00000004, + AwayModeRequired = 0x00000040, + Continuous = 0x80000000 + } + } +} \ No newline at end of file diff --git a/ArchiSteamFarm/Program.cs b/ArchiSteamFarm/Program.cs index 4c810cacb..4e1a7a186 100644 --- a/ArchiSteamFarm/Program.cs +++ b/ArchiSteamFarm/Program.cs @@ -266,8 +266,9 @@ namespace ArchiSteamFarm { } ArchiWebHandler.Init(); - WebBrowser.Init(); + OS.Init(); WCF.Init(); + WebBrowser.Init(); WebBrowser = new WebBrowser(ASF.ArchiLogger); } diff --git a/GUI/GUI.csproj b/GUI/GUI.csproj index e01b8c7c8..2fe895c74 100644 --- a/GUI/GUI.csproj +++ b/GUI/GUI.csproj @@ -148,6 +148,9 @@ MobileAuthenticator.cs + + OS.cs + Runtime.cs