Display of product versions? - nsis

My product has version w.x.y.z. My upgrade of that product has w.x.y.z+1. But the requirement is that before upgrade the fourth field of the version i.e; z should not be checked...i.e; myproduct should not take into consideration the z field.....Please do reply me with an answer.

You can read you App Version from registry and keep it as Variable, then trim it to specific number of chars. Check http://nsis.sourceforge.net/TrimText:_Trim_text_e.g._to_fit_in_a_label
Script below is will work for ANSI NSIS if you have such version installed if not, please change ReadRegStr accordingly.
Name "TrimTest"
OutFile "TrimTest.exe"
!include "TextFunc.nsh"
!include "nsDialogs.nsh"
!include "LogicLib.nsh"
!include "WinMessages.nsh"
!include "WordFunc.nsh"
!insertmacro VersionCompare
Function TrimText
Exch $R0 ; char
Exch
Exch $R1 ; length
Exch 2
Exch $R2 ; text
Push $R3
Push $R4
StrLen $R3 $R2
IntCmp $R3 $R1 Done Done
StrCpy $R2 $R2 $R1
StrCpy $R3 0
IntOp $R3 $R3 + 1
StrCpy $R4 $R2 1 -$R3
StrCmp $R4 "" Done
StrCmp $R4 $R0 0 -3
IntOp $R3 $R3 + 1
StrCpy $R4 $R2 1 -$R3
StrCmp $R4 "" Done
StrCmp $R4 $R0 -3
IntOp $R3 $R3 - 1
StrCpy $R2 $R2 -$R3
StrCpy $R2 $R2
Done:
StrCpy $R0 $R2
Pop $R4
Pop $R3
Pop $R2
Pop $R1
Exch $R0 ; output
FunctionEnd
!macro TrimText Text Length Char Var
Push "${Text}"
Push "${Length}"
Push "${Char}"
Call TrimText
Pop "${Var}"
!macroend
!define TrimText "!insertmacro TrimText"
Section
ReadRegStr $0 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\NSIS ANSI" "DisplayVersion"
${TrimText} $0 7 "-" $R0
;set max lenght to 7, where only 6 chars are needed, as trimmer will stop on - char
;in your case you will probably want to trim to first . from right side
;${TrimText} "string" "max_length" "up_to_char" "$out_var"
MessageBox MB_ICONEXCLAMATION "Line: $R0"
;Input $0 from registry should be "2.46.4-ANSI" if the latest ANSI NSIS is installed
;$R0 after trimm will be "Line: 2.46.4"
${VersionCompare} $R0 "2.46.6" $R2
${If} $R2 == 1 ;if 2.46.6 is lower that $R0
MessageBox MB_ICONEXCLAMATION "Your App Version is newer than this version of update"
${EndIf}
${If} $R2 == 0 ;versions are equal
MessageBox MB_ICONEXCLAMATION "Your App Version is equal to this version of update"
${EndIf}
${If} $R2 == 2 ;if 2.46.6 is higher that $R0
MessageBox MB_ICONEXCLAMATION "Your App Version is older and will be updated"
;here you can write your update section
${EndIf}
SectionEnd

