Skip to content

Commit 7e3d031

Browse files
committed
fix: nsis build issue
fix: nsis build issue fix: nsis build issue
1 parent 167de84 commit 7e3d031

File tree

1 file changed

+65
-27
lines changed

1 file changed

+65
-27
lines changed

backend/tauri/templates/installer.nsi

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ ${StrLoc}
4848
!define UNINSTALLERSIGNCOMMAND "{{uninstaller_sign_cmd}}"
4949
!define ESTIMATEDSIZE "{{estimated_size}}"
5050

51+
Var ProgramDataPathVar
52+
5153
Name "${PRODUCTNAME}"
5254
BrandingText "${COPYRIGHT}"
5355
OutFile "${OUTFILE}"
@@ -381,6 +383,23 @@ FunctionEnd
381383
${EndIf}
382384
!macroend
383385

386+
!define FOLDERID_ProgramData "{62AB5D82-FDC1-4DC3-A9DD-070D1D495D97}"
387+
!macro GetProgramDataPath
388+
; 调用SHGetKnownFolderIDList获取PIDL
389+
System::Call 'shell32::SHGetKnownFolderIDList(g"${FOLDERID_ProgramData}", i0x1000, i0, *i.r1)i.r0'
390+
${If} $0 = 0
391+
; 调用SHGetPathFromIDList将PIDL转换为路径
392+
System::Call 'shell32::SHGetPathFromIDList(ir1,t.r0)'
393+
StrCpy $ProgramDataPathVar $0 ; 将结果保存到变量
394+
; DetailPrint "ProgramData Path: $ProgramDataPathVar"
395+
396+
; 释放PIDL内存
397+
System::Call 'ole32::CoTaskMemFree(ir1)'
398+
${Else}
399+
DetailPrint "Failed to get ProgramData path, error code: $0"
400+
${EndIf}
401+
!macroend
402+
384403
Var PassiveMode
385404
Function .onInit
386405
${GetOptions} $CMDLINE "/P" $PassiveMode
@@ -469,9 +488,9 @@ FunctionEnd
469488
!insertmacro CheckNyanpasuProcess "mihomo-alpha.exe" "6"
470489
!macroend
471490

472-
Section CheckProcesses
473-
!insertmacro CheckAllNyanpasuProcesses
474-
SectionEnd
491+
; Section CheckProcesses
492+
; !insertmacro CheckAllNyanpasuProcesses
493+
; SectionEnd
475494

476495
Section EarlyChecks
477496
; Abort silent installer if downgrades is disabled
@@ -591,16 +610,29 @@ SectionEnd
591610
; !macroend
592611

593612
!macro StopCoreByService
594-
nsExec::ExecToStack `cmd /C if exist "%ProgramData%\nyanpasu-service\data\nyanpasu-service.exe" (
595-
"%ProgramData%\nyanpasu-service\data\nyanpasu-service.exe" rpc stop-core
596-
)`
597-
Pop $0 ; 获取退出代码
598-
DetailPrint "Stopping core service with exit code $0"
613+
; 构建服务可执行文件的完整路径
614+
StrCpy $1 "$ProgramDataPathVar\nyanpasu-service\data\nyanpasu-service.exe"
615+
616+
; 检查文件是否存在
617+
IfFileExists "$1" 0 SkipStopCore
618+
619+
; 文件存在,执行停止核心服务
620+
nsExec::ExecToLog '"$1" rpc stop-core'
621+
Pop $0 ; 弹出命令执行的返回值
622+
${If} $0 == "0"
623+
DetailPrint "Core service stopped successfully."
624+
${Else}
625+
DetailPrint "Core stop failed with exit code $0"
626+
${EndIf}
627+
SkipStopCore:
628+
; 如果文件不存在,打印错误
629+
DetailPrint "Nyanpasu Service is not installed, skipping stop-core"
599630
!macroend
600631

601632
Section Install
602-
SetOutPath $INSTDIR
633+
!insertmacro GetProgramDataPath
603634
!insertmacro StopCoreByService
635+
SetOutPath $INSTDIR
604636
!insertmacro CheckAllNyanpasuProcesses
605637
; !insertmacro CheckIfAppIsRunning
606638

@@ -718,29 +750,35 @@ FunctionEnd
718750
!macroend
719751

720752
!macro StopAndRemoveServiceDirectory
721-
; 先停止服务
722-
nsExec::ExecToStack `cmd /C if exist "%ProgramData%\nyanpasu-service\data\nyanpasu-service.exe" (
723-
"%ProgramData%\nyanpasu-service\data\nyanpasu-service.exe" stop
724-
)`
725-
Pop $0 ; 获取停止服务的退出代码
726-
DetailPrint "Stopping service with exit code $0"
753+
; 构建服务路径
754+
StrCpy $1 "$ProgramDataPathVar\nyanpasu-service\data\nyanpasu-service.exe"
727755

728-
; 如果停止服务成功(假设成功的退出代码为0),则继续删除目录
729-
StrCmp $0 0 +2
730-
Goto EndMacro
731-
732-
; 删除目录
733-
nsExec::ExecToStack `cmd /C if exist "%ProgramData%\nyanpasu-service\" (
734-
rmdir /s /q "%ProgramData%\nyanpasu-service"
735-
)`
736-
Pop $0 ; 获取删除目录的退出代码
737-
DetailPrint "Removed service directory with exit code $0"
756+
; 检查服务可执行文件是否存在
757+
IfFileExists "$1" 0 Skip
758+
nsExec::ExecToLog '"$1" stop'
759+
Pop $0
760+
DetailPrint "Stopping service with exit code $0"
738761

739-
EndMacro:
762+
; 检查停止服务是否成功(假设0, 100, 102为成功)
763+
IntCmp $0 0 0 StopFailed StopFailed
764+
IntCmp $0 100 0 StopFailed StopFailed
765+
IntCmp $0 102 0 StopFailed StopFailed
766+
StopFailed:
767+
Abort "Failed to stop the service. Aborting installation."
768+
769+
; 如果服务成功停止,继续检查目录是否存在并删除
770+
StrCpy $2 "$ProgramDataPathVar\nyanpasu-service"
771+
IfFileExists "$2\*" 0 Skip
772+
RMDir /r "$2"
773+
DetailPrint "Removed service directory successfully"
774+
775+
Skip:
776+
DetailPrint "Service directory does not exist, skipping stop and remove service directory"
740777
!macroend
741778

742779
Section Uninstall
743-
!insertmacro RemoveServiceDirectory
780+
!insertmacro GetProgramDataPath
781+
!insertmacro StopAndRemoveServiceDirectory
744782
!insertmacro CheckAllNyanpasuProcesses
745783
; !insertmacro CheckIfAppIsRunning
746784

0 commit comments

Comments
 (0)