diff --git a/ArchiSteamFarm/Steam/Data/EAssetRarity.cs b/ArchiSteamFarm/Steam/Data/EAssetRarity.cs index aae03dcfe..d56ca3fb5 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 61661dbd9..3fc6fc41f 100644 --- a/ArchiSteamFarm/Steam/Data/InventoryDescription.cs +++ b/ArchiSteamFarm/Steam/Data/InventoryDescription.cs @@ -214,18 +214,16 @@ public sealed class InventoryDescription { 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 052e0038c..37c14b7ac 100644 --- a/ArchiSteamFarm/Steam/Interaction/Commands.cs +++ b/ArchiSteamFarm/Steam/Interaction/Commands.cs @@ -520,6 +520,24 @@ public sealed class Commands { 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 @@ public sealed class Commands { HashSet? assetRarities = ParseAssetRarities(assetRaritiesText); - if (assetRarities == null) { + if ((assetRarities == null) || (assetRarities.Count == 0)) { return FormatBotResponse(Strings.FormatErrorIsInvalid(nameof(assetRarities))); } @@ -993,7 +1011,7 @@ public sealed class Commands { 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 @@ public sealed class Commands { HashSet? assetRarities = ParseAssetRarities(assetRaritiesText); - if (assetRarities == null) { + if ((assetRarities == null) || (assetRarities.Count == 0)) { return FormatBotResponse(Strings.FormatErrorIsInvalid(nameof(assetRarities))); } @@ -1085,7 +1103,7 @@ public sealed class Commands { HashSet? assetRarities = ParseAssetRarities(assetRaritiesText); - if (assetRarities == null) { + if ((assetRarities == null) || (assetRarities.Count == 0)) { return FormatStaticResponse(Strings.FormatErrorIsInvalid(nameof(assetRarities))); } @@ -3427,22 +3445,6 @@ public sealed class Commands { 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,