In this case this can be a solution (based on http://nsis.sourceforge.net/Sort_String_2).
I have created Test Registry Key just for check with your {3.1.123 4.0.112 4.1.119}. Please replace with the one your application uses.
Name "TrimTest2"
OutFile "TrimTest2.exe"
!include "TextFunc.nsh"
!include "nsDialogs.nsh"
!include "LogicLib.nsh"
!include "WinMessages.nsh"
!include "WordFunc.nsh"
!insertmacro VersionCompare
Function AdvStrSort
Exch $0 ; input string
Exch
Exch $1 ; count to get part
Exch
Exch 2
Exch $2 ; get ammount of chunks from end
Exch 2
Push $3
Push $4
Push $5
Push $6
Push $7
StrCpy $0 " $0"
StrCpy $3 0
StrCpy $4 0
loop:
IntOp $3 $3 - 1
StrCpy $6 $0 1 $3
StrCmp $6 "" skip
StrCmp $6 " " roger ; to change chunk seperators, edit this (" ")
Goto loop
roger:
StrCpy $7 $0 "" $3
IntOp $4 $4 + 1
StrCmp $4 $2 0 loop
StrCmp $1 $2 0 +3
StrCpy $0 $7
Goto end
skip:
StrCpy $4 0
StrLen $5 $7
top:
IntOp $4 $4 + 1
loop2:
IntOp $5 $5 - 1
StrCpy $3 $7 1 -$5
StrCmp $3 "" end
StrCmp $3 " " 0 loop2 ; to change chunk seperators, edit this too (" ")
StrCmp $4 $1 0 top
StrCpy $0 $7 -$5
end:
StrLen $1 $0
IntOp $1 $1 - 1
StrCpy $0 $0 $1 -$1
Pop $7
Pop $6
Pop $5
Pop $4
Pop $3
Pop $2
Pop $1
Exch $0 ; output string
FunctionEnd
Section
ReadRegStr $R1 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\UserApplicationName" "Version"
Push 1 ; get text after 1st chunk from string end
Push 1 ; get 1 chunk before the 1st
Push "$R1" ; input string (string that has spaces)
Call AdvStrSort
Pop $R0
MessageBox MB_ICONEXCLAMATION "Result: $R0"
${VersionCompare} $R0 "5.0.114" $R2
;MessageBox MB_ICONEXCLAMATION "Result: $R2"
${If} $R2 == 1 ;if 5.0.114 is lower that $R0
MessageBox MB_ICONEXCLAMATION "Your App Version is newer than this version of update"
${EndIf}
${If} $R2 == 0 ;versions are equal
MessageBox MB_ICONEXCLAMATION "Your App Version is equal to this version of update"
${EndIf}
${If} $R2 == 2 ;if 5.0.114 is higher that $R0
MessageBox MB_ICONEXCLAMATION "Your App Version is older and will be updated"
;here you can write your update section
${EndIf}
SectionEnd

Related

Replace line in file with NSIS

I am trying to replace line in my config file using nsis, based on the searched results.
I have following functions included in my nsis file
To search for string:
Function SearchInFile
; HOW USE
; FileOpen $1 'C:\WINNT\system32\drivers\etc\services' r
; Push $handle ; handle of file (open in 'a' or 'r' mode)
; Push '#' ; comments delimiter (All symbols more to the right
; ; are ignored)
; Push 'gds_db' ; sample of search
; ; push '' if comment_symbol are not defined
; Push 'no' ; search mode. If 'begin' to search only from the
; ; beginning of string
; Call SearchInFile
; Pop $0 ; Result of search: yes/no
;
Exch $3 ; search mode
Exch 3
Exch $2 ; file handle
Exch 2
Exch $1 ; comments delimiter
Exch
Exch $0 ; search sample
Exch 3
Exch
Exch 2
Exch
Push $4 ; string from file
Push $5 ; length of string
Push $6 ; not used
Push $7 ; search result
Push $8 ; not used
Push $9 ; not used
Push $R0 ;length of the search sample
Push $R1 ;length of the comment symbol
Push $R2 ;tmp
ClearErrors
StrLen $5 $0
StrCpy $7 no
StrLen $R0 $0
StrLen $R1 $1
lbl_SearchInFile_loop:
FileRead $2 $4
IfErrors lbl_SearchInFile_done
StrCmp $3 'begin' lbl_SearchFromBeginOnly lbl_SearchInString_loop
lbl_SearchFromBeginOnly:
StrLen $5 $4
StrCpy $R2 $4 $R0
StrCmp $R2 $0 lbl_SampleIsFound lbl_SearchInFile_loop
lbl_SearchInString_loop:
StrCmp "" $4 lbl_SearchInFile_loop
StrLen $5 $4
StrCmp "" $1 lbl_SearchSampleCont ; search for the comments delimiter
StrCpy $R2 $4 $R1
StrCmp $R2 $1 lbl_SearchInFile_loop
lbl_SearchSampleCont:
StrCpy $R2 $4 $R0 ; search as such the sample
StrCmp $R2 $0 lbl_SampleIsFound
IntOp $5 $5 - 1 ; cut a char at the left and continue search
StrCpy $4 $4 $5 1
Goto lbl_SearchInString_loop
lbl_SampleIsFound:
StrCpy $7 yes
lbl_SearchInFile_done:
StrCpy $0 $7
Pop $R2
Pop $R1
Pop $R0
Pop $9
Pop $8
Pop $7
Pop $6
Pop $5
Pop $4
Pop $3
Pop $2
Pop $1
Exch $0 ;output yes/no
FunctionEnd
To replace line
Function ReplaceLineStr
Exch $R0 ; string to replace that whole line with
Exch
Exch $R1 ; string that line should start with
Exch
Exch 2
Exch $R2 ; file
Push $R3 ; file handle
Push $R4 ; temp file
Push $R5 ; temp file handle
Push $R6 ; global
Push $R7 ; input string length
Push $R8 ; line string length
Push $R9 ; global
StrLen $R7 $R1
GetTempFileName $R4
FileOpen $R5 $R4 w
FileOpen $R3 $R2 r
ReadLoop:
ClearErrors
FileRead $R3 $R6
IfErrors Done
StrLen $R8 $R6
StrCpy $R9 $R6 $R7 -$R8
StrCmp $R9 $R1 0 +3
FileWrite $R5 "$R0$\r$\n"
Goto ReadLoop
FileWrite $R5 $R6
Goto ReadLoop
Done:
FileClose $R3
FileClose $R5
SetDetailsPrint none
Delete $R2
Rename $R4 $R2
SetDetailsPrint both
Pop $R9
Pop $R8
Pop $R7
Pop $R6
Pop $R5
Pop $R4
Pop $R3
Pop $R2
Pop $R1
Pop $R0
FunctionEnd
Now, my instllation part looks as below
DetailPrint "Agent already installed, will upgrade"
DetailPrint "Reading current config"
FileOpen $0 'pathTofile' r ;
Push $0 ;
Push '' ;
Push 'string1' ;
Push 'no' ;
Call SearchInFile
Pop $0
${If} $0 != yes
DetailPrint ",String1 not found in config. Continuing"
${Else}
DetailPrint "Updating config"
Push "pathTofile" ;
Push "foo" ;
Push "foo string3,string4" ;
Call ReplaceLineStr
${EndIf}
Problem is that when it goes to Else statement, than nothing happens - line is not replaced
When running following, without the search Function, it does the replacement. Am I missing something here ?
DetailPrint "Agent already installed, will upgrade"
DetailPrint "Reading current config"
DetailPrint "Updating config"
Push "pathTofile" ;
Push "foo" ;
Push "foo string3,string4" ;
Call ReplaceLineStr

NSIS window disappear

I have tried a lot of possibility with this code but it doesn't work. I need to check a VPN connection. If I run the code firs time it works, but if I don't restart the program and push the button secondly then the program just disappear. Any idea how to fix it? thanks.
Function VpnLoginWindow
${Do}
!insertmacro VpnLoginWindow
StrCpy $4 0
${If} $3 == 1
${Do}
!insertmacro VpnLoginWindow
StrCpy $4 1
StrCmp $3 "0" 0 +2
${ExitDo}
${Loop}
${EndIf}
${If} $4 == 1
${ExitDo}
${EndIf}
${Loop}
FunctionEnd
!macro VpnLoginWindow
StrCpy $5 $1
System::Get "(i.r1) iss"
Pop $R0
System::Call "user32::EnumWindows(k R0, i) i.s"
${Do}
Pop $0
StrCmp $0 "1" 0 +2
${ExitDo}
System::Call "user32::GetWindowText(ir1,t.r2,i ${NSIS_MAX_STRLEN})"
StrCpy $2 $2 7
StrCpy $3 0
${If} $2 == "SSL VPN"
StrCpy $3 1
${ExitDo}
${EndIf}
Push 1 # callback's return value
System::Call "$R0"
${Loop}
System::Free $R0
StrCpy $1 $5
!macroend
Callbacks are tricky to get right and the plug-in is a little buggy. Also, you should not use relative jumps to jump over macros like ${ExitDo}!
!include LogicLib.nsh
!macro FindVpnLoginWindow
Push "" ; Result: Window not found
System::Store S
System::Get '(p.r1, p)ir0r0'
Pop $9
System::Call 'USER32::EnumWindows(k r9, p 0)'
${Do}
${IfThen} $0 != "callback1" ${|} ${ExitDo} ${|} ; <-- adjust the callback# if required
StrCpy $0 1 ; Set callback return value, continue search
System::Call "USER32::GetWindowText(pr1, t.r2, i ${NSIS_MAX_STRLEN})"
StrCpy $2 $2 7
${If} $2 == "SSL VPN"
Pop $2 ; Throw away old result
Push $1 ; Result: HWND
StrCpy $0 0 ; Set callback return value, stop enum with 0
${EndIf}
System::Call $9 ; Return from callback function
${Loop}
System::Free $9
System::Store L
!macroend
Function BackgroundFindWindow
!insertmacro FindVpnLoginWindow
Pop $0
DetailPrint "Result:$0"
FunctionEnd
...
GetFunctionAddress $0 BackgroundFindWindow
BgWorker::CallAndWait
GetFunctionAddress $0 BackgroundFindWindow
BgWorker::CallAndWait

NSIS: How to validate IP address box

I have added a ${NSD_CreateIPaddress} box in my nsi script, but while validating each field of IP is come up with by default to 0. I am getting the IP address value by using ${GetText}.
Is there any way to remove default value 0 and validate each field of IP address. Please help me out.
I'm not really sure what you are asking about. If you want to tell the difference between a blank address and the valid "0.0.0.0" address you can use the IPM_ISBLANK message. There should be no need to validate the individual fields because they are already limited to 0..255 and you can even set a custom range with IPM_SETRANGE.
If you still feel the need to look at the individual fields you can
A) Manually parse the string you get from ${NSD_GetText} yourself.
or
B) Unpack the 32-bit address you get from IPM_GETADDRESS:
Page Custom myPage myPageLeave
Page InstFiles
!include nsDialogs.nsh
; Using custom version of http://nsis.sourceforge.net/NsDialogs_CreateIPaddress
!ifndef NSD_CreateIPaddress
!define NSD_CreateIPaddress "!insertmacro NSD_CreateIPaddress "
!include LogicLib.nsh
!include WinMessages.nsh
!ifndef ICC_INTERNET_CLASSES
!define ICC_INTERNET_CLASSES 0x00000800
!endif
Function CCInitIP
!insertmacro _LOGICLIB_TEMP
System::Call '*(i8,i${ICC_INTERNET_CLASSES})p.s' ; NSIS 2.50+
System::Call 'COMCTL32::InitCommonControlsEx(pss)'
Pop $_LOGICLIB_TEMP
System::Free $_LOGICLIB_TEMP
FunctionEnd
!macro NSD_CreateIPaddress x y w h t
!insertmacro _LOGICLIB_TEMP
Call CCInitIP
nsDialogs::CreateControl "SysIPAddress32" ${DEFAULT_STYLES}|${WS_TABSTOP} 0 ${x} ${y} ${w} ${h} "${t}"
Exch $0
CreateFont $_LOGICLIB_TEMP "$(^Font)" "$(^FontSize)"
SendMessage $0 ${WM_SETFONT} $_LOGICLIB_TEMP 1
Exch $0
!macroend
!define /math IPM_GETADDRESS ${WM_USER} + 102
!define /math IPM_ISBLANK ${WM_USER} + 105
!endif
Function OnIPNotify ; This function displays some information about the IP
Pop $0 ; Not used
${NSD_GetText} $1 $3
StrCpy $4 "NSD_GetText: $3"
SendMessage $1 ${IPM_ISBLANK} 0 0 $0
StrCpy $4 "$4$\nIPM_ISBLANK: $0"
System::Call 'USER32::SendMessage(pr1, i ${IPM_GETADDRESS}, p 0, *i0 r3)p.r0' ; NSIS 2.50+
IntFmt $5 "0x%.8x" $3
StrCpy $4 "$4$\nIPM_GETADDRESS: ValidFields=$0 PackedIP=$5"
IntOp $0 $3 >> 24
IntOp $0 $0 & 0xff
StrCpy $4 "$4$\n$\t Field1=$0"
IntOp $0 $3 >> 16
IntOp $0 $0 & 0xff
StrCpy $4 "$4$\n$\t Field2=$0"
IntOp $0 $3 >> 8
IntOp $0 $0 & 0xff
StrCpy $4 "$4$\n$\t Field3=$0"
IntOp $0 $3 & 0xff
StrCpy $4 "$4$\n$\t Field4=$0"
${NSD_SetText} $2 $4
FunctionEnd
Function myPage
nsDialogs::Create 1018
Pop $0
${NSD_CreateIPaddress} 1% 0 50% 12u ""
Pop $1
${NSD_CreateLabel} 1% 20u 98% -20u "Enter an IP address to see information here..."
Pop $2
${NSD_OnNotify} $1 OnIPNotify ; Display some information when the control is changed
nsDialogs::Show
FunctionEnd
Function myPageLeave
Push 0
Call OnIPNotify
MessageBox mb_ok $4
FunctionEnd

