[svn] / branches / release-1_3-branch / xvidextra / src / installer / CheckUpdate.ps1 Repository:
ViewVC logotype

Annotation of /branches/release-1_3-branch/xvidextra/src/installer/CheckUpdate.ps1

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2120 - (view) (download)

1 : Isibaar 2110 Function Get-IniContent {
2 :     <#
3 :     .Synopsis
4 :     Gets the content of an INI file
5 :    
6 :     .Description
7 :     Gets the content of an INI file and returns it as a hashtable
8 :    
9 :     .Notes
10 :     Author : Oliver Lipkau <oliver@lipkau.net>
11 :     Blog : http://oliver.lipkau.net/blog/
12 :     Source : https://github.com/lipkau/PsIni
13 :     http://gallery.technet.microsoft.com/scriptcenter/ea40c1ef-c856-434b-b8fb-ebd7a76e8d91
14 :     Version : 1.0 - 2010/03/12 - Initial release
15 :     1.1 - 2014/12/11 - Typo (Thx SLDR)
16 :     Typo (Thx Dave Stiff)
17 :    
18 :     #Requires -Version 2.0
19 :    
20 :     .Inputs
21 :     System.String
22 :    
23 :     .Outputs
24 :     System.Collections.Hashtable
25 :    
26 :     .Parameter FilePath
27 :     Specifies the path to the input file.
28 :    
29 :     .Example
30 :     $FileContent = Get-IniContent "C:\myinifile.ini"
31 :     -----------
32 :     Description
33 :     Saves the content of the c:\myinifile.ini in a hashtable called $FileContent
34 :    
35 :     .Example
36 :     $inifilepath | $FileContent = Get-IniContent
37 :     -----------
38 :     Description
39 :     Gets the content of the ini file passed through the pipe into a hashtable called $FileContent
40 :    
41 :     .Example
42 :     C:\PS>$FileContent = Get-IniContent "c:\settings.ini"
43 :     C:\PS>$FileContent["Section"]["Key"]
44 :     -----------
45 :     Description
46 :     Returns the key "Key" of the section "Section" from the C:\settings.ini file
47 :    
48 :     .Link
49 :     Out-IniFile
50 :     #>
51 :    
52 :     [CmdletBinding()]
53 :     Param(
54 :     [ValidateNotNullOrEmpty()]
55 :     [ValidateScript({(Test-Path $_) -and ((Get-Item $_).Extension -eq ".ini")})]
56 :     [Parameter(ValueFromPipeline=$True,Mandatory=$True)]
57 :     [string]$FilePath
58 :     )
59 :    
60 :     Begin
61 :     {Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started"}
62 :    
63 :     Process
64 :     {
65 :     Write-Verbose "$($MyInvocation.MyCommand.Name):: Processing file: $Filepath"
66 :    
67 :     $ini = @{}
68 :     switch -regex -file $FilePath
69 :     {
70 :     "^\[(.+)\]$" # Section
71 :     {
72 :     $section = $matches[1]
73 :     $ini[$section] = @{}
74 :     $CommentCount = 0
75 :     }
76 :     "^(;.*)$" # Comment
77 :     {
78 :     if (!($section))
79 :     {
80 :     $section = "No-Section"
81 :     $ini[$section] = @{}
82 :     }
83 :     $value = $matches[1]
84 :     $CommentCount = $CommentCount + 1
85 :     $name = "Comment" + $CommentCount
86 :     $ini[$section][$name] = $value
87 :     }
88 :     "(.+?)\s*=\s*(.*)" # Key
89 :     {
90 :     if (!($section))
91 :     {
92 :     $section = "No-Section"
93 :     $ini[$section] = @{}
94 :     }
95 :     $name,$value = $matches[1..2]
96 :     $ini[$section][$name] = $value
97 :     }
98 :     }
99 :     Write-Verbose "$($MyInvocation.MyCommand.Name):: Finished Processing file: $FilePath"
100 :     Return $ini
101 :     }
102 :    
103 :     End
104 :     {Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended"}
105 :     }
106 :    
107 :     function Get-WebClient
108 :     {
109 :     $wc = New-Object Net.WebClient
110 :     $wc.UseDefaultCredentials = $true
111 :     $wc.Proxy.Credentials = $wc.Credentials
112 :     $wc
113 :     }
114 :    
115 :     # http://blogs.msdn.com/b/jasonn/archive/2008/06/13/downloading-files-from-the-internet-in-powershell-with-progress.aspx
116 :     function downloadFile($my_url, $targetFile)
117 :     {
118 :     $ret = 0
119 :     $request = [System.Net.WebRequest]::Create($my_url)
120 :     $request.set_Timeout(15000) #15 second timeout
121 :     $response = $request.GetResponse()
122 :     If (!$response) {
123 :     return 1
124 :     }
125 :    
126 :     $totalLength = [System.Math]::Floor($response.get_ContentLength() / 1000)
127 :     $responseStream = $response.GetResponseStream()
128 :     $targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $targetFile, Create
129 :     $progressBar.Value = 0
130 :     $buffer = new-object byte[] 64KB
131 :     $count = $responseStream.Read($buffer, 0, $buffer.length)
132 :     $downloadedBytes = $count
133 :    
134 :     while ($count -gt 0)
135 :     {
136 :     $progressBar.Value = [System.Math]::Floor([System.Math]::Floor($downloadedBytes/10) / $totalLength)
137 :     $targetStream.Write($buffer, 0, $count)
138 :     $count = $responseStream.Read($buffer, 0, $buffer.length)
139 :     $downloadedBytes = $downloadedBytes + $count
140 :     $form.Refresh()
141 :     [System.Windows.Forms.Application]::DoEvents()
142 :    
143 :     if($script:canceledDownload -eq $true)
144 :     {
145 :     # Exit the loop
146 :     $ret = 1
147 :     break
148 :     }
149 :    
150 :     start-sleep -Milliseconds 100
151 :     }
152 :    
153 :     $targetStream.Flush()
154 :     $targetStream.Close()
155 :     $targetStream.Dispose()
156 :     $responseStream.Dispose()
157 :     $response.Close()
158 :    
159 :     return $ret
160 :     }
161 :    
162 :    
163 :     # Main
164 :    
165 : Isibaar 2120 $clmid = 'HKLM:\SOFTWARE\Microsoft\Cryptography'
166 :     $clmid = (Get-ItemProperty -Path $clmid -Name MachineGuid).MachineGuid
167 :     $clmid = $clmid -replace '[-]',''
168 :     $md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
169 :     $utf8 = [system.Text.Encoding]::UTF8
170 :     $hash = [System.BitConverter]::ToString($md5.ComputeHash($utf8.GetBytes($clmid)))
171 :     $hash = $hash.ToLower() -replace '[-]',''
172 :     $rkey = 'HKCU:\SOFTWARE\GNU\XviD'
173 :     $pf = (Get-ItemProperty -Path $rkey -Name PerfCount).PerfCount
174 :     $rkey = (Set-ItemProperty -Path $rkey -Name PerfCount -Value 0 -Type DWord)
175 : Isibaar 2110 $scriptpath = $MyInvocation.MyCommand.Path
176 :     $script:xvid_dir = Split-Path $scriptpath
177 :     $tmp = $env:temp
178 :    
179 :     $FileContent = Get-IniContent "$script:xvid_dir\update.ini"
180 :     If (!$FileContent) {
181 :     Exit 0
182 :     }
183 :    
184 :     $url = $FileContent["Update"]["url"]
185 : Isibaar 2120 $url = $url + "&p=" + $pf + "&h=" + $hash
186 : Isibaar 2110 $object = Get-WebClient
187 :     $localPath = “$tmp\update.xml”
188 :     $object.DownloadFile($url, $localPath)
189 :    
190 :     $xml = [xml](Get-Content $localPath)
191 :    
192 :     $script:local_xvid_version = $FileContent["Update"]["version_id"]
193 :    
194 :     # Now check if Update file from the server has a newer version than we have locally
195 :     If (!$xml -or $xml.installerInformation.versionId -le $script:local_xvid_version) {
196 :     Exit 0
197 :     }
198 :    
199 :     # Check if the current update is skipped by user-choice
200 :     If (test-path $env:userprofile\.xvid\skip_update.log) {
201 :     $skip_str = Get-Content $env:userprofile\.xvid\skip_update.log
202 :     If ($skip_str -and $xml.installerInformation.versionId -le [System.Decimal]::Parse($skip_str)) {
203 :     Exit 0
204 :     }
205 :     }
206 :    
207 :     $script:remote_xvid_version = $xml.installerInformation.versionId
208 :    
209 :     # Parse the xml child elements
210 :     $xml_root = $xml.get_DocumentElement()
211 :     $fileList = $xml_root.platformFileList
212 :     $serverList = $xml_root.downloadLocationList
213 :    
214 :     $newItem = $null
215 :     foreach($newItem in $fileList.ChildNodes)
216 :     {
217 :     $script:newInstallerFile = $newItem.filename
218 :     $platform = $newItem.platform
219 :    
220 :     If ($platform -eq "windows" -or $platform -eq "Windows") {
221 :     Break
222 :     }
223 :     }
224 :    
225 :     $newItem = $null
226 :     foreach($newItem in $serverList.ChildNodes)
227 :     {
228 :     $script:newDownloadURL = $newItem.url
229 :     Break # Go with the first always
230 :     }
231 :    
232 :     If (!$script:newInstallerFile -or !$script:newDownloadURL) {
233 :     Exit 0
234 :     }
235 :    
236 :     # We have an Update! Show the Dialogs...
237 :    
238 :     # Loosely based on http://www.vistax64.com/powershell/202216-display-image-powershell.html
239 :     [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
240 :    
241 :     $xvid_file = (get-item "$script:xvid_dir\xvid.png")
242 :     $xvid_img = [System.Drawing.Image]::Fromfile($xvid_file);
243 :    
244 :     # This tip from http://stackoverflow.com/questions/3358372/windows-forms-look-different-in-powershell-and-powershell-ise-why/3359274#3359274
245 :     [System.Windows.Forms.Application]::EnableVisualStyles();
246 :     $form = new-object Windows.Forms.Form
247 :     $form.Text = "Xvid Software Update"
248 :     $form.Size = New-Object System.Drawing.Size(640,480)
249 :     $form.StartPosition = "CenterScreen"
250 :     $form.FormBorderStyle = 'Fixed3D'
251 :     $form.MaximizeBox = $false
252 :     $form.MinimizeBox = $False
253 :     $form.WindowState = "Normal"
254 :    
255 :     $Icon = New-Object system.drawing.icon ("$script:xvid_dir\XVID.ICO")
256 :     $Form.Icon = $Icon
257 :    
258 :     $pictureBox = new-object Windows.Forms.PictureBox
259 :     $pictureBox.Width = $xvid_img.Size.Width;
260 :     $pictureBox.Height = $xvid_img.Size.Height;
261 :     $pictureBox.Location = New-Object System.Drawing.Size(20, 12)
262 :    
263 :     $pictureBox.Image = $xvid_img;
264 :     $form.controls.add($pictureBox)
265 :    
266 :     $label1 = New-Object System.Windows.Forms.Label
267 :     $label1.Text = "A new version of the Xvid software is available!"
268 :     If ($xml.installerInformation.mainScreenHeader) {
269 :     $label1.Text = $xml.installerInformation.mainScreenHeader
270 :     }
271 :     $label1.AutoSize = $True
272 :     $label1.ForeColor = "#0c3384"
273 :     $label1.Location = New-Object System.Drawing.Size(96, 12)
274 :     $font = New-Object System.Drawing.Font("Arial",14,[System.Drawing.FontStyle]::Regular)
275 :     $label1.Font = $font
276 :     $form.Controls.Add($label1)
277 :    
278 :     $label2 = New-Object System.Windows.Forms.Label
279 :     $new_version = $xml.installerInformation.version
280 :     $label2.Text = "We're proud to announce the new Xvid $new_version release."
281 :     If ($xml.installerInformation.mainScreenText) {
282 :     $label2.Text = $xml.installerInformation.mainScreenText
283 :     }
284 :     $label2.AutoSize = $True
285 :     $label2.Location = New-Object System.Drawing.Size(96, 40)
286 :     $font = New-Object System.Drawing.Font("Arial",10,[System.Drawing.FontStyle]::Regular)
287 :     $label2.Font = $font
288 :     $form.Controls.Add($label2)
289 :    
290 :     $pictureBox = new-object Windows.Forms.PictureBox
291 :     $pictureBox.Width = 500;
292 :     $pictureBox.Height = 300;
293 :     $pictureBox.Location = New-Object System.Drawing.Size(96, 72)
294 :    
295 :     $script:download_banner_link = $xml.installerInformation.downloadBannerLink
296 :     $script:banner_link = $xml.installerInformation.mainBannerLink
297 :     $pictureBox.Add_Click({ If ($script:banner_link) { Invoke-Expression “cmd.exe /C start $script:banner_link”} })
298 :    
299 :     $script:download_banner_url = $xml.installerInformation.downloadBannerURI
300 :     $script:error_banner_url = $xml.installerInformation.errorBannerURI
301 :     $main_banner_url = $xml.installerInformation.mainBannerURI
302 :    
303 :     If ($main_banner_url) {
304 :     $object = Get-WebClient
305 :     $localPath = “$tmp\xvid_main_banner.png”
306 :     $object.DownloadFile($main_banner_url, $localPath)
307 :     $update_file = (get-item "$tmp\xvid_main_banner.png")
308 :     $main_banner = [System.Drawing.Image]::Fromfile($update_file);
309 :     $pictureBox.Image = $main_banner;
310 :     }
311 :     $form.controls.add($pictureBox)
312 :    
313 :     $SkipButton = New-Object System.Windows.Forms.Button
314 :     $SkipButton.Text = "Skip this version"
315 :     $LaterButton = New-Object System.Windows.Forms.Button
316 :     $LaterButton.Text = "Remind me later"
317 :     $InstallButton = New-Object System.Windows.Forms.Button
318 :     $InstallButton.Text = "Install update"
319 :    
320 :     $SkipButton.Location = New-Object System.Drawing.Size(96, 400)
321 :     $SkipButton.Size = New-Object System.Drawing.Size(100, 25)
322 :     $form.controls.add($SkipButton)
323 :    
324 :     $LaterButton.Location = New-Object System.Drawing.Size(380, 400)
325 :     $LaterButton.Size = New-Object System.Drawing.Size(100, 25)
326 :     $form.controls.add($LaterButton)
327 :    
328 :     $InstallButton.Location = New-Object System.Drawing.Size(496, 400)
329 :     $InstallButton.Size = New-Object System.Drawing.Size(100, 25)
330 :     $form.controls.add($InstallButton)
331 :    
332 :     $SkipButton.Add_Click(
333 :     {
334 :     New-Item -ItemType Directory -Force -Path $env:userprofile\.xvid
335 :     "$script:remote_xvid_version" | Out-File $env:userprofile\.xvid\skip_update.log
336 :     $form.Close()
337 :     }
338 :     )
339 :    
340 :     $LaterButton.Add_Click(
341 :     {
342 :     If ($script:downloadSuccessful -eq 0 -or $script:canceledDownload -eq $true) {
343 :     $form.Close()
344 :     }
345 :    
346 :     $script:canceledDownload = $true;
347 :     }
348 :     )
349 :    
350 :     $InstallButton.Add_Click(
351 :     {
352 :     $label1.Text = "Preparing the Xvid Update"
353 :     $label2.Text = "Please stay tuned while the update is downloading..."
354 :     $form.Refresh()
355 :     $LaterButton.Location = New-Object System.Drawing.Size(496, 400)
356 :     $LaterButton.Text = "Cancel"
357 :     If ($script:download_banner_link) {
358 :     $script:banner_link = $script:download_banner_link
359 :     }
360 :     else {
361 :     $script:banner_link = $null
362 :     }
363 :     If ($script:download_banner_url) {
364 :     $object = Get-WebClient
365 :     $localPath = “$tmp\xvid_download_banner.png”
366 :     $object.DownloadFile($script:download_banner_url, $localPath)
367 :     $download_img = (get-item "$tmp\xvid_download_banner.png")
368 :     $download_banner = [System.Drawing.Image]::Fromfile($download_img);
369 :     $pictureBox.Image = $download_banner;
370 :     }
371 :     else {
372 :     $pictureBox.Image = $null
373 :     }
374 :     $pictureBox.Height = 220;
375 :     $form.controls.remove($InstallButton)
376 :     $form.controls.remove($SkipButton)
377 :    
378 :     $progressBar = New-Object System.Windows.Forms.ProgressBar
379 :     $progressBar.Name = 'progressBar00'
380 :     $progressBar.Value = 0
381 :     $progressBar.Style="Continuous"
382 :     $progressBar.Size = New-Object System.Drawing.Size(500, 30)
383 :     $progressBar.Left = 96
384 :     $progressBar.Top = 330
385 :     $form.Controls.Add($progressBar)
386 :     $labelDownload = New-Object System.Windows.Forms.Label
387 :     $labelDownload.Text = "Downloading $script:newInstallerFile..."
388 :     $labelDownload.AutoSize = $True
389 :     $labelDownload.Location = New-Object System.Drawing.Size(96, 310)
390 :     $form.Controls.Add($labelDownload)
391 :     $label0 = New-Object System.Windows.Forms.Label
392 :     $label0.Text = "0%"
393 :     $label0.AutoSize = $True
394 :     $label0.Location = New-Object System.Drawing.Size(96, 368)
395 :     $form.Controls.Add($label0)
396 :     $label100 = New-Object System.Windows.Forms.Label
397 :     $label100.Text = "100%"
398 :     $label100.AutoSize = $True
399 :     $label100.Location = New-Object System.Drawing.Size(564, 368)
400 :     $form.Controls.Add($label100)
401 :     $form.Refresh()
402 :    
403 :     $script:downloadSuccessful = 1
404 :     $downloadUrl = "$script:newDownloadURL$script:newInstallerFile"
405 :     $ret = (downloadFile "$downloadUrl" “$tmp\$script:newInstallerFile”)
406 :    
407 :     If ($ret -ne 0) {
408 :     If ($script:canceledDownload -eq $false) {
409 :     $label1.Text = "Download Error!"
410 :     $label2.Text = "We're sorry but the download failed. Please try again later."
411 :     }
412 :     else {
413 :     $label1.Text = "Update Canceled!"
414 :     $label2.Text = "We've stopped the update at your request. You may try again later."
415 :     }
416 :    
417 :     $LaterButton.Text = "OK"
418 :     $form.controls.remove($progressBar)
419 :     $form.controls.remove($labelDownload)
420 :     $form.controls.remove($label0)
421 :     $form.controls.remove($label100)
422 :    
423 :     If ($script:error_banner_url) {
424 :     $object = Get-WebClient
425 :     $localPath = “$tmp\xvid_error_banner.png”
426 :     $object.DownloadFile($script:error_banner_url, $localPath)
427 :     $error_img = (get-item "$tmp\xvid_error_banner.png")
428 :     $error_banner = [System.Drawing.Image]::Fromfile($error_img)
429 :     $pictureBox.Image = $error_banner
430 :     }
431 :     else {
432 :     $pictureBox.Image = $null
433 :     }
434 :     $script:banner_link = $null
435 :     $pictureBox.Height = 300;
436 :     $form.Refresh()
437 :     }
438 :     else {
439 :     $script:downloadSuccessful = 2
440 :     $form.Close();
441 :     }
442 :     })
443 :    
444 :     $form.Add_Closing({$script:canceledDownload = $true})
445 :    
446 :     $script:downloadSuccessful = 0
447 :     $script:canceledDownload = $false
448 :    
449 :     $form.Add_Shown( { $form.Activate(); $InstallButton.focus() } )
450 :     $ret = $form.ShowDialog()
451 :    
452 :     if ($script:downloadSuccessful -eq 2) {
453 :     $cmd="$tmp\$script:newInstallerFile"
454 :     Try {
455 :     $key = 'HKCU:\SOFTWARE\GNU\XviD'
456 :     $key = (Get-ItemProperty -Path $key -Name Supported_4CC).Supported_4CC
457 :    
458 :     $other = $divx = $3ivx = 0
459 :     If ($key -band 4) {
460 :     $other = 1
461 :     }
462 :     If ($key -band 1) {
463 :     $3ivx = 1
464 :     }
465 :     If ($key -band 2) {
466 :     $divx = 1
467 :     }
468 :    
469 :     If ((start-process $cmd -ArgumentList "--mode unattended --unattendedmodeui minimalWithDialogs --decode_divx $divx --decode_3ivx $3ivx --decode_other $other" -PassThru -Wait).ExitCode -ne 0) {
470 :     $ret = [System.Windows.Forms.MessageBox]::Show("Unfortunately, the Xvid update failed! Please try again." , "Xvid Update Error", 16)
471 :     }
472 :     else {
473 :     $ret = [System.Windows.Forms.MessageBox]::Show("The Xvid update got installed successfully. Have fun!" , "Xvid Update Success", 0, 64)
474 :     }
475 :     }
476 :     Catch {
477 :     $ret = [System.Windows.Forms.MessageBox]::Show("The Xvid update could not be run. Please try again." , "Xvid Update Error", 0, 16)
478 :     }
479 :     }
480 :     Exit 1

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4