forked from Revan654/staxrip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdate.ps1
218 lines (190 loc) · 7.5 KB
/
Update.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
function Check-7z {
$7zdir = (Get-Location).Path + "\7z"
if (-not (Test-Path ($7zdir + "\7za.exe")))
{
$download_file = (Get-Location).Path + "\7z.zip"
Write-Host "Downloading 7z" -ForegroundColor Green
Invoke-WebRequest -Uri "http://download.sourceforge.net/sevenzip/7za920.zip" -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::FireFox -OutFile $download_file
Write-Host "Extracting 7z" -ForegroundColor Green
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::ExtractToDirectory($download_file, $7zdir)
Remove-Item -Force $download_file
}
else
{
Write-Host "7z already exist. Skipped download" -ForegroundColor Green
}
}
function Obtain_Path{
$staxrip_path = (get-location).Path + "\StaxRip.exe"
return $staxrip_path
}
function Close_Application{
Get-Process StaxRip
Stop-Process -Name "StaxRip"
#get-process StaxRip | %{ $_.closemainwindow() Clean Exit Only. }
}
function Start_Application($Path) {
Start-Process $Path
}
function Clean_Up {
$Path = Obtain_Path
$Replace = $Path -replace "StaxRip.exe", "StaxRip.7z"
$Replace2 = $Path -replace "StaxRip.exe", "7z"
$Replace3 = $Path -replace "StaxRip.exe", "Update.bat"
Remove-Item $Replace
Remove-Item $Replace2
Remove-Item $Replace3
}
function Clean_Up_Bat_File {
$Path = Obtain_Path
$Replace = $Path -replace "StaxRip.exe", "Update.bat"
Remove-Item $Replace
}
function Check-PowershellVersion {
$version = $PSVersionTable.PSVersion.Major
Write-Host "Checking Windows PowerShell version -- $version" -ForegroundColor Green
if ($version -le 2)
{
Write-Host "Using Windows PowerShell $version is unsupported. Upgrade your Windows PowerShell." -ForegroundColor Red
throw
}
}
function Check-staxrip {
$staxrip = (get-location).Path + "\StaxRip.exe"
$is_exist = Test-Path $staxrip
return $is_exist
}
function Extract-staxrip {
$Links = Invoke-WebRequest -Uri "https://github.com/Revan654/staxrip/releases/latest" –UseBasicParsing -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::FireFox
$pattern = "([0-9].[0-9].[0-9].[0-9]).x64.7z"
$bool = $Links -match $pattern
$FileName = $matches[1] + ".x64.7z"
$7za = (Get-Location).Path + "\7z\7za.exe"
Write-Host "Extracting" $FileName -ForegroundColor Green
& $7za x -y $FileName
}
function Download-staxrip {
Write-Host "Downloading StaxRip" -ForegroundColor Green
$Links = Invoke-WebRequest -Uri "https://github.com/Revan654/staxrip/releases/latest" –UseBasicParsing -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::FireFox
$pattern = "([0-9].[0-9].[0-9].[0-9]).x64.7z"
$bool = $Links -match $pattern
$Build = "https://github.com/Revan654/staxrip/releases/download/" + $matches[1] + "/" + $matches[1] + ".x64.7z"
Invoke-WebRequest -Uri $Build -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0" -OutFile "Staxrip.7z"
}
function Get-Latest-staxrip {
$Links = Invoke-WebRequest -Uri "https://github.com/Revan654/staxrip/releases/latest" –UseBasicParsing -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::FireFox
$pattern = "([0-9].[0-9].[0-9].[0-9]).x64.7z"
$bool = $Links -match $pattern
$Build = "https://github.com/Revan654/staxrip/releases/download/" + $matches[1] + "/" + $matches[1] + ".x64.7z"
return $Build
}
function Get-Arch { #Not needed at This Time
# Reference: http://superuser.com/a/891443
$FilePath = [System.IO.Path]::Combine((Get-Location).Path, 'StaxRip.exe')
[int32]$MACHINE_OFFSET = 4
[int32]$PE_POINTER_OFFSET = 60
[byte[]]$data = New-Object -TypeName System.Byte[] -ArgumentList 4096
$stream = New-Object -TypeName System.IO.FileStream -ArgumentList ($FilePath, 'Open', 'Read')
$stream.Read($data, 0, 4096) | Out-Null
# DOS header is 64 bytes, last element, long (4 bytes) is the address of the PE header
[int32]$PE_HEADER_ADDR = [System.BitConverter]::ToInt32($data, $PE_POINTER_OFFSET)
[int32]$machineUint = [System.BitConverter]::ToUInt16($data, $PE_HEADER_ADDR + $MACHINE_OFFSET)
$result = "" | select FilePath, FileType
$result.FilePath = $FilePath
switch ($machineUint)
{
0 { $result.FileType = 'Native' }
0x014c { $result.FileType = 'i686' } # 32bit
0x0200 { $result.FileType = 'Itanium' }
0x8664 { $result.FileType = 'x86_64' } # 64bit
}
$result
}
function ExtractVersionFromFile {
$Version = (Get-ItemProperty ./staxrip.exe).VersionInfo.FileVersion
return "$Version"
}
function ExtractVersionFromURL {
$filename = Get-Latest-staxrip
$pattern = "([0-9].[0-9].[0-9].[0-9]).x64"
$bool = $filename -match $pattern
return $matches[1]
}
function Test-Admin
{
$user = [Security.Principal.WindowsIdentity]::GetCurrent();
(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
function Upgrade-staxrip {
$need_download = $false
Write-Host "Fetching URL Data for StaxRip Builds" -ForegroundColor Green
$localVersion = ExtractVersionFromFile
$remoteVersion = ExtractVersionFromURL
if ((ExtractVersionFromFile) -ge (ExtractVersionFromURL)) {
Write-Host "You are Already Using Latest Build." -ForegroundColor Red
Write-Host "Current Build: $remoteVersion" -ForegroundColor Red
$need_download = $false
Clean_Up_Bat_File }
else {
Write-Host "There is a Newer Build Available" -ForegroundColor Green
Write-Host "New Build: $remoteVersion" -ForegroundColor Green
$need_download = $true
}
if ($need_download) {
Download-staxrip
Check-7z
Extract-staxrip -Wait
Clean_Up
}
}
function Read-KeyOrTimeout ($prompt, $key){
$seconds = 9
$startTime = Get-Date
$timeOut = New-TimeSpan -Seconds $seconds
Write-Host "$prompt " -ForegroundColor Green
# Basic progress bar
[Console]::CursorLeft = 0
[Console]::Write("[")
[Console]::CursorLeft = $seconds + 2
[Console]::Write("]")
[Console]::CursorLeft = 1
while (-not [System.Console]::KeyAvailable) {
$currentTime = Get-Date
Start-Sleep -s 1
Write-Host "#" -ForegroundColor Green -NoNewline
if ($currentTime -gt $startTime + $timeOut) {
Break
}
}
if ([System.Console]::KeyAvailable) {
$response = [System.Console]::ReadKey($true).Key
}
else {
$response = $key
}
return $response.ToString()
}
#
# Main script entry point
#
if (Test-Admin) {
Write-Host "Running Script with Administrator Privileges" -ForegroundColor Yellow
}
else {
Write-Host "Running Script Without Administrator Privileges" -ForegroundColor Red
}
try {
Check-PowershellVersion
# Sourceforge only support TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$Path = Obtain_Path
Close_Application
Upgrade-staxrip
Start_Application $Path
Write-Host "Operation Complete" -ForegroundColor Magenta
}
catch [System.Exception] {
Write-Host $_.Exception.Message -ForegroundColor Red
exit 1
}