From 5a267eb225015059646abfe7a97c0d53a75c0238 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Wed, 14 Sep 2016 19:00:03 +0200 Subject: [PATCH] Fix logging in when steam API is unavailable Or at least part of it... --- ArchiSteamFarm/Bot.cs | 4 ++-- ArchiSteamFarm/MobileAuthenticator.cs | 32 ++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 972a018a7..b6f99b8bb 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -1596,7 +1596,7 @@ namespace ArchiSteamFarm { ArchiHandler.PlayGames(BotConfig.GamesPlayedWhileIdle, BotConfig.CustomGamePlayedWhileIdle); } - private async void OnConnected(SteamClient.ConnectedCallback callback) { + private void OnConnected(SteamClient.ConnectedCallback callback) { if (callback == null) { Logging.LogNullError(nameof(callback), BotName); return; @@ -1636,7 +1636,7 @@ namespace ArchiSteamFarm { // If we have ASF 2FA enabled, we can always provide TwoFactorCode, and save a request if (BotDatabase.MobileAuthenticator != null) { - TwoFactorCode = await BotDatabase.MobileAuthenticator.GenerateToken().ConfigureAwait(false); + TwoFactorCode = BotDatabase.MobileAuthenticator.GenerateTokenImmediately(); } SteamUser.LogOnDetails logOnDetails = new SteamUser.LogOnDetails { diff --git a/ArchiSteamFarm/MobileAuthenticator.cs b/ArchiSteamFarm/MobileAuthenticator.cs index 576f0c366..d7b06a052 100644 --- a/ArchiSteamFarm/MobileAuthenticator.cs +++ b/ArchiSteamFarm/MobileAuthenticator.cs @@ -195,6 +195,16 @@ namespace ArchiSteamFarm { return null; } + internal string GenerateTokenImmediately() { + uint time = GetSteamTimeImmediately(); + if (time != 0) { + return GenerateTokenForTime(time); + } + + Logging.LogNullError(nameof(time), Bot.BotName); + return null; + } + internal async Task> GetConfirmations() { if (!HasCorrectDeviceID) { Logging.LogGenericWarning("Can't execute properly due to invalid DeviceID!", Bot.BotName); @@ -274,16 +284,32 @@ namespace ArchiSteamFarm { return (uint) (Utilities.GetUnixTime() + SteamTimeDifference.GetValueOrDefault()); } - await TimeSemaphore.WaitAsync().ConfigureAwait(false); + if (!await TimeSemaphore.WaitAsync(0).ConfigureAwait(false)) { + return (uint) (Utilities.GetUnixTime() + SteamTimeDifference.GetValueOrDefault()); + } + + try { + if (SteamTimeDifference.HasValue) { + return (uint) (Utilities.GetUnixTime() + SteamTimeDifference.GetValueOrDefault()); + } - if (!SteamTimeDifference.HasValue) { uint serverTime = Bot.ArchiWebHandler.GetServerTime(); if (serverTime != 0) { SteamTimeDifference = (short) (serverTime - Utilities.GetUnixTime()); } + + return (uint) (Utilities.GetUnixTime() + SteamTimeDifference.GetValueOrDefault()); + } finally { + TimeSemaphore.Release(); + } + } + + private uint GetSteamTimeImmediately() { + if (!SteamTimeDifference.HasValue) { + // We must return time immediately, schedule update in the background and proceed + GetSteamTime().Forget(); } - TimeSemaphore.Release(); return (uint) (Utilities.GetUnixTime() + SteamTimeDifference.GetValueOrDefault()); }