diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 744866544..c03dc6817 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -104,6 +104,7 @@ namespace ArchiSteamFarm { private string AuthCode; private Timer ConnectionFailureTimer; + private string DeviceID; private Timer FamilySharingInactivityTimer; private bool FirstTradeSent; private byte HeartBeatFailures; @@ -618,6 +619,12 @@ namespace ArchiSteamFarm { return await ResponseAddLicense(steamID, args[1]).ConfigureAwait(false); case "!FARM": return await ResponseFarm(steamID, args[1]).ConfigureAwait(false); + case "!INPUT": + if (args.Length > 3) { + return await ResponseInput(steamID, args[1], args[2], args[3]).ConfigureAwait(false); + } + + return args.Length == 3 ? ResponseInput(steamID, args[1], args[2]) : ResponseUnknown(steamID); case "!LOOT": return await ResponseLoot(steamID, args[1]).ConfigureAwait(false); case "!LOOT^": @@ -663,16 +670,6 @@ namespace ArchiSteamFarm { } return await ResponseRedeem(steamID, args[1], ERedeemFlags.ForceForwarding | ERedeemFlags.SkipInitial).ConfigureAwait(false); - case "!INPUT": - if (args.Length > 3) { - return ResponseInput(steamID, args[1], args[2], args[3]); - } - - if (args.Length == 3) { - return ResponseInput(steamID, BotName, args[1], args[2]); - } - - return ResponseUnknown(steamID); case "!RESUME": return await ResponseResume(steamID, args[1]).ConfigureAwait(false); case "!START": @@ -941,13 +938,17 @@ namespace ArchiSteamFarm { if (!BotDatabase.MobileAuthenticator.HasCorrectDeviceID) { ArchiLogger.LogGenericWarning(Strings.BotAuthenticatorInvalidDeviceID); - string deviceID = Program.GetUserInput(ASF.EUserInputType.DeviceID, BotName); - if (string.IsNullOrEmpty(deviceID)) { - BotDatabase.MobileAuthenticator = null; - return; + if (string.IsNullOrEmpty(DeviceID)) { + string deviceID = Program.GetUserInput(ASF.EUserInputType.DeviceID, BotName); + if (string.IsNullOrEmpty(deviceID)) { + BotDatabase.MobileAuthenticator = null; + return; + } + + SetUserInput(ASF.EUserInputType.DeviceID, deviceID); } - BotDatabase.MobileAuthenticator.CorrectDeviceID(deviceID); + BotDatabase.MobileAuthenticator.CorrectDeviceID(DeviceID); BotDatabase.Save(); } @@ -978,18 +979,25 @@ namespace ArchiSteamFarm { private bool InitLoginAndPassword(bool requiresPassword) { if (string.IsNullOrEmpty(BotConfig.SteamLogin)) { - BotConfig.SteamLogin = Program.GetUserInput(ASF.EUserInputType.Login, BotName); - if (string.IsNullOrEmpty(BotConfig.SteamLogin)) { + string steamLogin = Program.GetUserInput(ASF.EUserInputType.Login, BotName); + if (string.IsNullOrEmpty(steamLogin)) { return false; } + + SetUserInput(ASF.EUserInputType.Login, steamLogin); } if (!string.IsNullOrEmpty(BotConfig.SteamPassword) || (!requiresPassword && !string.IsNullOrEmpty(BotDatabase.LoginKey))) { return true; } - BotConfig.SteamPassword = Program.GetUserInput(ASF.EUserInputType.Password, BotName); - return !string.IsNullOrEmpty(BotConfig.SteamPassword); + string steamPassword = Program.GetUserInput(ASF.EUserInputType.Password, BotName); + if (string.IsNullOrEmpty(steamPassword)) { + return false; + } + + SetUserInput(ASF.EUserInputType.Password, steamPassword); + return true; } private void InitModules() { @@ -1456,18 +1464,21 @@ namespace ArchiSteamFarm { switch (callback.Result) { case EResult.AccountLogonDenied: - AuthCode = Program.GetUserInput(ASF.EUserInputType.SteamGuard, BotName); - if (string.IsNullOrEmpty(AuthCode)) { + string authCode = Program.GetUserInput(ASF.EUserInputType.SteamGuard, BotName); + if (string.IsNullOrEmpty(authCode)) { Stop(); } + SetUserInput(ASF.EUserInputType.SteamGuard, authCode); break; case EResult.AccountLoginDeniedNeedTwoFactor: if (!HasMobileAuthenticator) { - TwoFactorCode = Program.GetUserInput(ASF.EUserInputType.TwoFactorAuthentication, BotName); - if (string.IsNullOrEmpty(TwoFactorCode)) { + string twoFactorCode = Program.GetUserInput(ASF.EUserInputType.TwoFactorAuthentication, BotName); + if (string.IsNullOrEmpty(twoFactorCode)) { Stop(); } + + SetUserInput(ASF.EUserInputType.TwoFactorAuthentication, twoFactorCode); } break; @@ -1501,11 +1512,13 @@ namespace ArchiSteamFarm { } if (string.IsNullOrEmpty(BotConfig.SteamParentalPIN)) { - BotConfig.SteamParentalPIN = Program.GetUserInput(ASF.EUserInputType.SteamParentalPIN, BotName); - if (string.IsNullOrEmpty(BotConfig.SteamParentalPIN)) { + string steamParentalPIN = Program.GetUserInput(ASF.EUserInputType.SteamParentalPIN, BotName); + if (string.IsNullOrEmpty(steamParentalPIN)) { Stop(); return; } + + SetUserInput(ASF.EUserInputType.SteamParentalPIN, steamParentalPIN); } if (!await ArchiWebHandler.Init(callback.ClientSteamID, SteamClient.ConnectedUniverse, callback.WebAPIUserNonce, BotConfig.SteamParentalPIN).ConfigureAwait(false)) { @@ -2040,9 +2053,9 @@ namespace ArchiSteamFarm { return null; } - private string ResponseInput(ulong steamID, string botName, string propertyName, string inputValue) { - if (steamID == 0) { - ASF.ArchiLogger.LogNullError(nameof(steamID)); + private string ResponseInput(ulong steamID, string propertyName, string inputValue) { + if ((steamID == 0) || string.IsNullOrEmpty(propertyName) || string.IsNullOrEmpty(inputValue)) { + ASF.ArchiLogger.LogNullError(nameof(steamID) + " || " + nameof(propertyName) + " || " + nameof(inputValue)); return null; } @@ -2055,19 +2068,45 @@ namespace ArchiSteamFarm { return "Avaliable only in headless mode!"; } - ASF.EUserInputType inputType = ASF.EUserInputType.Unknown; - + ASF.EUserInputType inputType; if (!Enum.TryParse(propertyName, out inputType) || (inputType == ASF.EUserInputType.Unknown)) { + // TODO: Proper response return ResponseUnknown(steamID); } - if (string.IsNullOrEmpty(inputValue)) { - return ResponseUnknown(steamID); + SetUserInput(inputType, inputValue); + return FormatBotResponse(Strings.Done); + } + + private static async Task ResponseInput(ulong steamID, string botNames, string propertyName, string inputValue) { + if ((steamID == 0) || string.IsNullOrEmpty(botNames) || string.IsNullOrEmpty(propertyName) || string.IsNullOrEmpty(inputValue)) { + ASF.ArchiLogger.LogNullError(nameof(steamID) + " || " + nameof(botNames) + " || " + nameof(propertyName) + " || " + nameof(inputValue)); + return null; } - Program.SetUserInput(inputType, botName, inputValue); + HashSet bots = GetBots(botNames); + if ((bots == null) || (bots.Count == 0)) { + return IsOwner(steamID) ? FormatStaticResponse(string.Format(Strings.BotNotFound, botNames)) : null; + } - return FormatStaticResponse(Strings.Done); + ICollection results; + IEnumerable> tasks = bots.Select(bot => Task.Run(() => bot.ResponseInput(steamID, propertyName, inputValue))); + + switch (Program.GlobalConfig.OptimizationMode) { + case GlobalConfig.EOptimizationMode.MinMemoryUsage: + results = new List(bots.Count); + foreach (Task task in tasks) { + results.Add(await task.ConfigureAwait(false)); + } + + break; + default: + results = await Task.WhenAll(tasks).ConfigureAwait(false); + break; + } + + List responses = new List(results.Where(result => !string.IsNullOrEmpty(result))); + return responses.Count > 0 ? string.Join("", responses) : null; } private async Task ResponseLoot(ulong steamID) { @@ -2999,6 +3038,48 @@ namespace ArchiSteamFarm { } } + private void SetUserInput(ASF.EUserInputType inputType, string inputValue) { + if ((inputType == ASF.EUserInputType.Unknown) || string.IsNullOrEmpty(inputValue)) { + ArchiLogger.LogNullError(nameof(inputValue) + " || " + nameof(inputValue)); + } + + switch (inputType) { + case ASF.EUserInputType.DeviceID: + DeviceID = inputValue; + break; + case ASF.EUserInputType.Login: + if (BotConfig != null) { + BotConfig.SteamLogin = inputValue; + } + + break; + case ASF.EUserInputType.Password: + if (BotConfig != null) { + BotConfig.SteamPassword = inputValue; + } + + break; + case ASF.EUserInputType.SteamGuard: + AuthCode = inputValue; + break; + case ASF.EUserInputType.SteamParentalPIN: + if (BotConfig != null) { + BotConfig.SteamParentalPIN = inputValue; + } + + break; + case ASF.EUserInputType.TwoFactorAuthentication: + TwoFactorCode = inputValue; + break; + case ASF.EUserInputType.WCFHostname: + // We don't handle ASF properties here + break; + default: + ASF.ArchiLogger.LogGenericWarning(string.Format(Strings.WarningUnknownValuePleaseReport, nameof(inputType), inputType)); + break; + } + } + private async Task Start() { if (!KeepRunning) { KeepRunning = true; diff --git a/ArchiSteamFarm/Program.cs b/ArchiSteamFarm/Program.cs index 1c63f0fe3..4dea2f998 100644 --- a/ArchiSteamFarm/Program.cs +++ b/ArchiSteamFarm/Program.cs @@ -48,12 +48,11 @@ namespace ArchiSteamFarm { private static readonly object ConsoleLock = new object(); private static readonly ManualResetEventSlim ShutdownResetEvent = new ManualResetEventSlim(false); + private static readonly Dictionary, string> UserInputs = new Dictionary, string>(); private static readonly WCF WCF = new WCF(); private static bool ShutdownSequenceInitialized; - private static readonly Dictionary, string> UserInputs = new Dictionary, string>(); - internal static async Task Exit(byte exitCode = 0) { if (exitCode != 0) { ASF.ArchiLogger.LogGenericError(Strings.ErrorExitingWithNonZeroErrorCode); @@ -139,20 +138,6 @@ namespace ArchiSteamFarm { Environment.Exit(0); } - internal static void SetUserInput(ASF.EUserInputType userInputType, string botName, string userInputValue) { - if (string.IsNullOrEmpty(botName) || string.IsNullOrEmpty(userInputValue) || (userInputType == ASF.EUserInputType.Unknown)) { - ASF.ArchiLogger.LogGenericWarning(Strings.ErrorObjectIsNull); - return; - } - - KeyValuePair userInputKey = new KeyValuePair(botName, userInputType); - if (UserInputs.ContainsKey(userInputKey)) { - UserInputs[userInputKey] = userInputValue; - } else { - UserInputs.Add(userInputKey, userInputValue); - } - } - private static async Task Init(string[] args) { AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler; TaskScheduler.UnobservedTaskException += UnobservedTaskExceptionHandler; diff --git a/GUI/Program.cs b/GUI/Program.cs index ae7a37f45..32d5740e1 100644 --- a/GUI/Program.cs +++ b/GUI/Program.cs @@ -29,7 +29,6 @@ namespace ArchiSteamFarm { } internal static string GetUserInput(ASF.EUserInputType userInputType, string botName = SharedInfo.ASF, string extraInformation = null) => null; // TODO - internal static string SetUserInput(ASF.EUserInputType userInputType, string botName, string userInputValue, string extraInformation = null) => null; // TODO internal static async Task InitASF() { ASF.ArchiLogger.LogGenericInfo("ASF V" + SharedInfo.Version);