Skip to content

Commit

Permalink
Decrease memory consumption from owned packages
Browse files Browse the repository at this point in the history
  • Loading branch information
JustArchi committed Dec 20, 2024
1 parent 7dc3d16 commit 267384a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
17 changes: 11 additions & 6 deletions ArchiSteamFarm/Steam/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private set {

[JsonIgnore]
[PublicAPI]
public FrozenDictionary<uint, SteamApps.LicenseListCallback.License> OwnedPackages { get; private set; } = FrozenDictionary<uint, SteamApps.LicenseListCallback.License>.Empty;
public FrozenDictionary<uint, LicenseData> OwnedPackages { get; private set; } = FrozenDictionary<uint, LicenseData>.Empty;

[JsonInclude]
[JsonRequired]
Expand Down Expand Up @@ -1127,7 +1127,7 @@ internal static string FormatBotResponse(string response, string botName) {
DateTime mostRecent = DateTime.MinValue;

foreach (uint packageID in packageIDs) {
if (!OwnedPackages.TryGetValue(packageID, out SteamApps.LicenseListCallback.License? packageData)) {
if (!OwnedPackages.TryGetValue(packageID, out LicenseData? packageData)) {
continue;
}

Expand All @@ -1152,7 +1152,7 @@ internal static string FormatBotResponse(string response, string botName) {
DateTime safePlayableBefore = DateTime.UtcNow.AddMonths(-RegionRestrictionPlayableBlockMonths);

foreach (uint packageID in packageIDs) {
if (!OwnedPackages.TryGetValue(packageID, out SteamApps.LicenseListCallback.License? ownedPackageData)) {
if (!OwnedPackages.TryGetValue(packageID, out LicenseData? ownedPackageData)) {
// We don't own that packageID, keep checking
continue;
}
Expand Down Expand Up @@ -2854,7 +2854,7 @@ private async void OnDisconnected(SteamClient.DisconnectedCallback callback) {
Trading.OnDisconnected();

FirstTradeSent = false;
OwnedPackages = FrozenDictionary<uint, SteamApps.LicenseListCallback.License>.Empty;
OwnedPackages = FrozenDictionary<uint, LicenseData>.Empty;

EResult lastLogOnResult = LastLogOnResult;

Expand Down Expand Up @@ -3178,7 +3178,7 @@ private async void OnLicenseList(SteamApps.LicenseListCallback callback) {

Commands.OnNewLicenseList();

Dictionary<uint, SteamApps.LicenseListCallback.License> ownedPackages = new();
Dictionary<uint, LicenseData> ownedPackages = new();

Dictionary<uint, ulong> packageAccessTokens = new();
Dictionary<uint, uint> packagesToRefresh = new();
Expand All @@ -3187,7 +3187,12 @@ private async void OnLicenseList(SteamApps.LicenseListCallback callback) {

// We want to record only the most relevant entry, therefore we apply ordering here so we end up preferably with the most recent non-borrowed entry
foreach (SteamApps.LicenseListCallback.License license in callback.LicenseList.OrderByDescending(static license => license.LicenseFlags.HasFlag(ELicenseFlags.Borrowed)).ThenBy(static license => license.TimeCreated)) {
ownedPackages[license.PackageID] = license;
ownedPackages[license.PackageID] = new LicenseData {
LicenseFlags = license.LicenseFlags,
PackageID = license.PackageID,
PaymentMethod = license.PaymentMethod,
TimeCreated = license.TimeCreated
};

if (!OwnedPackages.ContainsKey(license.PackageID)) {
hasNewEntries = true;
Expand Down
2 changes: 1 addition & 1 deletion ArchiSteamFarm/Steam/Cards/CardsFarmer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1500,7 +1500,7 @@ private async Task SortGamesToFarm() {

if (packageIDs != null) {
foreach (uint packageID in packageIDs) {
if (!Bot.OwnedPackages.TryGetValue(packageID, out SteamApps.LicenseListCallback.License? packageData)) {
if (!Bot.OwnedPackages.TryGetValue(packageID, out LicenseData? packageData)) {
Bot.ArchiLogger.LogNullError(packageData);

return;
Expand Down
34 changes: 34 additions & 0 deletions ArchiSteamFarm/Steam/Data/LicenseData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// ----------------------------------------------------------------------------------------------
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
// ----------------------------------------------------------------------------------------------
// |
// Copyright 2015-2024 Łukasz "JustArchi" Domeradzki
// Contact: [email protected]
// |
// 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;
using SteamKit2;

namespace ArchiSteamFarm.Steam.Data;

public sealed record LicenseData {

Check warning on line 29 in ArchiSteamFarm/Steam/Data/LicenseData.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

RoslynAnalyzers Consider making public types internal

Because an application's API isn't typically referenced from outside the assembly, types can be made internal
public required ELicenseFlags LicenseFlags { get; init; }
public required uint PackageID { get; init; }
public required EPaymentMethod PaymentMethod { get; init; }
public required DateTime TimeCreated { get; init; }
}
4 changes: 2 additions & 2 deletions ArchiSteamFarm/Steam/Interaction/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ internal void OnNewLicenseList() {
break;
}
default: {
if (Bot.OwnedPackages.TryGetValue(gameID, out SteamApps.LicenseListCallback.License? package) && !package.LicenseFlags.HasFlag(ELicenseFlags.Borrowed)) {
if (Bot.OwnedPackages.TryGetValue(gameID, out LicenseData? package) && !package.LicenseFlags.HasFlag(ELicenseFlags.Borrowed)) {
response.AppendLine(FormatBotResponse(Strings.FormatBotAddLicense($"sub/{gameID}", $"{EResult.Fail}/{EPurchaseResultDetail.AlreadyPurchased}")));

break;
Expand Down Expand Up @@ -2087,7 +2087,7 @@ internal void OnNewLicenseList() {

continue;
case "S" or "SUB" when uint.TryParse(game, out uint packageID) && (packageID > 0):
if (Bot.OwnedPackages.TryGetValue(packageID, out SteamApps.LicenseListCallback.License? package) && !package.LicenseFlags.HasFlag(ELicenseFlags.Borrowed)) {
if (Bot.OwnedPackages.TryGetValue(packageID, out LicenseData? package) && !package.LicenseFlags.HasFlag(ELicenseFlags.Borrowed)) {
result[$"sub/{packageID}"] = packageID.ToString(CultureInfo.InvariantCulture);
response.AppendLine(FormatBotResponse(Strings.FormatBotOwnedAlready($"sub/{packageID}")));
} else {
Expand Down

0 comments on commit 267384a

Please sign in to comment.