Misc code enhancements

This commit is contained in:
Łukasz Domeradzki 2024-04-04 02:15:16 +02:00
parent 990e696b37
commit 1841cde776
No known key found for this signature in database
GPG key ID: 6B138B4C64555AEA
8 changed files with 32 additions and 29 deletions

View file

@ -1149,9 +1149,7 @@ internal sealed class RemoteCommunication : IAsyncDisposable, IDisposable {
return;
}
#pragma warning disable CA2000 // False positive, we're actually wrapping it in the using clause below exactly for that purpose
using (await Bot.Actions.GetTradingLock().ConfigureAwait(false)) {
#pragma warning restore CA2000 // False positive, we're actually wrapping it in the using clause below exactly for that purpose
tradesSent = await MatchActively(response.Value.Users, assetsForMatching, acceptedMatchableTypes).ConfigureAwait(false);
}

View file

@ -110,7 +110,7 @@ public sealed class ConcurrentHashSet<T> : IReadOnlySet<T>, ISet<T> where T : no
public void IntersectWith(IEnumerable<T> other) {
ArgumentNullException.ThrowIfNull(other);
ISet<T> otherSet = other as ISet<T> ?? other.ToHashSet();
IReadOnlySet<T> otherSet = other as IReadOnlySet<T> ?? other.ToHashSet();
foreach (T item in this.Where(item => !otherSet.Contains(item))) {
Remove(item);
@ -120,7 +120,7 @@ public sealed class ConcurrentHashSet<T> : IReadOnlySet<T>, ISet<T> where T : no
public bool IsProperSubsetOf(IEnumerable<T> other) {
ArgumentNullException.ThrowIfNull(other);
ISet<T> otherSet = other as ISet<T> ?? other.ToHashSet();
IReadOnlySet<T> otherSet = other as IReadOnlySet<T> ?? other.ToHashSet();
return (otherSet.Count > Count) && IsSubsetOf(otherSet);
}
@ -128,7 +128,7 @@ public sealed class ConcurrentHashSet<T> : IReadOnlySet<T>, ISet<T> where T : no
public bool IsProperSupersetOf(IEnumerable<T> other) {
ArgumentNullException.ThrowIfNull(other);
ISet<T> otherSet = other as ISet<T> ?? other.ToHashSet();
IReadOnlySet<T> otherSet = other as IReadOnlySet<T> ?? other.ToHashSet();
return (otherSet.Count < Count) && IsSupersetOf(otherSet);
}
@ -136,7 +136,7 @@ public sealed class ConcurrentHashSet<T> : IReadOnlySet<T>, ISet<T> where T : no
public bool IsSubsetOf(IEnumerable<T> other) {
ArgumentNullException.ThrowIfNull(other);
ISet<T> otherSet = other as ISet<T> ?? other.ToHashSet();
IReadOnlySet<T> otherSet = other as IReadOnlySet<T> ?? other.ToHashSet();
return this.All(otherSet.Contains);
}
@ -144,7 +144,7 @@ public sealed class ConcurrentHashSet<T> : IReadOnlySet<T>, ISet<T> where T : no
public bool IsSupersetOf(IEnumerable<T> other) {
ArgumentNullException.ThrowIfNull(other);
ISet<T> otherSet = other as ISet<T> ?? other.ToHashSet();
IReadOnlySet<T> otherSet = other as IReadOnlySet<T> ?? other.ToHashSet();
return otherSet.All(Contains);
}
@ -152,7 +152,7 @@ public sealed class ConcurrentHashSet<T> : IReadOnlySet<T>, ISet<T> where T : no
public bool Overlaps(IEnumerable<T> other) {
ArgumentNullException.ThrowIfNull(other);
ISet<T> otherSet = other as ISet<T> ?? other.ToHashSet();
IReadOnlySet<T> otherSet = other as IReadOnlySet<T> ?? other.ToHashSet();
return otherSet.Any(Contains);
}
@ -172,7 +172,7 @@ public sealed class ConcurrentHashSet<T> : IReadOnlySet<T>, ISet<T> where T : no
public bool SetEquals(IEnumerable<T> other) {
ArgumentNullException.ThrowIfNull(other);
ISet<T> otherSet = other as ISet<T> ?? other.ToHashSet();
IReadOnlySet<T> otherSet = other as IReadOnlySet<T> ?? other.ToHashSet();
return (otherSet.Count == Count) && otherSet.All(Contains);
}
@ -180,7 +180,7 @@ public sealed class ConcurrentHashSet<T> : IReadOnlySet<T>, ISet<T> where T : no
public void SymmetricExceptWith(IEnumerable<T> other) {
ArgumentNullException.ThrowIfNull(other);
ISet<T> otherSet = other as ISet<T> ?? other.ToHashSet();
IReadOnlySet<T> otherSet = other as IReadOnlySet<T> ?? other.ToHashSet();
HashSet<T> removed = [];
foreach (T item in otherSet.Where(Contains)) {

View file

@ -288,7 +288,7 @@ public static class Utilities {
ASF.ArchiLogger.LogGenericDebug($"{fileName} {progressPercentage}%...");
}
internal static (bool IsWeak, string? Reason) TestPasswordStrength(string password, ISet<string>? additionallyForbiddenPhrases = null) {
internal static (bool IsWeak, string? Reason) TestPasswordStrength(string password, IEnumerable<string>? additionallyForbiddenPhrases = null) {
ArgumentException.ThrowIfNullOrEmpty(password);
HashSet<string> forbiddenPhrases = ForbiddenPasswordPhrases.ToHashSet(StringComparer.InvariantCultureIgnoreCase);
@ -514,9 +514,7 @@ public static class Utilities {
private static bool RelativeDirectoryStartsWith(string directory, params string[] prefixes) {
ArgumentException.ThrowIfNullOrEmpty(directory);
#pragma warning disable CA1508 // False positive, params could be null when explicitly set
if ((prefixes == null) || (prefixes.Length == 0)) {
#pragma warning restore CA1508 // False positive, params could be null when explicitly set
throw new ArgumentNullException(nameof(prefixes));
}

View file

@ -72,6 +72,6 @@ public sealed class IPCBansController : ArchiController {
/// Gets all IP addresses currently blocked by ASFs IPC module
/// </summary>
[HttpGet]
[ProducesResponseType<GenericResponse<ISet<string>>>((int) HttpStatusCode.OK)]
public ActionResult<GenericResponse<ISet<string>>> Get() => Ok(new GenericResponse<ISet<string>>(ApiAuthenticationMiddleware.GetCurrentlyBannedIPs().Select(static ip => ip.ToString()).ToHashSet()));
[ProducesResponseType<GenericResponse<IReadOnlySet<string>>>((int) HttpStatusCode.OK)]
public ActionResult<GenericResponse<IReadOnlySet<string>>> Get() => Ok(new GenericResponse<IReadOnlySet<string>>(ApiAuthenticationMiddleware.GetCurrentlyBannedIPs().Select(static ip => ip.ToString()).ToHashSet()));
}

View file

@ -488,7 +488,7 @@ internal static class Program {
// Stop all the active bots so they can disconnect cleanly
if (Bot.Bots?.Count > 0) {
// Stop() function can block due to SK2 sockets, don't forget a maximum delay
await Task.WhenAny(Utilities.InParallel(Bot.Bots.Values.Select(static bot => Task.Run(() => bot.Stop(true)))), Task.Delay(Bot.Bots.Count * WebBrowser.MaxTries * 1000)).ConfigureAwait(false);
await Task.WhenAny(Utilities.InParallel(Bot.Bots.Values.Select(static bot => Task.Run(() => bot.Stop(true)))), Task.Delay((Bot.Bots.Count + WebBrowser.MaxTries) * 1000)).ConfigureAwait(false);
// Extra second for Steam requests to go through
await Task.Delay(1000).ConfigureAwait(false);

View file

@ -622,17 +622,22 @@ public sealed class Bot : IAsyncDisposable, IDisposable {
Regex regex;
try {
#pragma warning disable CA3012
regex = new Regex(botsPattern, botsRegex);
#pragma warning restore CA3012
#pragma warning disable CA3012 // We're aware of a potential denial of service here, this is why we limit maximum matching time to a sane value
regex = new Regex(botsPattern, botsRegex, TimeSpan.FromSeconds(1));
#pragma warning restore CA3012 // We're aware of a potential denial of service here, this is why we limit maximum matching time to a sane value
} catch (ArgumentException e) {
ASF.ArchiLogger.LogGenericWarningException(e);
return null;
}
IEnumerable<Bot> regexMatches = Bots.Where(kvp => regex.IsMatch(kvp.Key)).Select(static kvp => kvp.Value);
result.UnionWith(regexMatches);
try {
IEnumerable<Bot> regexMatches = Bots.Where(kvp => regex.IsMatch(kvp.Key)).Select(static kvp => kvp.Value);
result.UnionWith(regexMatches);
} catch (RegexMatchTimeoutException e) {
ASF.ArchiLogger.LogGenericException(e);
}
continue;
}
@ -857,9 +862,7 @@ public sealed class Bot : IAsyncDisposable, IDisposable {
throw new ArgumentNullException(nameof(appIDs));
}
#pragma warning disable CA1508 // False positive, not every IReadOnlyCollection is ISet, and this is public API
ISet<uint> uniqueAppIDs = appIDs as ISet<uint> ?? appIDs.ToHashSet();
#pragma warning restore CA1508 // False positive, not every IReadOnlyCollection is ISet, and this is public API
IReadOnlySet<uint> uniqueAppIDs = appIDs as IReadOnlySet<uint> ?? appIDs.ToHashSet();
switch (ASF.GlobalConfig?.OptimizationMode) {
case GlobalConfig.EOptimizationMode.MinMemoryUsage:
@ -3114,6 +3117,14 @@ public sealed class Bot : IAsyncDisposable, IDisposable {
Task refreshTask = ASF.GlobalDatabase.RefreshPackages(this, packagesToRefresh);
try {
await refreshTask.WaitAsync(TimeSpan.FromSeconds(5)).ConfigureAwait(false);
} catch (TimeoutException) {
ArchiLogger.LogGenericInfo(Strings.BotRefreshingPackagesData);
displayFinish = true;
}
if (await Task.WhenAny(refreshTask, Task.Delay(5000)).ConfigureAwait(false) != refreshTask) {
ArchiLogger.LogGenericInfo(Strings.BotRefreshingPackagesData);

View file

@ -860,9 +860,7 @@ public sealed class ArchiHandler : ClientMsgHandler {
}
if (gameIDs.Count > 0) {
#pragma warning disable CA1508 // False positive, not every IReadOnlyCollection is ISet
ISet<uint> uniqueGameIDs = gameIDs as ISet<uint> ?? gameIDs.ToHashSet();
#pragma warning restore CA1508 // False positive, not every IReadOnlyCollection is ISet
IReadOnlySet<uint> uniqueGameIDs = gameIDs as IReadOnlySet<uint> ?? gameIDs.ToHashSet();
foreach (uint gameID in uniqueGameIDs.Where(static gameID => gameID > 0)) {
if (request.Body.games_played.Count >= MaxGamesPlayedConcurrently) {

View file

@ -161,9 +161,7 @@ public sealed class WebBrowser : IDisposable {
progressReporter?.Report(0);
#pragma warning disable CA2000 // False positive, we're actually wrapping it in the using clause below exactly for that purpose
MemoryStream ms = new((int) response.Length);
#pragma warning restore CA2000 // False positive, we're actually wrapping it in the using clause below exactly for that purpose
await using (ms.ConfigureAwait(false)) {
byte batch = 0;