Misc code enhancements

This commit is contained in:
Łukasz Domeradzki
2024-04-04 02:15:16 +02:00
parent 990e696b37
commit 1841cde776
8 changed files with 32 additions and 29 deletions

View File

@@ -1149,9 +1149,7 @@ internal sealed class RemoteCommunication : IAsyncDisposable, IDisposable {
return; 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)) { 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); 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) { public void IntersectWith(IEnumerable<T> other) {
ArgumentNullException.ThrowIfNull(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))) { foreach (T item in this.Where(item => !otherSet.Contains(item))) {
Remove(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) { public bool IsProperSubsetOf(IEnumerable<T> other) {
ArgumentNullException.ThrowIfNull(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); 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) { public bool IsProperSupersetOf(IEnumerable<T> other) {
ArgumentNullException.ThrowIfNull(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); 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) { public bool IsSubsetOf(IEnumerable<T> other) {
ArgumentNullException.ThrowIfNull(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); 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) { public bool IsSupersetOf(IEnumerable<T> other) {
ArgumentNullException.ThrowIfNull(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); 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) { public bool Overlaps(IEnumerable<T> other) {
ArgumentNullException.ThrowIfNull(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); 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) { public bool SetEquals(IEnumerable<T> other) {
ArgumentNullException.ThrowIfNull(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); 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) { public void SymmetricExceptWith(IEnumerable<T> other) {
ArgumentNullException.ThrowIfNull(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 = []; HashSet<T> removed = [];
foreach (T item in otherSet.Where(Contains)) { foreach (T item in otherSet.Where(Contains)) {

View File

@@ -288,7 +288,7 @@ public static class Utilities {
ASF.ArchiLogger.LogGenericDebug($"{fileName} {progressPercentage}%..."); 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); ArgumentException.ThrowIfNullOrEmpty(password);
HashSet<string> forbiddenPhrases = ForbiddenPasswordPhrases.ToHashSet(StringComparer.InvariantCultureIgnoreCase); HashSet<string> forbiddenPhrases = ForbiddenPasswordPhrases.ToHashSet(StringComparer.InvariantCultureIgnoreCase);
@@ -514,9 +514,7 @@ public static class Utilities {
private static bool RelativeDirectoryStartsWith(string directory, params string[] prefixes) { private static bool RelativeDirectoryStartsWith(string directory, params string[] prefixes) {
ArgumentException.ThrowIfNullOrEmpty(directory); ArgumentException.ThrowIfNullOrEmpty(directory);
#pragma warning disable CA1508 // False positive, params could be null when explicitly set
if ((prefixes == null) || (prefixes.Length == 0)) { if ((prefixes == null) || (prefixes.Length == 0)) {
#pragma warning restore CA1508 // False positive, params could be null when explicitly set
throw new ArgumentNullException(nameof(prefixes)); 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 /// Gets all IP addresses currently blocked by ASFs IPC module
/// </summary> /// </summary>
[HttpGet] [HttpGet]
[ProducesResponseType<GenericResponse<ISet<string>>>((int) HttpStatusCode.OK)] [ProducesResponseType<GenericResponse<IReadOnlySet<string>>>((int) HttpStatusCode.OK)]
public ActionResult<GenericResponse<ISet<string>>> Get() => Ok(new GenericResponse<ISet<string>>(ApiAuthenticationMiddleware.GetCurrentlyBannedIPs().Select(static ip => ip.ToString()).ToHashSet())); 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 // Stop all the active bots so they can disconnect cleanly
if (Bot.Bots?.Count > 0) { if (Bot.Bots?.Count > 0) {
// Stop() function can block due to SK2 sockets, don't forget a maximum delay // 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 // Extra second for Steam requests to go through
await Task.Delay(1000).ConfigureAwait(false); await Task.Delay(1000).ConfigureAwait(false);

View File

@@ -622,17 +622,22 @@ public sealed class Bot : IAsyncDisposable, IDisposable {
Regex regex; Regex regex;
try { try {
#pragma warning disable 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); regex = new Regex(botsPattern, botsRegex, TimeSpan.FromSeconds(1));
#pragma warning restore CA3012 #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) { } catch (ArgumentException e) {
ASF.ArchiLogger.LogGenericWarningException(e); ASF.ArchiLogger.LogGenericWarningException(e);
return null; return null;
} }
IEnumerable<Bot> regexMatches = Bots.Where(kvp => regex.IsMatch(kvp.Key)).Select(static kvp => kvp.Value); try {
result.UnionWith(regexMatches); 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; continue;
} }
@@ -857,9 +862,7 @@ public sealed class Bot : IAsyncDisposable, IDisposable {
throw new ArgumentNullException(nameof(appIDs)); throw new ArgumentNullException(nameof(appIDs));
} }
#pragma warning disable CA1508 // False positive, not every IReadOnlyCollection is ISet, and this is public API IReadOnlySet<uint> uniqueAppIDs = appIDs as IReadOnlySet<uint> ?? appIDs.ToHashSet();
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
switch (ASF.GlobalConfig?.OptimizationMode) { switch (ASF.GlobalConfig?.OptimizationMode) {
case GlobalConfig.EOptimizationMode.MinMemoryUsage: case GlobalConfig.EOptimizationMode.MinMemoryUsage:
@@ -3114,6 +3117,14 @@ public sealed class Bot : IAsyncDisposable, IDisposable {
Task refreshTask = ASF.GlobalDatabase.RefreshPackages(this, packagesToRefresh); 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) { if (await Task.WhenAny(refreshTask, Task.Delay(5000)).ConfigureAwait(false) != refreshTask) {
ArchiLogger.LogGenericInfo(Strings.BotRefreshingPackagesData); ArchiLogger.LogGenericInfo(Strings.BotRefreshingPackagesData);

View File

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

View File

@@ -161,9 +161,7 @@ public sealed class WebBrowser : IDisposable {
progressReporter?.Report(0); 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); 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)) { await using (ms.ConfigureAwait(false)) {
byte batch = 0; byte batch = 0;