-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from patrix87/2.4
Version 2.4
- Loading branch information
Showing
34 changed files
with
2,323 additions
and
416 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
function Invoke-Download { | ||
[CmdletBinding()] | ||
param ( | ||
[Parameter(Mandatory)] | ||
$Uri, | ||
$OutFile | ||
) | ||
|
||
# Create the HTTP client download request | ||
$httpClient = New-Object System.Net.Http.HttpClient | ||
$response = $httpClient.GetAsync($Uri) | ||
$response.Wait() | ||
|
||
# Create a file stream to pointed to the output file destination | ||
$outputFileStream = [System.IO.FileStream]::new($OutFile, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write) | ||
|
||
# Stream the download to the destination file stream | ||
$downloadTask = $response.Result.Content.CopyToAsync($outputFileStream) | ||
$downloadTask.Wait() | ||
|
||
# Close the file stream | ||
$outputFileStream.Close() | ||
|
||
} | ||
|
||
Export-ModuleMember -Function Invoke-Download |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
function Install-Dependency { | ||
#Define variables | ||
Write-ScriptMsg "Verifying Dependencies..." | ||
$Dependencies = @{ | ||
SevenZip = $Global.SevenZip | ||
Mcrcon = $Global.Mcrcon | ||
SteamCMD = $Global.SteamCMD | ||
} | ||
|
||
[System.Collections.ArrayList]$MissingDependencies = @() | ||
|
||
#For each dependency check if the excutable exist, if not, add the key of the dependency to the MissingDependencies list. | ||
foreach ($Key in $Dependencies.keys) { | ||
if (-not (Test-Path -Path $Dependencies[$Key] -ErrorAction SilentlyContinue)) { | ||
$null = $MissingDependencies.Add($Key) | ||
} | ||
} | ||
|
||
#If there is missing dependencies, create the download folder and for each missing dependency, run the installation script. | ||
if ($MissingDependencies.Count -gt 0){ | ||
#Create Temporary Download Folder | ||
New-Item -Path ".\downloads" -ItemType "directory" -ErrorAction SilentlyContinue | ||
|
||
foreach ($Item in $MissingDependencies) { | ||
$Cmd = "Install-$Item" | ||
&$Cmd -Application $Dependencies[$Item] | ||
} | ||
|
||
#Cleanup | ||
Remove-Item -Path ".\downloads" -Recurse -Force -ErrorAction SilentlyContinue | ||
} | ||
} | ||
|
||
Export-ModuleMember -Function Install-Dependency |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
function Optimize-ArgumentList { | ||
[OutputType([string])] | ||
param ( | ||
[Parameter(Mandatory)] | ||
[array] $Arguments | ||
) | ||
|
||
#Create Server.Arguments from cleaned ArgumentList. | ||
[System.Collections.ArrayList]$CleanedArguments=@() | ||
|
||
foreach($Argument in $Arguments){ | ||
if (-not ($Argument.EndsWith('=""') -or $Argument.EndsWith('=') -or $Argument.EndsWith(' '))){ | ||
$null = $CleanedArguments.Add($Argument) | ||
} | ||
} | ||
return ($CleanedArguments -join "") | ||
} | ||
|
||
Export-ModuleMember -Function Optimize-ArgumentList |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
function Read-Config { | ||
#Read configuration data and improve it by parsing full paths and cleaning arguments | ||
|
||
#Create Long Paths | ||
$Server.Exec = (Resolve-CompletePath -Path $Server.Exec -ParentPath ".\servers\") | ||
$Server.Path = (Resolve-CompletePath -Path $Server.Path -ParentPath ".\servers\") | ||
$Server.ConfigFolder = (Resolve-CompletePath -Path $Server.ConfigFolder -ParentPath ".\servers\") | ||
$Backups.Path = (Resolve-CompletePath -Path $Backups.Path -ParentPath ".\backups\") | ||
$Backups.Saves = (Resolve-CompletePath -Path $Backups.Saves -ParentPath ".\servers\") | ||
$Global.SevenZip = (Resolve-CompletePath -Path $Global.SevenZip -ParentPath ".\tools\") | ||
$Global.Mcrcon = (Resolve-CompletePath -Path $Global.Mcrcon -ParentPath ".\tools\") | ||
$Global.SteamCMD = (Resolve-CompletePath -Path $Global.SteamCMD -ParentPath ".\tools\") | ||
$Global.JavaDirectory = (Resolve-CompletePath -Path $Global.JavaDirectory -ParentPath ".\tools\") | ||
$Global.LogFolder = (Resolve-CompletePath -Path $Global.LogFolder -ParentPath ".\") | ||
|
||
#Create Arguments | ||
if ($Server.ArgumentList.length -gt 0) { | ||
Add-Member -InputObject $Server -Name "Arguments" -Type NoteProperty -Value (Optimize-ArgumentList -Arguments $Server.ArgumentList) | ||
} | ||
} | ||
Export-ModuleMember -Function Read-Config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
function Resolve-CompletePath { | ||
[CmdletBinding()] | ||
[OutputType([string])] | ||
param ( | ||
[Parameter(Mandatory)] | ||
[string] $Path, | ||
[string] $ParentPath | ||
) | ||
#Add the full path to the path from the config file. | ||
if ($Path.StartsWith($ParentPath)){ | ||
return "$(Get-Location)$($Path.substring(1))" | ||
} | ||
$ReturnPath = Resolve-Path -LiteralPath $Path -ErrorAction SilentlyContinue | ||
if ($null -eq $ReturnPath) { | ||
$ReturnPath = $Path | ||
} | ||
return $ReturnPath | ||
} | ||
|
||
Export-ModuleMember -Function Resolve-CompletePath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function Set-IP { | ||
#Get current internal ip from active network interface. | ||
Write-ScriptMsg "Finding server IPs..." | ||
$InternalIP = ( | ||
Get-NetIPConfiguration | Where-Object {(($null -ne $_.IPv4DefaultGateway) -and ($_.NetAdapter.Status -ne "Disconnected"))} | ||
).IPv4Address.IPAddress | ||
|
||
#Get current external ip from ifconfig.me | ||
$ExternalIP = (Invoke-WebRequest ifconfig.me/ip).Content.Trim() | ||
|
||
Write-ScriptMsg "Server local IP : $InternalIP" | ||
Write-ScriptMsg "Server external IP : $ExternalIP" | ||
|
||
#Add propreties to global. | ||
Add-Member -InputObject $Global -Name "InternalIP" -Type NoteProperty -Value $InternalIP | ||
Add-Member -InputObject $Global -Name "ExternalIP" -Type NoteProperty -Value $ExternalIP | ||
} | ||
Export-ModuleMember -Function Set-IP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
function Start-Server { | ||
try { | ||
Write-ScriptMsg "Starting Server Preparation..." | ||
Start-ServerPrep | ||
Write-ScriptMsg "Starting Server..." | ||
if ($Server.Arguments.length -gt 0){ | ||
$App = Start-Process -FilePath $Server.Launcher -WorkingDirectory $($Server.WorkingDirectory) -ArgumentList $Server.Arguments -PassThru | ||
} else { | ||
$App = Start-Process -FilePath $Server.Launcher -WorkingDirectory $($Server.WorkingDirectory) -PassThru | ||
} | ||
#Wait to see if the server is stable. | ||
Start-Sleep -Seconds $Server.StartupWaitTime | ||
if (($null -eq $App) -or ($App.HasExited)){ | ||
Exit-WithError "Server Failed to launch." | ||
} else { | ||
Write-ServerMsg "Server Started." | ||
Set-Priority -ServerProcess $App | ||
} | ||
if (-not (Register-PID -ServerProcess $App)){ | ||
Write-ServerMsg "Failed to Register PID file." | ||
} | ||
} | ||
catch { | ||
Write-Error $_ | ||
Exit-WithError -ErrorMsg "Unable to start server." | ||
} | ||
} | ||
Export-ModuleMember -Function Start-Server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,55 @@ | ||
function Stop-Server { | ||
[CmdletBinding()] | ||
[OutputType([boolean])] | ||
param ( | ||
[Parameter(Mandatory)] | ||
$ServerProcess | ||
) | ||
#if the server is still running | ||
|
||
Write-ServerMsg "Closing main window..." | ||
#Close the main windows. | ||
$ServerProcess.CloseMainWindow() | ||
#Wait for exit for at most 30 seconds. | ||
$ServerProcess.WaitForExit(30000) | ||
#if the process exited send success message | ||
if ($ServerProcess.HasExited) { | ||
Write-ServerMsg "Server succesfully stopped." | ||
}else{ | ||
Write-Warning "Trying again to stop the server..." | ||
#else try to stop server with stop-process. | ||
Stop-Process $ServerProcess | ||
#Wait for exit for at most 30 seconds. | ||
$ServerProcess.WaitForExit(30000) | ||
#If process is still running, force stop-process. | ||
if ($ServerProcess.HasExited) { | ||
Write-Warning "Server succesfully stopped on second try." | ||
}else{ | ||
Write-Warning "Forcefully stopping server..." | ||
Stop-Process $ServerProcess -Force | ||
if ($Server.UsePID){ | ||
#Get the PID from the .PID market file. | ||
$ServerPID = Get-PID | ||
#If it returned 0, it failed to get a PID | ||
if ($null -ne $ServerPID) { | ||
$ServerProcess = Get-Process -ID $ServerPID -ErrorAction SilentlyContinue | ||
} | ||
} | ||
|
||
#Safety timer for allowing the files to unlock before backup. | ||
Start-Sleep -Seconds 10 | ||
if ($ServerProcess.HasExited) { | ||
return $true | ||
#If the server process is none-existent, Get the process from the server process name. | ||
if ($null -eq $ServerProcess) { | ||
$ServerProcess = Get-Process -Name $Server.ProcessName -ErrorAction SilentlyContinue | ||
} | ||
#Check if the process was found. | ||
if ($null -eq $ServerProcess) { | ||
Write-ServerMsg "Server is not running." | ||
} else { | ||
return $false | ||
#Check if it's the right server via RCON if possible. | ||
$Success = $false | ||
if ($Warnings.Use){ | ||
$Success = Send-Command("help") | ||
if ($Success) { | ||
Write-ServerMsg "Server is responding to remote messages." | ||
} else { | ||
Write-ServerMsg "Server is not responding to remote messages." | ||
} | ||
} | ||
#If Rcon worked, send stop warning. | ||
if ($Success) { | ||
Write-ServerMsg "Server is running, warning users about upcomming restart." | ||
$Stopped = Send-RestartWarning -ServerProcess $ServerProcess | ||
} else { | ||
#If Server is allow to be closed, close it. | ||
if ($Server.AllowForceClose){ | ||
Write-ServerMsg "Server is running, stopping server." | ||
$Stopped = Stop-ServerProcess -ServerProcess $ServerProcess | ||
} | ||
} | ||
#If the server stopped, send messages, if not check if it's normal, then stopped it, if it fails, exit with error. | ||
if ($Stopped) { | ||
Write-ServerMsg "Server stopped." | ||
} else { | ||
if ($Server.AllowForceClose) { | ||
Exit-WithError "Failed to stop server." | ||
} else { | ||
Write-ServerMsg "Server not stopped." | ||
} | ||
} | ||
} | ||
#Unregister the PID | ||
if (-not $(Unregister-PID)) { | ||
Write-ServerMsg "Failed to remove PID file." | ||
} | ||
} | ||
Export-ModuleMember -Function Stop-Server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
function Stop-ServerProcess { | ||
[CmdletBinding()] | ||
[OutputType([boolean])] | ||
param ( | ||
[Parameter(Mandatory)] | ||
$ServerProcess | ||
) | ||
#if the server is still running | ||
|
||
Write-ServerMsg "Closing main window..." | ||
#Close the main windows. | ||
$ServerProcess.CloseMainWindow() | ||
#Wait for exit for at most 30 seconds. | ||
$ServerProcess.WaitForExit($Warnings.SaveDelay * 1000) | ||
#if the process exited send success message | ||
if ($ServerProcess.HasExited) { | ||
Write-ServerMsg "Server succesfully stopped." | ||
}else{ | ||
Write-Warning "Trying again to stop the server..." | ||
#else try to stop server with stop-process. | ||
Stop-Process $ServerProcess | ||
#Wait for exit for at most 30 seconds. | ||
$ServerProcess.WaitForExit($Warnings.SaveDelay * 1000) | ||
#If process is still running, force stop-process. | ||
if ($null -eq (Get-Process -ID ($ServerProcess.Id) -ErrorAction SilentlyContinue)) { | ||
Write-Warning "Server succesfully stopped on second try." | ||
}else{ | ||
Write-Warning "Forcefully stopping server..." | ||
Stop-Process $ServerProcess -Force | ||
} | ||
} | ||
#Safety timer for allowing the files to unlock before backup. | ||
Start-Sleep -Seconds 10 | ||
if ($ServerProcess.HasExited) { | ||
return $true | ||
} else { | ||
return $false | ||
} | ||
} | ||
Export-ModuleMember -Function Stop-ServerProcess |
Oops, something went wrong.