Download Using Mirrors: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Spelling fix.)
(BF: Separated example from function. SPF: Fixed function call.)
Line 1: Line 1:
== Description ==
== Description ==


'''Required usage of:''' [[NSISdl]] Plugin.
'''Requires:''' [[NSISdl]] Plugin.


Function to download files from internet with mirrors support. Needs internet connection to be established first.
Function to download files from internet with mirrors support. Needs internet connection to be established first.
Line 7: Line 7:
== Function Call ==
== Function Call ==


*'''InitPluginsDir''' ; Initialize $PLUGINSDIR (only if you didn't do it before)
<highlight-nsis>
*'''Push''' ''"[Path\]FileName.ext"'' ; File to be renamed
InitPluginsDir ; Initialize $PLUGINSDIR (only if you didn't do it before)
*'''Push''' $0
Push "[Path\]FileName.ext" ; File to be renamed
*'''GetFunctionAddress''' $0 ''"CustomFunction"'' ; Custom function name
Push $0
*'''Exch''' $0
GetFunctionAddress $0 "CustomFunction" ; Custom function name
*'''Call''' ''NSISDownload''
Exch $0
*'''Pop''' ''$0'' - Result (last error, "cancel", "success" or "download incompleted")
Call NSISDownload
Pop $0 ; Result (last error, "cancel", "success" or "download incompleted")
</highlight-nsis>


== How to use Callback Function ==
== How to use Callback Function ==
Line 302: Line 304:
   AfterAll:
   AfterAll:


FunctionEnd[/source]</ul>
FunctionEnd
<u><b>Example of callback function:</b></u>
</highlight-nsis>


<ul>[source] Function DownloadNSIS
== Callback Function Example ==
 
<highlight-nsis>
  Function DownloadNSIS
   StrCmp $R0 1 0 +3
   StrCmp $R0 1 0 +3
     Push "http://unc.dl.sourceforge.net/sourceforge/nsis/nsis205.exe"
     Push "http://unc.dl.sourceforge.net/sourceforge/nsis/nsis205.exe"
Line 348: Line 353:
     Push ""
     Push ""
     Push ""
     Push ""
  FunctionEnd</highlight-nsis>
  FunctionEnd
</highlight-nsis>

Revision as of 13:38, 3 April 2005

Description

Requires: NSISdl Plugin.

Function to download files from internet with mirrors support. Needs internet connection to be established first.

Function Call

InitPluginsDir ; Initialize $PLUGINSDIR (only if you didn't do it before)
Push "[Path\]FileName.ext" ; File to be renamed
Push $0
GetFunctionAddress $0 "CustomFunction" ; Custom function name
Exch $0
Call NSISDownload
Pop $0 ; Result (last error, "cancel", "success" or "download incompleted")

How to use Callback Function

  • You have first to push "Stop" or the mirror site and then push the timeout or nothing. You have to put StrCmp commands to assign each try to the address to download (like the first one to http://www.mysite.com/file.zip and the second one to ftp://ftp.mysite.com/file.zip) and the last try has to contain the "Stop" push to make the function return.
  • Do not push values on the stack without poping them later unless it's the return value.
  • You can use any variables, but the $R0 - $R5 contains function results.
  • See an example to "How to use callback function"

Function Code

;----------------------------------------------------------------------------
; Title             : Download using mirrors
; Short Name        : NSISDownload
; Last Changed      : 22/Feb/2005
; Code Type         : Function
; Code Sub-Type     : Callback Dependant
;----------------------------------------------------------------------------
; Description       : Downloads files from internet with mirrors support.
;                     Needs internet connection to be established first.
;----------------------------------------------------------------------------
; Function Call     : Call NSISDownload
;----------------------------------------------------------------------------
; Callback Variables: $R0 ;Mirror number.
; Callback Pushes   : Push "URL"
;                       Determines which url should NSISdl download.
;
;                     Push "(Stop|Go)"
;                       Normal callback push.
;----------------------------------------------------------------------------
; Author            : Diego Pedroso
; Author Reg. Name  : deguix
;----------------------------------------------------------------------------
 
Function NSISDownload
  # Push Variables
 
  Push $R0
  Push $R1
  Push $R2
  Push $R3
  Push $R4
  Exch 5
  Exch $R5 #Custom Function
  Exch 5
  Push $R6
  Push $R7
  Push $R8
  Push $R9
  StrCpy $R9 $R5
 
  # Detect Mirror Number
 
  ReadINIStr $R0 "$PLUGINSDIR\ars7923.ini" "MIRRORS" "NUMBER"
 
  IntOp $R0 $R0 + 1
 
  StrCmp $R0 "" 0 +2
    StrCpy $R0 1
 
  WriteINIStr "$PLUGINSDIR\ars7923.ini" "MIRRORS" "NUMBER" "$R0"
 
  # Call Custom Function
 
  StrCpy $R8 ""
  StrCpy $R7 ""
  StrCpy $R6 ""
  StrCpy $R5 ""
  StrCpy $R4 ""
  StrCpy $R3 ""
  StrCpy $R2 ""
  StrCpy $R1 ""
 
  Push $R9
 
  Call $R9
 
  Exch 3
  Pop $R9
  Exch 3
  Pop $R8
  Exch 3
  Pop $R7
  Exch 3
  Pop $R6
  Exch 3
  Pop $R4
  Exch 3
  Pop $R3
  Exch 3
  Pop $R2
  Exch 3
  Pop $R1
  Exch 3
  Pop $R0
  Exch 3
  Pop $R5
 
  Exch
  Exch 3
  Exch 2
 
  # Prepare Variables
 
  Push $R3
  Push $R2
  Exch 2
  Exch $R4 # TimeOut
  Exch 2
  Exch 5
  Exch $R5 # Function
  Exch 5
  Exch 4
  Exch $R1 # Name to Rename
  Exch 4
  Exch 3
  Exch $R0 # Link
  Exch 3
  Exch 2
  Exch
  Exch 5
  Exch 5
  Exch 2
  Exch 3
  Exch 5
 
  # Detect Stop Messages
 
  StrCmp $R0 "" +2
  StrCmp $R0 "stop" 0 NoError
    StrCpy $R0 "error"
    Goto End
  NoError:
 
  # Getting URL important part:
 
  Push $R0
  Push $R1
  Push $R2
  Push $R3
  Push $R4
 
  StrCpy $R1 0
  StrLen $R2 $R0
 
  NameLoop:
    IntOp $R1 $R1 + 1
    IntCmp $R1 $R2 NameDone 0 NameDone
    StrCpy $R3 $R0 1 -$R1
    StrCmp $R3 "\" NameDone
    StrCmp $R3 "/" NameDone
    Goto NameLoop
 
  NameDone:
    IntOp $R1 $R1 - 1
    StrCpy $R4 $R0 "" "-$R1"
 
  StrCpy $R1 $R0
  StrCpy $R2 0
  URLLoop:
    StrCpy $R3 $R1 3 $R2
    StrCmp $R3 :// URLDone
    StrCmp $R3 "" URLDone
    IntOp $R2 $R2 + 1
    Goto URLLoop
  URLDone:
  IntOp $R2 $R2 + 3
  StrCpy $R0 $R1 "" $R2
 
  StrCpy $R1 $R0
  StrCpy $R2 0
 
  URLLoop2:
    StrCpy $R3 $R1 1 $R2
    StrCmp $R3 / URLDone2
    StrCmp $R3 "" URLDone2
    IntOp $R2 $R2 + 1
    Goto URLLoop2
  URLDone2:
  StrCpy $R0 $R1 $R2
 
  Exch
  Pop $R3
  Exch
  Pop $R2
  Exch
  Pop $R1
 
  # Send "Downloading" Message
 
  ReadINIStr $R3 "$PLUGINSDIR\ars7923.ini" "MIRRORS" "NUMBER"
 
  StrCmp $R3 1 0 Mirrors
    DetailPrint "Downloading $R4 from: $R0"
    Goto Download
 
  Mirrors:
 
  IntOp $R3 $R3 - 1
  ReadINIStr $R2 "$PLUGINSDIR\ars7923.ini" "MIRRORS" "TRY"
 
  StrCmp $R2 1 0 +3
    WriteINIStr "$PLUGINSDIR\ars7923.ini" "MIRRORS" "TRY" "0"
    DetailPrint "Downloading $R4 from mirror $R3: $R0"
 
  IntOp $R3 $R3 + 1
 
  Pop $R4
  Pop $R0
 
  # Start Download
 
  Download:
 
  IntCmpU $R4 "" 0 WithTIMEOUT
    NSISdl::download "$R0" "$R1"
    Goto ExitTIMEOUT
  WithTIMEOUT:
 
  NSISdl::download /TIMEOUT=$R4 "$R0" "$R1"
  ExitTIMEOUT:
 
  Pop $R0
 
  # Verify Download Results
 
  StrCmp $R0 "success" 0 +3
    DetailPrint "Download Completed"
    Goto End
 
  StrCmp $R0 "cancel" 0 +3
    DetailPrint "Download Canceled"
    Goto End
 
  StrCmp $R0 "download incompleted" 0 +3
    DetailPrint "Download Incompleted"
    Goto End
 
  DetailPrint "Download Error: $R0"
  DetailPrint ""
 
  # Next Mirror
 
  WriteINIStr "$PLUGINSDIR\ars7923.ini" "MIRRORS" "TRY" "1"
 
  Push $R1
  Push $R5
 
  Exch 2
  Pop $R5
  Exch 2
  Pop $R4
  Exch 2
  Pop $R3
  Exch 2
  Pop $R2
  Exch 2
  Pop $R1
  Exch 2
  Pop $R0
 
  Call NSISDownload
  Pop $R0
 
  Goto AfterAll
 
  End:
 
  # Pop variables, after a stop, cancel, or completed message
 
  SetDetailsPrint None
  Delete "$PLUGINSDIR\ars7923.ini"
  SetDetailsPrint Both
 
  Exch 2
  Pop $R5
  Exch 2
  Pop $R4
  Exch 2
  Pop $R3
  Exch 2
  Pop $R2
  Exch 2
  Pop $R1
  Exch 2
  Exch $R0
 
  AfterAll:
 
FunctionEnd

Callback Function Example

 Function DownloadNSIS
   StrCmp $R0 1 0 +3
     Push "http://unc.dl.sourceforge.net/sourceforge/nsis/nsis205.exe"
     Push ""
   StrCmp $R0 2 0 +3
     Push "http://jaist.dl.sourceforge.net/sourceforge/nsis/nsis205.exe"
     Push ""
   StrCmp $R0 3 0 +3
     Push "http://heanet.dl.sourceforge.net/sourceforge/nsis/nsis205.exe"
     Push ""
   StrCmp $R0 4 0 +3
     Push "http://puzzle.dl.sourceforge.net/sourceforge/nsis/nsis205.exe"
     Push ""
   StrCmp $R0 5 0 +3
     Push "http://optusnet.dl.sourceforge.net/sourceforge/nsis/nsis205.exe"
     Push ""
   StrCmp $R0 6 0 +3
     Push "http://internap.dl.sourceforge.net/sourceforge/nsis/nsis205.exe"
     Push ""
   StrCmp $R0 7 0 +3
     Push "http://kent.dl.sourceforge.net/sourceforge/nsis/nsis205.exe"
     Push ""
   StrCmp $R0 8 0 +3
     Push "http://belnet.dl.sourceforge.net/sourceforge/nsis/nsis205.exe"
     Push ""
   StrCmp $R0 9 0 +3
     Push "http://mesh.dl.sourceforge.net/sourceforge/nsis/nsis205.exe"
     Push ""
   StrCmp $R0 10 0 +3
     Push "http://umn.dl.sourceforge.net/sourceforge/nsis/nsis205.exe"
     Push ""
   StrCmp $R0 11 0 +3
     Push "http://easynews.dl.sourceforge.net/sourceforge/nsis/nsis205.exe"
     Push ""
   StrCmp $R0 12 0 +3
     Push "http://ovh.dl.sourceforge.net/sourceforge/nsis/nsis205.exe"
     Push ""
   StrCmp $R0 13 0 +3
     Push "http://voxel.dl.sourceforge.net/sourceforge/nsis/nsis205.exe"
     Push ""
   StrCmp $R0 14 0 +3
     Push ""
     Push ""
 FunctionEnd