1
- using System ;
1
+ #nullable enable
2
+ using System ;
2
3
using System . Globalization ;
3
4
using System . IO ;
4
5
using System . Text . Json ;
@@ -11,62 +12,73 @@ namespace Flow.Launcher.Infrastructure.Storage
11
12
/// </summary>
12
13
public class JsonStorage < T > where T : new ( )
13
14
{
14
- protected T _data ;
15
+ protected T ? Data ;
15
16
// need a new directory name
16
17
public const string DirectoryName = "Settings" ;
17
18
public const string FileSuffix = ".json" ;
18
- public string FilePath { get ; set ; }
19
- public string DirectoryPath { get ; set ; }
19
+ protected string FilePath { get ; init ; } = null ! ;
20
+ private string TempFilePath => $ "{ FilePath } .tmp";
21
+ private string BackupFilePath => $ "{ FilePath } .bak";
22
+ protected string DirectoryPath { get ; init ; } = null ! ;
20
23
21
24
22
25
public T Load ( )
23
26
{
27
+ string ? serialized = null ;
28
+
24
29
if ( File . Exists ( FilePath ) )
25
30
{
26
- var serialized = File . ReadAllText ( FilePath ) ;
27
- if ( ! string . IsNullOrWhiteSpace ( serialized ) )
31
+ serialized = File . ReadAllText ( FilePath ) ;
32
+ }
33
+
34
+ if ( ! string . IsNullOrEmpty ( serialized ) )
35
+ {
36
+ try
28
37
{
29
- Deserialize ( serialized ) ;
38
+ Data = JsonSerializer . Deserialize < T > ( serialized ) ?? TryLoadBackup ( ) ?? LoadDefault ( ) ;
30
39
}
31
- else
40
+ catch ( JsonException )
32
41
{
33
- LoadDefault ( ) ;
42
+ Data = TryLoadBackup ( ) ?? LoadDefault ( ) ;
34
43
}
35
44
}
36
45
else
37
46
{
38
- LoadDefault ( ) ;
47
+ Data = TryLoadBackup ( ) ?? LoadDefault ( ) ;
39
48
}
40
- return _data . NonNull ( ) ;
49
+ return Data . NonNull ( ) ;
41
50
}
42
51
43
- private void Deserialize ( string serialized )
52
+ private T LoadDefault ( )
44
53
{
45
- try
46
- {
47
- _data = JsonSerializer . Deserialize < T > ( serialized ) ;
48
- }
49
- catch ( JsonException e )
54
+ if ( File . Exists ( FilePath ) )
50
55
{
51
- LoadDefault ( ) ;
52
- Log . Exception ( $ "|JsonStorage.Deserialize|Deserialize error for json <{ FilePath } >", e ) ;
56
+ BackupOriginFile ( ) ;
53
57
}
54
58
55
- if ( _data == null )
56
- {
57
- LoadDefault ( ) ;
58
- }
59
+ return new T ( ) ;
59
60
}
60
61
61
- private void LoadDefault ( )
62
+ private T ? TryLoadBackup ( )
62
63
{
63
- if ( File . Exists ( FilePath ) )
64
+ if ( ! File . Exists ( BackupFilePath ) )
65
+ return default ;
66
+
67
+ try
64
68
{
65
- BackupOriginFile ( ) ;
69
+ var data = JsonSerializer . Deserialize < T > ( File . ReadAllText ( BackupFilePath ) ) ;
70
+ if ( data != null )
71
+ {
72
+ Log . Info ( $ "|JsonStorage.Load|Failed to load settings.json, { BackupFilePath } restored successfully") ;
73
+ File . Replace ( BackupFilePath , FilePath , null ) ;
74
+ return data ;
75
+ }
76
+ return default ;
77
+ }
78
+ catch ( JsonException )
79
+ {
80
+ return default ;
66
81
}
67
-
68
- _data = new T ( ) ;
69
- Save ( ) ;
70
82
}
71
83
72
84
private void BackupOriginFile ( )
@@ -82,13 +94,14 @@ private void BackupOriginFile()
82
94
83
95
public void Save ( )
84
96
{
85
- string serialized = JsonSerializer . Serialize ( _data , new JsonSerializerOptions ( ) { WriteIndented = true } ) ;
97
+ string serialized = JsonSerializer . Serialize ( Data , new JsonSerializerOptions
98
+ {
99
+ WriteIndented = true
100
+ } ) ;
86
101
87
- File . WriteAllText ( FilePath , serialized ) ;
102
+ File . WriteAllText ( TempFilePath , serialized ) ;
103
+ File . Replace ( TempFilePath , FilePath , BackupFilePath ) ;
104
+ File . Delete ( TempFilePath ) ;
88
105
}
89
106
}
90
-
91
- [ Obsolete ( "Deprecated as of Flow Launcher v1.8.0, on 2021.06.21. " +
92
- "This is used only for Everything plugin v1.4.9 or below backwards compatibility" ) ]
93
- public class JsonStrorage < T > : JsonStorage < T > where T : new ( ) { }
94
107
}
0 commit comments