forked from R3dFruitRollUp/RainbowMiner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdater.ps1
171 lines (146 loc) · 7.57 KB
/
Updater.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
using module .\Modules\Include.psm1
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[String]$calledfrom = "bat"
)
if ($script:MyInvocation.MyCommand.Path) {Set-Location (Split-Path $script:MyInvocation.MyCommand.Path)}
#[Environment]::CurrentDirectory = $ExecutionContext.SessionState.Path.CurrentFileSystemLocation
if ([Net.ServicePointManager]::SecurityProtocol -notmatch [Net.SecurityProtocolType]::Tls12) {
[Net.ServicePointManager]::SecurityProtocol += [Net.SecurityProtocolType]::Tls12
}
if (-not (Test-Path ".\Data\version.json")) {
Write-Host "No version info found. Update will be stopped"
exit
}
if (Test-Path "Start.bat.saved") {
if ($calledfrom -ne "core") {exit}
Remove-Item "Start.bat.saved" -Force
if (Test-Path "start.sh.saved") {Remove-Item "start.sh.saved" -Force}
}
Set-OsFlags
$RBMVersion = Confirm-Version (Get-Content ".\Data\version.json" | ConvertFrom-Json).Version -Force -Silent
if (Test-Path ".\Downloads\config.json") {
try {
$DownloaderConfig = Get-ContentByStreamReader ".\Downloads\config.json" | ConvertFrom-Json -ErrorAction Ignore
} catch {
if ($Error.Count){$Error.RemoveAt(0)}
}
}
if (-not $DownloaderConfig) {
$DownloaderConfig = [PSCustomObject]@{
EnableMinerBackups = $true
EnableKeepDownloads = $true
}
}
$Name = "RainbowMiner"
try {
if ($RBMVersion.RemoteVersion -gt $RBMVersion.Version -and $RBMVersion.DownloadURI) {
Write-Host "Updating from v$($RBMVersion.Version) to v$($RBMVersion.RemoteVersion)" -ForegroundColor Yellow
Write-Host " (1/3) Downloading $($RBMVersion.DownloadURI) .. "
if (-not (Test-Path ".\Downloads")) {New-Item "Downloads" -ItemType "directory" | Out-Null}
$FileName = Join-Path ".\Downloads" (Split-Path $RBMVersion.DownloadURI -Leaf)
if (Test-Path $FileName) {Remove-Item $FileName}
if ($RBMVersion.DownloadURI -eq "") {throw}
Invoke-WebRequest $RBMVersion.DownloadURI -OutFile $FileName -UseBasicParsing
if (-not (Test-Path $FileName) -or (Get-Item $FileName).Length -lt 2MB) {throw}
Write-Host " (2/3) Deleting and backup old files .."
@("Start.bat","start.sh") | Foreach-Object {if (Test-Path $_) {Copy-Item $_ "$($_).saved" -Force -ErrorAction Ignore}}
if (Test-Path "MinersOldVersions") {$PreserveMiners = Compare-Object @(Get-ChildItem "Miners" | Select-Object -ExpandProperty Name) @(Get-ChildItem "MinersOldVersions" | Select-Object -ExpandProperty Name) -IncludeEqual -ExcludeDifferent | Select-Object -ExpandProperty InputObject}
@("Miners","APIs","Balances","Pools") | Foreach-Object {if (Test-Path ".\$($_)") {Remove-Item ".\$($_)" -Recurse -Force -ErrorAction Ignore}}
Get-ChildItem ".\Data" -Filter "*.json" -File | Where-Object {$_.Name -notin @("lastdrun.json","localapiport.json","minerdata.json","mrrinfo.json","poolsdata.json","unprofitable.json","version.json")} | Foreach-Object {Remove-Item $_.FullName -Force -ErrorAction Ignore}
Write-Host " (3/3) Extracting new files .."
$FromFullPath = [IO.Path]::GetFullPath($FileName)
$ToFullPath = [IO.Path]::GetFullPath(".")
if ($IsLinux) {
$Params = @{
FilePath = "7z"
ArgumentList = "x `"$FromFullPath`" -o`"$ToFullPath`" -y"
RedirectStandardOutput = Join-Path ".\Logs" "7z-console.log"
RedirectStandardError = Join-Path ".\Logs" "7z-error.log"
}
} else {
$Params = @{
FilePath = "7z"
ArgumentList = "x `"$FromFullPath`" -o`"$ToFullPath`" -y -spe"
}
}
$Params.PassThru = $true
(Start-Process @Params).WaitForExit() > $null
if ($PreserveMiners) {$PreserveMiners | Foreach-Object {if (Test-Path "MinersOldVersions\$_") {Copy-Item "MinersOldVersions\$_" "Miners\$_" -Force}}}
if ($IsLinux) {
Get-ChildItem ".\*.sh" -File | Foreach-Object {try {& chmod +x "$($_.FullName)" > $null} catch {}}
Get-ChildItem ".\IncludesLinux\bash\*" -File | Foreach-Object {try {& chmod +x "$($_.FullName)" > $null} catch {}}
Get-ChildItem ".\IncludesLinux\bin\*" -File | Foreach-Object {try {& chmod +x "$($_.FullName)" > $null} catch {}}
} elseif ($IsWindows) {
#Handle write locks
try {
if (-not (Test-Path "_update")) {New-Item "_update" -ItemType "directory" > $null}
$Params = @{
FilePath = "7z"
ArgumentList = "x `"$FromFullPath`" -o`"$(Join-Path $ToFullPath "_update")`" 7z.exe 7z.dll `"Includes\curl\x32\curl.exe`" `"Includes\curl\x64\curl.exe`" `"Includes\curl\x32\libcurl.dll`" `"Includes\curl\x64\libcurl-x64.dll`" `"Includes\getcpu\GetCPU.exe`" `"Includes\getcpu\OpenHardwareMonitorLib.dll`" -y -spe"
PassThru = $true
}
(Start-Process @Params).WaitForExit() > $null
Get-ChildItem "_update" -Recurse -File | Foreach-Object {
$FileNameTo = $_.FullName -replace "^.+\\_update\\"
if (-not (Test-Path $FileNameTo) -or ((Get-FileHash $FileNameTo -Algorithm MD5).Hash -ne (Get-FileHash $_.FullName -Algorithm MD5).Hash)) {
Write-Host "Update $FileNameTo"
try {
$RetryLock = 20
$IsLocked = $true
do {
Try {
Copy-Item -Path $_.FullName -Destination $FileNameTo -Force
$IsLocked = $False
} Catch {
$RetryLock--
if ($RetryLock -gt 0) {Sleep -Milliseconds 250}
}
} while ($IsLocked -and ($RetryLock -gt 0))
} catch {
if ($Error.Count){$Error.RemoveAt(0)}
}
if ($IsLocked) {
Write-Host "Failed to update $FileNameTo. Please download manually from Github." -ForegroundColor Yellow
}
}
}
if (Test-Path "_update") {
Remove-Item "_update" -Force -Recurse
}
} catch {
if ($Error.Count){$Error.RemoveAt(0)}
Write-Host "Failed to update exe files. Please download manually from Github." -ForegroundColor Yellow
}
}
if (-not $DownloaderConfig.EnableKeepDownloads -and (Test-Path $FileName)) {
Get-ChildItem $FileName -File | Foreach-Object {Remove-Item $_}
}
Write-Host "Update finished. Restarting $Name .." -ForegroundColor Green
} else {
Write-Host "RainbowMiner is already uptodate. Restarting .." -ForegroundColor Green
}
}
catch {
Write-Host "$Name failed to update. Please download manually at $($RBMVersion.ManuaURI)" -ForegroundColor Yellow
if ($calledfrom -ne "core") {
$message = "Press any key to return to $name"
if ($psISE)
{
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show("$message")
}
else
{
Write-Host "$message" -ForegroundColor Yellow
[void]($Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown'))
}
} else {
Sleep 2
}
exit 0
}
if (-not $psISE) {
exit 1
}