This repository has been archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.ps1
76 lines (64 loc) · 2.84 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
$resourcePackPath = '.\resourcepack.json'
$scriptName = 'update.ps1'
$scriptTempName = 'update.temp.ps1'
$scriptParentName = 'korean'
$global:progressPreference = 'silentlyContinue'
if ($MyInvocation.MyCommand.Name -eq $scriptName) {
Write-Host '[.] Copy script to update.temp.ps1'
Copy-Item ".\$scriptName" ".\$scriptTempName"
Write-Host '[.] Run copied script'
Start-Process powershell -ArgumentList ".\$scriptTempName"
Exit
}
if ((Get-Item .).Name -ne $scriptParentName) {
Write-Host '[!] This script is not placed properly: ' $MyInvocation.MyCommand.Path
Remove-Item ".\$($scriptTempName)"
Write-Host '[!] Update failed. Press enter key to exit.'
[void][System.Console]::ReadKey($true)
Exit
}
if (!(Test-Path $resourcePackPath -PathType Leaf)) {
Write-Host '[!] Cannot found resourcePackPath: ' $resourcePackPath
Remove-Item ".\$($scriptTempName)"
Write-Host '[!] Update failed. Press enter key to exit.'
[void][System.Console]::ReadKey($true)
Exit
}
try {
Write-Host '[-] Update the resource pack'
$resourcePackJson = Get-Content $resourcePackPath -Encoding UTF8 | Out-String | ConvertFrom-Json
Write-Host '[.] Current version: ' $resourcePackJson.version
$releaseInfo = (Invoke-RestMethod -Method GET -Uri "https://api.github.com/repos/yf-dev/majsoul-plus-korean/releases/latest")
$versionCode = $releaseInfo.tag_name
$downloadUri = $releaseInfo.assets[0].browser_download_url
Write-Host '[.] Latest version: ' $versionCode
$confirmMsg = 'Do you want to update? [y/n]'
$confirm = Read-Host $confirmMsg
while ($confirm -ne 'y') {
if ($confirm -eq 'n') { exit }
$confirm = Read-Host $confirmMsg
}
Write-Host '[.] Downloading the resource pack...'
$pathZip = Join-Path -Path $([System.IO.Path]::GetTempPath()) -ChildPath ($(Split-Path -Path $downloadUri -Leaf) + '.zip')
Invoke-WebRequest -Uri $downloadUri -Out $pathZip
Write-Host '[.] Extracting the resource pack...'
$tempExtract = Join-Path -Path $([System.IO.Path]::GetTempPath()) -ChildPath $((New-Guid).Guid)
Expand-Archive -Path $pathZip -DestinationPath $tempExtract -Force
Write-Host '[.] Remove old resource pack data...'
Get-ChildItem -Path '.\' -Exclude ".\$scriptTempName" | Remove-Item -Recurse -Force
Write-Host '[.] Copy new resource pack data...'
Move-Item -Path "$tempExtract\korean\*" -Destination '.\' -Force
Write-Host '[.] Clean temporary data...'
Remove-Item -Path $tempExtract -Force -Recurse -ErrorAction SilentlyContinue
Remove-Item $pathZip -Force
Write-Host '[-] Update Completed. Press enter key to exit.'
[void][System.Console]::ReadKey($true)
}
catch {
Write-Host $_
Write-Host '[!] Update failed. Press enter key to exit.'
[void][System.Console]::ReadKey($true)
}
finally {
Remove-Item ".\$($scriptTempName)"
}