From 3bb83610b8a85089c6391494c4541f1eb12a7306 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Domeradzki?= Date: Fri, 13 Sep 2024 10:04:56 +0200 Subject: [PATCH] Misc refactor after #3287 --- ArchiSteamFarm/Steam/Data/EAssetRarity.cs | 3 ++ .../Steam/Data/InventoryDescription.cs | 12 +++--- ArchiSteamFarm/Steam/Interaction/Commands.cs | 42 ++++++++++--------- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/ArchiSteamFarm/Steam/Data/EAssetRarity.cs b/ArchiSteamFarm/Steam/Data/EAssetRarity.cs index aae03dcfe1b97..d56ca3fb573f6 100644 --- a/ArchiSteamFarm/Steam/Data/EAssetRarity.cs +++ b/ArchiSteamFarm/Steam/Data/EAssetRarity.cs @@ -21,9 +21,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +using JetBrains.Annotations; + namespace ArchiSteamFarm.Steam.Data; #pragma warning disable CA1027 // Aliases are intentional, we don't plan to combine fields +[PublicAPI] public enum EAssetRarity : byte { Unknown, Common, diff --git a/ArchiSteamFarm/Steam/Data/InventoryDescription.cs b/ArchiSteamFarm/Steam/Data/InventoryDescription.cs index 61661dbd9b40b..3fc6fc41f0c1f 100644 --- a/ArchiSteamFarm/Steam/Data/InventoryDescription.cs +++ b/ArchiSteamFarm/Steam/Data/InventoryDescription.cs @@ -214,18 +214,16 @@ public EAssetRarity Rarity { return CachedRarity.Value; } case "RARITY": - string internalName = tag.internal_name.Split('_', StringSplitOptions.RemoveEmptyEntries).Skip(1).FirstOrDefault() ?? tag.internal_name; + string[] internalNameArgs = tag.internal_name.Split('_', 2, StringSplitOptions.RemoveEmptyEntries); - if (Enum.TryParse(internalName, true, out EAssetRarity assetRarity)) { + if ((internalNameArgs.Length >= 2) && Enum.TryParse(internalNameArgs[1], true, out EAssetRarity assetRarity) && Enum.IsDefined(assetRarity)) { CachedRarity = assetRarity; + } else { + ASF.ArchiLogger.LogGenericError(Strings.FormatWarningUnknownValuePleaseReport(nameof(tag.internal_name), tag.internal_name)); - return CachedRarity.Value; + CachedRarity = EAssetRarity.Unknown; } - ASF.ArchiLogger.LogGenericError(Strings.FormatWarningUnknownValuePleaseReport(nameof(tag.internal_name), tag.internal_name)); - - CachedRarity = EAssetRarity.Unknown; - return CachedRarity.Value; } } diff --git a/ArchiSteamFarm/Steam/Interaction/Commands.cs b/ArchiSteamFarm/Steam/Interaction/Commands.cs index 052e0038c0024..37c14b7ac4e8f 100644 --- a/ArchiSteamFarm/Steam/Interaction/Commands.cs +++ b/ArchiSteamFarm/Steam/Interaction/Commands.cs @@ -520,6 +520,24 @@ internal void OnNewLicenseList() { return gamesOwned; } + private static HashSet? ParseAssetRarities(string assetRaritiesText) { + ArgumentException.ThrowIfNullOrEmpty(assetRaritiesText); + + string[] assetRaritiesArgs = assetRaritiesText.Split(SharedInfo.ListElementSeparators, StringSplitOptions.RemoveEmptyEntries); + + HashSet assetRarities = []; + + foreach (string assetRarityArg in assetRaritiesArgs) { + if (!Enum.TryParse(assetRarityArg, true, out EAssetRarity assetRarity) || !Enum.IsDefined(assetRarity)) { + return null; + } + + assetRarities.Add(assetRarity); + } + + return assetRarities; + } + private async Task Response2FA(EAccess access) { if (!Enum.IsDefined(access)) { throw new InvalidEnumArgumentException(nameof(access), (int) access, typeof(EAccess)); @@ -780,7 +798,7 @@ internal void OnNewLicenseList() { HashSet? assetRarities = ParseAssetRarities(assetRaritiesText); - if (assetRarities == null) { + if ((assetRarities == null) || (assetRarities.Count == 0)) { return FormatBotResponse(Strings.FormatErrorIsInvalid(nameof(assetRarities))); } @@ -993,7 +1011,7 @@ internal void OnNewLicenseList() { return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null; } - private async Task ResponseAdvancedTransferByAssetRarity(EAccess access, uint appID, ulong contextID, Bot targetBot, HashSet assetRarities) { + private async Task ResponseAdvancedTransferByAssetRarity(EAccess access, uint appID, ulong contextID, Bot targetBot, IReadOnlyCollection assetRarities) { if (!Enum.IsDefined(access)) { throw new InvalidEnumArgumentException(nameof(access), (int) access, typeof(EAccess)); } @@ -1045,7 +1063,7 @@ internal void OnNewLicenseList() { HashSet? assetRarities = ParseAssetRarities(assetRaritiesText); - if (assetRarities == null) { + if ((assetRarities == null) || (assetRarities.Count == 0)) { return FormatBotResponse(Strings.FormatErrorIsInvalid(nameof(assetRarities))); } @@ -1085,7 +1103,7 @@ internal void OnNewLicenseList() { HashSet? assetRarities = ParseAssetRarities(assetRaritiesText); - if (assetRarities == null) { + if ((assetRarities == null) || (assetRarities.Count == 0)) { return FormatStaticResponse(Strings.FormatErrorIsInvalid(nameof(assetRarities))); } @@ -3427,22 +3445,6 @@ internal void OnNewLicenseList() { return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null; } - private static HashSet? ParseAssetRarities(string assetRaritiesText) { - string[] assetRaritiesArgs = assetRaritiesText.Split(SharedInfo.ListElementSeparators, StringSplitOptions.RemoveEmptyEntries); - - HashSet assetRarities = []; - - foreach (string assetRarityArg in assetRaritiesArgs) { - if (!Enum.TryParse(assetRarityArg, true, out EAssetRarity assetRarity) || !Enum.IsDefined(assetRarity)) { - return null; - } - - assetRarities.Add(assetRarity); - } - - return assetRarities; - } - [Flags] private enum ERedeemFlags : ushort { None = 0,