-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpaper.ps1
230 lines (187 loc) · 5.09 KB
/
paper.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
# Gradle
$Build = $true
# JVM
$Xms = "1G"
$Xmx = "2G"
$Debug = $true
$Jar = "paper.jar"
# Spigot
$Version = "1.16.4"
# Plugins
$Plugins = (
"https://github.com/noonmaru/kotlin-plugin/releases/download/1.4.21/Kotlin.jar",
"https://github.com/noonmaru/auto-reloader/releases/download/1.0.2/AutoReloader.jar",
"https://ci.dmulloy2.net/job/ProtocolLib/lastSuccessfulBuild/artifact/target/ProtocolLib.jar"
)
# Backup
$AskBackup = $false
Function Download-File
{
[CmdletBinding()]
Param (
[string]$Url,
[string]$Folder,
[Parameter(Mandatory = $False)] [string]$Filename
)
Create-Directory $Folder
$Location = (Get-Location).Path
$Destfolder = Resolve-Path("$Location/$Folder")
try
{
$WebRequest = [System.Net.HttpWebRequest]::Create($Url);
$WebRequest.Method = "GET"
$WebResponse = $WebRequest.GetResponse()
if ( [string]::IsNullOrEmpty($Filename))
{
$Disposition = $WebResponse.Headers['Content-Disposition']
if ( [string]::IsNullOrEmpty($Disposition))
{
$Filename = [System.IO.Path]::GetFileName($Url)
}
else
{
$Filename = [System.Net.Mime.ContentDisposition]::new($Disposition).FileName
}
}
$Dest = "$Destfolder/$Filename"
$FileInfo = [System.IO.FileInfo]$Dest
if (Test-Path $Dest)
{
$RemoteLastModified = $WebResponse.LastModified
$LocalLastModified = $FileInfo.LastWriteTime
if ([datetime]::Compare($RemoteLastModified, $LocalLastModified) -eq 0)
{
Write-Host "UP-TO-DATE $Filename($Url)"
$WebResponse.Dispose()
return
}
Write-Host "Updating $Filename from $url"
}
else
{
Write-Host "Downloading $Filename from $url"
}
$ResponseStream = $WebResponse.GetResponseStream()
$FileWriter = New-Object System.IO.FileStream ($Dest, [System.IO.FileMode]::Create)
[byte[]]$buffer = New-Object byte[] 4096
do
{
$length = $ResponseStream.Read($buffer, 0, 4096)
$FileWriter.Write($buffer, 0, $length)
} while ($length -ne 0)
$ResponseStream.Close()
$FileWriter.Close()
$FileInfo.LastWriteTime = $WebResponse.LastModified
}
catch [System.Net.WebException]
{
$Status = $_.Exception.Response.StatusCode
$Msg = $_.Exception
Write-Host " Failed to dowloading $Dest, Status code: $Status - $Msg" -ForegroundColor Red
}
}
Function Create-Directory([string]$PathName)
{
if (!(Test-Path $PathName))
{
New-Item $PathName -Type Directory | Out-Null
}
}
Function Agree-EULA
{
$Eula = "eula.txt"
if (!(Test-Path $Eula))
{
New-Item $Eula -ItemType "file" | Out-Null
Add-Content $Eula "eula=true"
}
}
function Choice
{
Param(
[string]$Prompt,
[string]$Choice,
[char]$Default,
[int]$Seconds
)
$Choice = $Choice.ToUpper()
$StartTime = Get-Date
$TimeOut = New-TimeSpan -Seconds $Seconds
Write-Host $Prompt
$Choose = $Default
while ($CurrentTime -lt $StartTime + $TimeOut)
{
if ($host.UI.RawUI.KeyAvailable)
{
[string]$Key = ($host.UI.RawUI.ReadKey("IncludeKeyDown,NoEcho")).character
$Key = $Key.ToUpper().ToCharArray()[0]
if ( $Choice.Contains($Key))
{
$Choose = $Key
Break
}
}
$CurrentTime = Get-Date
}
Write-Host $Choose
return $Choose
}
Function Backup
{
$Backup = "backup"
Create-Directory $Backup
$Date = Get-Date -Format "yyyy-MM-dd HHmmss"
$ArchiveName = "$Backup/$Date.zip"
7z a -tzip $ArchiveName ./ "-xr!*.gz" "-x!paper.jar" "-x!paper.ps1" "-x!backup" "-x!cache" | Out-Null
Write-Host "Backup completed $ArchiveName"
}
$ProjectFolder = Get-Location
$Folder = ".$( (Get-Item($MyInvocation.MyCommand.Name)).BaseName )"
# Setup
Create-Directory $Folder
Set-Location $Folder
$host.UI.RawUI.WindowTitle = "paper $Version"
Agree-EULA
# Download paper
Download-File "https://papermc.io/api/v1/paper/$Version/latest/download" "." $Jar
# Download plugins
foreach ($Plugin in $Plugins)
{
Download-File $Plugin "plugins"
}
# Build JVM args
$JVMArgs = [System.Collections.ArrayList]@(
"-Xms$Xms",
"-Xmx$Xmx"
)
if ($Debug)
{
[void]$JVMArgs.Add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005")
}
[void]$JVMArgs.Add("-jar")
[void]$JVMArgs.Add($Jar)
[void]$JVMArgs.Add("--nogui")
# Run
While ($true)
{
if ($Build)
{
Set-Location $ProjectFolder
./gradlew clean paper
Set-Location $Folder
}
java $JVMArgs
if ($AskBackup)
{
$SkipBackup = Choice "Skip the backup? [Y]es, [N]o" @('Y', 'N') 'N' 3
if ($SkipBackup -eq 'N')
{
Backup
}
}
$Restart = Choice "Restart? [Y]es [N]o" @('Y', 'N') 'Y' 2
if ($Restart -eq 'N')
{
Break
}
}