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
.Globalization
;
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
;
42 if (BackingLastStartup
== value) {
46 BackingLastStartup
= value;
47 Utilities
.InBackground(Save
);
51 internal byte StartupCount
{
52 get => BackingStartupCount
;
55 if (BackingStartupCount
== value) {
59 BackingStartupCount
= value;
60 Utilities
.InBackground(Save
);
65 private DateTime BackingLastStartup { get; set; }
68 private byte BackingStartupCount { get; set; }
70 private CrashFile(string filePath
) : this() {
71 ArgumentException
.ThrowIfNullOrEmpty(filePath
);
77 private CrashFile() { }
80 public bool ShouldSerializeBackingLastStartup() => BackingLastStartup
> DateTime
.MinValue
;
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
);
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
;