由于使用了SetOverwrite,NSIS部分大小翻了一番

我继承了一个安装程序脚本,我很恼火它声称它需要的空间是实际需要的两倍.

我发现这是由于每个部分如何使用SetOverwrite(因为它们被重新用于修复安装?).我知道由于SetOverwrite的工作原理(见下文),有必要在每个If / Else块中保留重复的File命令,但我已经确认它会导致自动段大小计算加倍.

${MementoSection} $(APP_Section_SecHelp) SecHelp
  SetDetailsPrint textonly
  DetailPrint  $(APP_DetailPrint_SecHelp)
  SetDetailsPrint listonly

  SetOutPath $INSTDIR
  SectionIn 1 2
    ${If} $SetOverwriteOn == TRUE
      SetOverwrite ifnewer
      File /r "${APP_SOURCE_DIR}\Help"
    ${Else}
      SetOverwrite off
      File /r "${APP_SOURCE_DIR}\Help"
    ${EndIf}
  SectionGetFlags ${SecHelp} $SecHelp_GetFlag
${MementoSectionEnd}

这是一个糟糕的设计模式,我应该改变吗?我是否需要添加一个hack来调用SectionGetSize,除以2并调用SectionSetSize?

最佳答案 就个人而言,我只会使用SetOverwrite ifnewer,但如果你绝对想按照自己的方式去做,那么使用SectionSetSize是一个选项.

您可以做的另一件事是将修复File / r指令放在您从该部分调用的单独函数中.函数的缺点是进度条在提取过程中移动不多.

第三种方法是将一些修复任务放在默认情况下未选中的单独部分中,并在处于修复模式时启用它.

编辑:这是一个使用SectionSetSize的示例:

!include LogicLib.nsh
InstallDir $Temp
Page Components InitComponentsPage
Page Directory
Page InstFiles  

!macro ModifySectionHack SID TEMPVAR
SectionGetSize ${SID} ${TEMPVAR}
IntOp ${TEMPVAR} ${TEMPVAR} / 2
SectionSetSize ${SID} ${TEMPVAR}
!macroend

Function InitComponentsPage
StrCpy $0 0
loop:
    ClearErrors
    SectionGetFlags $0 $1 ; The error flag is set if we try to access a section that does not exist
    IfErrors done
    !insertmacro ModifySectionHack $0 $1
    IntOp $0 $0 + 1
    Goto loop
done:
FunctionEnd

Section "Foo"
InitPluginsDir ; Need a place to extract to for this example
SetOutPath $PluginsDir

${If} 1 <> 2 ; Don't really care about the result
    SetOverwrite ifnewer
    File "${NSISDIR}\bin\makensis.exe" ; ~400kb
${Else}
    SetOverwrite off
    File "${NSISDIR}\bin\makensis.exe"
${EndIf}
SectionEnd

Section "Bar"
InitPluginsDir ; Need a place to extract to for this example
SetOutPath $PluginsDir

${If} 1 <> 2 ; Don't really care about the result
    SetOverwrite ifnewer
    File "${NSISDIR}\nsis.exe" ; ~700kb
${Else}
    SetOverwrite off
    File "${NSISDIR}\nsis.exe"
${EndIf}
SectionEnd

这循环遍历所有部分并调整所有部分,但我不确定这是否是一个好主意.如果这是我的脚本,那么我会在每个具有SetOverwrite问题的部分上手动调用ModifySectionHack:

!include LogicLib.nsh
InstallDir $Temp
Page Components
Page Directory
Page InstFiles

!macro ModifySectionHack SID TEMPVAR
SectionGetSize ${SID} ${TEMPVAR}
IntOp ${TEMPVAR} ${TEMPVAR} / 2
SectionSetSize ${SID} ${TEMPVAR}
!macroend

Section "Foo" SID_FOO
InitPluginsDir ; Need a place to extract to for this example
SetOutPath $PluginsDir

${If} 1 <> 2 ; Don't really care about the result
    SetOverwrite ifnewer
    File "${NSISDIR}\bin\makensis.exe" ; ~400kb
${Else}
    SetOverwrite off
    File "${NSISDIR}\bin\makensis.exe"
${EndIf}
SectionEnd

Section "Bar"
${If} 1 = 2
  File /r "${NSISDIR}\stubs"
${EndIf}
SectionEnd

Section "Baz" SID_BAZ
InitPluginsDir ; Need a place to extract to for this example
SetOutPath $PluginsDir

${If} 1 <> 2 ; Don't really care about the result
    SetOverwrite ifnewer
    File "${NSISDIR}\nsis.exe" ; ~700kb
${Else}
    SetOverwrite off
    File "${NSISDIR}\nsis.exe"
${EndIf}
SectionEnd

Function .onInit
; Adjust the section size for the sections that use SetOverwrite:
!insertmacro ModifySectionHack ${SID_FOO} $1
!insertmacro ModifySectionHack ${SID_BAZ} $1
FunctionEnd
点赞