Address SYSLIB1045

This commit is contained in:
JustArchi 2022-11-16 12:33:17 +01:00
parent cee7cd03b2
commit 3cf4ef3466
No known key found for this signature in database
GPG key ID: 6B138B4C64555AEA
5 changed files with 59 additions and 11 deletions

View file

@ -0,0 +1,51 @@
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
// |
// Copyright 2015-2022 Łukasz "JustArchi" Domeradzki
// Contact: JustArchi@JustArchi.net
// |
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// |
// http://www.apache.org/licenses/LICENSE-2.0
// |
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System.Text.RegularExpressions;
namespace ArchiSteamFarm.Core;
internal static class GeneratedRegexes {
private const string CdKeyPattern = @"^[0-9A-Z]{4,7}-[0-9A-Z]{4,7}-[0-9A-Z]{4,7}(?:(?:-[0-9A-Z]{4,7})?(?:-[0-9A-Z]{4,7}))?$";
private const string DecimalPattern = @"[0-9\.]+";
private const RegexOptions DefaultOptions = RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase;
private const string DigitsPattern = @"\d+";
private const string NonAsciiPattern = @"[^\u0000-\u007F]+";
#if NETFRAMEWORK
internal static Regex CdKey() => new(CdKeyPattern, DefaultOptions);
internal static Regex Decimal() => new(DecimalPattern, DefaultOptions);
internal static Regex Digits() => new(DigitsPattern, DefaultOptions);
internal static Regex NonAscii() => new(NonAsciiPattern, DefaultOptions);
#else
[GeneratedRegex(CdKeyPattern, DefaultOptions)]
internal static partial Regex CdKey();
[GeneratedRegex(DecimalPattern, DefaultOptions)]
internal static partial Regex Decimal();
[GeneratedRegex(DigitsPattern, DefaultOptions)]
internal static partial Regex Digits();
[GeneratedRegex(NonAsciiPattern, DefaultOptions)]
internal static partial Regex NonAscii();
#endif
}

View file

@ -28,7 +28,6 @@ using System.IO;
using System.Linq;
using System.Net;
using System.Resources;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using AngleSharp.Dom;
@ -176,7 +175,7 @@ public static class Utilities {
throw new ArgumentNullException(nameof(key));
}
return Regex.IsMatch(key, @"^[0-9A-Z]{4,7}-[0-9A-Z]{4,7}-[0-9A-Z]{4,7}(?:(?:-[0-9A-Z]{4,7})?(?:-[0-9A-Z]{4,7}))?$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
return GeneratedRegexes.CdKey().IsMatch(key);
}
[PublicAPI]

View file

@ -2300,9 +2300,7 @@ public sealed class Bot : IAsyncDisposable, IDisposable {
}
// Steam login and password fields can contain ASCII characters only, including spaces
const string nonAsciiPattern = @"[^\u0000-\u007F]+";
string username = Regex.Replace(BotConfig.SteamLogin!, nonAsciiPattern, "", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
string username = GeneratedRegexes.NonAscii().Replace(BotConfig.SteamLogin!, "");
if (string.IsNullOrEmpty(username)) {
ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsInvalid, nameof(BotConfig.SteamLogin)));
@ -2316,7 +2314,7 @@ public sealed class Bot : IAsyncDisposable, IDisposable {
if (!string.IsNullOrEmpty(password)) {
// ReSharper disable once RedundantSuppressNullableWarningExpression - required for .NET Framework
password = Regex.Replace(password!, nonAsciiPattern, "", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
password = GeneratedRegexes.NonAscii().Replace(password!, "");
if (string.IsNullOrEmpty(password)) {
ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsInvalid, nameof(BotConfig.SteamPassword)));

View file

@ -540,7 +540,7 @@ public sealed class CardsFarmer : IAsyncDisposable, IDisposable {
}
ushort cardsRemaining = 0;
Match progressMatch = Regex.Match(progressText, @"\d+");
Match progressMatch = GeneratedRegexes.Digits().Match(progressText);
// This might fail if we have no card drops remaining, 0 is not printed in this case - that's fine
if (progressMatch.Success) {
@ -578,7 +578,7 @@ public sealed class CardsFarmer : IAsyncDisposable, IDisposable {
continue;
}
Match cardsEarnedMatch = Regex.Match(cardsEarnedText, @"\d+");
Match cardsEarnedMatch = GeneratedRegexes.Digits().Match(cardsEarnedText);
if (!cardsEarnedMatch.Success) {
Bot.ArchiLogger.LogNullError(cardsEarnedMatch);
@ -624,7 +624,7 @@ public sealed class CardsFarmer : IAsyncDisposable, IDisposable {
}
float hours = 0.0F;
Match hoursMatch = Regex.Match(hoursText, @"[0-9\.,]+");
Match hoursMatch = GeneratedRegexes.Decimal().Match(hoursText);
// This might fail if we have exactly 0.0 hours played, as it's not printed in that case - that's fine
if (hoursMatch.Success) {
@ -1024,7 +1024,7 @@ public sealed class CardsFarmer : IAsyncDisposable, IDisposable {
return null;
}
Match match = Regex.Match(progress, @"\d+");
Match match = GeneratedRegexes.Digits().Match(progress);
if (!match.Success) {
return 0;

View file

@ -71,7 +71,7 @@
<DebugType>none</DebugType>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors />
<WarningsNotAsErrors>CS8002,IL2026,IL2057,IL2072,IL2075,IL2104,SYSLIB1045</WarningsNotAsErrors>
<WarningsNotAsErrors>CS8002,IL2026,IL2057,IL2072,IL2075,IL2104</WarningsNotAsErrors>
</PropertyGroup>
<!-- Enable public signing if not part of Visual Studio, which is too stupid to understand what public signing is -->