Update actions/attest-build-provenance action to v1.3.1
[ArchiSteamFarm.git] / ArchiSteamFarm / Storage / CrashFile.cs
blob4c68ccd7f85f2c3054d6378eb04c31810fd725f3
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.Globalization;
26 using System.IO;
27 using System.Text.Json.Serialization;
28 using System.Threading.Tasks;
29 using ArchiSteamFarm.Core;
30 using ArchiSteamFarm.Helpers;
31 using ArchiSteamFarm.Helpers.Json;
32 using ArchiSteamFarm.Localization;
33 using JetBrains.Annotations;
35 namespace ArchiSteamFarm.Storage;
37 internal sealed class CrashFile : SerializableFile {
38 internal DateTime LastStartup {
39 get => BackingLastStartup;
41 set {
42 if (BackingLastStartup == value) {
43 return;
46 BackingLastStartup = value;
47 Utilities.InBackground(Save);
51 internal byte StartupCount {
52 get => BackingStartupCount;
54 set {
55 if (BackingStartupCount == value) {
56 return;
59 BackingStartupCount = value;
60 Utilities.InBackground(Save);
64 [JsonInclude]
65 private DateTime BackingLastStartup { get; set; }
67 [JsonInclude]
68 private byte BackingStartupCount { get; set; }
70 private CrashFile(string filePath) : this() {
71 ArgumentException.ThrowIfNullOrEmpty(filePath);
73 FilePath = filePath;
76 [JsonConstructor]
77 private CrashFile() { }
79 [UsedImplicitly]
80 public bool ShouldSerializeBackingLastStartup() => BackingLastStartup > DateTime.MinValue;
82 [UsedImplicitly]
83 public bool ShouldSerializeBackingStartupCount() => BackingStartupCount > 0;
85 protected override Task Save() => Save(this);
87 internal static async Task<CrashFile> CreateOrLoad(string filePath) {
88 ArgumentException.ThrowIfNullOrEmpty(filePath);
90 if (!File.Exists(filePath)) {
91 return new CrashFile(filePath);
94 CrashFile? crashFile;
96 try {
97 string json = await File.ReadAllTextAsync(filePath).ConfigureAwait(false);
99 if (string.IsNullOrEmpty(json)) {
100 ASF.ArchiLogger.LogGenericError(string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsEmpty, nameof(json)));
102 return new CrashFile(filePath);
105 crashFile = json.ToJsonObject<CrashFile>();
106 } catch (Exception e) {
107 ASF.ArchiLogger.LogGenericException(e);
109 return new CrashFile(filePath);
112 if (crashFile == null) {
113 ASF.ArchiLogger.LogNullError(crashFile);
115 return new CrashFile(filePath);
118 crashFile.FilePath = filePath;
120 return crashFile;