From 92e28ecc5bc137a95f693885934f55d09b8b6e90 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Tue, 15 Jan 2019 12:30:29 +0100 Subject: [PATCH] Open more interfaces for PublicAPI --- ArchiSteamFarm/Bot.cs | 135 ++++++++++++++++++------------------ ArchiSteamFarm/Utilities.cs | 46 ++++++------ 2 files changed, 92 insertions(+), 89 deletions(-) diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 4bc38364c..5a0004a3b 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -298,6 +298,74 @@ namespace ArchiSteamFarm { Trading?.Dispose(); } + [PublicAPI] + public static HashSet GetBots(string args) { + if (string.IsNullOrEmpty(args)) { + ASF.ArchiLogger.LogNullError(nameof(args)); + + return null; + } + + string[] botNames = args.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); + + HashSet result = new HashSet(); + + foreach (string botName in botNames) { + if (botName.Equals(SharedInfo.ASF, StringComparison.OrdinalIgnoreCase)) { + foreach (Bot bot in Bots.OrderBy(bot => bot.Key).Select(bot => bot.Value)) { + result.Add(bot); + } + + return result; + } + + if (botName.Contains("..")) { + string[] botRange = botName.Split(new[] { ".." }, StringSplitOptions.RemoveEmptyEntries); + + if (botRange.Length == 2) { + if (Bots.TryGetValue(botRange[0], out Bot firstBot) && Bots.TryGetValue(botRange[1], out Bot lastBot)) { + bool inRange = false; + + foreach (Bot bot in Bots.OrderBy(bot => bot.Key).Select(bot => bot.Value)) { + if (bot == firstBot) { + inRange = true; + } else if (!inRange) { + continue; + } + + result.Add(bot); + + if (bot == lastBot) { + break; + } + } + + continue; + } + } + } else if (botName.StartsWith("r!", StringComparison.OrdinalIgnoreCase)) { + string botPattern = botName.Substring(2); + + try { + IEnumerable regexMatches = Bots.Where(kvp => Regex.IsMatch(kvp.Key, botPattern, BotsRegex)).Select(kvp => kvp.Value); + result.UnionWith(regexMatches); + } catch (ArgumentException e) { + ASF.ArchiLogger.LogGenericWarningException(e); + + return null; + } + } + + if (!Bots.TryGetValue(botName, out Bot targetBot)) { + continue; + } + + result.Add(targetBot); + } + + return result; + } + [PublicAPI] public bool HasPermission(ulong steamID, BotConfig.EPermission permission) { if ((steamID == 0) || (permission == BotConfig.EPermission.None)) { @@ -557,73 +625,6 @@ namespace ArchiSteamFarm { return ((productInfoResultSet.Complete && !productInfoResultSet.Failed) || optimisticDiscovery ? appID : 0, DateTime.MinValue); } - internal static HashSet GetBots(string args) { - if (string.IsNullOrEmpty(args)) { - ASF.ArchiLogger.LogNullError(nameof(args)); - - return null; - } - - string[] botNames = args.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); - - HashSet result = new HashSet(); - - foreach (string botName in botNames) { - if (botName.Equals(SharedInfo.ASF, StringComparison.OrdinalIgnoreCase)) { - foreach (Bot bot in Bots.OrderBy(bot => bot.Key).Select(bot => bot.Value)) { - result.Add(bot); - } - - return result; - } - - if (botName.Contains("..")) { - string[] botRange = botName.Split(new[] { ".." }, StringSplitOptions.RemoveEmptyEntries); - - if (botRange.Length == 2) { - if (Bots.TryGetValue(botRange[0], out Bot firstBot) && Bots.TryGetValue(botRange[1], out Bot lastBot)) { - bool inRange = false; - - foreach (Bot bot in Bots.OrderBy(bot => bot.Key).Select(bot => bot.Value)) { - if (bot == firstBot) { - inRange = true; - } else if (!inRange) { - continue; - } - - result.Add(bot); - - if (bot == lastBot) { - break; - } - } - - continue; - } - } - } else if (botName.StartsWith("r!", StringComparison.OrdinalIgnoreCase)) { - string botPattern = botName.Substring(2); - - try { - IEnumerable regexMatches = Bots.Where(kvp => Regex.IsMatch(kvp.Key, botPattern, BotsRegex)).Select(kvp => kvp.Value); - result.UnionWith(regexMatches); - } catch (ArgumentException e) { - ASF.ArchiLogger.LogGenericWarningException(e); - - return null; - } - } - - if (!Bots.TryGetValue(botName, out Bot targetBot)) { - continue; - } - - result.Add(targetBot); - } - - return result; - } - [ItemCanBeNull] internal async Task> GetMarketableAppIDs() => await ArchiWebHandler.GetAppList().ConfigureAwait(false); diff --git a/ArchiSteamFarm/Utilities.cs b/ArchiSteamFarm/Utilities.cs index 76972e4d5..eda883f43 100644 --- a/ArchiSteamFarm/Utilities.cs +++ b/ArchiSteamFarm/Utilities.cs @@ -37,6 +37,30 @@ namespace ArchiSteamFarm { // Normally we wouldn't need to use this singleton, but we want to ensure decent randomness across entire program's lifetime private static readonly Random Random = new Random(); + [PublicAPI] + public static string GetArgsAsText(string[] args, byte argsToSkip, string delimiter) { + if ((args == null) || (args.Length <= argsToSkip) || string.IsNullOrEmpty(delimiter)) { + ASF.ArchiLogger.LogNullError(nameof(args) + " || " + nameof(argsToSkip) + " || " + nameof(delimiter)); + + return null; + } + + return string.Join(delimiter, args.Skip(argsToSkip)); + } + + [PublicAPI] + public static string GetArgsAsText(string text, byte argsToSkip) { + if (string.IsNullOrEmpty(text)) { + ASF.ArchiLogger.LogNullError(nameof(text)); + + return null; + } + + string[] args = text.Split((char[]) null, argsToSkip + 1, StringSplitOptions.RemoveEmptyEntries); + + return args[args.Length - 1]; + } + [PublicAPI] public static uint GetUnixTime() => (uint) DateTimeOffset.UtcNow.ToUnixTimeSeconds(); @@ -203,28 +227,6 @@ namespace ArchiSteamFarm { [PublicAPI] public static string ToHumanReadable(this TimeSpan timeSpan) => timeSpan.Humanize(3, maxUnit: TimeUnit.Year, minUnit: TimeUnit.Second); - internal static string GetArgsAsText(string[] args, byte argsToSkip, string delimiter) { - if ((args == null) || (args.Length <= argsToSkip) || string.IsNullOrEmpty(delimiter)) { - ASF.ArchiLogger.LogNullError(nameof(args) + " || " + nameof(argsToSkip) + " || " + nameof(delimiter)); - - return null; - } - - return string.Join(delimiter, args.Skip(argsToSkip)); - } - - internal static string GetArgsAsText(string text, byte argsToSkip) { - if (string.IsNullOrEmpty(text)) { - ASF.ArchiLogger.LogNullError(nameof(text)); - - return null; - } - - string[] args = text.Split((char[]) null, argsToSkip + 1, StringSplitOptions.RemoveEmptyEntries); - - return args[args.Length - 1]; - } - internal static string GetCookieValue(this CookieContainer cookieContainer, string url, string name) { if ((cookieContainer == null) || string.IsNullOrEmpty(url) || string.IsNullOrEmpty(name)) { ASF.ArchiLogger.LogNullError(nameof(cookieContainer) + " || " + nameof(url) + " || " + nameof(name));