-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcopilot-agent.cmd
1503 lines (1151 loc) · 41.7 KB
/
copilot-agent.cmd
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@REM author: wzdnzd
@REM date: 2024-05-17
@REM describe: Github Copilot Proxy Agent Controller
@echo off & PUSHD %~DP0 & cd /d "%~dp0"
@REM change encoding
chcp 65001 >nul 2>nul
@REM https://blog.csdn.net/sanqima/article/details/37818115
setlocal enableDelayedExpansion
@REM output with color
call :setESC
@REM call workflow
goto :workflow
@REM ########################
@REM function define blow ###
@REM ########################
@REM process pipeline
:workflow
@REM batch file name
set "batname=%~nx0"
@REM microsoft terminal displays differently from cmd and powershell
set "msterminal=1"
@REM info color
set "infocolor=92"
set "warncolor=93"
if "!msterminal!" == "1" (
set "infocolor=95"
set "warncolor=97"
)
@REM enable replace max tokens
set "enable_replace_max_token=1"
@REM enable set auto start
set "enable_auto_start=1"
@REM enable set auto update
set "enable_auto_update=1"
@REM enable set environment for JetBrains IDE
set "enable_set_env=1"
@REM set env flag
set "setenvflag=0"
@REM start flag
set "startflag=0"
@REM restart clash.exe
set "restartflag=0"
@REM quit flag
set "quitflag=0"
@REM exit flag
set "shouldexit=0"
@REM update
set "updateflag=0"
@REM purge
set "purgeflag=0"
@REM project address on github
set "printflag=0"
set "address=https://github.com/linux-do/override"
@REM run on background
set "asdaemon=1"
set "show=0"
@REM setting workspace
set "dest="
@REM config file name
set "config=config.json"
@REM customize
set "customize=0"
@REM default executable file name
if "!customize!" == "1" (set "defaultname=copilot-agent.exe") else (set "defaultname=override.exe")
@REM last executable file name
set "software=!defaultname!"
@REM reg key name
set "application=GithubCopilotAgent"
@REM schedule name
set "scheduledname=GithubCopilotAgentUpdate"
@REM default base url for proxy github copilot
set "default_base_url=https://cocopilot.org"
@REM ja-netfilter address
set "ja_netfilter=https://gitee.com/ja-netfilter"
set "jetbra_in=https://3.jetbra.in"
@REM base url for proxy github copilot
set "base_url="
@REM subpath for API
set "sub_path="
@REM autostart registry configuration path
set "autostartregpath=HKCU\Software\Microsoft\Windows\CurrentVersion\Run"
set "startupapproved=HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\Run"
@REM regedit path for environment
set "env_regedit_path_user=HKCU\Environment"
set "env_regedit_path_all=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
@REM default env path
set "forall=0"
@REM parse arguments
call :argsparse %*
@REM invalid arguments
if "!shouldexit!" == "1" exit /b 1
@REM print project address
if "!printflag!" == "1" goto :print_address_info
@REM set environment
if "!setenvflag!" == "1" (
if "!base_url!" == "" set "base_url=!default_base_url!"
call :check_and_set "!base_url!" "!sub_path!"
exit /b
)
@REM regular file path
if "!dest!" == "" set "dest=%~dp0"
call :pathregular dest "!dest!"
@REM executable file not exist
if not exist "!dest!" (
@echo [%ESC%[91m错误%ESC%[0m] 文件夹 "!dest!" 不存在
goto :eof
)
@REM auto start vb script
set "startupvbs=!dest!\copilot-startup.vbs"
@REM auto update vb script
set "updatevbs=!dest!\update.vbs"
@REM quit service
if "!quitflag!" == "1" goto :quit
@REM clean all setting
if "!purgeflag!" == "1" goto :purge
@REM print usage if no action
if "!startflag!" == "0" if "!restartflag!" == "0" if "!updateflag!" == "0" (
if "!shouldexit!" == "0" goto :usage
exit /b
)
@REM start service
if "!startflag!" == "1" goto :start
@REM restart service
if "!restartflag!" == "1" goto :restart
@REM update program
if "!updateflag!" == "1" goto :upgrade
exit /b
@REM parse and validate arguments
:argsparse
set result=false
if "%1" == "-a" set result=true
if "%1" == "--all" set result=true
if "!result!" == "true" (
set "forall=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-b" set result=true
if "%1" == "--base" set result=true
if "!result!" == "true" (
@REM validate argument
call :trim url "%~2"
if "!url!" == "" set result=false
if "!url:~0,7!" NEQ "http://" if "!url:~0,8!" NEQ "https://" set result=false
if "!result!" == "false" (
@echo [%ESC%[91m错误%ESC%[0m] 如果指定参数 "%ESC%[!warncolor!m--base%ESC%[0m" 或者 "%ESC%[!warncolor!m-b%ESC%[0m" 则必须提供有效的 %ESC%[!warncolor!mAPI 接口%ESC%[0m
@echo.
goto :usage
)
set "base_url=!url!"
set result=false
shift & shift & goto :argsparse
)
if "%1" == "-d" set result=true
if "%1" == "--display" set result=true
if "!result!" == "true" (
set "asdaemon=0"
set result=false
shift & goto :argsparse
)
if "%1" == "-e" set result=true
if "%1" == "--env" set result=true
if "!result!" == "true" (
set "setenvflag=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-f" set result=true
if "%1" == "--filename" set result=true
if "!result!" == "true" (
@REM validate argument
call :trim filename "%~2"
if "!filename!" == "" set result=false
if "!filename:~0,2!" == "--" set result=false
if "!filename:~0,1!" == "-" set result=false
if "!result!" == "false" (
@echo [%ESC%[91m错误%ESC%[0m] 如果指定参数 "%ESC%[!warncolor!m--filename%ESC%[0m" 或者 "%ESC%[!warncolor!m-f%ESC%[0m" 则必须提供有效的%ESC%[!warncolor!m可执行文件名%ESC%[0m
@echo.
goto :usage
)
set "software=!filename!"
set result=false
shift & shift & goto :argsparse
)
if "%1" == "-g" set result=true
if "%1" == "--github" set result=true
if "!result!" == "true" (
set "printflag=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-h" set result=true
if "%1" == "--help" set result=true
if "!result!" == "true" (
goto :usage
)
if "%1" == "-i" set result=true
if "%1" == "--interactive" set result=true
if "!result!" == "true" (
set "show=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-l" set result=true
if "%1" == "--link" set result=true
if "!result!" == "true" (
@REM validate argument
call :trim link "%~2"
if "!link!" == "" set result=false
if "!link:~0,2!" == "--" set result=false
if "!link:~0,1!" == "-" set result=false
if "!result!" == "false" (
@echo [%ESC%[91m错误%ESC%[0m] 如果指定参数 "%ESC%[!warncolor!m--link%ESC%[0m" 或者 "%ESC%[!warncolor!m-l%ESC%[0m" 则必须提供有效的%ESC%[!warncolor!m子路径%ESC%[0m
@echo.
goto :usage
)
set "sub_path=!link!"
set result=false
shift & shift & goto :argsparse
)
if "%1" == "-p" set result=true
if "%1" == "--purge" set result=true
if "!result!" == "true" (
set "purgeflag=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-q" set result=true
if "%1" == "--quit" set result=true
if "!result!" == "true" (
set "quitflag=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-r" set result=true
if "%1" == "--restart" set result=true
if "!result!" == "true" (
set "restartflag=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-s" set result=true
if "%1" == "--start" set result=true
if "!result!" == "true" (
set "startflag=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-u" set result=true
if "%1" == "--update" set result=true
if "!result!" == "true" (
set "updateflag=1"
set result=false
shift & goto :argsparse
)
if "%1" == "-w" set result=true
if "%1" == "--workspace" set result=true
if "!result!" == "true" (
@REM validate argument
call :trim param "%~2"
if "!param!" == "" set result=false
if "!param:~0,2!" == "--" set result=false
if "!param:~0,1!" == "-" set result=false
if "!result!" == "false" (
@echo [%ESC%[91m错误%ESC%[0m] 无效的参数,如果指定 "%ESC%[!warncolor!m--workspace%ESC%[0m","%ESC%[!warncolor!m!param!%ESC%[0m",则需提供有效的路径
@echo.
goto :usage
)
call :pathconvert directory "!param!"
if not exist "!directory!" (set "shouldexit=1")
if "!shouldexit!" == "1" (
@echo [%ESC%[91m错误%ESC%[0m] 参数 "%ESC%[!warncolor!m--workspace%ESC%[0m" 指定的文件夹路径 "%ESC%[!warncolor!m!directory!%ESC%[0m" %ESC%[91m无效%ESC%[0m
@echo.
goto :eof
)
set "dest=!directory!"
set result=false
shift & shift & goto :argsparse
)
if "%1" == "" goto :eof
if "%1" NEQ "" (
call :trim syntax "%~1"
if "!syntax!" == "goto" (
call :trim funcname "%~2"
if "!funcname!" == "" (
@echo [%ESC%[91m错误%ESC%[0m] 无效的语法,调用 "%ESC%[!warncolor!mgoto%ESC%[0m" 时必须提供函数名
goto :usage
)
for /f "tokens=1-2,* delims= " %%a in ("%*") do set "params=%%c"
if "!params!" == "" (
call !funcname!
exit /b
) else (
call !funcname! !params!
exit /b
)
)
@echo [%ESC%[91m错误%ESC%[0m] 未知参数:%ESC%[91m%1%ESC%[0m
@echo.
goto :usage
)
goto :eof
@REM help
:usage
@echo 使用方法:!batname! [%ESC%[!warncolor!m功能选项%ESC%[0m] [%ESC%[!warncolor!m其他参数%ESC%[0m],支持 %ESC%[!warncolor!m-%ESC%[0m 和 %ESC%[!warncolor!m--%ESC%[0m 两种模式
@echo.
@echo 功能选项:
@REM @echo. if this line contains Chinese output, it will be garbled
@echo -e, --env 设置环境变量,配合 %ESC%[!warncolor!m-a%ESC%[0m、%ESC%[!warncolor!m-b%ESC%[0m 以及 %ESC%[!warncolor!m-l%ESC%[0m 使用
@REM @echo. if this line contains Chinese output, it will be garbled
@echo -g, --github 打印 %ESC%[!warncolor!moverride 项目%ESC%[0m及 %ESC%[!warncolor!mcocopilot 平台%ESC%[0m地址
@REM @echo. if this line contains Chinese output, it will be garbled
@echo -h, --help 打印帮助信息
@echo -p, --purge 禁止服务开机自启、自动更新及环境变量设置等
@REM @echo. if this line contains Chinese output, it will be garbled
@echo -q, --quit 退出服务
@REM @echo. if this line contains Chinese output, it will be garbled
@echo -r, --restart 重启服务
@echo -s, --start 启动服务
@echo -u, --update 检查并更新 %ESC%[!warncolor!m!software!%ESC%[0m 可执行程序
echo.
@echo 其他参数:
@REM @echo. if this line contains Chinese output, it will be garbled
@echo -a, --all 为所有用户设置环境变量,默认只对当前用户有效
@REM @echo. if this line contains Chinese output, it will be garbled
@echo -b, --base 接口地址,默认为 %ESC%[!warncolor!m!default_base_url!%ESC%[0m
@REM @echo. if this line contains Chinese output, it will be garbled
@echo -d, --display 前台运行,默认启动守护进程静默执行
@REM @echo. if this line contains Chinese output, it will be garbled
@echo -f, --filename 可执行程序文件名,默认为 %ESC%[!warncolor!m!defaultname!%ESC%[0m
@echo -i, --interactive 新窗口中执行,不隐藏窗口
@REM @echo. if this line contains Chinese output, it will be garbled
@echo -l, --link API 接口子路径,默认为空
@REM @echo. if this line contains Chinese output, it will be garbled
@echo -w, --workspace %ESC%[!warncolor!m!software!%ESC%[0m 所在文件夹路径,默认为当前脚本所在目录
@echo.
set "shouldexit=1"
goto :eof
@REM print project-related sites information
:print_address_info
@echo [%ESC%[!infocolor!m信息%ESC%[0m] %ESC%[!warncolor!mcocopilot%ESC%[0m 平台地址:%ESC%[!infocolor!m!default_base_url!/dash%ESC%[0m
@echo.
@echo [%ESC%[!infocolor!m信息%ESC%[0m] %ESC%[!warncolor!moverride%ESC%[0m 项目地址:%ESC%[!infocolor!m!address!%ESC%[0m
@echo.
@echo [%ESC%[!infocolor!m信息%ESC%[0m] %ESC%[!warncolor!mja-netfilter%ESC%[0m 项目地址:%ESC%[!infocolor!m!ja_netfilter!%ESC%[0m
@echo.
@echo [%ESC%[!infocolor!m信息%ESC%[0m] %ESC%[!warncolor!mjetbra.in%ESC%[0m 网站地址:%ESC%[!infocolor!m!jetbra_in!%ESC%[0m
exit /b
@REM start program
:start
call :execute
@REM check
for /l %%i in (1,1,3) do (
@REM check running status
call :isrunning status
if "!status!" == "1" (
@echo [%ESC%[!infocolor!m信息%ESC%[0m] 服务启动%ESC%[!infocolor!m成功%ESC%[0m
call :postprocess
exit /b
) else (
@REM waiting
timeout /t 1 /nobreak >nul 2>nul
)
)
@echo [%ESC%[91m错误%ESC%[0m] 服务启动%ESC%[91m失败%ESC%[0m,请检查配置 %ESC%[91m!config!%ESC%[0m 是否正确
goto :eof
@REM execute program
:execute
if not exist "!dest!\!software!" (
@echo [%ESC%[91m错误%ESC%[0m] 可执行文件 %ESC%[91m!software!%ESC%[0m 不存在,无法启动服务
goto :eof
)
if not exist "!dest!\!config!" (
@echo [%ESC%[91m错误%ESC%[0m] 配置文件 %ESC%[91m!config!%ESC%[0m 不存在,无法启动服务
goto :eof
)
cd /d "!dest!"
if "!asdaemon!" == "1" (
@REM run on backend
if "!show!" == "1" (
start "" "!software!" >nul 2>&1
) else (
start /min /b "" "!software!" >nul 2>&1
)
) else (
@REM executing !software! directly will block and the postprocess method cannot be called
start "" "!software!"
)
goto :eof
@REM delect running status
:isrunning <result>
tasklist | findstr /i "!software!" >nul 2>nul && set "%~1=1" || set "%~1=0"
goto :eof
@REM restart program
:restart
call :isrunning status
if "!status!" == "0" (
goto :start
) else (
@REM kill process
call :privilege "goto :killprocess !software!" !show!
@REM check
for /l %%i in (1,1,5) do (
call :isrunning status
if "!status!" == "0" (
call :start
exit /b
) else (
@REM wait a moment
timeout /t 1 /nobreak >nul 2>nul
)
)
)
@echo [%ESC%[91m错误%ESC%[0m] 无法关闭进程,服务重启%ESC%[91m失败%ESC%[0m,请到%ESC%[91m任务管理中心%ESC%[0m手动退出 %ESC%[!warncolor!m!software!%ESC%[0m
goto :eof
@REM quit program
:quit
call :isrunning status
if "!status!" == "0" (
@echo [%ESC%[!infocolor!m信息%ESC%[0m] 服务%ESC%[!warncolor!m未启动%ESC%[0m,无须关闭
goto :eof
)
set "tips=[%ESC%[!warncolor!m警告%ESC%[0m] 即将退出程序,是否继续?(%ESC%[!warncolor!mY%ESC%[0m/%ESC%[!warncolor!mN%ESC%[0m) "
if "!msterminal!" == "1" (
choice /t 6 /d y /n /m "!tips!"
) else (
set /p "=!tips!" <nul
choice /t 6 /d y /n
)
if !errorlevel! == 2 exit /b 1
@REM kill process with admin
call :privilege "goto :killprocess !software!" 0
goto :eof
@REM privilege escalation
:privilege <args> <show>
set "hidewindow=0"
set "operation=%~1"
if "!operation!" == "" (
@echo [%ESC%[91m错误%ESC%[0m] 非法操作,必须指定函数名
exit /b 1
)
@REM parse window parameter
call :trim param "%~2"
set "display=" & for /f "delims=0123456789" %%i in ("!param!") do set "display=%%i"
if defined display (set "hidewindow=0") else (set "hidewindow=!param!")
if "!hidewindow!" NEQ "0" set "hidewindow=1"
cacls "%SystemDrive%\System Volume Information" >nul 2>&1 && (
if "!hidewindow!" == "1" (
!operation!
exit /b
) else (
start "" mshta vbscript:CreateObject^("Shell.Application"^).ShellExecute^("%~snx0","%~1","","runas",0^)^(window.close^)&exit /b
)
) || (start "" mshta vbscript:CreateObject^("Shell.Application"^).ShellExecute^("%~snx0","%~1","","runas",!hidewindow!^)^(window.close^)&exit /b)
goto :eof
@REM stop
:killprocess <name>
call :trim name "%~1"
if "!name!" == "" (set "name=!software!")
tasklist | findstr /i "!name!" >nul 2>nul && taskkill /im "!name!" /f >nul 2>nul
set "exitcode=!errorlevel!"
@REM no prompt
call :nopromptrunas success
@REM detect
for /l %%i in (1,1,3) do (
@REM detect running status
call :isrunning status
if "!status!" == "0" (
@echo [%ESC%[!infocolor!m信息%ESC%[0m] 服务已关闭,可使用 "%ESC%[!warncolor!m!batname! -r%ESC%[0m" 命令重启
goto :eof
) else (
@REM waiting for release
timeout /t 1 /nobreak >nul 2>nul
)
)
@echo [%ESC%[91m错误%ESC%[0m] 服务关闭失败,请到%ESC%[91m任务管理中心%ESC%[0m手动结束 %ESC%[!warncolor!m!software!%ESC%[0m 进程
goto :eof
@REM to absolute path
:pathconvert <result> <filename>
call :trim filepath %~2
set "%~1="
if "!filepath!" == "" goto :eof
@echo "!filepath!" | findstr ":" >nul 2>nul && (
set "%~1=!filepath!"
goto :eof
) || (
if "!dest!" NEQ "" (set "basedir=!dest!") else (set "basedir=%~dp0")
if "!basedir:~-1!" == "\" set "basedir=!basedir:~0,-1!"
if "!filepath!" == "." (
set "%~1=!basedir!"
goto :eof
)
set "filepath=!filepath:/=\!"
if "!filepath:~0,3!" == ".\\" (
set "%~1=!basedir!\!filepath:~3!"
) else if "!filepath:~0,2!" == ".\" (
set "%~1=!basedir!\!filepath:~2!"
) else (
set "%~1=!basedir!\!filepath!"
)
)
goto :eof
@REM compare file with md5
:md5compare <changed> <source> <target>
set "%~1=0"
call :trim source "%~2"
call :trim target "%~3"
if not exist "!source!" if not exist "!target!" goto :eof
if not exist "!source!" goto :eof
if not exist "!target!" (
set "%~1=1"
goto :eof
)
@REM source md5
set "original=" & for /F "skip=1 delims=" %%h in ('2^> nul CertUtil -hashfile "!source!" MD5') do if not defined original set "original=%%h"
@REM target md5
set "received=" & for /F "skip=1 delims=" %%h in ('2^> nul CertUtil -hashfile "!target!" MD5') do if not defined received set "received=%%h"
if "!original!" NEQ "!received!" (set "%~1=1")
goto :eof
@REM update program
:upgrade
if "!asdaemon!" == "1" (
cacls "%SystemDrive%\System Volume Information" >nul 2>&1 || (start "" mshta vbscript:CreateObject^("Shell.Application"^).ShellExecute^("%~snx0"," %*","","runas",!show!^)^(window.close^)&exit /b)
)
@REM downlaod binary if necessary
call :download binary
if "!binary!" NEQ "" if exist "!binary!" (
@REM stop process
call :privilege "goto :killprocess !software!" 0
@REM copy and replace old file
del /f /q "!dest!\!software!" >nul 2>nul
move "!binary!" "!dest!\!software!" >nul 2>nul
@echo [%ESC%[!infocolor!m信息%ESC%[0m] 检查到较新版本程序并下载完毕,即将重启服务
@REM start
call :restart
@echo [%ESC%[!infocolor!m信息%ESC%[0m] 程序更新完毕,服务已重启
) else (
@echo [%ESC%[!infocolor!m信息%ESC%[0m] 当前已是最新版本,无需更新
)
if "!show!" == "1" pause
goto :eof
@REM wintun
:download <target>
set "%~1="
set "release="
@REM amd64 or 386
call :get_arch version
if "!version!" == "" (
@echo [%ESC%[91m错误%ESC%[0m] 下载失败,无法获取 操作系统 及 CPU 架构信息
goto :eof
)
@REM extract download url
for /f "tokens=1* delims=:" %%a in ('curl --retry 5 -s -L "https://api.github.com/repos/linux-do/override/releases/latest?per_page=1" ^| findstr /i /r "https://github.com/linux-do/override/releases/download/.*/override-windows-amd64-.*.zip"') do set "release=%%b"
call :trim rawurl "!release!"
if !rawurl! == "" (
@echo [%ESC%[91m错误%ESC%[0m] 获取 !software! 下载链接失败
goto :eof
)
set "rawurl=!rawurl:~1,-1!"
call :ghproxywrapper downloadurl !rawurl!
@echo [%ESC%[!infocolor!m信息%ESC%[0m] 开始下载 !software!,下载链接:"!downloadurl!"
set "savedpath=!temp!\override.zip"
call :retrydownload "!downloadurl!" "!savedpath!"
if exist "!savedpath!" (
set "filepath=!temp!\copilot-override"
mkdir "!filepath!"
@REM unzip
tar -xzf "!savedpath!" -C !filepath! >nul 2>nul
@REM clean workspace
del /f /q "!savedpath!" >nul 2>nul
set "program=!temp!\override.exe"
del /f /q "!program!" >nul 2>nul
for /f "delims=" %%i in ('dir /ad/b/s "!filepath!"') do (
if exist "%%i\override.exe" (
move "%%i\override.exe" "!temp!" >nul 2>nul
)
)
@REM delete tmep diretory
rd "!filepath!" /s /q >nul 2>nul
if exist "!program!" (
@REM compare and update
call :md5compare diff "!program!" "!dest!\!software!"
if "!diff!" == "1" (set "%~1=!program!")
) else (
@echo [%ESC%[!warncolor!m警告%ESC%[0m] 下载 override 成功,但未找到 override.exe 文件
)
) else (
@echo [%ESC%[!warncolor!m警告%ESC%[0m] override 下载失败,请确认下载链接是否正确
)
goto :eof
@REM download with retry
:retrydownload <url> <filename>
set maxretries=3
call :trim downloadurl "%~1"
call :trim savepath "%~2"
set "valid=0"
if "!downloadurl:~0,7!" == "http://" set "valid=1"
if "!downloadurl:~0,8!" == "https://" set "valid=1"
if "!valid!" == "0" (
@echo [%ESC%[!warncolor!m警告%ESC%[0m] 下载失败,无效的下载链接:"!downloadurl!"
goto :eof
)
if "!savepath!" == "" goto :eof
set /a "count=0"
:retry
if !count! GEQ !maxretries! (
@echo [%ESC%[91m错误%ESC%[0m] 文件 %ESC%[!warncolor!m!savepath!%ESC%[0m 下载失败,已达最大重试次数,请尝试再次执行此命令
goto :eof
)
curl.exe --retry 5 --retry-max-time 120 --connect-timeout 20 -s -L -C - -o "!savepath!" "!downloadurl!"
set "failflag=!errorlevel!"
if not exist "!savepath!" set "failflag=1"
if "!failflag!" NEQ "0" (
set /a "count+=1"
@echo [%ESC%[!warncolor!m提示%ESC%[0m] 文件下载失败,正在进行第 %ESC%[!warncolor!m!count!%ESC%[0m 次重试,下载链接:!downloadurl!
goto :retry
)
goto :eof
@REM get cpu and os version, see: https://github.com/linux-do/override/releases
:get_arch <version>
set "%~1="
if "!PROCESSOR_ARCHITECTURE!" == "AMD64" (
set "%~1=amd64"
) else if "!PROCESSOR_ARCHITECTURE!" == "X86" (
set "%~1=386"
)
goto :eof
@REM config autostart and auto update
:postprocess
@REM allow change max tokens
if "!enable_replace_max_token!" == "1" call :replace_max_tokens 0
call :privilege "goto :nopromptrunas" 0
@REM allow auto start when user login
if "!enable_auto_start!" == "1" call :autostart
@REM allow auto check update
if "!enable_auto_update!" == "1" call :autoupdate
@REM set environment
if "!enable_set_env!" == "1" call :add_environment
goto :eof
@REM change max tokens to 2048
:replace_max_tokens <force>
call :trim force "%~1"
if "!force!" == "" set "force=0"
@REM subpath of the file to be replaced
set "subpath=dist\extension.js"
set "pattern=\.maxPromptCompletionTokens\(([a-zA-Z0-9_]+),([0-9]+)\)"
set "replacement=.maxPromptCompletionTokens($1,2048)"
@REM iterate over all github copilot directories
for /d %%d in (%USERPROFILE%\.vscode\extensions\github.copilot-*) do (
set "extension_path=%%d\!subpath!"
if exist "!extension_path!" (
set "backupfile=!extension_path!.bak"
@REM delete if exist backup file
if exist "!backupfile!" (
if "!force!" == "0" goto :eof
del /f /q "!backupfile!" >nul 2>nul
)
@REM backup
copy /y "!extension_path!" "!backupfile!" >nul 2>nul
@REM do search and replace with pattern
powershell -Command "(Get-Content '!extension_path!') -replace '!pattern!', '!replacement!' | Set-Content '!extension_path!'"
)
)
@echo [%ESC%[!infocolor!m信息%ESC%[0m] 最大 tokens 数值设置%ESC%[!infocolor!m完成%ESC%[0m
goto :eof
@REM recovery max tokens
:recovery_max_tokens
@REM subpath of the file to be recovery
set "subpath=dist\extension.js"
@REM iterate over all github copilot directories
for /d %%d in (%USERPROFILE%\.vscode\extensions\github.copilot-*) do (
set "extension_path=%%d\!subpath!"
set "backupfile=!extension_path!.bak"
if exist "!backupfile!" (
@REM delete if exist extension file
if exist "!extension_path!" (
del /f /q "!extension_path!" >nul 2>nul
)
@REM replace
move "!backupfile!" "!extension_path!" >nul 2>nul
@echo [%ESC%[!infocolor!m信息%ESC%[0m] 最大 tokens 数值恢复%ESC%[!infocolor!m成功%ESC%[0m
)
)
goto :eof
@REM leading and trailing whitespace
:trim <result> <rawtext>
set "rawtext=%~2"
set "%~1="
if "!rawtext!" == "" goto :eof
for /f "tokens=* delims= " %%a in ("!rawtext!") do set "rawtext=%%a"
@REM for /l %%a in (1,1,100) do if "!rawtext:~-1!"==" " set "rawtext=!rawtext:~0,-1!"
@REM for speed, iteration set to 10
for /l %%a in (1,1,10) do if "!rawtext:~-1!"==" " set "rawtext=!rawtext:~0,-1!"
set "%~1=!rawtext!"
goto :eof
@REM wrapper github
:ghproxywrapper <result> <rawurl>
set "%~1="
call :trim rawurl %~2
if "!rawurl!" == "" goto :eof
set proxy_urls[0]=https://mirror.ghproxy.com
set proxy_urls[1]=https://gh.ddlc.top
set proxy_urls[2]=https://hub.gitmirror.com
set proxy_urls[3]=https://proxy.api.030101.xyz
@REM random [0, 2]
set /a num=!random! %% 4
set "ghproxy=!proxy_urls[%num%]!"
@REM github proxy
if "!rawurl:~0,18!" == "https://github.com" set "rawurl=!ghproxy!/!rawurl!"
if "!rawurl:~0,33!" == "https://raw.githubusercontent.com" set "rawurl=!ghproxy!/!rawurl!"
if "!rawurl:~0,34!" == "https://gist.githubusercontent.com" set "rawurl=!ghproxy!/!rawurl!"
set "%~1=!rawurl!"
goto :eof
@REM replace '\\' to '\' for directory
:pathregular <result> <directory>
set "%~1="
call :trim directory "%~2"
if "!directory!" == "" goto :eof
@REM '\\' to '\'
set "directory=!directory:\\=\!"
@REM '/' to '\'
set "directory=!directory:/=\!"
@REM remove last '\'
if "!directory:~-1!" == "\" set "directory=!directory:~0,-1!"
set "%~1=!directory!"
goto :eof
@REM output with color
:setESC
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set ESC=%%b
exit /b 0
)
exit /b 0
@REM auto start when user login
:autostart
call :regquery program "!autostartregpath!" "!application!" "REG_SZ"
if "!startupvbs!" NEQ "!program!" (
set "tips=[%ESC%[!warncolor!m提示%ESC%[0m] 是否允许服务开机自启?(%ESC%[!warncolor!mY%ESC%[0m/%ESC%[!warncolor!mN%ESC%[0m) "
if "!msterminal!" == "1" (
choice /t 5 /d y /n /m "!tips!"
) else (
set /p "=!tips!" <nul
choice /t 5 /d y /n
)
if !errorlevel! == 2 exit /b 1
call :nopromptrunas success
if "!success!" == "0" (
@echo [%ESC%[91m错误%ESC%[0m] 权限受限,%ESC%[91m无法设置%ESC%[0m开机自启
goto :eof
)
call :generatevbs "!startupvbs!" "-r"
call :register success "!startupvbs!"
if "!success!" == "1" (
@echo [%ESC%[!infocolor!m信息%ESC%[0m] 服务开机自启设置%ESC%[!infocolor!m完成%ESC%[0m
) else (
@echo [%ESC%[91m错误%ESC%[0m] 服务开机自启设置%ESC%[91m失败%ESC%[0m
)
)
goto :eof
@REM disable auto start
:disableautostart <result>
set "%~1=0"
call :regquery program "!autostartregpath!" "!application!" "REG_SZ"
if "!program!" == "" (
set "%~1=1"
) else (
set "shoulddelete=1"
if "!startupvbs!" NEQ "!program!" (
set "tips=[%ESC%[!warncolor!m警告%ESC%[0m] 发现相同名字但执行路径不同的配置,是否继续?(%ESC%[!warncolor!mY%ESC%[0m/%ESC%[!warncolor!mN%ESC%[0m) "
if "!msterminal!" == "1" (
choice /t 5 /d n /n /m "!tips!"
) else (
set /p "=!tips!" <nul
choice /t 5 /d n /n
)
if !errorlevel! == 2 set "shoulddelete=0"