-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.ahk
134 lines (127 loc) · 3.55 KB
/
update.ahk
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
#include meta.ahk
if FileExist("updater.exe") {
try {
FileDelete("updater.exe")
}
}
if FileExist("___delete_me___.exe") {
try {
FileDelete("___delete_me___.exe")
}
}
if !FileExist("setting.ini") {
IniWrite(version, "setting.ini", "update", "ver")
}
lastUpdate := IniRead("setting.ini", "update", "last", 0)
autoUpdate := IniRead("setting.ini", "update", "autoupdate", 1)
updateMirror := IniRead("setting.ini", "update", "mirror", 1)
IniWrite(updateMirror, "setting.ini", "update", "mirror")
mirrorList := [
"https://github.com",
"https://mirror.ghproxy.com/https://github.com",
]
updatemirrorTried := Array()
today := A_MM . A_DD
if (autoUpdate) {
if (lastUpdate != today) {
get_latest_version()
} else {
version_str := IniRead("setting.ini", "update", "ver", "0")
if (version_str != version) {
IniWrite(version, "setting.ini", "update", "ver")
MsgBox(version . "`nUpdate log`n`n" . update_log)
}
}
} else {
TrayTip "Update Skiped`n`nCurrent version`nv" version, "Update", 1
}
updateTimeout(*)
{
tryNextUpdate()
Return
}
get_latest_version() {
global
req := ComObject("MSXML2.ServerXMLHTTP")
updateMirror := IsNumber(updateMirror) ? updateMirror + 0 : 1
if (updateMirror > mirrorList.Length or updateMirror <= 0) {
updateMirror := 1
}
updateSite := mirrorList[updateMirror]
; MsgBox("GET:" . mirrorList[updateMirror] . downloadUrl . versionFilename)
updateReqDone := 0
req.open("GET", mirrorList[updateMirror] . downloadUrl . versionFilename, true)
req.onreadystatechange := updateReady
req.send()
SetTimer(updateTimeout, -10000)
Return
}
tryNextUpdate()
{
global mirrorList, updateMirror, updatemirrorTried
SetTimer(updateTimeout, 0)
updatemirrorTried.Push(updateMirror)
For k, v in mirrorList
{
local tested
tested := False
for , p in updatemirrorTried
{
if (p = k) {
tested := True
break
}
}
if not tested {
updateMirror := k
get_latest_version()
Return
}
}
TrayTip "Update Timeout", "update failed", 0x3
}
; with MSXML2.ServerXMLHTTP method, there would be multiple callback called
updateReady() {
global req, version, updateReqDone, downloadUrl, downloadFilename, mirrorList, updateMirror, updatemirrorTried
; log("update req.readyState=" req.readyState, 1)
if (req.readyState != 4) { ; Not done yet.
return
}
if (updateReqDone) {
; log("state already changed", 1)
Return
}
updateReqDone := 1
; log("update req.status=" req.status, 1)
if (req.status == 200 and StrLen(req.responseText) <= 64) { ; OK.
SetTimer(updateTimeout, 0)
; MsgBox % "Latest version: " req.responseText
RegExMatch(version, "(\d+)\.(\d+)\.(\d+)", &verNow)
RegExMatch(req.responseText, "^(\d+)\.(\d+)\.(\d+)$", &verNew)
if ((verNew[1] > verNow[1])
|| (verNew[1] == verNow[1] && ((verNew[2] > verNow[2])
|| (verNew[2] == verNow[2] && verNew[3] > verNow[3])))) {
result := MsgBox("Found new version " . req.responseText . ", download?", "Download", 0x2024)
if result = "Yes"
{
try {
Download(mirrorList[updateMirror] . downloadUrl . downloadFilename, "./" . downloadFilename)
MsgBox("Download finished`nProgram will restart now", , "T3")
todayUpdated()
FileInstall("updater.exe", "updater.exe", 1)
Run("updater.exe ___ORIGIN_ME___ " downloadFilename " " binaryFilename, A_ScriptDir, "Hide")
ExitApp
} catch as e {
TrayTip "An exception was thrown!`nSpecifically: " . e.Message, "upgrade failed", 0x3
}
}
} else {
todayUpdated()
}
} else {
tryNextUpdate()
}
}
todayUpdated() {
IniWrite(A_MM . A_DD, "setting.ini", "update", "last")
}