NSIS自动更新安装程序本身的示例?

NSIS自动更新安装程序本身的示例?,nsis,Nsis,我正在寻找一个NSIS的例子,能够更新自己的最新版本的安装程序 逻辑很简单:检查发布URL是否比当前可执行文件更新,然后下载它并使用相同的参数调用它自己 主要是像wget-timestamp URL这样的东西,但到目前为止,我找不到任何NSIS文件下载扩展名,只有在文件较新时才能下载文件。::head将只从服务器获取HTTP头,但IMHO从服务器下载带有版本信息的.ini文件更好…我写了一些东西那可能对你有帮助。我还没有彻底测试过它,但在我运行的前几次测试中它确实起了作用。这只是我为一个项目准备

我正在寻找一个NSIS的例子,能够更新自己的最新版本的安装程序

逻辑很简单:检查发布URL是否比当前可执行文件更新,然后下载它并使用相同的参数调用它自己


主要是像wget-timestamp URL这样的东西,但到目前为止,我找不到任何NSIS文件下载扩展名,只有在文件较新时才能下载文件。

::head将只从服务器获取HTTP头,但IMHO从服务器下载带有版本信息的.ini文件更好…

我写了一些东西那可能对你有帮助。我还没有彻底测试过它,但在我运行的前几次测试中它确实起了作用。这只是我为一个项目准备的一个例子。希望对你有用

版本比较函数

UpdateCheck函数

WGetDownload函数

OpenWebPage宏

用法示例:

;=#
;= Taken and modified from:
;= http://nsis.sourceforge.net/VersionCompare
Function VersionCompare
    !define VersionCompare `!insertmacro _VersionCompare`
    !macro _VersionCompare _VER1 _VER2 _RESULT
        Push `${_VER1}`
        Push `${_VER2}`
        Call VersionCompare
        Pop ${_RESULT}
    !macroend
    Exch $1
    Exch
    Exch $0
    Exch
    Push $2
    Push $3
    Push $4
    Push $5
    Push $6
    Push $7
    BEGIN:
    StrCpy $2 -1
    IntOp $2 $2 + 1
    StrCpy $3 $0 1 $2
    StrCmp $3 '' +2
    StrCmp $3 '.' 0 -3
    StrCpy $4 $0 $2
    IntOp $2 $2 + 1
    StrCpy $0 $0 '' $2
    StrCpy $2 -1
    IntOp $2 $2 + 1
    StrCpy $3 $1 1 $2
    StrCmp $3 '' +2
    StrCmp $3 '.' 0 -3
    StrCpy $5 $1 $2
    IntOp $2 $2 + 1
    StrCpy $1 $1 '' $2
    StrCmp $4$5 '' EQUAL
    StrCpy $6 -1
    IntOp $6 $6 + 1
    StrCpy $3 $4 1 $6
    StrCmp $3 '0' -2
    StrCmp $3 '' 0 +2
    StrCpy $4 0
    StrCpy $7 -1
    IntOp $7 $7 + 1
    StrCpy $3 $5 1 $7
    StrCmp $3 '0' -2
    StrCmp $3 '' 0 +2
    StrCpy $5 0
    StrCmp $4 0 0 +2
    StrCmp $5 0 BEGIN NEWER2
    StrCmp $5 0 NEWER1
    IntCmp $6 $7 0 NEWER1 NEWER2
    StrCpy $4 '1$4'
    StrCpy $5 '1$5'
    IntCmp $4 $5 BEGIN NEWER2 NEWER1
    EQUAL:
    StrCpy $0 0
    Goto FINISH
    NEWER1:
    StrCpy $0 1
    Goto FINISH
    NEWER2:
    StrCpy $0 2
    FINISH:
    Pop $7
    Pop $6
    Pop $5
    Pop $4
    Pop $3
    Pop $2
    Pop $1
    Exch $0
FunctionEnd
;=#
;= Author: demon.devin
;= http://softables.tk/
Function UpdateCheck
    !define UpdateCheck `!insertmacro _UpdateCheck`
    !macro _UpdateCheck _NAME _LINK _URL _VERSION
        Push `${_URL}`
        Push `${_VERSION}`
        Call UpdateCheck
        Pop `${_NAME}`
        Pop `${_LINK}`
    !macroend
    Exch $1
    Exch
    Exch $0
    Push $0
    Push $1
    DetailPrint "Current Version: $1"
    GetTempFileName $R1
    NSISdl::download $0 $R1
    ReadINIStr $R0 "$R1" "PAFInfo" "Version"
    DetailPrint "Latest Version: $R0"
    ${VersionCompare} $1 $R0 $R4
    ${If} $R4 == 0
    ${OrIf} $R4 == 1
        ReadINIStr $R3 "$R1" "PAFInfo" "Name"
        StrCpy $File `$R3`
        StrCpy $R2 ""
        StrCpy $Link ``
    ${Else}
        ReadINIStr $R2 "$R1" "PAFInfo" "DownloadLink"
        StrCpy $Link `$R2`
        ReadINIStr $R3 "$R1" "PAFInfo" "Filename"
        StrCpy $File `$R3`
    ${EndIf}
    SetDetailsPrint none
    Delete $R1
    SetDetailsPrint lastused
    Pop $1
    Pop $0
    Exch $R2
    Exch
    Exch $R3
