forked from mrpond/BlockTheSpot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.ps1
368 lines (319 loc) · 12.1 KB
/
install.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
param (
[Parameter()]
[switch]
$UninstallSpotifyStoreEdition = (Read-Host -Prompt 'Uninstall Spotify Windows Store edition if it exists (Y/N)') -eq 'y',
[Parameter()]
[switch]
$UpdateSpotify,
[Parameter()]
[switch]
$RemoveAdPlaceholder = (Read-Host -Prompt 'Optional - Remove ad placeholder and upgrade button. (Y/N)') -eq 'y'
)
# Ignore errors from `Stop-Process`
$PSDefaultParameterValues['Stop-Process:ErrorAction'] = [System.Management.Automation.ActionPreference]::SilentlyContinue
[System.Version] $minimalSupportedSpotifyVersion = '1.1.73.517'
[System.Version] $maximalSupportedSpotifyVersion = '1.1.78.765'
function Get-File
{
param (
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[System.Uri]
$Uri,
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[System.IO.FileInfo]
$TargetFile,
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[Int32]
$BufferSize = 1,
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[ValidateSet('KB, MB')]
[String]
$BufferUnit = 'MB',
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[ValidateSet('KB, MB')]
[Int32]
$Timeout = 10000
)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$useBitTransfer = $null -ne (Get-Module -Name BitsTransfer -ListAvailable) -and ($PSVersionTable.PSVersion.Major -le 5) -and ((Get-Service -Name BITS).StartType -ne [System.ServiceProcess.ServiceStartMode]::Disabled)
if ($useBitTransfer)
{
Write-Information -MessageData 'Using a fallback BitTransfer method since you are running Windows PowerShell'
Start-BitsTransfer -Source $Uri -Destination "$($TargetFile.FullName)"
}
else
{
$request = [System.Net.HttpWebRequest]::Create($Uri)
$request.set_Timeout($Timeout) #15 second timeout
$response = $request.GetResponse()
$totalLength = [System.Math]::Floor($response.get_ContentLength() / 1024)
$responseStream = $response.GetResponseStream()
$targetStream = New-Object -TypeName ([System.IO.FileStream]) -ArgumentList "$($TargetFile.FullName)", Create
switch ($BufferUnit)
{
'KB' { $BufferSize = $BufferSize * 1024 }
'MB' { $BufferSize = $BufferSize * 1024 * 1024 }
Default { $BufferSize = 1024 * 1024 }
}
Write-Verbose -Message "Buffer size: $BufferSize B ($($BufferSize/("1$BufferUnit")) $BufferUnit)"
$buffer = New-Object byte[] $BufferSize
$count = $responseStream.Read($buffer, 0, $buffer.length)
$downloadedBytes = $count
$downloadedFileName = $Uri -split '/' | Select-Object -Last 1
while ($count -gt 0)
{
$targetStream.Write($buffer, 0, $count)
$count = $responseStream.Read($buffer, 0, $buffer.length)
$downloadedBytes = $downloadedBytes + $count
Write-Progress -Activity "Downloading file '$downloadedFileName'" -Status "Downloaded ($([System.Math]::Floor($downloadedBytes/1024))K of $($totalLength)K): " -PercentComplete ((([System.Math]::Floor($downloadedBytes / 1024)) / $totalLength) * 100)
}
Write-Progress -Activity "Finished downloading file '$downloadedFileName'"
$targetStream.Flush()
$targetStream.Close()
$targetStream.Dispose()
$responseStream.Dispose()
}
}
function Test-SpotifyVersion
{
param (
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[System.Version]
$MinimalSupportedVersion,
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[System.Version]
$MaximalSupportedVersion,
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[System.Version]
$TestedVersion
)
process
{
return ($MinimalSupportedVersion.CompareTo($TestedVersion) -le 0) -and ($MaximalSupportedVersion.CompareTo($TestedVersion) -ge 0)
}
}
Write-Host @'
*****************
@mrpond message:
#Thailand #ThaiProtest #ThailandProtest #freeYOUTH
Please retweet these hashtag, help me stop dictator government!
*****************
'@
Write-Host @'
*****************
Authors: @Nuzair46, @KUTlime
*****************
'@
$spotifyDirectory = Join-Path -Path $env:APPDATA -ChildPath 'Spotify'
$spotifyExecutable = Join-Path -Path $spotifyDirectory -ChildPath 'Spotify.exe'
$spotifyApps = Join-Path -Path $spotifyDirectory -ChildPath 'Apps'
[System.Version] $actualSpotifyClientVersion = (Get-ChildItem -LiteralPath $spotifyExecutable -ErrorAction:SilentlyContinue).VersionInfo.ProductVersionRaw
Write-Host "Stopping Spotify...`n"
Stop-Process -Name Spotify
Stop-Process -Name SpotifyWebHelper
if ($PSVersionTable.PSVersion.Major -ge 7)
{
Import-Module Appx -UseWindowsPowerShell
}
if (Get-AppxPackage -Name SpotifyAB.SpotifyMusic)
{
Write-Host "The Microsoft Store version of Spotify has been detected which is not supported.`n"
if ($UninstallSpotifyStoreEdition)
{
Write-Host "Uninstalling Spotify.`n"
Get-AppxPackage -Name SpotifyAB.SpotifyMusic | Remove-AppxPackage
}
else
{
Read-Host "Exiting...`nPress any key to exit..."
exit
}
}
Push-Location -LiteralPath $env:TEMP
try
{
# Unique directory name based on time
New-Item -Type Directory -Name "BlockTheSpot-$(Get-Date -UFormat '%Y-%m-%d_%H-%M-%S')" |
Convert-Path |
Set-Location
}
catch
{
Write-Output $_
Read-Host 'Press any key to exit...'
exit
}
Write-Host "Downloading latest patch (chrome_elf.zip)...`n"
$elfPath = Join-Path -Path $PWD -ChildPath 'chrome_elf.zip'
try
{
$uri = 'https://github.com/mrpond/BlockTheSpot/releases/latest/download/chrome_elf.zip'
Get-File -Uri $uri -TargetFile "$elfPath"
}
catch
{
Write-Output $_
Start-Sleep
}
Expand-Archive -Force -LiteralPath "$elfPath" -DestinationPath $PWD
Remove-Item -LiteralPath "$elfPath" -Force
$spotifyInstalled = Test-Path -LiteralPath $spotifyExecutable
$unsupportedClientVersion = ($actualSpotifyClientVersion | Test-SpotifyVersion -MinimalSupportedVersion $minimalSupportedSpotifyVersion -MaximalSupportedVersion $maximalSupportedSpotifyVersion) -eq $false
if (-not $UpdateSpotify -and $unsupportedClientVersion)
{
if ((Read-Host -Prompt 'In order to install Block the Spot, your Spotify client must be updated. Do you want to continue? (Y/N)') -ne 'y')
{
exit
}
}
if (-not $spotifyInstalled -or $UpdateSpotify -or $unsupportedClientVersion)
{
Write-Host 'Downloading the latest Spotify full setup, please wait...'
$spotifySetupFilePath = Join-Path -Path $PWD -ChildPath 'SpotifyFullSetup.exe'
try
{
$uri = 'https://download.scdn.co/SpotifyFullSetup.exe'
Get-File -Uri $uri -TargetFile "$spotifySetupFilePath"
}
catch
{
Write-Output $_
Read-Host 'Press any key to exit...'
exit
}
New-Item -Path $spotifyDirectory -ItemType:Directory -Force | Write-Verbose
[System.Security.Principal.WindowsPrincipal] $principal = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$isUserAdmin = $principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
Write-Host 'Running installation...'
if ($isUserAdmin)
{
Write-Host
Write-Host 'Creating scheduled task...'
$apppath = 'powershell.exe'
$taskname = 'Spotify install'
$action = New-ScheduledTaskAction -Execute $apppath -Argument "-NoLogo -NoProfile -Command & `'$spotifySetupFilePath`'"
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date)
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -WakeToRun
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $taskname -Settings $settings -Force | Write-Verbose
Write-Host 'The install task has been scheduled. Starting the task...'
Start-ScheduledTask -TaskName $taskname
Start-Sleep -Seconds 2
Write-Host 'Unregistering the task...'
Unregister-ScheduledTask -TaskName $taskname -Confirm:$false
Start-Sleep -Seconds 2
}
else
{
Start-Process -FilePath "$spotifySetupFilePath"
}
while ($null -eq (Get-Process -Name Spotify -ErrorAction SilentlyContinue))
{
# Waiting until installation complete
Start-Sleep -Milliseconds 100
}
# Create a Shortcut to Spotify in %APPDATA%\Microsoft\Windows\Start Menu\Programs and Desktop
# (allows the program to be launched from search and desktop)
$wshShell = New-Object -ComObject WScript.Shell
$desktopShortcutPath = "$env:USERPROFILE\Desktop\Spotify.lnk"
if ((Test-Path $desktopShortcutPath) -eq $false)
{
$desktopShortcut = $wshShell.CreateShortcut($desktopShortcutPath)
$desktopShortcut.TargetPath = "$env:APPDATA\Spotify\Spotify.exe"
$desktopShortcut.Save()
}
$startMenuShortcutPath = "$env:APPDATA\Microsoft\Windows\Start Menu\Programs\Spotify.lnk"
if ((Test-Path $startMenuShortcutPath) -eq $false)
{
$startMenuShortcut = $wshShell.CreateShortcut($startMenuShortcutPath)
$startMenuShortcut.TargetPath = "$env:APPDATA\Spotify\Spotify.exe"
$startMenuShortcut.Save()
}
Write-Host 'Stopping Spotify...Again'
Stop-Process -Name Spotify
Stop-Process -Name SpotifyWebHelper
Stop-Process -Name SpotifyFullSetup
}
$elfDllBackFilePath = Join-Path -Path $spotifyDirectory -ChildPath 'chrome_elf_bak.dll'
$elfBackFilePath = Join-Path -Path $spotifyDirectory -ChildPath 'chrome_elf.dll'
if ((Test-Path $elfDllBackFilePath) -eq $false)
{
Move-Item -LiteralPath "$elfBackFilePath" -Destination "$elfDllBackFilePath" | Write-Verbose
}
Write-Host 'Patching Spotify...'
$patchFiles = (Join-Path -Path $PWD -ChildPath 'chrome_elf.dll'), (Join-Path -Path $PWD -ChildPath 'config.ini')
Copy-Item -LiteralPath $patchFiles -Destination "$spotifyDirectory"
if ($RemoveAdPlaceholder)
{
$xpuiBundlePath = Join-Path -Path $spotifyApps -ChildPath 'xpui.spa'
$xpuiUnpackedPath = Join-Path -Path (Join-Path -Path $spotifyApps -ChildPath 'xpui') -ChildPath 'xpui.js'
$fromZip = $false
# Try to read xpui.js from xpui.spa for normal Spotify installations, or
# directly from Apps/xpui/xpui.js in case Spicetify is installed.
if (Test-Path $xpuiBundlePath)
{
Add-Type -Assembly 'System.IO.Compression.FileSystem'
Copy-Item -Path $xpuiBundlePath -Destination "$xpuiBundlePath.bak"
$zip = [System.IO.Compression.ZipFile]::Open($xpuiBundlePath, 'update')
$entry = $zip.GetEntry('xpui.js')
# Extract xpui.js from zip to memory
$reader = New-Object System.IO.StreamReader($entry.Open())
$xpuiContents = $reader.ReadToEnd()
$reader.Close()
$fromZip = $true
}
elseif (Test-Path $xpuiUnpackedPath)
{
Copy-Item -LiteralPath $xpuiUnpackedPath -Destination "$xpuiUnpackedPath.bak"
$xpuiContents = Get-Content -LiteralPath $xpuiUnpackedPath -Raw
Write-Host 'Spicetify detected - You may need to reinstall BTS after running "spicetify apply".';
}
else
{
Write-Host 'Could not find xpui.js, please open an issue on the BlockTheSpot repository.'
}
if ($xpuiContents)
{
# Replace ".ads.leaderboard.isEnabled" + separator - '}' or ')'
# With ".ads.leaderboard.isEnabled&&false" + separator
$xpuiContents = $xpuiContents -replace '(\.ads\.leaderboard\.isEnabled)(}|\))', '$1&&false$2'
# Delete ".createElement(XX,{(spec:X),?onClick:X,className:XX.X.UpgradeButton}),X()"
$xpuiContents = $xpuiContents -replace '\.createElement\([^.,{]+,{(?:spec:[^.,]+,)?onClick:[^.,]+,className:[^.]+\.[^.]+\.UpgradeButton}\),[^.(]+\(\)', ''
if ($fromZip)
{
# Rewrite it to the zip
$writer = New-Object System.IO.StreamWriter($entry.Open())
$writer.BaseStream.SetLength(0)
$writer.Write($xpuiContents)
$writer.Close()
$zip.Dispose()
}
else
{
Set-Content -LiteralPath $xpuiUnpackedPath -Value $xpuiContents
}
}
}
else
{
Write-Host "Won't remove ad placeholder and upgrade button.`n"
}
$tempDirectory = $PWD
Pop-Location
Remove-Item -LiteralPath $tempDirectory -Recurse
Write-Host 'Patching Complete, starting Spotify...'
Start-Process -WorkingDirectory $spotifyDirectory -FilePath $spotifyExecutable
Write-Host 'Done.'
Write-Host @'
*****************
@mrpond message:
#Thailand #ThaiProtest #ThailandProtest #freeYOUTH
Please retweet these hashtag, help me stop dictator government!
*****************
'@