-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial implementation of announce with diff * Add missing logic pieces * Change in logic * Fix checksums * Add deduplication logic * Update SetPart.cs * Use standalone endpoint for diff * Use different hashcode impl * Update AssetForListing.cs * Misc * Push all the changes for this to finally work * Use original index rather than self-calculated ASFB makes some calculations based on index, it's better for us to have holes rather than hiding skipped items. * Handle edge case of no assets after deduplication * Remove dead code * Address trim warnings * Misc optimization
- Loading branch information
Showing
17 changed files
with
754 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
ArchiSteamFarm.OfficialPlugins.ItemsMatcher/BotCache.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// _ _ _ ____ _ _____ | ||
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ | ||
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ | ||
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | | ||
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| | ||
// | | ||
// Copyright 2015-2023 Ł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 System.Globalization; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using ArchiSteamFarm.Collections; | ||
using ArchiSteamFarm.Core; | ||
using ArchiSteamFarm.Helpers; | ||
using ArchiSteamFarm.Localization; | ||
using ArchiSteamFarm.OfficialPlugins.ItemsMatcher.Data; | ||
using JetBrains.Annotations; | ||
using Newtonsoft.Json; | ||
|
||
namespace ArchiSteamFarm.OfficialPlugins.ItemsMatcher; | ||
|
||
internal sealed class BotCache : SerializableFile { | ||
[JsonProperty(Required = Required.DisallowNull)] | ||
internal readonly ConcurrentList<AssetForListing> LastAnnouncedAssetsForListing = new(); | ||
|
||
internal string? LastAnnouncedTradeToken { | ||
get => BackingLastAnnouncedTradeToken; | ||
|
||
set { | ||
if (BackingLastAnnouncedTradeToken == value) { | ||
return; | ||
} | ||
|
||
BackingLastAnnouncedTradeToken = value; | ||
Utilities.InBackground(Save); | ||
} | ||
} | ||
|
||
[JsonProperty] | ||
private string? BackingLastAnnouncedTradeToken; | ||
|
||
private BotCache(string filePath) : this() { | ||
ArgumentException.ThrowIfNullOrEmpty(filePath); | ||
|
||
FilePath = filePath; | ||
} | ||
|
||
[JsonConstructor] | ||
private BotCache() => LastAnnouncedAssetsForListing.OnModified += OnObjectModified; | ||
|
||
[UsedImplicitly] | ||
public bool ShouldSerializeBackingLastAnnouncedTradeToken() => !string.IsNullOrEmpty(BackingLastAnnouncedTradeToken); | ||
|
||
[UsedImplicitly] | ||
public bool ShouldSerializeLastAnnouncedAssetsForListing() => LastAnnouncedAssetsForListing.Count > 0; | ||
|
||
protected override void Dispose(bool disposing) { | ||
if (disposing) { | ||
// Events we registered | ||
LastAnnouncedAssetsForListing.OnModified -= OnObjectModified; | ||
} | ||
|
||
// Base dispose | ||
base.Dispose(disposing); | ||
} | ||
|
||
internal static async Task<BotCache> CreateOrLoad(string filePath) { | ||
ArgumentException.ThrowIfNullOrEmpty(filePath); | ||
|
||
if (!File.Exists(filePath)) { | ||
return new BotCache(filePath); | ||
} | ||
|
||
BotCache? botCache; | ||
|
||
try { | ||
string json = await File.ReadAllTextAsync(filePath).ConfigureAwait(false); | ||
|
||
if (string.IsNullOrEmpty(json)) { | ||
ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsEmpty, nameof(json))); | ||
|
||
return new BotCache(filePath); | ||
} | ||
|
||
botCache = JsonConvert.DeserializeObject<BotCache>(json); | ||
} catch (Exception e) { | ||
ASF.ArchiLogger.LogGenericException(e); | ||
|
||
return new BotCache(filePath); | ||
} | ||
|
||
if (botCache == null) { | ||
ASF.ArchiLogger.LogNullError(botCache); | ||
|
||
return new BotCache(filePath); | ||
} | ||
|
||
botCache.FilePath = filePath; | ||
|
||
return botCache; | ||
} | ||
|
||
private async void OnObjectModified(object? sender, EventArgs e) { | ||
if (string.IsNullOrEmpty(FilePath)) { | ||
return; | ||
} | ||
|
||
await Save().ConfigureAwait(false); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
ArchiSteamFarm.OfficialPlugins.ItemsMatcher/Data/AnnouncementDiffRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// _ _ _ ____ _ _____ | ||
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ | ||
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ | ||
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | | ||
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| | ||
// | | ||
// Copyright 2015-2023 Ł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 System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using ArchiSteamFarm.Steam.Data; | ||
using ArchiSteamFarm.Steam.Storage; | ||
using Newtonsoft.Json; | ||
using SteamKit2; | ||
|
||
namespace ArchiSteamFarm.OfficialPlugins.ItemsMatcher.Data; | ||
|
||
internal sealed class AnnouncementDiffRequest : AnnouncementRequest { | ||
[JsonProperty(Required = Required.Always)] | ||
private readonly ImmutableHashSet<AssetForListing> InventoryRemoved; | ||
|
||
[JsonProperty(Required = Required.Always)] | ||
private readonly string PreviousInventoryChecksum; | ||
|
||
internal AnnouncementDiffRequest(Guid guid, ulong steamID, ICollection<AssetForListing> inventory, string inventoryChecksum, IReadOnlyCollection<Asset.EType> matchableTypes, uint totalInventoryCount, bool matchEverything, byte maxTradeHoldDuration, string tradeToken, ICollection<AssetForListing> inventoryRemoved, string previousInventoryChecksum, string? nickname = null, string? avatarHash = null) : base(guid, steamID, inventory, inventoryChecksum, matchableTypes, totalInventoryCount, matchEverything, maxTradeHoldDuration, tradeToken, nickname, avatarHash) { | ||
ArgumentOutOfRangeException.ThrowIfEqual(guid, Guid.Empty); | ||
|
||
if ((steamID == 0) || !new SteamID(steamID).IsIndividualAccount) { | ||
throw new ArgumentOutOfRangeException(nameof(steamID)); | ||
} | ||
|
||
ArgumentNullException.ThrowIfNull(inventory); | ||
ArgumentException.ThrowIfNullOrEmpty(inventoryChecksum); | ||
|
||
if ((matchableTypes == null) || (matchableTypes.Count == 0)) { | ||
throw new ArgumentNullException(nameof(matchableTypes)); | ||
} | ||
|
||
ArgumentOutOfRangeException.ThrowIfZero(totalInventoryCount); | ||
ArgumentException.ThrowIfNullOrEmpty(tradeToken); | ||
|
||
if (tradeToken.Length != BotConfig.SteamTradeTokenLength) { | ||
throw new ArgumentOutOfRangeException(nameof(tradeToken)); | ||
} | ||
|
||
ArgumentNullException.ThrowIfNull(inventoryRemoved); | ||
ArgumentException.ThrowIfNullOrEmpty(previousInventoryChecksum); | ||
|
||
InventoryRemoved = inventoryRemoved.ToImmutableHashSet(); | ||
PreviousInventoryChecksum = previousInventoryChecksum; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.