1 // ----------------------------------------------------------------------------------------------
3 // / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
4 // / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
5 // / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
6 // /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
7 // ----------------------------------------------------------------------------------------------
9 // Copyright 2015-2024 Ćukasz "JustArchi" Domeradzki
10 // Contact: JustArchi@JustArchi.net
12 // Licensed under the Apache License, Version 2.0 (the "License");
13 // you may not use this file except in compliance with the License.
14 // You may obtain a copy of the License at
16 // http://www.apache.org/licenses/LICENSE-2.0
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the License is distributed on an "AS IS" BASIS,
20 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 // See the License for the specific language governing permissions and
22 // limitations under the License.
26 using System
.Text
.Json
.Serialization
;
27 using System
.Threading
.Tasks
;
28 using ArchiSteamFarm
.Collections
;
29 using ArchiSteamFarm
.Core
;
30 using ArchiSteamFarm
.Helpers
;
31 using ArchiSteamFarm
.Helpers
.Json
;
32 using ArchiSteamFarm
.Localization
;
33 using ArchiSteamFarm
.OfficialPlugins
.ItemsMatcher
.Data
;
34 using JetBrains
.Annotations
;
36 namespace ArchiSteamFarm
.OfficialPlugins
.ItemsMatcher
;
38 internal sealed class BotCache
: SerializableFile
{
41 internal ConcurrentList
<AssetForListing
> LastAnnouncedAssetsForListing { get; private init; }
= [];
43 internal string? LastAnnouncedTradeToken
{
44 get => BackingLastAnnouncedTradeToken
;
47 if (BackingLastAnnouncedTradeToken
== value) {
51 BackingLastAnnouncedTradeToken
= value;
52 Utilities
.InBackground(Save
);
56 internal string? LastInventoryChecksumBeforeDeduplication
{
57 get => BackingLastInventoryChecksumBeforeDeduplication
;
60 if (BackingLastInventoryChecksumBeforeDeduplication
== value) {
64 BackingLastInventoryChecksumBeforeDeduplication
= value;
65 Utilities
.InBackground(Save
);
69 internal DateTime
? LastRequestAt
{
70 get => BackingLastRequestAt
;
73 if (BackingLastRequestAt
== value) {
77 BackingLastRequestAt
= value;
78 Utilities
.InBackground(Save
);
83 private string? BackingLastAnnouncedTradeToken { get; set; }
86 private string? BackingLastInventoryChecksumBeforeDeduplication { get; set; }
89 private DateTime
? BackingLastRequestAt { get; set; }
91 private BotCache(string filePath
) : this() {
92 ArgumentException
.ThrowIfNullOrEmpty(filePath
);
98 private BotCache() => LastAnnouncedAssetsForListing
.OnModified
+= OnObjectModified
;
101 public bool ShouldSerializeBackingLastAnnouncedTradeToken() => !string.IsNullOrEmpty(BackingLastAnnouncedTradeToken
);
104 public bool ShouldSerializeBackingLastInventoryChecksumBeforeDeduplication() => !string.IsNullOrEmpty(BackingLastInventoryChecksumBeforeDeduplication
);
107 public bool ShouldSerializeBackingLastRequestAt() => BackingLastRequestAt
.HasValue
;
110 public bool ShouldSerializeLastAnnouncedAssetsForListing() => LastAnnouncedAssetsForListing
.Count
> 0;
112 protected override void Dispose(bool disposing
) {
114 // Events we registered
115 LastAnnouncedAssetsForListing
.OnModified
-= OnObjectModified
;
119 base.Dispose(disposing
);
122 protected override Task
Save() => Save(this);
124 internal static async Task
<BotCache
> CreateOrLoad(string filePath
) {
125 ArgumentException
.ThrowIfNullOrEmpty(filePath
);
127 if (!File
.Exists(filePath
)) {
128 return new BotCache(filePath
);
134 string json
= await File
.ReadAllTextAsync(filePath
).ConfigureAwait(false);
136 if (string.IsNullOrEmpty(json
)) {
137 ASF
.ArchiLogger
.LogGenericError(Strings
.FormatErrorIsEmpty(nameof(json
)));
139 return new BotCache(filePath
);
142 botCache
= json
.ToJsonObject
<BotCache
>();
143 } catch (Exception e
) {
144 ASF
.ArchiLogger
.LogGenericException(e
);
146 return new BotCache(filePath
);
149 if (botCache
== null) {
150 ASF
.ArchiLogger
.LogNullError(botCache
);
152 return new BotCache(filePath
);
155 botCache
.FilePath
= filePath
;
160 private async void OnObjectModified(object? sender
, EventArgs e
) {
161 if (string.IsNullOrEmpty(FilePath
)) {
165 await Save().ConfigureAwait(false);