Repository navigation
Expand file tree
/
Copy pathloader.ahk
More file actions
475 lines (371 loc) · 11.1 KB
/
Copy pathloader.ahk
File metadata and controls
475 lines (371 loc) · 11.1 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
*********************************************************************************
*
* loader.ahk
*
* all files are UTF-8 no BOM encoded
*
* from https://www.autohotkey.com/board/topic/10384-download-progress-bar/
*
* Copyright (c) 2022 jvr.de. All rights reserved.
*
* Licens -> Licenses.txt
*
*********************************************************************************
*/
#NoEnv
#Warn
#SingleInstance
SendMode Input
SetWorkingDir %A_ScriptDir%
fontDefault := "Calibri"
font := fontDefault
fontsizeDefault := 10
fontsize := fontsizeDefault
wrkDir := A_ScriptDir . "\"
configFile := wrkDir . "loader.ini"
appName := "Loader"
appnameLower := "loader"
extension := ".exe"
appVersion := "0.022"
bit := (A_PtrSize=8 ? "64" : "32")
if (!A_IsUnicode)
bit := "A" . bit
app := appName . " " . appVersion . " " . bit . "-bit"
theCmd := ""
result := ""
CR := "`n"
CR2 := "`n`n"
quot := """"
disableini := 0
javasourceurlDefault := "https://www.graalvm.org/downloads/index.html"
javasourceurl := javasourceurlDefault
executorDefault := "echo Hello world!"
executor := executorDefault
consoleapp := 0
classpath := ""
mainclass := ""
parameter := ""
showresult := 1
showresulttime := 5000
checkjava := 1
requiresadmin := 0
splash := "Loader running ..."
hasParams := A_Args.Length()
;Autoclose in case of failure
setTimer,exit,-30000
if (hasParams > 0){
Loop % hasParams
{
if(eq(StrLower(StrLower(A_Args[A_index])),"disableini")){
disableini := 1
}
}
}
if (!disableini && FileExist(configFile)){
readIni()
}
if (hasParams > 0)
readParams()
if (requiresadmin){
; force admin rights
if (A_IsCompiled){
allparams := ""
for keyGL, valueGL in A_Args {
allparams .= valueGL . " "
}
full_command_line := DllCall("GetCommandLine", "str")
if (!A_IsAdmin){
if (!RegExMatch(full_command_line, " /restart(?!\S)")){
try
{
tipTopTime("Restart as admin ...", 2000)
sleep, 2000
Run *RunAs %A_ScriptFullPath% /restart %allparams%
}
exitApp
}
MsgBox, Error`, could not get admin-rights`, exiting %appName% due to this error!
exit()
}
} else {
if (!A_IsAdmin){
MsgBox, Script must be run as an admin!
exit()
}
}
}
if (checkjava)
if (!isJavaOk())
installJava()
quot1 := quot
quot2 := quot
if (classpath == "")
quot1 := ""
if (parameter == "")
quot2 := ""
theCmd := trim(executor . A_Space . quot1 . classpath . quot1 . A_Space . mainclass . A_Space . quot2 . parameter . quot2)
if (splash != ""){
tipTopLeftTime(splash, 5000)
}
runwait, %comspec% /c %theCmd% | clip,,hide
result := clipboard
if (showresult){
tipTopTime(result, showresulttime)
sleep, %showresulttime%
}
if (consoleapp){
if (0 + result == 0)
exitcode := 0
else
exitcode := 1
exit(exitcode)
}
exit(0)
return
Escape::
exit(1)
;-------------------------------- readParams --------------------------------
readParams(){
global hasParams, disableini, consoleapp, showresult
global executor, classpath, mainclass, parameter, showresulttime, splash, checkjava, requiresadmin
Loop % hasParams
{
if(eq(StrLower(StrLower(A_Args[A_index])),"consoleapp")){
consoleapp := 1
}
if(eq(StrLower(StrLower(A_Args[A_index])),"showresult")){
showresult := 1
}
if(eq(StrLower(StrLower(A_Args[A_index])),"nojavacheck")){
checkjava := 1
}
if(eq(StrLower(StrLower(A_Args[A_index])),"requiresadmin")){
requiresadmin := 1
}
; parameter with arguments
FoundPos := RegExMatch(A_Args[A_index],"Oi)--executor=(.*)", m)
if(FoundPos > 0){
executor := m.value(m.Count())
}
FoundPos := RegExMatch(A_Args[A_index],"Oi)--classpath=(.*)", m)
if(FoundPos > 0){
classpath := m.value(m.Count())
}
FoundPos := RegExMatch(A_Args[A_index],"Oi)--mainclass=(.*)", m)
if(FoundPos > 0){
mainclass := m.value(m.Count())
}
FoundPos := RegExMatch(A_Args[A_index],"Oi)--parameter=(.*)", m)
if(FoundPos > 0){
parameter := m.value(m.Count())
}
FoundPos := RegExMatch(A_Args[A_index],"Oi)--showresulttime=(.*)", m)
if(FoundPos > 0){
showresulttime := m.value(m.Count())
}
FoundPos := RegExMatch(A_Args[A_index],"Oi)--splash=(.*)", m)
if(FoundPos > 0){
splash := m.value(m.Count())
}
}
return
}
;--------------------------------- StrLower ---------------------------------
StrLower(s){
r := ""
StringLower, r, s
return r
}
;---------------------------------- readIni ----------------------------------
readIni(){
global configFile
global javasourceurlDefault, javasourceurl, executorDefault, executor
global consoleapp, classpath, mainclass
global parameter, showresult, showresulttime, checkjava
global requiresadmin, splash
IniRead, javasourceurl, %configFile%, config, javasourceurl, javasourceurlDefault
IniRead, executor, %configFile%, config, executor, executorDefault
IniRead, consoleapp, %configFile%, config, consoleapp, 0
IniRead, classpath, %configFile%, config, classpath, %A_Space%
IniRead, mainclass, %configFile%, config, mainclass, %A_Space%
IniRead, parameter, %configFile%, config, parameter, %A_Space%
IniRead, showresult, %configFile%, config, showresult, 0
IniRead, showresulttime, %configFile%, config, showresulttime, 5000
IniRead, checkjava, %configFile%, config, checkjava, 1
IniRead, requiresadmin, %configFile%, config, requiresadmin, 0
IniRead, splash, %configFile%, config, splash, %A_Space%
return
}
;------------------------------------ eq ------------------------------------
eq(a, b) {
if (InStr(a, b) && InStr(b, a))
return 1
return 0
}
;--------------------------------- errorExit ---------------------------------
errorExit(theMsgArr,clp := "") {
global CR, result
msgComplete := ""
for index, element in theMsgArr
{
msgComplete .= element . CR
}
msgbox, 48, ERROR, %msgComplete%
if (clp != "")
clipboard := clp
result := msgComplete
exit(1)
return
}
;---------------------------------- CmdRet ----------------------------------
CmdRet() {
global theCmd, result
static HANDLE_FLAG_INHERIT := 0x00000001, flags := HANDLE_FLAG_INHERIT
, STARTF_USESTDHANDLES := 0x100, CREATE_NO_WINDOW := 0x08000000 , hPipeRead := "", hPipeWrite := "", sOutput := ""
callBackFuncObj := ""
encoding := "CP0"
DllCall("CreatePipe", "PtrP", hPipeRead, "PtrP", hPipeWrite, "Ptr", 0, "UInt", 0)
DllCall("SetHandleInformation", "Ptr", hPipeWrite, "UInt", flags, "UInt", HANDLE_FLAG_INHERIT)
VarSetCapacity(STARTUPINFO , siSize := A_PtrSize*4 + 4*8 + A_PtrSize*5, 0)
NumPut(siSize , STARTUPINFO)
NumPut(STARTF_USESTDHANDLES, STARTUPINFO, A_PtrSize*4 + 4*7)
NumPut(hPipeWrite , STARTUPINFO, A_PtrSize*4 + 4*8 + A_PtrSize*3)
NumPut(hPipeWrite , STARTUPINFO, A_PtrSize*4 + 4*8 + A_PtrSize*4)
VarSetCapacity(PROCESS_INFORMATION, A_PtrSize*2 + 4*2, 0)
if !DllCall("CreateProcess", "Ptr", 0, "Str", theCmd, "Ptr", 0, "Ptr", 0, "UInt", true, "UInt", CREATE_NO_WINDOW
, "Ptr", 0, "Ptr", 0, "Ptr", &STARTUPINFO, "Ptr", &PROCESS_INFORMATION)
{
DllCall("CloseHandle", "Ptr", hPipeRead)
DllCall("CloseHandle", "Ptr", hPipeWrite)
throw Exception("CreateProcess is failed")
}
DllCall("CloseHandle", "Ptr", hPipeWrite)
VarSetCapacity(sTemp, 4096), nSize := 0
while DllCall("ReadFile", "Ptr", hPipeRead, "Ptr", &sTemp, "UInt", 4096, "UIntP", nSize, "UInt", 0) {
sOutput .= stdOut := StrGet(&sTemp, nSize, encoding)
( callBackFuncObj && callBackFuncObj.Call(stdOut) )
}
DllCall("CloseHandle", "Ptr", NumGet(PROCESS_INFORMATION))
DllCall("CloseHandle", "Ptr", NumGet(PROCESS_INFORMATION, A_PtrSize))
DllCall("CloseHandle", "Ptr", hPipeRead)
result := sOutput
Return
}
;--------------------------------- isOkJava ---------------------------------
isJavaOk(){
global theCmd, result
ret := false
try {
theCmd := "java -version"
CmdRet()
javaFoundPos := RegExMatch(result,"O)version(.*)", mJava)
if(javaFoundPos > 0){
a := mJava.value(mJava.Count())
ret := true
}
} catch e {
eMsg := e.Message
msgArr := {}
msgArr.push("Error: " . eMsg)
msgArr.push("Closing Loader due to an error!")
errorExit(msgArr,eMsg)
}
result := ""
return ret
}
;-------------------------------- installJava --------------------------------
installJava(){
global javasourceurl, CR, CR2
msgLines := {}
msgLines.push("No suitable Java runtime found, please install Java first!" . CR)
msgLines.push("Closing Loader and opening your browser" . CR)
msgLines.push("with a suitable Java-download webpage now!" . CR)
msgLines.push("(Current Java selection: GraalVM.)" . CR2)
msgLines.push("Look for the latest Java installation-package, like: graalvm-ce-java11-windows-amd64-22.x.y.z.zip")
msgLines.push("Follow the docs and install Java!" . CR)
msgComplete := ""
for index, element in msgLines
{
msgComplete .= element . CR
}
msgbox,48,STOP,%msgComplete%
; a temporary file, running directory must be writeable
outputFile := "webpageloader_$$$$$$.html"
if (FileExist(outputFile))
FileDelete, %outputFile%
FileAppend,
(
<html>
<body>
<script>
document.location.href="
),%outputFile%
FileAppend,%javasourceurl%,%outputFile%
FileAppend,
(
"
</script>
</body>
</html>
),%outputFile%
run, %comspec% /c %outputFile%
sleep,5000
; remove temporary file
if (FileExist(outputFile))
FileDelete, %outputFile%
exit()
return
}
;-------------------------------- tipTopTime --------------------------------
tipTopTime(msg, t := 1000){
n := Min(n, 9)
n := Max(n, 1)
s := StrReplace(msg,"^",",")
toolX := Floor(A_ScreenWidth / 2)
toolY := 2
CoordMode, ToolTip, Screen
ToolTip,%s%, toolX, toolY
WinGetPos, X,Y,W,H, ahk_class tooltips_class32
toolX := (A_ScreenWidth / 2) - W / 2
ToolTip,%s%, toolX, toolY
duration := t * -1
settimer,tipTopTimeRemove,%duration%
return
}
;----------------------------- tipTopTimeRemove -----------------------------
tipTopTimeRemove(){
ToolTip
return
}
;------------------------------ tipTopLeftTime ------------------------------
tipTopLeftTime(msg, t := 1000){
; fixed boxnumber 9
s := StrReplace(msg,"^",",")
toolX := 5
toolY := 5
CoordMode, ToolTip, Screen
ToolTip,%s%, toolX, toolY, 9
duration := t * -1
settimer,tipTopLeftTimeRemove,%duration%
return
}
;--------------------------- tipTopLeftTimeRemove ---------------------------
tipTopLeftTimeRemove(){
ToolTip,,,9
return
}
;----------------------------------- Beep -----------------------------------
Beep(Freq, Duration) {
; example: Beep(750, 300)
if !(DllCall("kernel32.dll\Beep", "UInt", Freq, "UInt", Duration))
return DllCall("kernel32.dll\GetLastError")
return 1
}
;----------------------------------- exit -----------------------------------
exit(exitCode := 1) {
setTimer,exit,delete
exitApp, exitCode
return
}
;----------------------------------------------------------------------------