Skip to content

Commit

Permalink
Merge pull request #4 from patrix87/2.4
Browse files Browse the repository at this point in the history
Version 2.4
  • Loading branch information
patrix87 authored Sep 21, 2021
2 parents e11a37d + 24b421d commit 5e86cbb
Show file tree
Hide file tree
Showing 34 changed files with 2,323 additions and 416 deletions.
3 changes: 3 additions & 0 deletions configs/global.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ $GlobalDetails = @{
#SteamCMD
SteamCMD = ".\tools\SteamCMD\steamcmd.exe"

#Java Directory
JavaDirectory =".\tools\java"

#Path of the logs folder.
LogFolder = ".\logs"

Expand Down
8 changes: 2 additions & 6 deletions functions/Backup-Server.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ function Backup-Server {
#Create backup name from date and time
$BackupName = Get-TimeStamp
#Check if Backups Destination directory exist and create it.
if (-not (Test-Path -Path "$($Backups.Path)\$Type" -PathType "Container")){
if (-not (Test-Path -Path "$($Backups.Path)\$Type" -PathType "Container" -ErrorAction SilentlyContinue)){
New-Item -Path "$($Backups.Path)\$Type" -ItemType "directory" -ErrorAction SilentlyContinue
}
#Check if Backups Source directory exist and create it.
if (-not (Test-Path -Path $Backups.Saves -PathType "Container")){
if (-not (Test-Path -Path $Backups.Saves -PathType "Container" -ErrorAction SilentlyContinue)){
New-Item -Path $Backups.Saves -ItemType "directory" -ErrorAction SilentlyContinue
}
#Resolve Server Saves Path
$Backups.Saves = Resolve-Path -Path $Backups.Saves -ErrorAction SilentlyContinue
#Resolve Backup Path
$Backups.Path = Resolve-Path -Path $Backups.Path -ErrorAction SilentlyContinue
#Check if it's friday (Sunday is 0)
if ((Get-Date -UFormat %u) -eq 5){
$Type = "Weekly"
Expand Down
26 changes: 26 additions & 0 deletions functions/Get-WebFile.psm1
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
34 changes: 34 additions & 0 deletions functions/Install-Dependency.psm1
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
2 changes: 1 addition & 1 deletion functions/Install-Mcrcon.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function Install-Mcrcon {
#Create Install Directory.
New-Item -Path (Split-Path -Path $Application) -ItemType "directory"
#Download zip file.
Invoke-WebRequest -Uri "https://github.com/Tiiffi/mcrcon/releases/download/v0.7.1/mcrcon-0.7.1-windows-x86-32.zip" -OutFile ".\downloads\mcrcon.zip" -ErrorAction SilentlyContinue
Invoke-Download -Uri "https://github.com/Tiiffi/mcrcon/releases/download/v0.7.1/mcrcon-0.7.1-windows-x86-32.zip" -OutFile ".\downloads\mcrcon.zip" -ErrorAction SilentlyContinue
#Unzip file.
Expand-Archive -Path ".\downloads\mcrcon.zip" -DestinationPath ".\downloads\mcrcon\" -Force
#Copy executable to install directory.
Expand Down
4 changes: 2 additions & 2 deletions functions/Install-SevenZip.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ function Install-SevenZip {
Write-ServerMsg "Installing 7Zip Portable."
Write-ServerMsg "Downloading 7zip 9.20 to extract 7zip 19.00"
#Download 7zip 9.20
Invoke-WebRequest -Uri "https://www.7-zip.org/a/7za920.zip" -OutFile ".\downloads\7za920.zip"
Invoke-Download -Uri "https://www.7-zip.org/a/7za920.zip" -OutFile ".\downloads\7za920.zip"
#Unzip 7zip 9.20
Expand-Archive -Path ".\downloads\7za920.zip" -DestinationPath ".\downloads\7z920\" -Force
Write-ServerMsg "Downloading 7zip 19.00"
#Download 7zip 19.00
Invoke-WebRequest -Uri "https://www.7-zip.org/a/7z1900-extra.7z" -OutFile ".\downloads\7z1900-extra.7z" -ErrorAction SilentlyContinue
Invoke-Download -Uri "https://www.7-zip.org/a/7z1900-extra.7z" -OutFile ".\downloads\7z1900-extra.7z" -ErrorAction SilentlyContinue
#Use 7zip 9.20 to unzip 7zip 19.00
& ".\downloads\7z920\7za.exe" x ".\downloads\7z1900-extra.7z" -o".\downloads\7z1900\" -y
#Copy the executable and dll to the 7zip directory.
Expand Down
2 changes: 1 addition & 1 deletion functions/Install-SteamCMD.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function Install-SteamCMD {
)
Write-ServerMsg "Downloading SteamCMD."
#Download file
Invoke-WebRequest -Uri "https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip" -OutFile ".\downloads\steamcmd.zip" -ErrorAction SilentlyContinue
Invoke-Download -Uri "https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip" -OutFile ".\downloads\steamcmd.zip" -ErrorAction SilentlyContinue
#Unzip file in installation directory
Expand-Archive -Path ".\downloads\steamcmd.zip" -DestinationPath (Split-Path -Path $Application) -Force
Write-ServerMsg "SteamCMD Installed."
Expand Down
19 changes: 19 additions & 0 deletions functions/Optimize-ArgumentList.psm1
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
21 changes: 21 additions & 0 deletions functions/Read-Config.psm1
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
21 changes: 21 additions & 0 deletions functions/Resolve-CompletePath.psm1
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
2 changes: 1 addition & 1 deletion functions/Send-RestartWarning.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function Send-RestartWarning {
}
#if the process is still running, if allowed, stop process.
if(-not ($ServerProcess.HasExited) -and ($Server.AllowForceClose)){
$Stopped = Stop-Server -ServerProcess $ServerProcess
$Stopped = Stop-ServerProcess -ServerProcess $ServerProcess
}
return $Stopped
}
Expand Down
18 changes: 18 additions & 0 deletions functions/Set-IP.psm1
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
28 changes: 28 additions & 0 deletions functions/Start-Server.psm1
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
82 changes: 48 additions & 34 deletions functions/Stop-Server.psm1
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
40 changes: 40 additions & 0 deletions functions/Stop-ServerProcess.psm1
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
Loading

0 comments on commit 5e86cbb

Please sign in to comment.