Skip to content

Commit

Permalink
fix: nsis build issue
Browse files Browse the repository at this point in the history
fix: nsis build issue

fix: nsis build issue
  • Loading branch information
greenhat616 committed Aug 26, 2024
1 parent 167de84 commit 7e3d031
Showing 1 changed file with 65 additions and 27 deletions.
92 changes: 65 additions & 27 deletions backend/tauri/templates/installer.nsi
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ ${StrLoc}
!define UNINSTALLERSIGNCOMMAND "{{uninstaller_sign_cmd}}"
!define ESTIMATEDSIZE "{{estimated_size}}"

Var ProgramDataPathVar

Name "${PRODUCTNAME}"
BrandingText "${COPYRIGHT}"
OutFile "${OUTFILE}"
Expand Down Expand Up @@ -381,6 +383,23 @@ FunctionEnd
${EndIf}
!macroend

!define FOLDERID_ProgramData "{62AB5D82-FDC1-4DC3-A9DD-070D1D495D97}"
!macro GetProgramDataPath
; 调用SHGetKnownFolderIDList获取PIDL
System::Call 'shell32::SHGetKnownFolderIDList(g"${FOLDERID_ProgramData}", i0x1000, i0, *i.r1)i.r0'
${If} $0 = 0
; 调用SHGetPathFromIDList将PIDL转换为路径
System::Call 'shell32::SHGetPathFromIDList(ir1,t.r0)'
StrCpy $ProgramDataPathVar $0 ; 将结果保存到变量
; DetailPrint "ProgramData Path: $ProgramDataPathVar"

; 释放PIDL内存
System::Call 'ole32::CoTaskMemFree(ir1)'
${Else}
DetailPrint "Failed to get ProgramData path, error code: $0"
${EndIf}
!macroend

Var PassiveMode
Function .onInit
${GetOptions} $CMDLINE "/P" $PassiveMode
Expand Down Expand Up @@ -469,9 +488,9 @@ FunctionEnd
!insertmacro CheckNyanpasuProcess "mihomo-alpha.exe" "6"
!macroend

Section CheckProcesses
!insertmacro CheckAllNyanpasuProcesses
SectionEnd
; Section CheckProcesses
; !insertmacro CheckAllNyanpasuProcesses
; SectionEnd

Section EarlyChecks
; Abort silent installer if downgrades is disabled
Expand Down Expand Up @@ -591,16 +610,29 @@ SectionEnd
; !macroend

!macro StopCoreByService
nsExec::ExecToStack `cmd /C if exist "%ProgramData%\nyanpasu-service\data\nyanpasu-service.exe" (
"%ProgramData%\nyanpasu-service\data\nyanpasu-service.exe" rpc stop-core
)`
Pop $0 ; 获取退出代码
DetailPrint "Stopping core service with exit code $0"
; 构建服务可执行文件的完整路径
StrCpy $1 "$ProgramDataPathVar\nyanpasu-service\data\nyanpasu-service.exe"

; 检查文件是否存在
IfFileExists "$1" 0 SkipStopCore

; 文件存在,执行停止核心服务
nsExec::ExecToLog '"$1" rpc stop-core'
Pop $0 ; 弹出命令执行的返回值
${If} $0 == "0"
DetailPrint "Core service stopped successfully."
${Else}
DetailPrint "Core stop failed with exit code $0"
${EndIf}
SkipStopCore:
; 如果文件不存在,打印错误
DetailPrint "Nyanpasu Service is not installed, skipping stop-core"
!macroend

Section Install
SetOutPath $INSTDIR
!insertmacro GetProgramDataPath
!insertmacro StopCoreByService
SetOutPath $INSTDIR
!insertmacro CheckAllNyanpasuProcesses
; !insertmacro CheckIfAppIsRunning

Expand Down Expand Up @@ -718,29 +750,35 @@ FunctionEnd
!macroend

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

; 如果停止服务成功(假设成功的退出代码为0),则继续删除目录
StrCmp $0 0 +2
Goto EndMacro

; 删除目录
nsExec::ExecToStack `cmd /C if exist "%ProgramData%\nyanpasu-service\" (
rmdir /s /q "%ProgramData%\nyanpasu-service"
)`
Pop $0 ; 获取删除目录的退出代码
DetailPrint "Removed service directory with exit code $0"
; 检查服务可执行文件是否存在
IfFileExists "$1" 0 Skip
nsExec::ExecToLog '"$1" stop'
Pop $0
DetailPrint "Stopping service with exit code $0"

EndMacro:
; 检查停止服务是否成功(假设0, 100, 102为成功)
IntCmp $0 0 0 StopFailed StopFailed
IntCmp $0 100 0 StopFailed StopFailed
IntCmp $0 102 0 StopFailed StopFailed
StopFailed:
Abort "Failed to stop the service. Aborting installation."

; 如果服务成功停止,继续检查目录是否存在并删除
StrCpy $2 "$ProgramDataPathVar\nyanpasu-service"
IfFileExists "$2\*" 0 Skip
RMDir /r "$2"
DetailPrint "Removed service directory successfully"

Skip:
DetailPrint "Service directory does not exist, skipping stop and remove service directory"
!macroend

Section Uninstall
!insertmacro RemoveServiceDirectory
!insertmacro GetProgramDataPath
!insertmacro StopAndRemoveServiceDirectory
!insertmacro CheckAllNyanpasuProcesses
; !insertmacro CheckIfAppIsRunning

Expand Down

0 comments on commit 7e3d031

Please sign in to comment.