comparison nsis/download_mirror.nsh @ 0:1fd2201f5c36

Initial commit of parpg-core.
author M. George Hansen <technopolitica@gmail.com>
date Sat, 14 May 2011 01:12:35 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:1fd2201f5c36
1 var RandomSeed # The seed for the random number generator
2
3 ###############################################################
4 #
5 # Returns a random number
6 #
7 # Usage:
8 # Push Seed (or previously generated number)
9 # Call RandomNumber
10 # Pop Generated Random Number
11 Function RandomNumber
12 Exch $R0
13
14 IntOp $R0 $R0 * "13"
15 IntOp $R0 $R0 + "3"
16 IntOp $R0 $R0 % "1048576" # Values goes from 0 to 1048576 (2^20)
17
18 Exch $R0
19 FunctionEnd
20
21 ####################################################
22 #
23 # Returns a random number between 0 and MaxValue-1
24 #
25 # Usage:
26 # Push Seed (or previously generated number)
27 # Push MaxValue
28 # Call RandomNumber
29 # Pop Generated Random Number
30 # Pop NewSeed
31 Function LimitedRandomNumber
32 Exch $R0
33 Exch
34 Exch $R1
35 Push $R2
36 Push $R3
37
38 StrLen $R2 $R0
39 Push $R1
40 RandLoop:
41 Call RandomNumber
42 Pop $R1 #Random Number
43 IntCmp $R1 $R0 0 NewRnd
44 StrLen $R3 $R1
45 IntOp $R3 $R3 - $R2
46 IntOp $R3 $R3 / "2"
47 StrCpy $R3 $R1 $R2 $R3
48 IntCmp $R3 $R0 0 RndEnd
49 NewRnd:
50 Push $R1
51 Goto RandLoop
52 RndEnd:
53 StrCpy $R0 $R3
54 IntOp $R0 $R0 + "0" #removes initial 0's
55 Pop $R3
56 Pop $R2
57 Exch $R1
58 Exch
59 Exch $R0
60 FunctionEnd
61
62 ###################################################
63 #
64 # Downloads a file from a list of mirrors
65 # (the fist mirror is selected at random)
66 #
67 # Usage:
68 # Push Mirror1
69 # [Push Mirror2]
70 # ...
71 # [Push Mirror10]
72 # Push NumMirrors # 10 Max
73 # Push FileName
74 # Call DownloadFromRandomMirror
75 # Pop Return
76 #
77 # Returns the last NSISdl result
78 Function DownloadFromRandomMirror
79 Exch $R1 #File name
80 Exch
81 Exch $R0 #Number of Mirrors
82 Push $0
83 Exch 3
84 Pop $0 #Mirror 1
85 IntCmpU "2" $R0 0 0 +4
86 Push $1
87 Exch 4
88 Pop $1 #Mirror 2
89 IntCmpU "3" $R0 0 0 +4
90 Push $2
91 Exch 5
92 Pop $2 #Mirror 3
93 IntCmpU "4" $R0 0 0 +4
94 Push $3
95 Exch 6
96 Pop $3 #Mirror 4
97 IntCmpU "5" $R0 0 0 +4
98 Push $4
99
100 Exch 7
101 Pop $4 #Mirror 5
102 IntCmpU "6" $R0 0 0 +4
103 Push $5
104 Exch 8
105 Pop $5 #Mirror 6
106 IntCmpU "7" $R0 0 0 +4
107 Push $6
108 Exch 9
109 Pop $6 #Mirror 7
110 IntCmpU "8" $R0 0 0 +4
111 Push $7
112 Exch 10
113 Pop $7 #Mirror 8
114 IntCmpU "9" $R0 0 0 +4
115 Push $8
116 Exch 11
117 Pop $8 #Mirror 9
118 IntCmpU "10" $R0 0 0 +4
119 Push $9
120 Exch 12
121 Pop $9 #Mirror 10
122 Push $R4
123 Push $R2
124 Push $R3
125 Push $R5
126 Push $R6
127
128 StrCmp $RandomSeed "" 0 +2
129 StrCpy $RandomSeed $HWNDPARENT #init RandomSeed
130
131 Push $RandomSeed
132 Push $R0
133 Call LimitedRandomNumber
134 Pop $R3
135 Pop $RandomSeed
136
137 StrCpy $R5 "0"
138 MirrorsStart:
139 IntOp $R5 $R5 + "1"
140 StrCmp $R3 "0" 0 +3
141 StrCpy $R2 $0
142 Goto MirrorsEnd
143 StrCmp $R3 "1" 0 +3
144 StrCpy $R2 $1
145 Goto MirrorsEnd
146 StrCmp $R3 "2" 0 +3
147 StrCpy $R2 $2
148 Goto MirrorsEnd
149 StrCmp $R3 "3" 0 +3
150 StrCpy $R2 $3
151 Goto MirrorsEnd
152 StrCmp $R3 "4" 0 +3
153 StrCpy $R2 $4
154 Goto MirrorsEnd
155 StrCmp $R3 "5" 0 +3
156 StrCpy $R2 $5
157 Goto MirrorsEnd
158 StrCmp $R3 "6" 0 +3
159 StrCpy $R2 $6
160 Goto MirrorsEnd
161 StrCmp $R3 "7" 0 +3
162 StrCpy $R2 $7
163 Goto MirrorsEnd
164 StrCmp $R3 "8" 0 +3
165 StrCpy $R2 $8
166 Goto MirrorsEnd
167 StrCmp $R3 "9" 0 +3
168 StrCpy $R2 $9
169 Goto MirrorsEnd
170 StrCmp $R3 "10" 0 +3
171 StrCpy $R2 $10
172 Goto MirrorsEnd
173
174 MirrorsEnd:
175 IntOp $R6 $R3 + "1"
176 DetailPrint "Downloading from mirror $R6: $R2"
177
178 NSISdl::download "$R2" "$R1"
179 Pop $R4
180 StrCmp $R4 "success" Success
181 StrCmp $R4 "cancel" DownloadCanceled
182 IntCmp $R5 $R0 NoSuccess
183 DetailPrint "Download failed (error $R4), trying with other mirror"
184 IntOp $R3 $R3 + "1"
185 IntCmp $R3 $R0 0 MirrorsStart
186 StrCpy $R3 "0"
187 Goto MirrorsStart
188
189 DownloadCanceled:
190 DetailPrint "Download Canceled: $R2"
191 Goto End
192 NoSuccess:
193 DetailPrint "Download Failed: $R1"
194 Goto End
195 Success:
196 DetailPrint "Download completed."
197 End:
198 Pop $R6
199 Pop $R5
200 Pop $R3
201 Pop $R2
202 Push $R4
203 Exch
204 Pop $R4
205 Exch 2
206 Pop $R1
207 Exch 2
208 Pop $0
209 Exch
210
211 IntCmpU "2" $R0 0 0 +4
212 Exch 2
213 Pop $1
214 Exch
215 IntCmpU "3" $R0 0 0 +4
216 Exch 2
217 Pop $2
218 Exch
219 IntCmpU "4" $R0 0 0 +4
220 Exch 2
221 Pop $3
222 Exch
223 IntCmpU "5" $R0 0 0 +4
224 Exch 2
225 Pop $4
226 Exch
227 IntCmpU "6" $R0 0 0 +4
228 Exch 2
229 Pop $5
230 Exch
231 IntCmpU "7" $R0 0 0 +4
232 Exch 2
233 Pop $6
234 Exch
235 IntCmpU "8" $R0 0 0 +4
236 Exch 2
237 Pop $7
238 Exch
239 IntCmpU "9" $R0 0 0 +4
240 Exch 2
241 Pop $8
242 Exch
243 IntCmpU "10" $R0 0 0 +4
244 Exch 2
245 Pop $9
246 Exch
247 Pop $R0
248 FunctionEnd
249