chore(deps): update asf-ui digest to 6cfb52e
[ArchiSteamFarm.git] / ArchiSteamFarm.OfficialPlugins.ItemsMatcher / BotCache.cs
blobb33032ac7a1161a4684c83b10a941461495111aa
1 // ----------------------------------------------------------------------------------------------
2 // _ _ _ ____ _ _____
3 // / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
4 // / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
5 // / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
6 // /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
7 // ----------------------------------------------------------------------------------------------
8 // |
9 // Copyright 2015-2024 Ɓukasz "JustArchi" Domeradzki
10 // Contact: JustArchi@JustArchi.net
11 // |
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
15 // |
16 // http://www.apache.org/licenses/LICENSE-2.0
17 // |
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.
24 using System;
25 using System.IO;
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 {
39 [JsonDisallowNull]
40 [JsonInclude]
41 internal ConcurrentList<AssetForListing> LastAnnouncedAssetsForListing { get; private init; } = [];
43 internal string? LastAnnouncedTradeToken {
44 get => BackingLastAnnouncedTradeToken;
46 set {
47 if (BackingLastAnnouncedTradeToken == value) {
48 return;
51 BackingLastAnnouncedTradeToken = value;
52 Utilities.InBackground(Save);
56 internal string? LastInventoryChecksumBeforeDeduplication {
57 get => BackingLastInventoryChecksumBeforeDeduplication;
59 set {
60 if (BackingLastInventoryChecksumBeforeDeduplication == value) {
61 return;
64 BackingLastInventoryChecksumBeforeDeduplication = value;
65 Utilities.InBackground(Save);
69 internal DateTime? LastRequestAt {
70 get => BackingLastRequestAt;
72 set {
73 if (BackingLastRequestAt == value) {
74 return;
77 BackingLastRequestAt = value;
78 Utilities.InBackground(Save);
82 [JsonInclude]
83 private string? BackingLastAnnouncedTradeToken { get; set; }
85 [JsonInclude]
86 private string? BackingLastInventoryChecksumBeforeDeduplication { get; set; }
88 [JsonInclude]
89 private DateTime? BackingLastRequestAt { get; set; }
91 private BotCache(string filePath) : this() {
92 ArgumentException.ThrowIfNullOrEmpty(filePath);
94 FilePath = filePath;
97 [JsonConstructor]
98 private BotCache() => LastAnnouncedAssetsForListing.OnModified += OnObjectModified;
100 [UsedImplicitly]
101 public bool ShouldSerializeBackingLastAnnouncedTradeToken() => !string.IsNullOrEmpty(BackingLastAnnouncedTradeToken);
103 [UsedImplicitly]
104 public bool ShouldSerializeBackingLastInventoryChecksumBeforeDeduplication() => !string.IsNullOrEmpty(BackingLastInventoryChecksumBeforeDeduplication);
106 [UsedImplicitly]
107 public bool ShouldSerializeBackingLastRequestAt() => BackingLastRequestAt.HasValue;
109 [UsedImplicitly]
110 public bool ShouldSerializeLastAnnouncedAssetsForListing() => LastAnnouncedAssetsForListing.Count > 0;
112 protected override void Dispose(bool disposing) {
113 if (disposing) {
114 // Events we registered
115 LastAnnouncedAssetsForListing.OnModified -= OnObjectModified;
118 // Base dispose
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);
131 BotCache? botCache;
133 try {
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;
157 return botCache;
160 private async void OnObjectModified(object? sender, EventArgs e) {
161 if (string.IsNullOrEmpty(FilePath)) {
162 return;
165 await Save().ConfigureAwait(false);