FunctionEnd
;=#
;= Author: demon.devin
;= http://softables.tk/
Function WGetDownload
    !define WGetDownload "!insertmacro _WGetDownload" 
    !macro _WGetDownload _RETURN _TEMPFILE _URL _FILENAME
        Push ${_URL}
        Push ${_FILENAME}
        Call WGetDownload
        Pop ${_TEMPFILE}
        Pop ${_RETURN}
    !macroend
    Exch $1
    Exch
    Exch $2
    Push $1
    Push $2
    !define WGET     `$PLUGINSDIR\wget.exe`
    !define WGETCMD  `"${WGET}" --html-extension --referer="http://www.google.com" --user-agent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6" -T 10 --no-check-certificate "$Link" --output-document=$File`
    MessageBox MB_OK|MB_USERICON|MB_TOPMOST "Package: $File$\r$\nLink: $Link$\r$\n$\r$\nClick OK to start download."
    DetailPrint "Downloading: $Link"
    SetOutPath `$PLUGINSDIR`
    File `${NSISDIR}\Contrib\7zip\wget.exe`
    WGET:
    SetOutPath `$PLUGINSDIR`
    ExecDos::Exec /TOSTACK `${WGETCMD}`
    Pop $0
    Pop $1
    ${If} $0 = 1
        StrCmp $2 1 +3
        StrCpy $2 1
        Goto WGET
        Goto FAIL
    ${EndIf}
    ${If} ${FileExists} "$PLUGINSDIR\$File"
        ClearErrors
        StrCpy $R5 true
        StrCpy $R7 "$PLUGINSDIR\$File"
        ${GetSize} "$PLUGINSDIR" "/M=$File /S=0K" $0 $1 $2 
        IfErrors 0 +2 
        StrCpy $0 $0 * 1024 
        IntCmp $0 100 +3 0 +3 
        IntCmp $0 0 +2 +2 0 
        IntOp $0 103 + 0 
        IntOp $0 $0 * 10 
        IntOp $0 $0 / 1024 
        StrCpy $1 "$0" "" -1 
        IntCmp $0 9 +3 +3 0 
        StrCpy $0 "$0" -1 "" 
        Goto +2 
        StrCpy $0 "0" 
        MessageBox MB_OK|MB_USERICON|MB_TOPMOST "Your file $File was successfully downloaded! ($0.$1 MiB)"
        Goto DONE
    ${Else}
        FAIL:
        StrCpy $R5 "Your file $File has failed to download using the WGet utility!"
        Goto CANCEL
    ${EndIf}
    CANCEL:
    MessageBox MB_OK|MB_ICONSTOP|MB_TOPMOST "ERROR!$\r$\n$\r$\n$R5"
    StrCpy $R5 false
    StrCpy $R7 ""
    SetErrors
    DONE:
    Pop $2
    Pop $1
    Exch $R5
    Exch
    Exch $R7
FunctionEnd
;=#
;= Author: demon.devin
;= http://softables.tk/
!define OpenWebPage '!insertmacro "_OpenWebPage"'
!macro _OpenWebPage _URL
    ExecShell open "${_URL}"
!macroend
;=#
;= Author: demon.devin
;= More NSIS help:
;= http://softables.tk/docs
Section "MAIN"
    ClearErrors
    MessageBox MB_USERICON|MB_YESNO|MB_TOPMOST `Would you like to check for any possible upgrades?` IDYES CHECK IDNO NOCHECK
    NOCHECK:
    DetailPrint "What?! How can you improve if you don't check for upgrades?"
    Goto DONE
    CHECK:
    DetailPrint "Checking for new version..."
    ${UpdateCheck} $0 $1 "http://softables.tk/repository/docs/manifests.ini" "6.25.11.0"
    StrCmp $1 "" 0 +5
    DetailPrint "You have the most recent version..."
    MessageBox MB_USERICON|MB_OK|MB_TOPMOST `$0 is up to date!`
    DetailPrint "Continuing with the rest of the operation..."
    Goto DONE
    DetailPrint "There's a new version available..."
    MessageBox MB_ICONEXCLAMATION|MB_YESNO|MB_TOPMOST "New version of $File available.$\r$\nDownload now?" IDYES YES IDNO NO
    NO:
    DetailPrint "Fine! Stick with the old stuff. See if I care..."
    Goto DONE
    YES:
    DetailPrint "Great! Downloading new updates now..."
    ${WGetDownload} $R0 $R1 "$Link" "$File"
    ${If} $R0 == true
    ${AndIf} $R1 != ""
        DetailPrint "Downloaded: $R1"
        DetailPrint "Awesome! The download was successful..."
        CopyFiles /SILENT `$R1` `$INSTDIR\WGET\$R1`
        DetailPrint "Extracting the contents now..."
        File '/oname=$PLUGINSDIR\7z.dll' `${NSISDIR}\Contrib\7zip\7z.dll`
        File '/oname=$PLUGINSDIR\7z.exe' `${NSISDIR}\Contrib\7zip\7z.exe`
        ExecDos::Exec /TOSTACK `"$PLUGINSDIR\7z.exe" x "$R1" -o"$INSTDIR" -aoa -y` "" ""
        Pop $0
        DetailPrint "All done! Cleaning up temporary files..."
        Delete `$R1`
        DetailPrint "Finished. Enjoy..."
    ${Else}
        DetailPrint "Something went wrong! The download was unsuccessful..."
        MessageBox MB_ICONEXCLAMATION|MB_YESNO|MB_TOPMOST "You can manually download the upgrades by visiting:$\r$\n$Link$\r$\n$\r$\nGo there now?" IDYES WEB IDNO DONE
        WEB:
        DetailPrint "Launching web page now... "
        SetDetailsPrint none
        ${OpenWebPage} "$Link"
        SetDetailsPrint lastused
    ${EndIf}
    DONE:
    DetailPrint "Moving right along..."
SectionEnd