Use new C# features for serializable files

This commit is contained in:
Łukasz Domeradzki
2025-08-13 22:15:41 +02:00
parent 2b43cec8fe
commit ff2a2a728d
5 changed files with 100 additions and 117 deletions

View File

@@ -41,54 +41,51 @@ internal sealed class BotCache : SerializableFile {
[JsonObjectCreationHandling(JsonObjectCreationHandling.Populate)]
internal ConcurrentList<AssetForListing> LastAnnouncedAssetsForListing { get; private init; } = [];
[JsonInclude]
[JsonPropertyName("BackingLastAnnouncedTradeToken")]
internal string? LastAnnouncedTradeToken {
get => BackingLastAnnouncedTradeToken;
get;
set {
if (BackingLastAnnouncedTradeToken == value) {
if (field == value) {
return;
}
BackingLastAnnouncedTradeToken = value;
field = value;
Utilities.InBackground(Save);
}
}
[JsonInclude]
[JsonPropertyName("BackingLastInventoryChecksumBeforeDeduplication")]
internal string? LastInventoryChecksumBeforeDeduplication {
get => BackingLastInventoryChecksumBeforeDeduplication;
get;
set {
if (BackingLastInventoryChecksumBeforeDeduplication == value) {
if (field == value) {
return;
}
BackingLastInventoryChecksumBeforeDeduplication = value;
field = value;
Utilities.InBackground(Save);
}
}
[JsonInclude]
[JsonPropertyName("BackingLastRequestAt")]
internal DateTime? LastRequestAt {
get => BackingLastRequestAt;
get;
set {
if (BackingLastRequestAt == value) {
if (field == value) {
return;
}
BackingLastRequestAt = value;
field = 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);
@@ -98,18 +95,18 @@ internal sealed class BotCache : SerializableFile {
[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;
[UsedImplicitly]
public bool ShouldSerializeLastAnnouncedTradeToken() => !string.IsNullOrEmpty(LastAnnouncedTradeToken);
[UsedImplicitly]
public bool ShouldSerializeLastInventoryChecksumBeforeDeduplication() => !string.IsNullOrEmpty(LastInventoryChecksumBeforeDeduplication);
[UsedImplicitly]
public bool ShouldSerializeLastRequestAt() => LastRequestAt.HasValue;
protected override void Dispose(bool disposing) {
if (disposing) {
// Events we registered

View File

@@ -67,7 +67,7 @@ public abstract class SerializableFile : IDisposable {
ArgumentNullException.ThrowIfNull(serializableFile);
if (string.IsNullOrEmpty(serializableFile.FilePath)) {
throw new InvalidOperationException(nameof(serializableFile.FilePath));
return;
}
if (serializableFile.ReadOnly) {

View File

@@ -50,30 +50,33 @@ public sealed class BotDatabase : GenericDatabase {
internal bool HasGamesToRedeemInBackground => GamesToRedeemInBackgroundCount > 0;
[JsonIgnore]
[JsonInclude]
[JsonPropertyName("BackingTradeRestrictionsAcknowledged")]
[PublicAPI]
public bool TradeRestrictionsAcknowledged {
get => BackingTradeRestrictionsAcknowledged;
get;
internal set {
if (BackingTradeRestrictionsAcknowledged == value) {
if (field == value) {
return;
}
BackingTradeRestrictionsAcknowledged = value;
field = value;
Utilities.InBackground(Save);
}
}
[JsonInclude]
[JsonPropertyName("BackingAccessToken")]
internal string? AccessToken {
get => BackingAccessToken;
get;
set {
if (BackingAccessToken == value) {
if (field == value) {
return;
}
BackingAccessToken = value;
field = value;
Utilities.InBackground(Save);
}
}
@@ -83,15 +86,17 @@ public sealed class BotDatabase : GenericDatabase {
[JsonObjectCreationHandling(JsonObjectCreationHandling.Populate)]
internal ConcurrentHashSet<uint> ExtraStorePackages { get; private init; } = [];
[JsonInclude]
[JsonPropertyName("BackingExtraStorePackagesRefreshedAt")]
internal DateTime ExtraStorePackagesRefreshedAt {
get => BackingExtraStorePackagesRefreshedAt;
get => field;
set {
if (BackingExtraStorePackagesRefreshedAt == value) {
if (field == value) {
return;
}
BackingExtraStorePackagesRefreshedAt = value;
field = value;
Utilities.InBackground(Save);
}
}
@@ -121,41 +126,47 @@ public sealed class BotDatabase : GenericDatabase {
[JsonObjectCreationHandling(JsonObjectCreationHandling.Populate)]
internal ConcurrentHashSet<uint> MatchActivelyBlacklistAppIDs { get; private init; } = [];
[JsonInclude]
[JsonPropertyName($"_{nameof(MobileAuthenticator)}")]
internal MobileAuthenticator? MobileAuthenticator {
get => BackingMobileAuthenticator;
get;
set {
if (BackingMobileAuthenticator == value) {
if (field == value) {
return;
}
BackingMobileAuthenticator = value;
field = value;
Utilities.InBackground(Save);
}
}
[JsonInclude]
[JsonPropertyName("BackingRefreshToken")]
internal string? RefreshToken {
get => BackingRefreshToken;
get;
set {
if (BackingRefreshToken == value) {
if (field == value) {
return;
}
BackingRefreshToken = value;
field = value;
Utilities.InBackground(Save);
}
}
[JsonInclude]
[JsonPropertyName("BackingSteamGuardData")]
internal string? SteamGuardData {
get => BackingSteamGuardData;
get;
set {
if (BackingSteamGuardData == value) {
if (field == value) {
return;
}
BackingSteamGuardData = value;
field = value;
Utilities.InBackground(Save);
}
}
@@ -165,25 +176,6 @@ public sealed class BotDatabase : GenericDatabase {
[JsonObjectCreationHandling(JsonObjectCreationHandling.Populate)]
internal ConcurrentHashSet<ulong> TradingBlacklistSteamIDs { get; private init; } = [];
[JsonInclude]
private string? BackingAccessToken { get; set; }
[JsonInclude]
private DateTime BackingExtraStorePackagesRefreshedAt { get; set; }
[JsonInclude]
[JsonPropertyName($"_{nameof(MobileAuthenticator)}")]
private MobileAuthenticator? BackingMobileAuthenticator { get; set; }
[JsonInclude]
private string? BackingRefreshToken { get; set; }
[JsonInclude]
private string? BackingSteamGuardData { get; set; }
[JsonInclude]
private bool BackingTradeRestrictionsAcknowledged { get; set; }
[JsonDisallowNull]
[JsonInclude]
private OrderedDictionary GamesToRedeemInBackground { get; init; } = new();
@@ -232,26 +224,14 @@ public sealed class BotDatabase : GenericDatabase {
}
[UsedImplicitly]
public bool ShouldSerializeBackingAccessToken() => !string.IsNullOrEmpty(BackingAccessToken);
[UsedImplicitly]
public bool ShouldSerializeBackingExtraStorePackagesRefreshedAt() => BackingExtraStorePackagesRefreshedAt > DateTime.MinValue;
[UsedImplicitly]
public bool ShouldSerializeBackingMobileAuthenticator() => BackingMobileAuthenticator != null;
[UsedImplicitly]
public bool ShouldSerializeBackingRefreshToken() => !string.IsNullOrEmpty(BackingRefreshToken);
[UsedImplicitly]
public bool ShouldSerializeBackingSteamGuardData() => !string.IsNullOrEmpty(BackingSteamGuardData);
[UsedImplicitly]
public bool ShouldSerializeBackingTradeRestrictionsAcknowledged() => BackingTradeRestrictionsAcknowledged;
public bool ShouldSerializeAccessToken() => !string.IsNullOrEmpty(AccessToken);
[UsedImplicitly]
public bool ShouldSerializeExtraStorePackages() => ExtraStorePackages.Count > 0;
[UsedImplicitly]
public bool ShouldSerializeExtraStorePackagesRefreshedAt() => ExtraStorePackagesRefreshedAt > DateTime.MinValue;
[UsedImplicitly]
public bool ShouldSerializeFarmingBlacklistAppIDs() => FarmingBlacklistAppIDs.Count > 0;
@@ -270,6 +250,18 @@ public sealed class BotDatabase : GenericDatabase {
[UsedImplicitly]
public bool ShouldSerializeMatchActivelyBlacklistAppIDs() => MatchActivelyBlacklistAppIDs.Count > 0;
[UsedImplicitly]
public bool ShouldSerializeMobileAuthenticator() => MobileAuthenticator != null;
[UsedImplicitly]
public bool ShouldSerializeRefreshToken() => !string.IsNullOrEmpty(RefreshToken);
[UsedImplicitly]
public bool ShouldSerializeSteamGuardData() => !string.IsNullOrEmpty(SteamGuardData);
[UsedImplicitly]
public bool ShouldSerializeTradeRestrictionsAcknowledged() => TradeRestrictionsAcknowledged;
[UsedImplicitly]
public bool ShouldSerializeTradingBlacklistSteamIDs() => TradingBlacklistSteamIDs.Count > 0;
@@ -285,7 +277,7 @@ public sealed class BotDatabase : GenericDatabase {
TradingBlacklistSteamIDs.OnModified -= OnObjectModified;
// Those are objects that might be null and the check should be in-place
BackingMobileAuthenticator?.Dispose();
MobileAuthenticator?.Dispose();
}
// Base dispose

View File

@@ -34,38 +34,36 @@ using JetBrains.Annotations;
namespace ArchiSteamFarm.Storage;
internal sealed class CrashFile : SerializableFile {
[JsonInclude]
[JsonPropertyName("BackingLastStartup")]
internal DateTime LastStartup {
get => BackingLastStartup;
get;
set {
if (BackingLastStartup == value) {
if (field == value) {
return;
}
BackingLastStartup = value;
field = value;
Utilities.InBackground(Save);
}
}
[JsonInclude]
[JsonPropertyName("BackingStartupCount")]
internal byte StartupCount {
get => BackingStartupCount;
get;
set {
if (BackingStartupCount == value) {
if (field == value) {
return;
}
BackingStartupCount = value;
field = value;
Utilities.InBackground(Save);
}
}
[JsonInclude]
private DateTime BackingLastStartup { get; set; }
[JsonInclude]
private byte BackingStartupCount { get; set; }
private CrashFile(string filePath) : this() {
ArgumentException.ThrowIfNullOrEmpty(filePath);
@@ -76,10 +74,10 @@ internal sealed class CrashFile : SerializableFile {
private CrashFile() { }
[UsedImplicitly]
public bool ShouldSerializeBackingLastStartup() => BackingLastStartup > DateTime.MinValue;
public bool ShouldSerializeLastStartup() => LastStartup > DateTime.MinValue;
[UsedImplicitly]
public bool ShouldSerializeBackingStartupCount() => BackingStartupCount > 0;
public bool ShouldSerializeStartupCount() => StartupCount > 0;
protected override Task Save() => Save(this);

View File

@@ -65,28 +65,32 @@ public sealed class GlobalDatabase : GenericDatabase {
[JsonObjectCreationHandling(JsonObjectCreationHandling.Populate)]
internal ObservableConcurrentDictionary<uint, byte> CardCountsPerGame { get; private init; } = new();
[JsonInclude]
[JsonPropertyName($"_{nameof(CellID)}")]
internal uint CellID {
get => BackingCellID;
get;
set {
if (BackingCellID == value) {
if (field == value) {
return;
}
BackingCellID = value;
field = value;
Utilities.InBackground(Save);
}
}
[JsonInclude]
[JsonPropertyName($"_{nameof(LastChangeNumber)}")]
internal uint LastChangeNumber {
get => BackingLastChangeNumber;
get;
set {
if (BackingLastChangeNumber == value) {
if (field == value) {
return;
}
BackingLastChangeNumber = value;
field = value;
Utilities.InBackground(Save);
}
}
@@ -96,14 +100,6 @@ public sealed class GlobalDatabase : GenericDatabase {
[JsonObjectCreationHandling(JsonObjectCreationHandling.Populate)]
internal InMemoryServerListProvider ServerListProvider { get; private init; } = new();
[JsonInclude]
[JsonPropertyName($"_{nameof(CellID)}")]
private uint BackingCellID { get; set; }
[JsonInclude]
[JsonPropertyName($"_{nameof(LastChangeNumber)}")]
private uint BackingLastChangeNumber { get; set; }
[JsonDisallowNull]
[JsonInclude]
private ConcurrentDictionary<uint, ulong> PackagesAccessTokens { get; init; } = new();
@@ -151,18 +147,18 @@ public sealed class GlobalDatabase : GenericDatabase {
SaveToJsonStorage(this, key, value);
}
[UsedImplicitly]
public bool ShouldSerializeBackingCellID() => BackingCellID != 0;
[UsedImplicitly]
public bool ShouldSerializeBackingLastChangeNumber() => BackingLastChangeNumber != 0;
[UsedImplicitly]
public bool ShouldSerializeCachedBadBots() => CachedBadBots.Count > 0;
[UsedImplicitly]
public bool ShouldSerializeCardCountsPerGame() => !CardCountsPerGame.IsEmpty;
[UsedImplicitly]
public bool ShouldSerializeCellID() => CellID != 0;
[UsedImplicitly]
public bool ShouldSerializeLastChangeNumber() => LastChangeNumber != 0;
[UsedImplicitly]
public bool ShouldSerializePackagesAccessTokens() => !PackagesAccessTokens.IsEmpty;