chore(deps): update dependency markdig.signed to 0.39.0
[ArchiSteamFarm.git] / ArchiSteamFarm / Storage / CrashFile.cs
blob869a7d6fb6a7315bb26fb27fcdbfda8929946a5c
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.Core;
29 using ArchiSteamFarm.Helpers;
30 using ArchiSteamFarm.Helpers.Json;
31 using ArchiSteamFarm.Localization;
32 using JetBrains.Annotations;
34 namespace ArchiSteamFarm.Storage;
36 internal sealed class CrashFile : SerializableFile {
37 internal DateTime LastStartup {
38 get => BackingLastStartup;
40 set {
41 if (BackingLastStartup == value) {
42 return;
45 BackingLastStartup = value;
46 Utilities.InBackground(Save);
50 internal byte StartupCount {
51 get => BackingStartupCount;
53 set {
54 if (BackingStartupCount == value) {
55 return;
58 BackingStartupCount = value;
59 Utilities.InBackground(Save);
63 [JsonInclude]
64 private DateTime BackingLastStartup { get; set; }
66 [JsonInclude]
67 private byte BackingStartupCount { get; set; }
69 private CrashFile(string filePath) : this() {
70 ArgumentException.ThrowIfNullOrEmpty(filePath);
72 FilePath = filePath;
75 [JsonConstructor]
76 private CrashFile() { }
78 [UsedImplicitly]
79 public bool ShouldSerializeBackingLastStartup() => BackingLastStartup > DateTime.MinValue;
81 [UsedImplicitly]
82 public bool ShouldSerializeBackingStartupCount() => BackingStartupCount > 0;
84 protected override Task Save() => Save(this);
86 internal static async Task<CrashFile> CreateOrLoad(string filePath) {
87 ArgumentException.ThrowIfNullOrEmpty(filePath);
89 if (!File.Exists(filePath)) {
90 return new CrashFile(filePath);
93 CrashFile? crashFile;
95 try {
96 string json = await File.ReadAllTextAsync(filePath).ConfigureAwait(false);
98 if (string.IsNullOrEmpty(json)) {
99 ASF.ArchiLogger.LogGenericError(Strings.FormatErrorIsEmpty(nameof(json)));
101 return new CrashFile(filePath);
104 crashFile = json.ToJsonObject<CrashFile>();
105 } catch (Exception e) {
106 ASF.ArchiLogger.LogGenericException(e);
108 return new CrashFile(filePath);
111 if (crashFile == null) {
112 ASF.ArchiLogger.LogNullError(crashFile);
114 return new CrashFile(filePath);
117 crashFile.FilePath = filePath;
119 return crashFile;