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.
25 using System
.Collections
.Concurrent
;
26 using System
.Collections
.Generic
;
27 using System
.Text
.Json
;
28 using System
.Text
.Json
.Serialization
;
29 using ArchiSteamFarm
.Core
;
30 using ArchiSteamFarm
.Helpers
;
31 using ArchiSteamFarm
.Helpers
.Json
;
32 using JetBrains
.Annotations
;
34 namespace ArchiSteamFarm
.Storage
;
36 public abstract class GenericDatabase
: SerializableFile
{
39 private ConcurrentDictionary
<string, JsonElement
> KeyValueJsonStorage { get; init; }
= new();
42 public JsonElement
LoadFromJsonStorage(string key
) {
43 ArgumentException
.ThrowIfNullOrEmpty(key
);
45 return KeyValueJsonStorage
.GetValueOrDefault(key
);
49 public bool ShouldSerializeKeyValueJsonStorage() => !KeyValueJsonStorage
.IsEmpty
;
51 protected static void DeleteFromJsonStorage
<T
>(T genericDatabase
, string key
) where T
: GenericDatabase
{
52 ArgumentNullException
.ThrowIfNull(genericDatabase
);
53 ArgumentException
.ThrowIfNullOrEmpty(key
);
55 if (!genericDatabase
.KeyValueJsonStorage
.TryRemove(key
, out _
)) {
59 Utilities
.InBackground(() => Save(genericDatabase
));
62 protected static void SaveToJsonStorage
<TDatabase
, TValue
>(TDatabase genericDatabase
, string key
, TValue
value) where TDatabase
: GenericDatabase where TValue
: notnull
{
63 ArgumentNullException
.ThrowIfNull(genericDatabase
);
64 ArgumentException
.ThrowIfNullOrEmpty(key
);
65 ArgumentNullException
.ThrowIfNull(value);
67 JsonElement jsonElement
= value.ToJsonElement();
69 SaveToJsonStorage(genericDatabase
, key
, jsonElement
);
72 protected static void SaveToJsonStorage
<T
>(T genericDatabase
, string key
, JsonElement
value) where T
: GenericDatabase
{
73 ArgumentNullException
.ThrowIfNull(genericDatabase
);
74 ArgumentException
.ThrowIfNullOrEmpty(key
);
76 if (value.ValueKind
== JsonValueKind
.Undefined
) {
77 throw new ArgumentOutOfRangeException(nameof(value));
80 if (genericDatabase
.KeyValueJsonStorage
.TryGetValue(key
, out JsonElement currentValue
) && currentValue
.Equals(value)) {
84 genericDatabase
.KeyValueJsonStorage
[key
] = value;
85 Utilities
.InBackground(() => Save(genericDatabase
));