From 555316f7f91757f21f7d2c068211265fc7fb198f Mon Sep 17 00:00:00 2001 From: Patrick Veilleux Date: Fri, 17 Sep 2021 18:31:26 -0400 Subject: [PATCH] Bug Fix and add Squad --- functions/Get-PID.psm1 | 4 +- functions/Register-PID.psm1 | 2 +- functions/Stop-Server.psm1 | 36 +++--- functions/Unregister-PID.psm1 | 2 +- launchers/run.cmd | 2 +- main.ps1 | 10 +- templates/7daystodie.psm1 | 4 +- templates/insurgencysandstorm.psm1 | 4 +- templates/killingfloor2.psm1 | 4 +- templates/pixark.psm1 | 4 +- templates/projectzomboid.psm1 | 4 +- templates/rust.psm1 | 4 +- templates/squad.psm1 | 200 +++++++++++++++++++++++++++++ templates/valheim.psm1 | 4 +- 14 files changed, 242 insertions(+), 42 deletions(-) create mode 100644 templates/squad.psm1 diff --git a/functions/Get-PID.psm1 b/functions/Get-PID.psm1 index 8118d0e..1de012f 100644 --- a/functions/Get-PID.psm1 +++ b/functions/Get-PID.psm1 @@ -5,10 +5,10 @@ function Get-PID { ) try { #Read the process ID from the PID file named by the UID defined in the server cfg file. - $ServerPID = Get-Content -Path ".\servers\$($Server.UID).PID" -ErrorAction Continue + $ServerPID = Get-Content -Path ".\servers\$($Server.UID).PID" -ErrorAction SilentlyContinue } catch { - return 0 + return $null } return $ServerPID } diff --git a/functions/Register-PID.psm1 b/functions/Register-PID.psm1 index 94be123..e743458 100644 --- a/functions/Register-PID.psm1 +++ b/functions/Register-PID.psm1 @@ -6,7 +6,7 @@ function Register-PID { $ServerProcess ) try { - New-Item -Path ".\servers\" -Name "$($Server.UID).PID" -ItemType "file" -Value "$($ServerProcess.ID)" -Force -ErrorAction Continue + New-Item -Path ".\servers\" -Name "$($Server.UID).PID" -ItemType "file" -Value "$($ServerProcess.ID)" -Force -ErrorAction SilentlyContinue } catch { return $false diff --git a/functions/Stop-Server.psm1 b/functions/Stop-Server.psm1 index 11119d1..b1acc3c 100644 --- a/functions/Stop-Server.psm1 +++ b/functions/Stop-Server.psm1 @@ -6,30 +6,30 @@ function Stop-Server { $ServerProcess ) #if the server is still running - if(-not $Server.HasExited){ - Write-ServerMsg "Closing main window..." - #Close the main windows. - $ServerProcess.CloseMainWindow() + + 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 the process exited send success message + #If process is still running, force stop-process. if ($ServerProcess.HasExited) { - Write-ServerMsg "Server succesfully stopped." + Write-Warning "Server succesfully stopped on second try." }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 - } + 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) { diff --git a/functions/Unregister-PID.psm1 b/functions/Unregister-PID.psm1 index 8b85d6a..da9ecfe 100644 --- a/functions/Unregister-PID.psm1 +++ b/functions/Unregister-PID.psm1 @@ -5,7 +5,7 @@ function Unregister-PID { ) try { #Delete the PID file based on the Server UID. - Remove-Item -Path ".\servers\$($Server.UID).PID" -Confirm:$false -ErrorAction Continue + Remove-Item -Path ".\servers\$($Server.UID).PID" -Confirm:$false -ErrorAction SilentlyContinue } catch { return $false diff --git a/launchers/run.cmd b/launchers/run.cmd index 2dffa2c..1465e1f 100644 --- a/launchers/run.cmd +++ b/launchers/run.cmd @@ -1,2 +1,2 @@ cd .. -start powershell.exe -noprofile -executionpolicy bypass -file ".\main.ps1" -ServerCfg "rust" \ No newline at end of file +start powershell.exe -noprofile -executionpolicy bypass -file ".\main.ps1" -ServerCfg "squad" \ No newline at end of file diff --git a/main.ps1 b/main.ps1 index e7fd6e2..757e244 100644 --- a/main.ps1 +++ b/main.ps1 @@ -149,15 +149,15 @@ if (-not ($FreshInstall)) { #Get the PID from the .PID market file. $ServerPID = Get-PID #If it returned 0, it failed to get a PID - if ($ServerPID -ne 0) { - $ServerProcess = Get-Process -ID $ServerPID -ErrorAction Continue + if ($null -ne $ServerPID) { + $ServerProcess = Get-Process -ID $ServerPID -ErrorAction SilentlyContinue } #If the server process is none-existent, Get the process from the server process name. - if (-not $ServerProcess) { - $ServerProcess = Get-Process -Name $Server.ProcessName -ErrorAction Continue + if ($null -ne $ServerProcess) { + $ServerProcess = Get-Process -Name $Server.ProcessName -ErrorAction SilentlyContinue } #Check if the process was found. - if (-not ($ServerProcess) -or $ServerProcess.HasExited) { + if ($null -eq $ServerProcess) { Write-ServerMsg "Server is not running." } else { #Check if it's the right server via RCON if possible. diff --git a/templates/7daystodie.psm1 b/templates/7daystodie.psm1 index 44438fd..4bab4c0 100644 --- a/templates/7daystodie.psm1 +++ b/templates/7daystodie.psm1 @@ -176,8 +176,8 @@ foreach($Argument in $Arguments){ $ArgumentList = $CleanedArguments -join "" #Server Launcher -$Launcher = $(Resolve-Path -Path $Server.Exec) -$WorkingDirectory = $(Resolve-Path -Path $Server.Path) +$Launcher = "$(Get-Location)$($Server.Exec.substring(1))" +$WorkingDirectory = "$(Get-Location)$($Server.Path.substring(1))" Add-Member -InputObject $Server -Name "ArgumentList" -Type NoteProperty -Value $ArgumentList Add-Member -InputObject $Server -Name "Launcher" -Type NoteProperty -Value $Launcher diff --git a/templates/insurgencysandstorm.psm1 b/templates/insurgencysandstorm.psm1 index 93e275f..49bb171 100644 --- a/templates/insurgencysandstorm.psm1 +++ b/templates/insurgencysandstorm.psm1 @@ -234,8 +234,8 @@ if ($Server.EnableRcon){ $ArgumentList = $CleanedArguments -join "" #Server Launcher -$Launcher = $(Resolve-Path -Path $Server.Exec) -$WorkingDirectory = $(Resolve-Path -Path $Server.Path) +$Launcher = "$(Get-Location)$($Server.Exec.substring(1))" +$WorkingDirectory = "$(Get-Location)$($Server.Path.substring(1))" Add-Member -InputObject $Server -Name "ArgumentList" -Type NoteProperty -Value $ArgumentList Add-Member -InputObject $Server -Name "Launcher" -Type NoteProperty -Value $Launcher diff --git a/templates/killingfloor2.psm1 b/templates/killingfloor2.psm1 index 350ee92..e12fd2a 100644 --- a/templates/killingfloor2.psm1 +++ b/templates/killingfloor2.psm1 @@ -201,8 +201,8 @@ foreach($Argument in $Arguments){ $ArgumentList = $CleanedArguments -join "" #Server Launcher -$Launcher = $(Resolve-Path -Path $Server.Exec) -$WorkingDirectory = $(Resolve-Path -Path $Server.Path) +$Launcher = "$(Get-Location)$($Server.Exec.substring(1))" +$WorkingDirectory = "$(Get-Location)$($Server.Path.substring(1))" Add-Member -InputObject $Server -Name "ArgumentList" -Type NoteProperty -Value $ArgumentList Add-Member -InputObject $Server -Name "Launcher" -Type NoteProperty -Value $Launcher diff --git a/templates/pixark.psm1 b/templates/pixark.psm1 index 3710fe9..dfa64c9 100644 --- a/templates/pixark.psm1 +++ b/templates/pixark.psm1 @@ -242,8 +242,8 @@ foreach($Argument in $Arguments){ $ArgumentList = $CleanedArguments -join "" #Server Launcher -$Launcher = $(Resolve-Path -Path $Server.Exec) -$WorkingDirectory = $(Resolve-Path -Path $Server.Path) +$Launcher = "$(Get-Location)$($Server.Exec.substring(1))" +$WorkingDirectory = "$(Get-Location)$($Server.Path.substring(1))" Add-Member -InputObject $Server -Name "ArgumentList" -Type NoteProperty -Value $ArgumentList Add-Member -InputObject $Server -Name "Launcher" -Type NoteProperty -Value $Launcher diff --git a/templates/projectzomboid.psm1 b/templates/projectzomboid.psm1 index cc5a095..0f48bc8 100644 --- a/templates/projectzomboid.psm1 +++ b/templates/projectzomboid.psm1 @@ -212,8 +212,8 @@ foreach($Argument in $Arguments){ $ArgumentList = $CleanedArguments -join "" #Server Launcher -$Launcher = $(Resolve-Path -Path $Server.Exec) -$WorkingDirectory = $(Resolve-Path -Path $Server.Path) +$Launcher = "$(Get-Location)$($Server.Exec.substring(1))" +$WorkingDirectory = "$(Get-Location)$($Server.Path.substring(1))" Add-Member -InputObject $Server -Name "ArgumentList" -Type NoteProperty -Value $ArgumentList Add-Member -InputObject $Server -Name "Launcher" -Type NoteProperty -Value $Launcher diff --git a/templates/rust.psm1 b/templates/rust.psm1 index 341c8ca..732a9b5 100644 --- a/templates/rust.psm1 +++ b/templates/rust.psm1 @@ -251,8 +251,8 @@ foreach($Argument in $Arguments){ $ArgumentList = $CleanedArguments -join "" #Server Launcher -$Launcher = $(Resolve-Path -Path $Server.Exec) -$WorkingDirectory = $(Resolve-Path -Path $Server.Path) +$Launcher = "$(Get-Location)$($Server.Exec.substring(1))" +$WorkingDirectory = "$(Get-Location)$($Server.Path.substring(1))" Add-Member -InputObject $Server -Name "ArgumentList" -Type NoteProperty -Value $ArgumentList Add-Member -InputObject $Server -Name "Launcher" -Type NoteProperty -Value $Launcher diff --git a/templates/squad.psm1 b/templates/squad.psm1 new file mode 100644 index 0000000..3e03914 --- /dev/null +++ b/templates/squad.psm1 @@ -0,0 +1,200 @@ +<# +Configure server in .\servers\Squad\SquadGame\ServerConfig\ +#> +#Server Name, use the same name to share game files. +$Name = "Squad" + +#--------------------------------------------------------- +# Server Configuration +#--------------------------------------------------------- + +$ServerDetails = @{ + + #Unique Identifier used to track processes. Must be unique to each servers. + UID = 1 + + #Randomize First Map NONE OR ALWAYS + RandomMap = "ALWAYS" + + #Max number of Players + MaxPlayers = 80 + + #Server Port + Port = 2459 + + #Query Port + QueryPort = 27165 + + #Rcon IP + ManagementIP = "127.0.0.1" + + #Rcon Port + ManagementPort = 21114 + + #Rcon Password Change in .\servers\Squad\SquadGame\ServerConfig\Rcon.cfg + ManagementPassword = "CHANGEME" + +#--------------------------------------------------------- +# Server Installation Details +#--------------------------------------------------------- + + #Name of the Server Instance + Name = $Name + + #Server Installation Path + Path = ".\servers\$Name" + + #Steam Server App Id + AppID = 403240 + + #Use Beta builds $true or $false + Beta = $false + + #Name of the Beta Build + BetaBuild = "" + + #Beta Build Password + BetaBuildPassword = "" + + #Process name in the task manager + ProcessName = "SquadGameServer" + + #ProjectZomboid64.exe + Exec = ".\servers\$Name\SquadGameServer.exe" + + #Allow force close, usefull for server without RCON and Multiple instances. + AllowForceClose = $true + + #Process Priority Realtime, High, Above normal, Normal, Below normal, Low + UsePriority = $true + AppPriority = "High" + + <# + Process Affinity (Core Assignation) + Core 1 = > 00000001 = > 1 + Core 2 = > 00000010 = > 2 + Core 3 = > 00000100 = > 4 + Core 4 = > 00001000 = > 8 + Core 5 = > 00010000 = > 16 + Core 6 = > 00100000 = > 32 + Core 7 = > 01000000 = > 64 + Core 8 = > 10000000 = > 128 + ---------------------------- + 8 Cores = > 11111111 = > 255 + 4 Cores = > 00001111 = > 15 + 2 Cores = > 00000011 = > 3 + #> + + UseAffinity = $false + AppAffinity = 15 + + #Should the server validate install after installation or update *(recommended) + Validate = $true + + #How long should it wait to check if the server is stable + StartupWaitTime = 10 +} +#Create the object +$Server = New-Object -TypeName PsObject -Property $ServerDetails + +#--------------------------------------------------------- +# Backups +#--------------------------------------------------------- + +$BackupsDetails = @{ + #Do Backups + Use = $true + + #Backup Folder + Path = ".\backups\$($Server.Name)" + + #Number of days of backups to keep. + Days = 7 + + #Number of weeks of weekly backups to keep. + Weeks = 4 + + #Folder to include in backup + Saves = ".\servers\$Name\SquadGame\ServerConfig\" +} +#Create the object +$Backups = New-Object -TypeName PsObject -Property $BackupsDetails + +#--------------------------------------------------------- +# Restart Warnings (Require RCON, Telnet or WebSocket API) +#--------------------------------------------------------- + +$WarningsDetails = @{ + #Use Rcon to restart server softly. + Use = $false + + #What protocol to use : Rcon, Telnet, Websocket + Protocol = "Rcon" + + #Times at which the servers will warn the players that it is about to restart. (in seconds between each timers) + Timers = [System.Collections.ArrayList]@(240,50,10) #Total wait time is 240+50+10 = 300 seconds or 5 minutes + + #message that will be sent. % is a wildcard for the timer. + MessageMin = "The server will restart in % minutes !" + + #message that will be sent. % is a wildcard for the timer. + MessageSec = "The server will restart in % seconds !" + + #command to send a message. + CmdMessage = "AdminBroadcast" + + #command to save the server + CmdSave = "AdminEndMatch" + + #How long to wait in seconds after the save command is sent. + SaveDelay = 15 + + #command to stop the server + CmdStop = "exit" +} +#Create the object +$Warnings = New-Object -TypeName PsObject -Property $WarningsDetails + +#--------------------------------------------------------- +# Launch Arguments +#--------------------------------------------------------- + +#Launch Arguments +$Arguments = @( + "Multihome=$($Global.InternalIP) ", + "Port=$($Server.Port) ", + "QueryPort=$($Server.QueryPort) ", + "FIXEDMAXPLAYERS=$($Server.MaxPlayers) ", + "RANDOM $($Server.RandomMap) ", + "-log" +) + +[System.Collections.ArrayList]$CleanedArguments=@() + +foreach($Argument in $Arguments){ + if (!($Argument.EndsWith('=""') -or $Argument.EndsWith('=') -or $Argument.EndsWith(' '))){ + $CleanedArguments.Add($Argument) + } +} + +$ArgumentList = $CleanedArguments -join "" + +#Server Launcher +$Launcher = "$(Get-Location)$($Server.Exec.substring(1))" +$WorkingDirectory = "$(Get-Location)$($Server.Path.substring(1))" + +Add-Member -InputObject $Server -Name "ArgumentList" -Type NoteProperty -Value $ArgumentList +Add-Member -InputObject $Server -Name "Launcher" -Type NoteProperty -Value $Launcher +Add-Member -InputObject $Server -Name "WorkingDirectory" -Type NoteProperty -Value $WorkingDirectory + +#--------------------------------------------------------- +# Function that runs just before the server starts. +#--------------------------------------------------------- + +function Start-ServerPrep { + + Write-ScriptMsg "Port Forward : $($server.Port) in TCP and UDP to $($Global.InternalIP)" + +} + +Export-ModuleMember -Function Start-ServerPrep -Variable @("Server","Backups","Warnings") \ No newline at end of file diff --git a/templates/valheim.psm1 b/templates/valheim.psm1 index 8d85048..9d3712f 100644 --- a/templates/valheim.psm1 +++ b/templates/valheim.psm1 @@ -179,8 +179,8 @@ foreach($Argument in $Arguments){ $ArgumentList = $CleanedArguments -join "" #Server Launcher -$Launcher = $(Resolve-Path -Path $Server.Exec) -$WorkingDirectory = $(Resolve-Path -Path $Server.Path) +$Launcher = "$(Get-Location)$($Server.Exec.substring(1))" +$WorkingDirectory = "$(Get-Location)$($Server.Path.substring(1))" Add-Member -InputObject $Server -Name "ArgumentList" -Type NoteProperty -Value $ArgumentList Add-Member -InputObject $Server -Name "Launcher" -Type NoteProperty -Value $Launcher