nsis refresh environment during setup

During my setup i install redistributables. After installing my program i want to execute the program during the setup. For that the redistributables are needed. But the environment is not refreshed during setup, so i can't start the program. Can i refresh the environtment during the setup, or reread the environment? Or read the system environment which has changed after the redistributable installation?
I need the environment changes made during the installation of the redistributables during the setup.
Thanks for your help.
I had your same issue, that I solved with a workaround, but I came across this solution and I would like to share with you.
This should reload environment variables during setup:
!include LogicLib.nsh
!include WinCore.nsh
!ifndef NSIS_CHAR_SIZE
!define NSIS_CHAR_SIZE 1
!define SYSTYP_PTR i
!else
!define SYSTYP_PTR p
!endif
!ifndef ERROR_MORE_DATA
!define ERROR_MORE_DATA 234
!endif
/*!ifndef KEY_READ
!define KEY_READ 0x20019
!endif*/
Function RegReadExpandStringAlloc
System::Store S
Pop $R2 ; reg value
Pop $R3 ; reg path
Pop $R4 ; reg hkey
System::Alloc 1 ; mem
StrCpy $3 0 ; size
loop:
System::Call 'SHLWAPI::SHGetValue(${SYSTYP_PTR}R4,tR3,tR2,i0,${SYSTYP_PTR}sr2,*ir3r3)i.r0' ; NOTE: Requires SHLWAPI 4.70 (IE 3.01+ / Win95OSR2+)
${If} $0 = 0
Push $2
Push $0
${Else}
System::Free $2
${If} $0 = ${ERROR_MORE_DATA}
IntOp $3 $3 + ${NSIS_CHAR_SIZE} ; Make sure there is room for SHGetValue to \0 terminate
System::Alloc $3
Goto loop
${Else}
Push $0
${EndIf}
${EndIf}
System::Store L
FunctionEnd
Function RefreshProcessEnvironmentPath
System::Store S
Push ${HKEY_CURRENT_USER}
Push "Environment"
Push "Path"
Call RegReadExpandStringAlloc
Pop $0
${IfThen} $0 <> 0 ${|} System::Call *(i0)${SYSTYP_PTR}.s ${|}
Pop $1
Push ${HKEY_LOCAL_MACHINE}
Push "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Push "Path"
Call RegReadExpandStringAlloc
Pop $0
${IfThen} $0 <> 0 ${|} System::Call *(i0)${SYSTYP_PTR}.s ${|}
Pop $2
System::Call 'KERNEL32::lstrlen(t)(${SYSTYP_PTR}r1)i.R1'
System::Call 'KERNEL32::lstrlen(t)(${SYSTYP_PTR}r2)i.R2'
System::Call '*(&t$R2 "",&t$R1 "",i)${SYSTYP_PTR}.r0' ; The i is 4 bytes, enough for a ';' separator and a '\0' terminator (Unicode)
StrCpy $3 ""
${If} $R1 <> 0
${AndIf} $R2 <> 0
StrCpy $3 ";"
${EndIf}
System::Call 'USER32::wsprintf(${SYSTYP_PTR}r0,t"%s%s%s",${SYSTYP_PTR}r2,tr3,${SYSTYP_PTR}r1)?c'
System::Free $1
System::Free $2
System::Call 'KERNEL32::SetEnvironmentVariable(t"PATH",${SYSTYP_PTR}r0)'
System::Free $0
System::Store L
FunctionEnd
Section
Call RefreshProcessEnvironmentPath
SectionEnd
Source: http://forums.winamp.com/showpost.php?p=3028153&postcount=12

NSIS example of auto-update the installer itself?

I am looking for a NSIS example that is able to update itself with the latest version of the installer.
The logic is simple: check if the publish URL is newer than the current executable and download it and recall itself with the same parameters.
Mainly that's would me something like "wget --timestamp URL" but so far I wasn't able to find any NSIS file download extension that is able to download a file only if is newer.
INetC::head will grab just the HTTP headers from the server but IMHO downloading a .ini file with version info from the server is better...
Here's something I wrote that might be of help to you. I have not tested this thoroughly but it did work on the first couple tests I ran. This is just an example I threw together for a project I'm working on. Hope it's of use to you. =)
VersionCompare Function
;=#
;= 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
UpdateCheck Function
;=#
;= 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
WGetDownload Function
;=#
;= 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
OpenWebPage Macro
;=#
;= Author: demon.devin
;= http://softables.tk/
!define OpenWebPage '!insertmacro "_OpenWebPage"'
!macro _OpenWebPage _URL
ExecShell open "${_URL}"
!macroend
EXAMPLE USAGE:
;=#
;= 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

Resources