First commit

This commit is contained in:
Vladislav Salikov
2024-10-26 18:28:15 +03:00
parent e2db1a5787
commit acdcf23a1b
3 changed files with 465 additions and 0 deletions

407
wDayzManager.ps1 Normal file
View File

@@ -0,0 +1,407 @@
# wDayzManager v0.1
# Copyright (c) 2024 Vladislav Salikov aka W0LF aka 'dreamforce'
# https://github.com/dreamforceinc
# Required module: PSIni
# Installation: Install-Module -Name PsIni
[CmdletBinding()]
param (
[Parameter()]
[int]
$param = 0
)
# $date = (Get-Date).ToString()
$date = Get-Date
[string]$logFile = ".\wDayzManager.log"
# $date | Out-File -FilePath $logFile
[bool]$update = $param
if ($update) {
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Warning "You do not have Administrator rights to run this script!`nPlease re-run this script as an Administrator!"
Break
}
}
$ini = $config = $servers = $clientMods = $serverMods = $steamProcess = $null
$namalskMission = $nPath = $nPath2 = $dayzProcess = $becProcess = $instance = $null
$becLocation = $rotatedLogsFolderName = $atFolderName = $serverLocation = $null
$steamCMD = $steamUser = $appidServer = $appidGame = $rotateLogs = $daysAmount = $null
$serverPort = $serverConfig = $serverExeName = $serverCPU = $null
$startupClientMods = $startupServerMods = $startupMods = $startupParams = $null
$serverProfile = $additionalParams = $dayzArguments = $dayz = $bec = $becArguments = $null
$logsName = $atLogsLocation = $becLogsLocation = $rotatedLogsLocation = $null
[bool]$noDelete = $true # For tests - don't delete logs
[string]$steamScript = "wDayzManager.txt"
[string]$iniFile = "wDayzManager.ini"
if (-not (Test-Path $iniFile)) {
Write-Error "Can't find file '$iniFile'"
Exit 1
}
Write-Host "=========="
$ini = (Get-IniContent -FilePath $iniFile)
$config = $ini.Config
$serverLocation = $config.serverLocation
$steamCMD = $config.steamCMD
$steamUser = $config.steamUser
$appidServer = $config.appidServer
$appidGame = $config.appidGame
[bool]$rotateLogs = [int]$config.rotateLogs
$clientMods = New-Object 'Collections.Generic.List[Tuple[string,string]]'
foreach ($mod in $ini.ClientMods.GetEnumerator()) {
if ($mod.Key -notlike "Comment*" ) {
$clientMods.Add([Tuple]::Create($mod.Key, $mod.Value))
}
}
$serverMods = New-Object 'Collections.Generic.List[Tuple[string,string]]'
foreach ($mod in $ini.ServerMods.GetEnumerator()) {
if ($mod.Key -notlike "Comment*" ) {
$serverMods.Add([Tuple]::Create($mod.Key, $mod.Value))
}
}
# $clientMods.Item1 | ForEach-Object {
# if ($_ -like "*namalsk*" -and $_ -like "*survival*") {
# $namalskMission = $config.namalskMission
# if (($namalskMission -eq "regular") -or ($namalskMission -eq "hardcore")) {
# $nPath = "$($namalskMission)\$($namalskMission).namalsk"
# $nPath2 = Split-Path -Path $nPath -Leaf
# }
# }
# }
if ($update) {
Write-Host "Start updating server..."
if (-not (Test-Path $steamCMD)) {
Write-Error "Can't find folder '$steamCMD'"
Exit 2
}
$currentPath = Get-Location
# Making Steam script
$arr = @()
$arr += "@NoPromptForPassword 1"
$arr += "@ShutdownOnFailedCommand 1"
$arr += "force_install_dir `"$($serverLocation)`""
$arr += "login $($steamUser)"
$arr += "app_update $($appidServer) validate"
$clientMods | ForEach-Object { $arr += "workshop_download_item $($appidGame) $($_.Item2) validate" }
$serverMods | ForEach-Object { $arr += "workshop_download_item $($appidGame) $($_.Item2) validate" }
$arr += "quit"
# Saving Steam script
"// wDayzManager.txt - Steam script" | Out-File -FilePath "$($currentPath)\$($steamScript)" -Encoding ascii
$arr | Out-File -FilePath "$($currentPath)\$($steamScript)" -Encoding ascii -Append
# Run Steam script
Write-Host "Run Steam script from `"$($currentPath)\$($steamScript)`""
$steamProcess = Start-Process -FilePath "$($steamCMD)\steamcmd.exe" -ArgumentList "+runscript $($currentPath)\$($steamScript) +quit" -PassThru -Wait -NoNewWindow
if ($steamProcess.ExitCode -ne 0) {
Write-Error "$_ failed with error code $($steamProcess.ExitCode)"
Exit $steamProcess.ExitCode
}
# Making symbolic links to mods
Write-Host "`r`nMaking symbolic links to mods:"
$clientMods | ForEach-Object {
New-Item -ItemType SymbolicLink -Path "$($serverLocation)\$($_.Item1)" -Target "$($serverLocation)\steamapps\workshop\content\$($appidGame)\$($_.Item2)" -Force
Copy-Item -Path "$($serverLocation)\$($_.Item1)\Keys\*.bikey" -Destination "$($serverLocation)\keys\" -Force
}
$serverMods | ForEach-Object {
New-Item -ItemType SymbolicLink -Path "$($serverLocation)\$($_.Item1)" -Target "$($serverLocation)\steamapps\workshop\content\$($appidGame)\$($_.Item2)" -Force
Copy-Item -Path "$($serverLocation)\$($_.Item1)\Keys\*.bikey" -Destination "$($serverLocation)\keys\" -Force
}
Start-Sleep -Seconds 1
# Patching Namalsk
$clientMods.Item1 | ForEach-Object {
if ($_ -like "*namalsk*" -and $_ -like "*survival*") {
$namalskMission = $config.namalskMission
if (($namalskMission -eq "regular") -or ($namalskMission -eq "hardcore")) {
Write-Host "`r`nCopying $($namalskMission).namalsk ..."
$nPath = "$($namalskMission)\$($namalskMission).namalsk"
$nPath2 = Split-Path -Path $nPath -Leaf
$whatFind_NI = "2288339650"
$replaceWith_NI = "2289456201"
$whatFind_NS = "2288336145"
$replaceWith_NS = "2289461232"
Copy-Item -Path "${serverLocation}\@Namalsk_Survival\Extras\${nPath}\" -Destination "${serverLocation}\mpmissions\" -Recurse -Force
Start-Sleep -Seconds 1
# Patching meta.cpp files
Write-Host "`r`nPatching Namalsk ..."
$content = Get-Content -Path "${serverLocation}\@Namalsk_Island\meta.cpp"
$content = $content -replace $whatFind_NI, $replaceWith_NI
Set-Content -Path "${serverLocation}\@Namalsk_Island\meta.cpp" -Value $content
$content = Get-Content -Path "${serverLocation}\@Namalsk_Survival\meta.cpp"
$content = $content -replace $whatFind_NS, $replaceWith_NS
Set-Content -Path "${serverLocation}\@Namalsk_Survival\meta.cpp" -Value $content
}
}
}
# Customizing servers
# Overwriting default messages.xml from Namalsk by custom file
if (Test-Path -Path "$($serverLocation)\.customs") {
Copy-Item -Path "$($serverLocation)\.customs\messages.xml" -Destination "$($serverLocation)\mpmissions\$($nPath2)\db\"
}
Write-Host "`r`nEnd of update region.`r`n"
Start-Sleep -Seconds 1
}
if (-not ([string]::IsNullOrEmpty($config.becFolderName))) {
$becLocation = "$($serverLocation)\$($config.becFolderName)"
# Write-Host "becLocation: $($becLocation)"
}
if (-not ([string]::IsNullOrEmpty($config.atFolderName))) {
$atFolderName = $config.atFolderName
# Write-Host "atFolderName: $($atFolderName)"
}
if ($rotateLogs) {
$daysAmount = [Math]::Abs($config.daysAmount)
if (-not ([string]::IsNullOrEmpty($config.rotatedLogsFolderName))) {
$rotatedLogsFolderName = $config.rotatedLogsFolderName
}
}
$servers += $ini.Keys.Where({ $_ -Match "^[Ss]erver[\s_-]?[0-9]+$" })
$processID = 0
$dayzProcess = [Object[]]::new($servers.Count)
$becProcess = [Object[]]::new($servers.Count)
$dayzArguments = @()
foreach ($srv in $servers) {
# Write-Host ":$($srv):"
$instance = ${srv}
$serverPort = $ini.${srv}.serverPort
$serverConfig = $ini.${srv}.serverConfig
$serverExeName = $ini.${srv}.serverExeName
$serverCPU = $ini.${srv}.serverCPU
$startupClientMods = $ini.${srv}.startupClientMods
$startupServerMods = $ini.${srv}.startupServerMods
$serverProfile = "$($serverLocation)\profiles\$($instance)"
$startupMods = @("`"-mod=$($startupClientMods)`"", "`"-serverMod=$($startupServerMods)`"")
$startupParams = @("`"-profiles=$($serverProfile)`"", "`"-config=$($serverProfile)\$($serverConfig)`"", "-port=$($serverPort)", "-name=Server")
$additionalParams = @("-cpuCount=$($serverCPU)", "-adminLog -filePatching")
$dayzArguments = $startupParams + $startupMods + $additionalParams
$dayz = "`"$($serverLocation)\$($serverExeName)`""
$bec = "`"$($becLocation)\bec.exe`""
$becArguments = "-f `"$($instance).cfg`" --dsc"
# Rotate logs
if ($rotateLogs) {
if ($atFolderName) {
$atLogsLocation = "$($serverProfile)\$($atFolderName)"
$logsName = Get-ChildItem -Path $atLogsLocation -Directory -Filter "Log*"
$atLogsLocation += '\' + $logsName
}
if ($becLocation) {
$becLogsLocation = "$($becLocation)\Log\$($instance)"
}
if ($rotatedLogsFolderName) {
$rotatedLogsLocation = "$($serverProfile)\$($rotatedLogsFolderName)"
}
If (!(Test-Path -PathType Container $rotatedLogsLocation)) {
New-Item -ItemType Directory -Path $rotatedLogsLocation | Out-Null
Write-Host "Created new folder: $($rotatedLogsLocation)"
}
$fileList = $null
$destDelDir = "$($rotatedLogsLocation)\DeletedLogs"
$becDestDir = "$($rotatedLogsLocation)\BEC"
$atDestDir = "$($rotatedLogsLocation)\$($atFolderName)"
# Write-Host "destDelDir: $($destDelDir)"
# Write-Host "becDestDir: $($becDestDir)"
# Write-Host " atDestDir: $($atDestDir)"
# Write-Host "----------"
#region ### DayZ Game ###
Write-Host "Rotating DAYZ logs..."
$fileList = Get-Item -Path $serverProfile\*.rpt, $serverProfile\*.log, $serverProfile\*.adm, $serverProfile\*.mdmp | Where-Object { $_.LastWriteTime.Date -lt $date.Date }
# Write-Host $fileList -Separator "`n"
Write-Host "Moving DayZ logs to $($rotatedLogsLocation):"
foreach ($file in $fileList) {
Move-Item -Path $file -Destination $rotatedLogsLocation -Force
Write-Host "Moved $($file) to $($rotatedLogsLocation)"
}
Write-Host "Total: $($fileList.Count)"
$fileList = Get-Item -Path $rotatedLogsLocation\*.rpt, $rotatedLogsLocation\*.log, $rotatedLogsLocation\*.adm, $rotatedLogsLocation\*.mdmp | Where-Object { $_.LastWriteTime -lt $date.AddDays(-($daysAmount)) }
# Write-Host $fileList -Separator "`n"
Write-Host "Removing logs:"
if ($noDelete) {
If (!(Test-Path -PathType Container $destDelDir)) {
New-Item -ItemType Directory -Path $destDelDir | Out-Null
Write-Host "Created new folder: $($destDelDir)"
}
foreach ($file in $fileList) {
Move-Item -Path $file -Destination $destDelDir -Force
Write-Host "Moved $($file) to $($destDelDir)"
}
} else {
foreach ($file in $fileList) {
Remove-Item -Path $file
Write-Host "Removed $($file)!"
}
}
Write-Host "Total: $($fileList.Count)"
Write-Host ""
#endregion
#region ### BEC ###
if ($becDestDir) {
Write-Host "Rotating BEC logs..."
$fileList = $null
If (!(Test-Path -PathType Container $becDestDir)) {
New-Item -ItemType Directory -Path $becDestDir | Out-Null
Write-Host "Created new folder: $($becDestDir)"
}
$fileList = Get-ChildItem -Path $becLogsLocation\*.log -Recurse | Where-Object { $_.LastWriteTime.Date -lt $date.Date }
# Write-Host $fileList -Separator "`n"
Write-Host "Moving BEC's LOGs to $($becDestDir):"
foreach ($file in $fileList) {
Move-Item -Path $file -Destination $becDestDir -Force
Write-Host "Moved $($file) to $($becDestDir)"
}
Write-Host "Total: $($fileList.Count)"
$fileList = Get-Item -Path $becDestDir\*.log | Where-Object { $_.LastWriteTime -lt $date.AddDays(-($daysAmount)) }
# Write-Host $fileList -Separator "`n"
Write-Host "Removing BEC's LOGs:"
if ($noDelete) {
If (!(Test-Path -PathType Container $destDelDir)) {
New-Item -ItemType Directory -Path $destDelDir | Out-Null
Write-Host "Created new folder: $($destDelDir)"
}
foreach ($file in $fileList) {
Move-Item -Path $file -Destination $destDelDir
Write-Host "Moved $($file) to $($destDelDir)"
}
} else {
foreach ($file in $fileList) {
Remove-Item -Path $file
Write-Host "Removed $($file)!"
}
}
Write-Host "Total: $($fileList.Count)"
Write-Host ""
}
#endregion
#region ### Admin Tool ###
if ($atDestDir) {
Write-Host "Rotating AdminTool logs..."
$fileList = $null
If (!(Test-Path -PathType Container $atDestDir)) {
New-Item -ItemType Directory -Path $atDestDir | Out-Null
Write-Host "Created new folder: $($atDestDir)"
}
$fileList = Get-ChildItem -Path $atLogsLocation\*.txt, $atLogsLocation\*.log -Recurse | Where-Object { $_.LastWriteTime.Date -lt $date.Date }
# Write-Host $fileList -Separator "`n"
Write-Host "Moving AdminTool's LOGs to $($atDestDir):"
foreach ($file in $fileList) {
Move-Item -Path $file -Destination $atDestDir -Force
Write-Host "Moved $($file) to $($atDestDir)"
}
Write-Host "Total: $($fileList.Count)"
$fileList = Get-Item -Path $atDestDir\*.txt, $atDestDir\*.log | Where-Object { $_.LastWriteTime -lt $date.AddDays(-($daysAmount)) }
# Write-Host $fileList -Separator "`n"
Write-Host "Removing AdminTool's LOGs:"
if ($noDelete) {
If (!(Test-Path -PathType Container $destDelDir)) {
New-Item -ItemType Directory -Path $destDelDir | Out-Null
Write-Host "Created new folder: $($destDelDir)"
}
foreach ($file in $fileList) {
Move-Item -Path $file -Destination $destDelDir -Force
Write-Host "Moved $($file) to $($destDelDir)"
}
} else {
foreach ($file in $fileList) {
Remove-Item -Path $file
Write-Host "Removed $($file)!"
}
}
Write-Host "Total: $($fileList.Count)"
Write-Host ""
}
#endregion
}
# Write-Host " instance: $($instance)"
# Write-Host " serverProfile: $($serverProfile)"
# Write-Host " serverPort: $($serverPort)"
# Write-Host " serverConfig: $($serverConfig)"
# Write-Host " serverExeName: $($serverExeName)"
# Write-Host " serverCPU: $($serverCPU)"
# Write-Host " startupClientMods: $($startupClientMods)"
# Write-Host " startupServerMods: $($startupServerMods)"
# Write-Host " startupMods: $($startupMods)"
# Write-Host " startupParams: $($startupParams)"
# Write-Host " additionalParams: $($additionalParams)"
# Write-Host
# Write-Host " dayz: $($dayz)"
# Write-Host " dayzArguments: $($dayzArguments)"
# Write-Host
# Write-Host " becLocation: $($becLocation)"
# Write-Host " bec: $($bec)"
# Write-Host " becArguments: $($becArguments)"
# Write-Host
# Write-Host " atLogsLocation: $($atLogsLocation)"
# Write-Host " becLogsLocation: $($becLogsLocation)"
# Write-Host
# Write-Host " rotateLogs: $($rotateLogs)"
# Write-Host "rotatedLogsFolderName: $($rotatedLogsFolderName)"
# Write-Host " rotatedLogsLocation: $($rotatedLogsLocation)"
# Write-Host " daysAmount: $($daysAmount)"
# Write-Host " noDelete: $($noDelete)"
# Starting servers
Set-Location -Path "$($serverLocation)"
$dayzProcess[$processID] = Start-Process -FilePath $dayz -ArgumentList $dayzArguments -PassThru
$dayzProcess[$processID]
if ($becLocation) {
Set-Location -Path "$($becLocation)"
$becProcess[$processID] = Start-Process -FilePath $bec -ArgumentList $becArguments -PassThru
$becProcess[$processID]
}
$processID++
}
Write-Host "=========="