-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.vb
More file actions
160 lines (131 loc) · 5.94 KB
/
Program.vb
File metadata and controls
160 lines (131 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
Imports System.Configuration
Imports System.IO
Imports System.Linq
Imports System.Reflection
Imports Squirrel
Public Module Program
' <summary>
' Make a backup of our settings.
' Used to persist settings across updates.
' </summary>
Public Sub BackupSettings()
Dim settingsFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath
Dim destination = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\..\\last.config"
File.Copy(settingsFile, destination, True)
End Sub
''' <summary>
''' This code was built to handle the version setting upgrade from 1.2.6 to future versions.
''' </summary>
Private Sub HandleOldVersionSettingUpgrade()
Dim folderRoot = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"ALTTPRandomizer")
Dim matchingFolders = New DirectoryInfo(folderRoot).GetDirectories().Where(Function(x) x.Name.StartsWith("croptool.exe_Url_"))
Dim checkSubPath = "1.2.1.0\user.config"
Dim found = False
Dim mostRecent As DateTime
Dim mostRecentPath As String = Nothing
For Each folder In matchingFolders
Dim targetPath = Path.Combine(folder.FullName, checkSubPath)
If File.Exists(targetPath) Then
Dim modified = File.GetLastWriteTime(targetPath)
If Not found OrElse modified > mostRecent Then
mostRecent = modified
mostRecentPath = targetPath
found = True
End If
End If
Next
If found AndAlso mostRecentPath IsNot Nothing AndAlso File.Exists(mostRecentPath) Then
RestoreSettings(mostRecentPath, False)
End If
End Sub
' <summary>
' Restore our settings backup if any.
' Used to persist settings across updates.
' </summary>
Private Sub RestoreSettings(Optional sourceFile As String = Nothing, Optional runOldVersionUpgrade As Boolean = True)
'Restore settings after application update
Dim destFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath
If sourceFile Is Nothing Then
sourceFile = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\..\\last.config"
If runOldVersionUpgrade AndAlso Not File.Exists(sourceFile) Then
HandleOldVersionSettingUpgrade()
Exit Sub
End If
End If
' Check if we have settings that we need to restore
If Not File.Exists(sourceFile) Then
Exit Sub
End If
' Create directory as needed
Try
Directory.CreateDirectory(Path.GetDirectoryName(destFile))
Catch e As Exception
End Try
' Copy our backup file in place
Try
File.Copy(sourceFile, destFile, True)
Catch e As Exception
End Try
' Delete backup file
Try
File.Delete(sourceFile)
Catch e As Exception
End Try
My.Settings.Reload()
End Sub
Private Sub CheckForUpdate(updatePath As String)
Task.Run(Async Sub()
Try
Using updateMgr = New UpdateManager(updatePath)
BackupSettings()
Dim ReleaseEntry = Await updateMgr.UpdateApp()
GlobalParam.UpdateVersion = $"Update Version: {If(ReleaseEntry?.Version.ToString(), "No update")}"
End Using
Catch ex As Exception
MessageBox.Show($"Error checking for update: ${ex}")
End Try
End Sub)
End Sub
Private Sub CreateShortcuts(mgr As UpdateManager, v As Version)
Dim thePath = Path.Combine(Path.GetDirectoryName(Application.StartupPath), $"app-{mgr.CurrentlyInstalledVersion()}", "dashboard.exe")
mgr.CreateShortcutsForExecutable(thePath, ShortcutLocation.Desktop Or ShortcutLocation.StartMenu, False)
mgr.CreateShortcutForThisExe()
End Sub
Private Sub HandleUpdate(mgr As UpdateManager, v As Version)
CreateShortcuts(mgr, v)
RestoreSettings()
End Sub
Private Sub DeleteShortcuts(mgr As UpdateManager, v As Version)
Dim thePath = Path.Combine(Path.GetDirectoryName(Application.StartupPath), $"app-{mgr.CurrentlyInstalledVersion()}", "dashboard.exe")
mgr.RemoveShortcutsForExecutable(thePath, ShortcutLocation.Desktop Or ShortcutLocation.StartMenu)
mgr.RemoveShortcutForThisExe()
End Sub
Public Sub Main()
If My.Settings.UpgradeRequired = True Then
My.Settings.Upgrade()
My.Settings.UpgradeRequired = False
My.Settings.Save()
End If
Dim testFolder = "C:\TestReleases"
Dim updatePath = If(ConfigurationManager.AppSettings("ReleasesURL"), testFolder)
If updatePath <> testFolder OrElse System.IO.Directory.Exists(testFolder) Then
Try
Using updateMgr = New UpdateManager(updatePath)
' ReSharper disable AccessToDisposedClosure
SquirrelAwareApp.HandleEvents(
onInitialInstall:=Sub(v) CreateShortcuts(updateMgr, v),
onAppUpdate:=Sub(v) HandleUpdate(updateMgr, v),
onAppUninstall:=Sub(v) DeleteShortcuts(updateMgr, v))
' ReSharper enable AccessToDisposedClosure
End Using
CheckForUpdate(updatePath)
Catch ex As Exception
'MessageBox.Show($"Error checking for update: ${ex}")
End Try
End If
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New ObsWebSocketCropper)
End Sub
End Module