-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLaunch.ps1
279 lines (244 loc) · 11.5 KB
/
Launch.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
#Script courtesy of TnT
#Version 1.0.3
#This script will launch your server and zip up your server directory into a folder of your choice upon the server stopping.
#No warranty is provided whatsoever. Learn this script and what it does before using it.
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-# BEGIN STATIC VARIABLE DECLARATION #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
#Set the minimum PowerShell version this script works with. (DO NOT CHANGE).
New-Variable -Name POWERSHELL_VERSION -Option Constant -Value ([int]3)
#Set the minimum .NET Framework version this script works with. (DO NOT CHANGE).
New-Variable -Name FRAMEWORK_VERSION -Option Constant -Value ([decimal]4.5)
#Set the version number of this script. (DO NOT CHANGE)
New-Variable -Name SCRIPT_VERSION -Option Constant -Value ([string]"1.0.3")
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-# BEGIN POWERSHELL CUSTOMIZATION #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
$Host.UI.RawUI.WindowTitle = "PowerLaunch v$SCRIPT_VERSION"
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-# BEGIN FUNCTION DECLARATION #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
#Function to create the config.txt file. If the file exists, it will not be overwrote.
Function Create-Config() {
If (!(Test-Path config.txt)) {
$currentlocation=Get-Location
$parentfolder=(get-item $currentlocation).parent.FullName
$backupfolder = Join-Path -path $parentfolder -childpath backup
New-Item config.txt -ItemType "file"
Add-Content config.txt "SERVER_NAME=MyServer"
Add-Content config.txt "SERVER_LOCATION=$currentlocation"
Add-Content config.txt "BACKUP_LOCATION=$backupfolder"
Add-Content config.txt "CRAFTBUKKIT=craftbukkit.jar"
Add-Content config.txt "JAVA_FLAGS=-Xmx1G"
Add-Content config.txt "CRAFTBUKKIT_OPTIONS=-o True -p 25565"
Add-Content config.txt "TEST_DEPENDENCIES=True"
Add-Content config.txt "DELETE_LOG=False"
Add-Content config.txt "TAKE_BACKUP=True"
Add-Content config.txt "RESTART_PAUSE=5"
}
}
#Loads all variables from the config.txt file.
Function Load-Variables() {
$types = @{
SERVER_NAME = {$args[0] -as [string]}
SERVER_LOCATION = {$args[0] -as [string]}
BACKUP_LOCATION = {$args[0] -as [string]}
CRAFTBUKKIT = {$args[0] -as [string]}
JAVA_FLAGS = {
($args[0].split(' ')[0] -as [string]),
($args[0].split(' ')[1] -as [string]),
($args[0].split(' ')[2] -as [string]),
($args[0].split(' ')[3] -as [string]),
($args[0].split(' ')[4] -as [string]),
($args[0].split(' ')[5] -as [string]),
($args[0].split(' ')[6] -as [string]),
($args[0].split(' ')[7] -as [string]),
($args[0].split(' ')[8] -as [string])
}
CRAFTBUKKIT_OPTIONS = {
($args[0].split(' ')[0] -as [string]),
([bool]::Parse($args[0].split(' ')[1])),
($args[0].split(' ')[2] -as [string]),
($args[0].split(' ')[3] -as [int])
}
TEST_DEPENDENCIES = {([bool]::Parse($args[0]))}
DELETE_LOG = {([bool]::Parse($args[0]))}
TAKE_BACKUP = {([bool]::Parse($args[0]))}
RESTART_PAUSE = {$args[0] -as [int]}
}
$ht = [ordered]@{}
Get-Content config.txt |
ForEach-Object {
$parts = $_.split('=').trim()
$ht[$parts[0]] = &$types[$parts[0]] $parts[1]
}
New-object PSObject -Property $ht
}
#Function to ensure the correct version of PowerShell can be found to run this script.
#If the wrong version is found, it will provide a link to the correct version.
Function Test-PowerShell([int]$minVersion) {
$psVer = $PSVersionTable.PSVersion.Major
If ($psVer -ge $minVersion) {
Write-Host -foreground Green "PowerShell version $psVer is compatible"
Return $true
}
Write-Host -foreground Red "PowerShell version $psVer is NOT compatible"
Write-Host -foreground Red "You need to install PowerShell v$minVersion or greater."
Write-Host -foreground Red "Get Powershell 4.0 here (Requires Windows 7 SP1 or Windows Server 2008 R2 SP1):"
Write-Host -foreground Red "http://www.microsoft.com/en-us/download/details.aspx?id=40855"
Write-Host -foreground Red "Aborting script"
Return $false
}
#Function to ensure the correct version of .NET Framework is found to run this script.
#If an older version is found, it will provide a link to the correct version.
Function Test-Framework([decimal]$minVersion) {
$frameworkVer = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client'
$frameworkVer = $frameworkVer.Version
If ($frameworkVer -ge $minVersion) {
Write-Host -foreground Green ".NET Framework $frameworkVer is compatible."
Return $true
}
Write-Host -foreground Red ".NET Framework version $frameworkVer is NOT compatible"
Write-Host -foreground Red "You need to install .NET Framework $minVersion or greater."
Write-Host -foreground Red "Get .NET Framework $minVersion here (Requires Windows 7 SP1 or Windows Server 2008 R2 SP1):"
Write-Host -foreground Red "http://www.microsoft.com/en-ca/download/details.aspx?id=30653"
Write-Host -foreground Red "Aborting script"
Return $false
}
#Function to launch the server. Arguments passed into this is Function is the directory of the server, the location of the server jar file
#the Java flags to use and any command line options to pass to the server.
Function Launch-Server([string]$location, [string]$jarfile, [array]$flags, [array]$options) {
If (($location -eq "") -or ($location -eq $NULL)) {
Write-Host ""
Write-Host -foreground Red "Launch Aborted. Server folder has not been specified."
Return
}
If (!(Test-Path $location)) {
Write-Host ""
Write-Host -foreground Red "No server folder $location found. You must create this folder yourself. Launch aborted."
Return
}
$jarfound = (Get-ChildItem $location -filter "*.jar").Name
If (($jarfile -eq "") -or ($jarfile -eq $NULL)) {
Write-Host ""
Write-Host -foreground Red "CraftBukkit executable $jarfile not found, however, we did find:"
Write-Host -foreground Red "$jarfound"
Write-Host -foreground Red "Do you have a typo in your config.txt?"
Return
}
$server = Join-Path -path $location -childpath $jarfile
$runlocation = Get-Location
If (!(Test-Path $server)) {
If ($jarfound -ne $NULL) {
Write-Host -foreground Red "$server not found, however, we did find:"
Write-Host -foreground Red "$jarfound"
Write-Host -foreground Red "Do you have a typo in your config.txt?"
Return
}
Write-Host -foreground Red "$server not found. Aborting."
}
Write-Host "PowerLaunch - Premium CraftBukkit Start Script"
Set-Location $location
java $flags -jar $server $options
Set-Location $runlocation
}
#Function to find the size of the server directory
Function Get-DirectorySize() {
param ([string]$root = $(Resolve-Path .))
Get-ChildItem -re $root | ?{ -not $_.PSIsContainer } | Measure-Object -sum -property Length
}
#Function to backup the contents of a directory.
#This will break If not using .NET 4.5 or above
Function Write-Backup([string]$backup, [string]$name, [string]$server, [int]$delay, [decimal]$size) {
$date = get-date -UFormat %Y-%m-%d-%H-%M
If (($backup -eq "") -or ($backup -eq $NULL)) {
Write-Host ""
Write-Host -foreground Red "Backup Aborted. Backup folder has not been specified."
Return
}
If (!(Test-Path $backup)) {
Write-Host ""
Write-Host -foreground Red "No backup folder $backup found. You must create this folder yourself. Aborting backup."
Return
}
If (($server -eq "") -or ($server -eq $NULL)) {
Write-Host ""
Write-Host -foreground Red "Backup Aborted. Server folder has not been specified."
Return
}
If (!(Test-Path $server)) {
Write-Host ""
Write-Host -foreground Red "No server folder $server found. Backup aborted."
Return
}
If (($name -eq "") -or ($name -eq $NULL)) {
$backupFile = Join-Path -path $backup -childpath ("UNNAMED" + "-" + $date + ".zip")
}
Else {
$backupFile = Join-Path -path $backup -childpath ($name + "-" + $date + ".zip")
}
If (Test-Path $backupFile) {
Write-Host ""
Write-Host -foreground Red "Backup Aborted. $backupFile already exists."
Return
}
Write-Host ""
Write-Host -background Red "Press q to abort backup - you have $delay seconds to decide"
Start-Sleep -s $delay
If ($Host.UI.RawUI.KeyAvailable -and ("q" -eq $Host.UI.RawUI.ReadKey("IncludeKeyUp,NoEcho").Character)) {
Write-Host -foreground Red "Key pressed. Aborting Backup."
Return
}
Write-Host -foreground Green "Backup starting. For large server directories, this may take a while."
Write-Host -foreground Green "Size to be backed up: $size GB. Hold please."
#Dump the output to null to avoid spamming the console. Backups will still run without issue
[Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem") > $NULL
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
[System.IO.Compression.ZipFile]::CreateFromDirectory($server, $backupFile, $compressionLevel, $false)
Write-Host ""
Write-Host -foreground Green "Backup Completed. See $backupFile"
}
#Function to quit the auto relaunch based on a time set.
Function Set-Quit([int]$delay) {
Write-Host ""
Write-Host -background Red "Press q to quit - you have $delay seconds to decide"
Start-Sleep -s $delay
If ($Host.UI.RawUI.KeyAvailable -and ("q" -eq $Host.UI.RawUI.ReadKey("IncludeKeyUp,NoEcho").Character)) {
Write-Host -foreground Red "Exiting Script. Good-bye."
Return $true;
}
Return $false
}
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-# BEGIN FUNCTION CALLS #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
#Create the config file to be used.
Create-Config
#Main area of the script. This launches all functions above (PowerShell needs the functions declared before they can be ran).
#First the dependencies get tested, then the server gets launched.
#If backups are enabled, take the backup. Delete the server.log If that ability is enabled.
#If backups are disabled, Do not take a backup or delete the server.log
#Keep looping until the user quits the script.
Do {
Clear-Host
#Import Variables
$HT = Load-Variables
If ($HT.TEST_DEPENDENCIES) {
If (!(Test-PowerShell ($POWERSHELL_VERSION))) {
Break;
}
If (!(Test-Framework ($FRAMEWORK_VERSION))) {
Break;
}
}
Launch-Server $HT.SERVER_LOCATION $HT.CRAFTBUKKIT $HT.JAVA_FLAGS $HT.CRAFTBUKKIT_OPTIONS
If ($HT.TAKE_BACKUP) {
$foldersize = (Get-DirectorySize $HT.SERVER_LOCATION).Sum/1gb
$roundedsize = [math]::Round($foldersize,2)
Write-Backup $HT.BACKUP_LOCATION $HT.SERVER_NAME $HT.SERVER_LOCATION $HT.RESTART_PAUSE $roundedsize
If ($HT.DELETE_LOG) {
$log = Join-Path -path $HT.SERVER_LOCATION -childpath "logs\latest.log"
If (Test-Path $log) {
Remove-Item $log
}
Else {
Write-Host ""
Write-Host -foreground Red "Deleting server.log aborted. File $log does not exists."
}
}
}
} Until (Set-Quit ($HT.RESTART_PAUSE))
Write-Host "Press any key to exit"
$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") > $NULL