forked from Dice-Developer-Team/mirai-dice-release-noextra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ps1
199 lines (174 loc) · 5.61 KB
/
main.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
$JreURL = "https://gitee.com/suhuiw4123/mirai-dice-release/attach_files/637574/download/OpenJDK11U-Windows-x86.zip"
$GitURL = "https://gitee.com/suhuiw4123/mirai-dice-release/attach_files/637695/download/MinGit-Windows-x86.zip"
$JAVA = ""
$GIT = ""
if (!$PSScriptRoot)
{
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
}
cd "$PSScriptRoot"
function DownloadFile($url, $targetFile)
{
$uri = New-Object "System.Uri" "$url"
$request = [System.Net.HttpWebRequest]::Create($uri)
$request.set_Timeout(15000) #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, Create
$buffer = new-object byte[] 256KB
$count = $responseStream.Read($buffer,0,$buffer.length)
$downloadedBytes = $count
while ($count -gt 0)
{
$targetStream.Write($buffer, 0, $count)
$count = $responseStream.Read($buffer,0,$buffer.length)
$downloadedBytes = $downloadedBytes + $count
Write-Progress -activity "正在下载文件 '$($url.split('/') | Select -Last 1)'" -status "已下载 ($([System.Math]::Floor($downloadedBytes/1024))K of $($totalLength)K): " -PercentComplete ((([System.Math]::Floor($downloadedBytes/1024)) / $totalLength) * 100)
}
Write-Progress -activity "文件 '$($url.split('/') | Select -Last 1)' 下载已完成"
$targetStream.Flush()
$targetStream.Close()
$targetStream.Dispose()
$responseStream.Dispose()
}
Write-Host "Mirai Dice 启动脚本"
Write-Host "初始化"
if (-Not (Test-Path -Path "$PSScriptRoot\.git" -PathType Container))
{
Write-Host "警告:.git文件夹不存在" -ForegroundColor red
Write-Host "这可能代表你未使用正确方式安装此程序" -ForegroundColor red
Write-Host "虽然大多数功能仍将正常工作,但更新功能将无法正常使用" -ForegroundColor red
Write-Host "请尝试使用正确方式重新安装以解决此问题" -ForegroundColor red
}
Try
{
Add-Type -AssemblyName System.IO.Compression.FileSystem -ErrorAction Stop
function Unzip
{
param([string]$zipfile, [string]$outpath)
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
}
Catch {
if (-Not (Test-Path -Path "$PSScriptRoot\unzip.exe" -PathType Leaf))
{
$ZipURL = "https://gitee.com/suhuiw4123/mirai-dice-release/attach_files/646126/download/unzip.exe"
DownloadFile $ZipURL "$PSScriptRoot\unzip.exe"
}
if (-Not (Test-Path -Path "$PSScriptRoot\unzip.exe" -PathType Leaf))
{
Write-Host "无法加载Unzip" -ForegroundColor red
Exit
}
function Unzip
{
param([string]$zipfile, [string]$outpath)
& .\unzip.exe $zipfile -d $outpath
}
}
Write-Host "检测Java"
Try
{
$Command = Get-Command -Name java -ErrorAction Stop
if (($Command | Select-Object -ExpandProperty FileVersionInfo | Select-Object ProductMajorPart).ProductMajorPart -ge 11)
{
$JAVA = "java"
}
}
Catch {}
Try {
$Command = Get-Command -Name "$PSScriptRoot\jre\bin\java" -ErrorAction Stop
if (($Command | Select-Object -ExpandProperty FileVersionInfo | Select-Object ProductMajorPart).ProductMajorPart -ge 11)
{
$JAVA = "$PSScriptRoot\jre\bin\java"
}
}
Catch {}
if ($JAVA -eq "")
{
DownloadFile $JreURL "$PSScriptRoot\java.zip"
Unzip "$PSScriptRoot\java.zip" "$PSScriptRoot\jre\"
Remove-Item "$PSScriptRoot\java.zip"
Try
{
$Command = Get-Command -Name "$PSScriptRoot\jre\bin\java" -ErrorAction Stop
if (($Command | Select-Object -ExpandProperty FileVersionInfo | Select-Object ProductMajorPart).ProductMajorPart -ge 11)
{
$JAVA = "$PSScriptRoot\jre\bin\java"
}
}
Catch
{
Write-Host "无法加载Java!" -ForegroundColor red
Exit
}
}
Write-Host "Java: $JAVA" -ForegroundColor green
Write-Host "检测Git"
Try
{
$Command = Get-Command -Name git -ErrorAction Stop
$GIT = "git"
}
Catch {}
Try
{
$Command = Get-Command -Name "$PSScriptRoot\git\cmd\git" -ErrorAction Stop
$GIT = "$PSScriptRoot\git\cmd\git"
}
Catch {}
if ($GIT -eq "")
{
DownloadFile $GitURL "$PSScriptRoot\git.zip"
Unzip "$PSScriptRoot\git.zip" "$PSScriptRoot\git\"
Remove-Item "$PSScriptRoot\git.zip"
Try
{
$Command = Get-Command -Name "$PSScriptRoot\git\cmd\git" -ErrorAction Stop
$GIT = "$PSScriptRoot\git\cmd\git"
}
Catch
{
Write-Host "无法加载Git!" -ForegroundColor red
Exit
}
}
Write-Host "Git: $GIT" -ForegroundColor green
if (($args[0] -eq "--update") -or ($args[0] -eq "-u"))
{
& "$GIT" fetch --depth=1
& "$GIT" reset --hard origin/master
Write-Host "更新操作已执行完毕" -ForegroundColor green
}
elseif (($args[0] -eq "--revert") -or ($args[0] -eq "-r"))
{
& "$GIT" reset --hard "HEAD@{1}"
Write-Host "回滚操作已执行完毕" -ForegroundColor green
}
elseif (($args[0] -eq "--fullautoslider") -or ($args[0] -eq "-f"))
{
del -Path "$PSScriptRoot\plugins\mirai-automatic-slider*"
del -Path "$PSScriptRoot\plugins\mirai-login-solver-selenium*"
copy -Path "$PSScriptRoot\fslider\mirai-automatic-slider*" "$PSScriptRoot\plugins\"
& "$JAVA" -jar mcl.jar
}
elseif (($args[0] -eq "--autoslider") -or ($args[0] -eq "-a"))
{
del -Path "$PSScriptRoot\plugins\mirai-automatic-slider*"
del -Path "$PSScriptRoot\plugins\mirai-login-solver-selenium*"
copy -Path "$PSScriptRoot\slider\mirai-login-solver-selenium*" "$PSScriptRoot\plugins\"
& "$JAVA" -jar mcl.jar
}
elseif (($args[0] -eq "--slider") -or ($args[0] -eq "-s"))
{
del -Path "$PSScriptRoot\plugins\mirai-automatic-slider*"
del -Path "$PSScriptRoot\plugins\mirai-login-solver-selenium*"
& "$JAVA" "-Dmirai.slider.captcha.supported" "-jar" "mcl.jar"
}
else
{
del -Path "$PSScriptRoot\plugins\mirai-automatic-slider*"
del -Path "$PSScriptRoot\plugins\mirai-login-solver-selenium*"
& "$JAVA" -jar mcl.jar
}