Files
ArchiSteamFarm/ArchiSteamFarm.OfficialPlugins.ItemsMatcher/BotCache.cs
Łukasz Domeradzki 08fb3ccb76 Fix serializable files not always getting written on change
In some cases, STJ might decide to replace the object rather than populating it. This will work for majority of properties and use cases, however, we actually set up events on some properties to notify us back if they change during runtime. That event registration did not work properly, as the object was replaced with the new, that did not have appropriate listeners set up.

Populate rather than replacing those selected properties, which fixes the problem.
2025-06-28 13:10:13 +02:00

169 lines
4.8 KiB
C#

// ----------------------------------------------------------------------------------------------
// _ _ _ ____ _ _____
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
// ----------------------------------------------------------------------------------------------
// |
// Copyright 2015-2025 Łukasz "JustArchi" Domeradzki
// Contact: JustArchi@JustArchi.net
// |
// 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.IO;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using ArchiSteamFarm.Collections;
using ArchiSteamFarm.Core;
using ArchiSteamFarm.Helpers;
using ArchiSteamFarm.Helpers.Json;
using ArchiSteamFarm.Localization;
using ArchiSteamFarm.OfficialPlugins.ItemsMatcher.Data;
using JetBrains.Annotations;
namespace ArchiSteamFarm.OfficialPlugins.ItemsMatcher;
internal sealed class BotCache : SerializableFile {
[JsonDisallowNull]
[JsonInclude]
[JsonObjectCreationHandling(JsonObjectCreationHandling.Populate)]
internal ConcurrentList<AssetForListing> LastAnnouncedAssetsForListing { get; private init; } = [];
internal string? LastAnnouncedTradeToken {
get => BackingLastAnnouncedTradeToken;
set {
if (BackingLastAnnouncedTradeToken == value) {
return;
}
BackingLastAnnouncedTradeToken = value;
Utilities.InBackground(Save);
}
}
internal string? LastInventoryChecksumBeforeDeduplication {
get => BackingLastInventoryChecksumBeforeDeduplication;
set {
if (BackingLastInventoryChecksumBeforeDeduplication == value) {
return;
}
BackingLastInventoryChecksumBeforeDeduplication = value;
Utilities.InBackground(Save);
}
}
internal DateTime? LastRequestAt {
get => BackingLastRequestAt;
set {
if (BackingLastRequestAt == value) {
return;
}
BackingLastRequestAt = value;
Utilities.InBackground(Save);
}
}
[JsonInclude]
private string? BackingLastAnnouncedTradeToken { get; set; }
[JsonInclude]
private string? BackingLastInventoryChecksumBeforeDeduplication { get; set; }
[JsonInclude]
private DateTime? BackingLastRequestAt { get; set; }
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 ShouldSerializeBackingLastInventoryChecksumBeforeDeduplication() => !string.IsNullOrEmpty(BackingLastInventoryChecksumBeforeDeduplication);
[UsedImplicitly]
public bool ShouldSerializeBackingLastRequestAt() => BackingLastRequestAt.HasValue;
[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);
}
protected override Task Save() => Save(this);
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(Strings.FormatErrorIsEmpty(nameof(json)));
return new BotCache(filePath);
}
botCache = json.ToJsonObject<BotCache>();
} 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);
}
}