-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
update.ahk
134 lines (130 loc) · 3.85 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
IfExist, updater.exe
{
FileDelete, updater.exe
}
IfExist, ___delete_me___.exe
{
FileDelete, ___delete_me___.exe
}
IniRead, logLevel, setting.ini, update, log, 0
IniRead, lastUpdate, setting.ini, update, last, 0
IniRead, autoUpdate, setting.ini, update, autoupdate, 1
IniRead, updateMirror, setting.ini, update, mirror, 1
IniWrite, % updateMirror, setting.ini, update, mirror
IniRead, version_str, setting.ini, update, ver, 0
IniRead, inst, setting.ini, update, inst, 11
log_write("Start at " A_YYYY "-" A_MM "-" A_DD, 0)
mirrorList:=["https://github.com"
,"https://ghproxy.com/https://github.com"]
updatemirrorTried:=Array()
today:=A_MM . A_DD
if(betaBuild!=1) {
if(autoUpdate) {
if(lastUpdate!=today) {
log_write("Getting Update",0)
; MsgBox,,Update,Getting Update`n获取最新版本,2
get_latest_version()
} else {
TrayTip,,% name_en " Start`nv" version "`n" name_zh "启动"
}
} else {
log_write("Update Skiped",0)
if(lastUpdate!=today) {
TrayTip, Update,Update Skiped`n跳过升级`n`nCurrent version`n当前版本`nv%version%
IniWrite, % A_MM A_DD, setting.ini, update, last
}
}
} else {
TrayTip,,% name_en " BETA Start`nv" version "`n" name_zh "内测版本启动"
}
get_latest_version(){
global
req := ComObjCreate("MSXML2.ServerXMLHTTP")
updateMirror:=updateMirror+0
if(updateMirror > mirrorList.Length() or updateMirror <= 0) {
updateMirror := 1
}
updateSite:=mirrorList[updateMirror]
updateReqDone:=0
req.open("GET", updateSite downloadUrl versionFilename, true)
req.onreadystatechange := Func("updateReady")
req.send()
SetTimer, updateTimeout, -10000
Return
updateTimeout:
tryNextUpdate()
Return
}
tryNextUpdate()
{
global mirrorList, updateMirror, updatemirrorTried
updatemirrorTried.Push(updateMirror)
SetTimer, updateTimeout, Off
For k, v in mirrorList
{
tested:=False
for _, p in updatemirrorTried
{
if(p=k) {
tested:=True
break
}
}
if not tested {
updateMirror:=k
get_latest_version()
Return
}
}
TrayTip, , % "Update failed`n`n更新失败",, 0x3
}
; with MSXML2.ServerXMLHTTP method, there would be multiple callback called
updateReady(){
global req, version, updateReqDone, updateSite, downloadUrl, downloadFilename, binaryFilename
log_write("update req.readyState=" req.readyState, 1)
if (req.readyState != 4){ ; Not done yet.
return
}
if(updateReqDone){
; log_write("state already changed", 1)
Return
}
updateReqDone := 1
log_write("update req.status=" req.status, 1)
if (req.status == 200){ ; OK.
SetTimer, updateTimeout, Off
; MsgBox % "Latest version: " req.responseText
RegExMatch(version, "(\d+)\.(\d+)\.(\d+)", verNow)
RegExMatch(req.responseText, "^(\d+)\.(\d+)\.(\d+)$", verNew)
if((verNew1>verNow1)
|| (verNew1==verNow1 && ((verNew2>verNow2)
|| (verNew2==verNow2 && verNew3>verNow3)))){
MsgBox, 0x24, Download, % "Found new version " req.responseText ", download?`n`n发现新版本 " req.responseText " 是否下载?"
IfMsgBox Yes
{
try {
IfExist, %downloadFilename%
{
FileDelete, %downloadFilename%
}
UrlDownloadToFile, % updateSite downloadUrl downloadFilename, % "./" downloadFilename
MsgBox, ,, % "Download finished`n更新下载完成`n`nProgram will restart now`n软件即将重启", 3
IniWrite, % A_MM A_DD, setting.ini, update, last
FileInstall, updater.exe, updater.exe, 1
Run, updater.exe ___ORIGIN_ME___ %downloadFilename% %binaryFilename%, A_ScriptDir, Hide
ExitApp
} catch e {
MsgBox, 16,, % "Upgrade failed`nAn exception was thrown!`nSpecifically: " e
}
}
} else {
; MsgBox, ,, % "Current version: v" version "`n`nIt is the latest version`n`n软件已是最新版本", 2
IniWrite, % A_MM A_DD, setting.ini, update, last
}
} else {
tryNextUpdate()
; MsgBox, 16,, % "Update failed`n`n更新失败`n`nStatus=" req.status
}
}