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

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

${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}


这是我应该改变的不良设计模式吗?我是否需要添加技巧以调用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

09-19 12:37