From fd4c1ef59b98d33c16c9253a24bcb14c6c23bb33 Mon Sep 17 00:00:00 2001 From: Vitaliya Date: Sat, 10 Jul 2021 18:31:48 +0300 Subject: [PATCH] Use ArchiHandler to fetch owned games (#2370) * Use ArchiHandler to fetch owned games * Mark deprecated methods with Obsolete attribute --- .../Steam/Integration/ArchiHandler.cs | 41 +++++++++++++++++++ .../Steam/Integration/ArchiWebHandler.cs | 6 +++ ArchiSteamFarm/Steam/Interaction/Commands.cs | 4 +- Directory.Packages.props | 2 +- 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/ArchiSteamFarm/Steam/Integration/ArchiHandler.cs b/ArchiSteamFarm/Steam/Integration/ArchiHandler.cs index f5bfe1d6c..747ac0c1b 100644 --- a/ArchiSteamFarm/Steam/Integration/ArchiHandler.cs +++ b/ArchiSteamFarm/Steam/Integration/ArchiHandler.cs @@ -89,6 +89,47 @@ namespace ArchiSteamFarm.Steam.Integration { return response.Result == EResult.OK; } + [PublicAPI] + public async Task?> GetOwnedGames(ulong steamID) { + if ((steamID == 0) || !new SteamID(steamID).IsIndividualAccount) { + throw new ArgumentOutOfRangeException(nameof(steamID)); + } + + if (Client == null) { + throw new InvalidOperationException(nameof(Client)); + } + + if (!Client.IsConnected) { + return null; + } + + CPlayer_GetOwnedGames_Request request = new() { + steamid = steamID, + include_appinfo = true, + include_free_sub = true, + include_played_free_games = true, + skip_unvetted_apps = false + }; + + SteamUnifiedMessages.ServiceMethodResponse response; + + try { + response = await UnifiedPlayerService.SendMessage(x => x.GetOwnedGames(request)).ToLongRunningTask().ConfigureAwait(false); + } catch (Exception e) { + ArchiLogger.LogGenericWarningException(e); + + return null; + } + + if (response.Result != EResult.OK) { + return null; + } + + CPlayer_GetOwnedGames_Response body = response.GetDeserializedResponse(); + + return body.games.ToDictionary(game => (uint) game.appid, game => game.name); + } + public override void HandleMsg(IPacketMsg packetMsg) { if (packetMsg == null) { throw new ArgumentNullException(nameof(packetMsg)); diff --git a/ArchiSteamFarm/Steam/Integration/ArchiWebHandler.cs b/ArchiSteamFarm/Steam/Integration/ArchiWebHandler.cs index 1e41b6afb..a57623a0e 100644 --- a/ArchiSteamFarm/Steam/Integration/ArchiWebHandler.cs +++ b/ArchiSteamFarm/Steam/Integration/ArchiWebHandler.cs @@ -254,8 +254,11 @@ namespace ArchiSteamFarm.Steam.Integration { } } + [Obsolete("Use " + nameof(ArchiHandler) + "." + nameof(ArchiHandler.GetOwnedGames) + " instead")] [PublicAPI] public async Task?> GetMyOwnedGames() { + ASF.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.WarningDeprecated, nameof(GetMyOwnedGames), nameof(ArchiHandler) + "." + nameof(ArchiHandler.GetOwnedGames))); + Uri request = new(SteamCommunityURL, "/my/games?l=english&xml=1"); XmlDocumentResponse? response = await UrlGetToXmlDocumentWithSession(request, checkSessionPreemptively: false).ConfigureAwait(false); @@ -303,12 +306,15 @@ namespace ArchiSteamFarm.Steam.Integration { return result; } + [Obsolete("Use " + nameof(ArchiHandler) + "." + nameof(ArchiHandler.GetOwnedGames) + " instead")] [PublicAPI] public async Task?> GetOwnedGames(ulong steamID) { if ((steamID == 0) || !new SteamID(steamID).IsIndividualAccount) { throw new ArgumentOutOfRangeException(nameof(steamID)); } + ASF.ArchiLogger.LogGenericWarning(string.Format(CultureInfo.CurrentCulture, Strings.WarningDeprecated, nameof(GetOwnedGames), nameof(ArchiHandler) + "." + nameof(ArchiHandler.GetOwnedGames))); + (bool success, string? steamApiKey) = await CachedApiKey.GetValue().ConfigureAwait(false); if (!success || string.IsNullOrEmpty(steamApiKey)) { diff --git a/ArchiSteamFarm/Steam/Interaction/Commands.cs b/ArchiSteamFarm/Steam/Interaction/Commands.cs index 695ba519b..4acc97397 100644 --- a/ArchiSteamFarm/Steam/Interaction/Commands.cs +++ b/ArchiSteamFarm/Steam/Interaction/Commands.cs @@ -481,9 +481,7 @@ namespace ArchiSteamFarm.Steam.Interaction { return null; } - bool? hasValidApiKey = await Bot.ArchiWebHandler.HasValidApiKey().ConfigureAwait(false); - - Dictionary? gamesOwned = hasValidApiKey.GetValueOrDefault() ? await Bot.ArchiWebHandler.GetOwnedGames(Bot.SteamID).ConfigureAwait(false) : await Bot.ArchiWebHandler.GetMyOwnedGames().ConfigureAwait(false); + Dictionary? gamesOwned = await Bot.ArchiHandler.GetOwnedGames(Bot.SteamID).ConfigureAwait(false); if (gamesOwned?.Count > 0) { lock (CachedGamesOwned) { diff --git a/Directory.Packages.props b/Directory.Packages.props index 02d19fae5..7070b77bc 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -13,7 +13,7 @@ - +