diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 9290b2ad4..9fa0232d4 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -62,6 +62,10 @@ namespace ArchiSteamFarm { internal readonly ArchiLogger ArchiLogger; internal readonly ArchiWebHandler ArchiWebHandler; + + [JsonProperty] + internal readonly string BotName; + internal readonly ConcurrentDictionary OwnedPackageIDs = new ConcurrentDictionary(); internal bool CanReceiveSteamCards => !IsAccountLimited && !IsAccountLocked; @@ -75,9 +79,6 @@ namespace ArchiSteamFarm { private readonly ArchiHandler ArchiHandler; private readonly BotDatabase BotDatabase; - [JsonProperty] - internal readonly string BotName; - private readonly CallbackManager CallbackManager; private readonly SemaphoreSlim CallbackSemaphore = new SemaphoreSlim(1, 1); @@ -567,47 +568,6 @@ namespace ArchiSteamFarm { return result; } - private async Task> GetKeysFromFile(string filePath) { - if (string.IsNullOrEmpty(filePath)) { - ArchiLogger.LogNullError(nameof(filePath)); - return null; - } - - if (!File.Exists(filePath)) { - return new Dictionary(0); - } - - try { - Dictionary keys = new Dictionary(); - - using (StreamReader reader = new StreamReader(filePath)) { - string line; - - while ((line = await reader.ReadLineAsync().ConfigureAwait(false)) != null) { - if (line.Length == 0) { - continue; - } - - string[] parsedArgs = line.Split(DefaultBackgroundKeysRedeemerSeparator, StringSplitOptions.RemoveEmptyEntries); - if (parsedArgs.Length < 4) { - ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorIsInvalid, line)); - continue; - } - - string name = parsedArgs[0]; - string key = parsedArgs[parsedArgs.Length - 1]; - - keys[key] = name; - } - } - - return keys; - } catch (Exception e) { - ArchiLogger.LogGenericException(e); - return null; - } - } - internal async Task> GetMarketableAppIDs() => await ArchiWebHandler.GetAppList().ConfigureAwait(false); internal async Task AppIDs)>> GetPackagesData(IReadOnlyCollection packageIDs) { @@ -689,14 +649,13 @@ namespace ArchiSteamFarm { return await ArchiWebHandler.GetTradeHoldDurationForTrade(tradeID).ConfigureAwait(false); } - internal async Task<(Dictionary Used, Dictionary Unused)> GetUsedAndUnusedKeys() { - IEnumerable>> tasks = new string[] { KeysToRedeemUsedFilePath, KeysToRedeemUnusedFilePath }.Select(file => GetKeysFromFile(file)); - + internal async Task<(Dictionary UnusedKeys, Dictionary UsedKeys)> GetUsedAndUnusedKeys() { + IEnumerable>> tasks = new[] { KeysToRedeemUnusedFilePath, KeysToRedeemUsedFilePath }.Select(GetKeysFromFile); IList> results; switch (Program.GlobalConfig.OptimizationMode) { case GlobalConfig.EOptimizationMode.MinMemoryUsage: - results = new List>(); + results = new List>(2); foreach (Task> task in tasks) { results.Add(await task.ConfigureAwait(false)); } @@ -1405,6 +1364,47 @@ namespace ArchiSteamFarm { private ulong GetFirstSteamMasterID() => BotConfig.SteamUserPermissions.Where(kv => (kv.Key != 0) && (kv.Value == BotConfig.EPermission.Master)).Select(kv => kv.Key).OrderByDescending(steamID => steamID != CachedSteamID).ThenBy(steamID => steamID).FirstOrDefault(); + private async Task> GetKeysFromFile(string filePath) { + if (string.IsNullOrEmpty(filePath)) { + ArchiLogger.LogNullError(nameof(filePath)); + return null; + } + + if (!File.Exists(filePath)) { + return new Dictionary(0); + } + + try { + Dictionary keys = new Dictionary(); + + using (StreamReader reader = new StreamReader(filePath)) { + string line; + + while ((line = await reader.ReadLineAsync().ConfigureAwait(false)) != null) { + if (line.Length == 0) { + continue; + } + + string[] parsedArgs = line.Split(DefaultBackgroundKeysRedeemerSeparator, StringSplitOptions.RemoveEmptyEntries); + if (parsedArgs.Length < 4) { + ArchiLogger.LogGenericWarning(string.Format(Strings.ErrorIsInvalid, line)); + continue; + } + + string name = parsedArgs[0]; + string key = parsedArgs[parsedArgs.Length - 1]; + + keys[key] = name; + } + } + + return keys; + } catch (Exception e) { + ArchiLogger.LogGenericException(e); + return null; + } + } + private BotConfig.EPermission GetSteamUserPermission(ulong steamID) { if (steamID == 0) { ArchiLogger.LogNullError(nameof(steamID)); diff --git a/ArchiSteamFarm/IPC.cs b/ArchiSteamFarm/IPC.cs index a3eddd781..01b3364e1 100644 --- a/ArchiSteamFarm/IPC.cs +++ b/ArchiSteamFarm/IPC.cs @@ -574,13 +574,13 @@ namespace ArchiSteamFarm { return true; } - IEnumerable<(string BotName, Task<(Dictionary Used, Dictionary Unused)> Task)> tasks = bots.Select(bot => (bot.BotName, bot.GetUsedAndUnusedKeys())); - ICollection<(string BotName, (Dictionary Used, Dictionary Unused))> results; + IEnumerable<(string BotName, Task<(Dictionary UnusedKeys, Dictionary UsedKeys)> Task)> tasks = bots.Select(bot => (bot.BotName, bot.GetUsedAndUnusedKeys())); + ICollection<(string BotName, (Dictionary UnusedKeys, Dictionary UsedKeys))> results; switch (Program.GlobalConfig.OptimizationMode) { case GlobalConfig.EOptimizationMode.MinMemoryUsage: - results = new List<(string BotName, (Dictionary Used, Dictionary Unused))>(bots.Count); - foreach ((string botName, Task<(Dictionary Used, Dictionary Unused)> task) in tasks) { + results = new List<(string BotName, (Dictionary UnusedKeys, Dictionary UsedKeys))>(bots.Count); + foreach ((string botName, Task<(Dictionary UnusedKeys, Dictionary UsedKeys)> task) in tasks) { results.Add((botName, await task.ConfigureAwait(false))); } @@ -593,8 +593,8 @@ namespace ArchiSteamFarm { Dictionary jsonResponse = new Dictionary(); - foreach((string botName, (Dictionary Used, Dictionary Unused) keys) in results) { - jsonResponse[botName] = new GamesToRedeemInBackgroundResponse(keys.Used, keys.Unused); + foreach ((string botName, (Dictionary UnusedKeys, Dictionary UsedKeys) taskResult) in results) { + jsonResponse[botName] = new GamesToRedeemInBackgroundResponse(taskResult.UnusedKeys, taskResult.UsedKeys); } await ResponseJsonObject(request, response, new GenericResponse>(true, "OK", jsonResponse)).ConfigureAwait(false); @@ -1351,15 +1351,15 @@ namespace ArchiSteamFarm { } private sealed class GamesToRedeemInBackgroundResponse { - [JsonProperty] - internal readonly Dictionary UsedKeys; - [JsonProperty] internal readonly Dictionary UnusedKeys; - internal GamesToRedeemInBackgroundResponse(Dictionary usedKeys, Dictionary unusedKeys) { - UsedKeys = usedKeys; + [JsonProperty] + internal readonly Dictionary UsedKeys; + + internal GamesToRedeemInBackgroundResponse(Dictionary unusedKeys = null, Dictionary usedKeys = null) { UnusedKeys = unusedKeys; + UsedKeys = usedKeys; } }