Open more interfaces for PublicAPI

This commit is contained in:
JustArchi
2019-01-15 12:30:29 +01:00
parent 5e7053cd90
commit 92e28ecc5b
2 changed files with 92 additions and 89 deletions

View File

@@ -298,6 +298,74 @@ namespace ArchiSteamFarm {
Trading?.Dispose();
}
[PublicAPI]
public static HashSet<Bot> GetBots(string args) {
if (string.IsNullOrEmpty(args)) {
ASF.ArchiLogger.LogNullError(nameof(args));
return null;
}
string[] botNames = args.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
HashSet<Bot> result = new HashSet<Bot>();
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<Bot> 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<Bot> GetBots(string args) {
if (string.IsNullOrEmpty(args)) {
ASF.ArchiLogger.LogNullError(nameof(args));
return null;
}
string[] botNames = args.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
HashSet<Bot> result = new HashSet<Bot>();
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<Bot> 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<HashSet<uint>> GetMarketableAppIDs() => await ArchiWebHandler.GetAppList().ConfigureAwait(false);

View File

@@ -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));