在 NSIS 安装程序期间自动忽略错误并记录错误消息
Automaticalli ignore error during NSIS installer and log the error message
我有一个用 NSIS 编写的安装程序,其中一部分尝试安装一些字体,但失败了:
代码部分如下所示:
SetOverwrite ifnewer
File ".\target\fonts\*.*"
WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" "Arial Narrow (TrueType)" "arialn.ttf"
WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" "Arial Narrow Bold (TrueType)" "arialnb.ttf"
WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" "Arial Narrow Bold Italic (TrueType)" "arialnbi.ttf"
WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" "Arial Narrow Italic (TrueType)" "arialni.ttf"
GetTempFileName $R0
File /oname=$R0 ".\target\additions\fonts.dll"
Push "arialn.ttf"
CallInstDLL $R0 registerFont
Push "arialnb.ttf"
CallInstDLL $R0 registerFont
Push "arialnbi.ttf"
CallInstDLL $R0 registerFont
Push "arialni.ttf"
CallInstDLL $R0 registerFont
SetOverwrite on
DetailPrint "Fonts Installed"
这条错误信息对我来说不是什么大问题,所以我可以忽略它。
我想知道,如何自动忽略这些字体安装部分的错误消息弹出窗口,但记录 (DetailPrint) 错误消息?
该对话框由 SetOverwrite
控制,但无法立即获得您想要的内容。
您需要放弃 File *
命令,改为手动处理每个文件。
如果您不关心新文件是否较旧,那么您可以使用 SetOverwrite try
:
!include LogicLib.nsh
!macro TryExtractWithDetailPrint file
SetOverwrite try
ClearErrors
File "${file}"
${If} ${Errors}
DetailPrint 'Could not extract "${file}", ignoring error...'
${EndIf}
SetOverwrite lastused
!macroend
Section
SetOutPath $Fonts
!insertmacro TryExtractWithDetailPrint "c:\myfonts\font1.ttf"
!insertmacro TryExtractWithDetailPrint "c:\myfonts\font2.ttf"
SectionEnd
如果要考虑日期戳,则需要将新文件提取到临时位置,然后通过比较新旧文件来决定是否需要尝试覆盖:
!macro Make64 high low result
System::Int64Op "${high}" << 32
IntFmt ${result} "%#x" "${low}" ; Must reformat as hex because we don't want negative numbers to be sign extended
System::Int64Op ${result} |
Pop ${result}
!macroend
!macro CompareFilesLastWrite oldfile newfile result
Push "${newfile}"
Push "${oldfile}"
Call CompareFilesLastWrite
Pop ${result}
!macroend
Function CompareFilesLastWrite
System::Store S
Pop ; oldpath
Pop ; newpath
GetFileTime $R2 $R1
IntOp $R1 $R1 & 0xfc000000 ; Chop off the bottom because of FAT
GetFileTime
IntOp & 0xfc000000 ; Chop off the bottom because of FAT
!insertmacro Make64 ; old
!insertmacro Make64 $R2 $R1 ; new
System::Call 'KERNEL32::CompareFileTime(*lr1,*lr2)i.s'
System::Store L
FunctionEnd
!macro TryExtractIfNewer src dst
InitPluginsDir
Push [=11=]
SetDateSave on ; Must be on for this to work
File "/oname=$PluginsDir\temp.tmp" "${src}"
!insertmacro CompareFilesLastWrite "${dst}" "$PluginsDir\temp.tmp" [=11=]
${If} [=11=] < 0
SetOverwrite try
ClearErrors
File "/oname=${dst}" "${src}"
${If} ${Errors}
DetailPrint 'Could not extract "${dst}", ignoring error...'
${EndIf}
SetOverwrite lastused
${Else}
DetailPrint "Existing file is newer, skipping"
${EndIf}
Pop [=11=]
!macroend
Section
!insertmacro TryExtractIfNewer "myfiles\myfont.ttf" "$fonts\myfont.ttf"
SectionEnd
我有一个用 NSIS 编写的安装程序,其中一部分尝试安装一些字体,但失败了:
代码部分如下所示:
SetOverwrite ifnewer
File ".\target\fonts\*.*"
WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" "Arial Narrow (TrueType)" "arialn.ttf"
WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" "Arial Narrow Bold (TrueType)" "arialnb.ttf"
WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" "Arial Narrow Bold Italic (TrueType)" "arialnbi.ttf"
WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" "Arial Narrow Italic (TrueType)" "arialni.ttf"
GetTempFileName $R0
File /oname=$R0 ".\target\additions\fonts.dll"
Push "arialn.ttf"
CallInstDLL $R0 registerFont
Push "arialnb.ttf"
CallInstDLL $R0 registerFont
Push "arialnbi.ttf"
CallInstDLL $R0 registerFont
Push "arialni.ttf"
CallInstDLL $R0 registerFont
SetOverwrite on
DetailPrint "Fonts Installed"
这条错误信息对我来说不是什么大问题,所以我可以忽略它。
我想知道,如何自动忽略这些字体安装部分的错误消息弹出窗口,但记录 (DetailPrint) 错误消息?
该对话框由 SetOverwrite
控制,但无法立即获得您想要的内容。
您需要放弃 File *
命令,改为手动处理每个文件。
如果您不关心新文件是否较旧,那么您可以使用 SetOverwrite try
:
!include LogicLib.nsh
!macro TryExtractWithDetailPrint file
SetOverwrite try
ClearErrors
File "${file}"
${If} ${Errors}
DetailPrint 'Could not extract "${file}", ignoring error...'
${EndIf}
SetOverwrite lastused
!macroend
Section
SetOutPath $Fonts
!insertmacro TryExtractWithDetailPrint "c:\myfonts\font1.ttf"
!insertmacro TryExtractWithDetailPrint "c:\myfonts\font2.ttf"
SectionEnd
如果要考虑日期戳,则需要将新文件提取到临时位置,然后通过比较新旧文件来决定是否需要尝试覆盖:
!macro Make64 high low result
System::Int64Op "${high}" << 32
IntFmt ${result} "%#x" "${low}" ; Must reformat as hex because we don't want negative numbers to be sign extended
System::Int64Op ${result} |
Pop ${result}
!macroend
!macro CompareFilesLastWrite oldfile newfile result
Push "${newfile}"
Push "${oldfile}"
Call CompareFilesLastWrite
Pop ${result}
!macroend
Function CompareFilesLastWrite
System::Store S
Pop ; oldpath
Pop ; newpath
GetFileTime $R2 $R1
IntOp $R1 $R1 & 0xfc000000 ; Chop off the bottom because of FAT
GetFileTime
IntOp & 0xfc000000 ; Chop off the bottom because of FAT
!insertmacro Make64 ; old
!insertmacro Make64 $R2 $R1 ; new
System::Call 'KERNEL32::CompareFileTime(*lr1,*lr2)i.s'
System::Store L
FunctionEnd
!macro TryExtractIfNewer src dst
InitPluginsDir
Push [=11=]
SetDateSave on ; Must be on for this to work
File "/oname=$PluginsDir\temp.tmp" "${src}"
!insertmacro CompareFilesLastWrite "${dst}" "$PluginsDir\temp.tmp" [=11=]
${If} [=11=] < 0
SetOverwrite try
ClearErrors
File "/oname=${dst}" "${src}"
${If} ${Errors}
DetailPrint 'Could not extract "${dst}", ignoring error...'
${EndIf}
SetOverwrite lastused
${Else}
DetailPrint "Existing file is newer, skipping"
${EndIf}
Pop [=11=]
!macroend
Section
!insertmacro TryExtractIfNewer "myfiles\myfont.ttf" "$fonts\myfont.ttf"
SectionEnd