Skip to content

Commit e336354

Browse files
committed
Added checks to settings.json and watcher.json to rebuild the files if they throw an error on load (corruption, broken, etc)
1 parent 5c5a47b commit e336354

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

CompactGUI.Watcher/Watcher.vb

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Imports Microsoft.Toolkit.Mvvm.ComponentModel
44
Imports CompactGUI.Core
55
Imports System.Threading
66
Imports System.Collections.Specialized
7+
Imports System.Runtime
78

89
Public Class Watcher : Inherits ObservableObject
910

@@ -121,21 +122,33 @@ Public Class Watcher : Inherits ObservableObject
121122
If Not _DataFolder.Exists Then _DataFolder.Create()
122123
If Not WatcherJSONFile.Exists Then Await WatcherJSONFile.Create().DisposeAsync()
123124

124-
Dim WatcherJSON = IO.File.ReadAllText(WatcherJSONFile.FullName)
125-
If WatcherJSON = "" Then WatcherJSON = "{}"
126-
127-
Dim ret = JsonSerializer.Deserialize(Of (DateTime, ObservableCollection(Of WatchedFolder)))(WatcherJSON, New JsonSerializerOptions With {.IncludeFields = True})
125+
Dim ret = DeserializeAndValidateJSON(WatcherJSONFile)
128126
LastAnalysed = ret.Item1
129127
Dim _WatchedFolders = ret.Item2
130128

131129

132130
Return _WatchedFolders
133-
134131
End Function
135132

133+
Private Shared Function DeserializeAndValidateJSON(inputjsonFile As IO.FileInfo) As (DateTime, ObservableCollection(Of WatchedFolder))
134+
Dim WatcherJSON = IO.File.ReadAllText(inputjsonFile.FullName)
135+
If WatcherJSON = "" Then WatcherJSON = "{}"
136+
137+
Dim validatedResult As (DateTime, ObservableCollection(Of WatchedFolder))
138+
Try
139+
validatedResult = JsonSerializer.Deserialize(Of (DateTime, ObservableCollection(Of WatchedFolder)))(WatcherJSON, New JsonSerializerOptions With {.IncludeFields = True})
140+
141+
Catch ex As Exception
142+
validatedResult = (DateTime.Now, Nothing)
143+
144+
End Try
145+
146+
Return validatedResult
147+
148+
End Function
136149
Private Sub WriteToFile()
137150

138-
Dim output = JsonSerializer.Serialize((LastAnalysed, WatchedFolders), New JsonSerializerOptions With {.IncludeFields = True})
151+
Dim output = JsonSerializer.Serialize((LastAnalysed, WatchedFolders), New JsonSerializerOptions With {.IncludeFields = True, .WriteIndented = True})
139152
IO.File.WriteAllText(WatcherJSONFile.FullName, output)
140153

141154
End Sub

CompactGUI/Models/SetttingsHandler.vb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ Public Class SettingsHandler : Inherits ObservableObject
1313
If Not DataFolder.Exists Then DataFolder.Create()
1414
If Not SettingsJSONFile.Exists Then Await SettingsJSONFile.Create().DisposeAsync()
1515

16-
Dim SettingsJSON = IO.File.ReadAllText(SettingsJSONFile.FullName)
17-
If SettingsJSON = "" Then SettingsJSON = "{}"
18-
19-
AppSettings = JsonSerializer.Deserialize(Of Settings)(SettingsJSON, New JsonSerializerOptions With {.IncludeFields = True})
16+
AppSettings = DeserializeAndValidateJSON(SettingsJSONFile)
2017

2118
If AppSettings.SettingsVersion = 0 OrElse SettingsVersion > AppSettings.SettingsVersion Then
2219
AppSettings = New Settings
@@ -33,6 +30,28 @@ Public Class SettingsHandler : Inherits ObservableObject
3330

3431
End Sub
3532

33+
Private Shared Function DeserializeAndValidateJSON(inputjsonFile As IO.FileInfo) As Settings
34+
Dim SettingsJSON = IO.File.ReadAllText(inputjsonFile.FullName)
35+
If SettingsJSON = "" Then SettingsJSON = "{}"
36+
37+
Dim validatedSettings As Settings
38+
39+
Try
40+
validatedSettings = JsonSerializer.Deserialize(Of Settings)(SettingsJSON, New JsonSerializerOptions With {.IncludeFields = True})
41+
Catch ex As Exception
42+
validatedSettings = New Settings
43+
validatedSettings.SettingsVersion = SettingsVersion
44+
45+
Dim msgError As New ModernWpf.Controls.ContentDialog With {.Title = $"Corrupted Settings File Detected", .Content = "Your settings have been reset to their default.", .CloseButtonText = "OK"}
46+
msgError.ShowAsync()
47+
48+
49+
End Try
50+
51+
Return validatedSettings
52+
53+
End Function
54+
3655

3756
Private Shared Sub InitialiseWindowSize()
3857

CompactGUI/Models/UpdateHandler.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Imports System.Text.Json
33
Public Class UpdateHandler
44

5-
Public Shared CurrentVersion As New SemVersion(3, 1, 0)
5+
Public Shared CurrentVersion As New SemVersion(3, 1, 1)
66
Public Shared NewVersion As SemVersion
77
Shared UpdateURL As String = "https://raw.githubusercontent.com/IridiumIO/CompactGUI/database/version.json"
88

0 commit comments

Comments
 (0)