-
Notifications
You must be signed in to change notification settings - Fork 9
/
Updater.ahk
111 lines (97 loc) · 3.6 KB
/
Updater.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
;---------------------------------------------------------------------------------------------------------------------------------------------------------------
; 这里用于存放更新功能相关的代码
;---------------------------------------------------------------------------------------------------------------------------------------------------------------
Global IsTesting := False ; Turn it to "True" for download testing.
;---------------------------------------------------------------------------------------------------------------------------------------------------------------
Updater()
{
IsRequestDone := False
Random, ChosenMirror, 1, MirrorList.Length()
If (IsTesting)
Request.Open("GET", MirrorList[ChosenMirror] "/Spr-Aachen/Genshin-to-Honkai-PC-Control-Modification-Project/releases/download/v0.4.0-beta/Version.txt", True)
Else
Request.Open("GET", MirrorList[ChosenMirror] "/Spr-Aachen/Genshin-to-Honkai-PC-Control-Modification-Project/releases/latest/download/Version.txt", True) ; 打开启用异步的请求,获取最新版本号记录
Request.onreadystatechange := Func("Ready") ; 设置回调函数
Request.Send()
SetTimer, Retry, -9999
Return
}
Ready()
{
If (Request.readyState == 4) ; readyState的每次变化都会触发onreadystatechange函数而status不会,故先判断readyStatus
{
If (!IsRequestDone)
{
IsRequestDone := !IsRequestDone
If (Request.status == 200)
{
SetTimer, Retry, Off
RegExMatch(Version, "(\d+)\.(\d+)\.(\d+)", Version_Current)
RegExMatch(Request.responseText, "^(\d+)\.(\d+)\.(\d+)$", Version_Latest) ; DOMString是XMLHttpRequest返回的纯文本的值。当DOMString为null时,表示请求失败了;当DOMString为""时,表示这个请求还没有被send()。PS: StrLen(Request.responseText) <=
If ((Version_Current1*10000 + Version_Current2*100 + Version_Current3) < (Version_Latest1*10000 + Version_Latest2*100 + Version_Latest3))
{
MsgBox, 0x24, 询问, % "检测到新版本" Request.responseText "`n是否下载?"
IfMsgBox Yes
{
Try
{
FileCreateDir, ./Temp
If (IsTesting)
UrlDownloadToFile, % MirrorList[ChosenMirror] "/Spr-Aachen/Genshin-to-Honkai-PC-Control-Modification-Project/releases/download/v0.4.0-beta/BH3_Hotkey.exe", ./Temp/BH3_Hotkey.exe
Else
UrlDownloadToFile, % MirrorList[ChosenMirror] "/Spr-Aachen/Genshin-to-Honkai-PC-Control-Modification-Project/releases/latest/download/BH3_Hotkey.exe", ./Temp/BH3_Hotkey.exe
}
Catch
{
MsgBox, 16, 警告, 下载失败`n进行再次尝试
Return
}
Finally
{
MsgBox, 0, 提示, 更新下载完成`n软件即将重启
FileInstall, EXE/Installer.exe, ./Temp/Installer.exe, 1
Run, ./Temp/Installer.exe
ExitAPP
}
}
}
Else
MsgBox, 0, 提示, 当前已是最新版本
}
Else
{
;TrayTip, , % "尝试更新失败`nStatus=" Request.status, , 0x3
TryNextMirror()
}
}
}
}
TryNextMirror()
{
ChosenMirror_Tried.Push(ChosenMirror)
For Key, Value in MirrorList
{
Tried := False
For _, Element in ChosenMirror_Tried ; "_"为占位符
{
If (Element == Key)
{
Tried := True
Break
}
}
If (!Tried)
{
ChosenMirror := Key
Updater()
Return
}
}
TrayTip, , % "再次尝试更新失败`nStatus=" Request.status, , 0x3
}
Retry()
{
TryNextMirror()
Return
}
;---------------------------------------------------------------------------------------------------------------------------------------------------------------