Skip to content

Commit

Permalink
Use ArchiHandler to fetch owned games (#2370)
Browse files Browse the repository at this point in the history
* Use ArchiHandler to fetch owned games

* Mark deprecated methods with Obsolete attribute
  • Loading branch information
ezhevita authored Jul 10, 2021
1 parent 1e30b84 commit fd4c1ef
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
41 changes: 41 additions & 0 deletions ArchiSteamFarm/Steam/Integration/ArchiHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,47 @@ public async Task<bool> AddFriend(ulong steamID) {
return response.Result == EResult.OK;
}

[PublicAPI]
public async Task<Dictionary<uint, string>?> 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<CPlayer_GetOwnedGames_Response>();

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));
Expand Down
6 changes: 6 additions & 0 deletions ArchiSteamFarm/Steam/Integration/ArchiWebHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,11 @@ public async IAsyncEnumerable<Asset> GetInventoryAsync(ulong steamID = 0, uint a
}
}

[Obsolete("Use " + nameof(ArchiHandler) + "." + nameof(ArchiHandler.GetOwnedGames) + " instead")]
[PublicAPI]
public async Task<Dictionary<uint, string>?> 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);
Expand Down Expand Up @@ -303,12 +306,15 @@ public async IAsyncEnumerable<Asset> GetInventoryAsync(ulong steamID = 0, uint a
return result;
}

[Obsolete("Use " + nameof(ArchiHandler) + "." + nameof(ArchiHandler.GetOwnedGames) + " instead")]
[PublicAPI]
public async Task<Dictionary<uint, string>?> 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)) {
Expand Down
4 changes: 1 addition & 3 deletions ArchiSteamFarm/Steam/Interaction/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -481,9 +481,7 @@ internal void OnNewLicenseList() {
return null;
}

bool? hasValidApiKey = await Bot.ArchiWebHandler.HasValidApiKey().ConfigureAwait(false);

Dictionary<uint, string>? gamesOwned = hasValidApiKey.GetValueOrDefault() ? await Bot.ArchiWebHandler.GetOwnedGames(Bot.SteamID).ConfigureAwait(false) : await Bot.ArchiWebHandler.GetMyOwnedGames().ConfigureAwait(false);
Dictionary<uint, string>? gamesOwned = await Bot.ArchiHandler.GetOwnedGames(Bot.SteamID).ConfigureAwait(false);

if (gamesOwned?.Count > 0) {
lock (CachedGamesOwned) {
Expand Down
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageVersion Include="Nito.AsyncEx.Coordination" Version="5.1.0" />
<PackageVersion Include="NLog" Version="4.7.10" />
<PackageVersion Include="NLog.Web.AspNetCore" Version="4.12.0" />
<PackageVersion Include="SteamKit2" Version="2.3.0" />
<PackageVersion Include="SteamKit2" Version="2.4.0-Alpha.2" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.1.4" />
<PackageVersion Include="Swashbuckle.AspNetCore.Annotations" Version="6.1.4" />
<PackageVersion Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.1.4" />
Expand Down

0 comments on commit fd4c1ef

Please sign in to comment.