Files
Abiotic-Factor-Server-Manager/AFSM.ps1
T
2026-05-10 12:51:00 +00:00

485 lines
18 KiB
PowerShell

# Abiotic Factor Server Manager
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$configPath = "$scriptDir\server_config.json"
$script:serverProcess = $null
# Load/Save Config
function Load-Config {
if (Test-Path $configPath) {
try {
$cfg = Get-Content $configPath -Raw | ConvertFrom-Json
$txtSteamCMDDir.Text = $cfg.SteamCMDDir
$txtInstallDir.Text = $cfg.InstallDir
$txtMaxPlayers.Text = $cfg.MaxPlayers
$txtPort.Text = $cfg.Port
$txtQueryPort.Text = $cfg.QueryPort
$txtServerPassword.Text = $cfg.ServerPassword
$txtAdminPassword.Text = $cfg.AdminPassword
$txtServerName.Text = $cfg.ServerName
$chkUseLogin.Checked = $cfg.UseLogin
$txtSteamUser.Text = $cfg.SteamUser
} catch {
Set-Defaults
}
} else {
Set-Defaults
}
Update-SteamCMDStatus
}
function Set-Defaults {
$txtSteamCMDDir.Text = "$env:ProgramFiles\SteamCMD"
$txtInstallDir.Text = ""
$txtMaxPlayers.Text = "6"
$txtPort.Text = "7777"
$txtQueryPort.Text = "27015"
$txtServerPassword.Text = ""
$txtAdminPassword.Text = ""
$txtServerName.Text = "My Abiotic Factor Server"
$chkUseLogin.Checked = $false
$txtSteamUser.Text = ""
$txtSteamPass.Text = ""
}
function Save-Config {
$cfg = @{
SteamCMDDir = $txtSteamCMDDir.Text
InstallDir = $txtInstallDir.Text
MaxPlayers = $txtMaxPlayers.Text
Port = $txtPort.Text
QueryPort = $txtQueryPort.Text
ServerPassword = $txtServerPassword.Text
AdminPassword = $txtAdminPassword.Text
ServerName = $txtServerName.Text
UseLogin = $chkUseLogin.Checked
SteamUser = $txtSteamUser.Text
}
$cfg | ConvertTo-Json | Out-File $configPath -Encoding UTF8 -Force
}
function Update-SteamCMDStatus {
$exe = "$($txtSteamCMDDir.Text)\steamcmd.exe"
if (Test-Path $exe) {
$lblStatus.Text = "SteamCMD: Ready"
$lblStatus.ForeColor = "Green"
$btnInstallSteamCMD.Enabled = $false
$btnInstallSteamCMD.Text = "SteamCMD Installed"
} else {
$lblStatus.Text = "SteamCMD: Not Found"
$lblStatus.ForeColor = "Red"
$btnInstallSteamCMD.Enabled = $true
$btnInstallSteamCMD.Text = "Install SteamCMD"
}
}
function Install-SteamCMD {
$path = $txtSteamCMDDir.Text
if (-not $path) { return }
if (Test-Path "$path\steamcmd.exe") {
Update-SteamCMDStatus
return
}
New-Item -ItemType Directory -Path $path -Force | Out-Null
$zip = "$env:TEMP\steamcmd.zip"
$wc = New-Object System.Net.WebClient
$wc.DownloadFile("https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip", $zip)
$shell = New-Object -ComObject Shell.Application
$shell.NameSpace($zip).Items() | ForEach-Object { $shell.NameSpace($path).CopyHere($_, 16) }
Start-Sleep -Seconds 2
Remove-Item $zip -Force
Update-SteamCMDStatus
Save-Config
}
function Update-Server {
if (-not (Test-Path "$($txtSteamCMDDir.Text)\steamcmd.exe")) {
[System.Windows.Forms.MessageBox]::Show("Please install SteamCMD first!", "Error", "OK", "Error")
return
}
$installDir = $txtInstallDir.Text
if (-not $installDir) {
$installDir = "$($txtSteamCMDDir.Text)\AbioticFactorServer"
$txtInstallDir.Text = $installDir
Save-Config
}
New-Item -ItemType Directory -Path $installDir -Force | Out-Null
$appId = "2857200"
$scriptFile = [System.IO.Path]::GetTempFileName() + ".txt"
$scriptContent = "force_install_dir `"$installDir`"`n"
if ($chkUseLogin.Checked -and $txtSteamUser.Text) {
$scriptContent += "login `"$($txtSteamUser.Text)`" `"$($txtSteamPass.Text)`"`n"
} else {
$scriptContent += "login anonymous`n"
}
$scriptContent += "app_update $appId validate`nquit`n"
$scriptContent | Out-File $scriptFile -Encoding ASCII
$batchFile = [System.IO.Path]::GetTempFileName() + ".bat"
$cmdContent = "@echo off`ncd /d `"$($txtSteamCMDDir.Text)`"`n`"$($txtSteamCMDDir.Text)\steamcmd.exe`" +runscript `"$scriptFile`"`necho Update complete`ntimeout /t 3 /nobreak > nul"
$cmdContent | Out-File $batchFile -Encoding ASCII
Start-Process "cmd.exe" -ArgumentList "/c `"$batchFile`"" -Wait -NoNewWindow
Start-Sleep -Seconds 1
Remove-Item $scriptFile -Force -ErrorAction SilentlyContinue
Remove-Item $batchFile -Force -ErrorAction SilentlyContinue
}
function Start-Server {
$installDir = $txtInstallDir.Text
if (-not $installDir) {
[System.Windows.Forms.MessageBox]::Show("Please set Game Install Directory first!", "Error", "OK", "Error")
return
}
$serverExe = Join-Path $installDir "AbioticFactor\Binaries\Win64\AbioticFactorServer-Win64-Shipping.exe"
if (-not (Test-Path $serverExe)) {
[System.Windows.Forms.MessageBox]::Show("Server not found! Please run Update Server first.", "Error", "OK", "Error")
return
}
$binariesDir = Join-Path $installDir "AbioticFactor\Binaries\Win64"
$appIdFile = Join-Path $binariesDir "steam_appid.txt"
"2896390" | Out-File -FilePath $appIdFile -Encoding ASCII -Force
$args = "-log -newconsole -useperfthreads -NoAsyncLoadingThread -MaxServerPlayers=$($txtMaxPlayers.Text) -PORT=$($txtPort.Text) -QueryPort=$($txtQueryPort.Text)"
if ($txtServerPassword.Text) { $args += " -ServerPassword=`"$($txtServerPassword.Text)`"" }
if ($txtAdminPassword.Text) { $args += " -AdminPassword=`"$($txtAdminPassword.Text)`"" }
if ($txtServerName.Text) { $args += " -SteamServerName=`"$($txtServerName.Text)`"" }
try {
$script:serverProcess = Start-Process -FilePath $serverExe -ArgumentList $args -WorkingDirectory $binariesDir -PassThru
$btnStop.Enabled = $true
$btnStart.Enabled = $false
$btnUpdate.Enabled = $false
$lblStatusValue.Text = "Server: RUNNING"
$lblStatusValue.ForeColor = "Green"
} catch {
[System.Windows.Forms.MessageBox]::Show("Failed to start server: $_", "Error", "OK", "Error")
}
}
function Stop-Server {
if ($script:serverProcess -and (-not $script:serverProcess.HasExited)) {
$script:serverProcess.Kill()
$script:serverProcess.WaitForExit(3000)
}
Get-Process -Name "AbioticFactorServer-Win64-Shipping" -ErrorAction SilentlyContinue | ForEach-Object {
try { $_.Kill() } catch {}
}
$script:serverProcess = $null
$btnStop.Enabled = $false
$btnStart.Enabled = $true
$btnUpdate.Enabled = $true
$lblStatusValue.Text = "Server: STOPPED"
$lblStatusValue.ForeColor = "Red"
}
function Browse-Directory {
param($txtBox)
$dlg = New-Object System.Windows.Forms.FolderBrowserDialog
if ($dlg.ShowDialog() -eq "OK") {
$txtBox.Text = $dlg.SelectedPath
if ($txtBox.Name -eq "txtSteamCMDDir") { Update-SteamCMDStatus }
Save-Config
}
}
# Create Form
$form = New-Object System.Windows.Forms.Form
$form.Text = "Abiotic Factor Server Manager"
$form.Size = New-Object System.Drawing.Size(850, 600)
$form.StartPosition = "CenterScreen"
# Status Panel
$statusPanel = New-Object System.Windows.Forms.Panel
$statusPanel.BackColor = "Control"
$statusPanel.Location = New-Object System.Drawing.Point(0, 0)
$statusPanel.Size = New-Object System.Drawing.Size(850, 80)
$statusPanel.BorderStyle = "FixedSingle"
$lblStatusValue = New-Object System.Windows.Forms.Label
$lblStatusValue.Text = "Server: STOPPED"
$lblStatusValue.ForeColor = "Red"
$lblStatusValue.Font = New-Object System.Drawing.Font("Microsoft Sans Serif", 10, [System.Drawing.FontStyle]::Bold)
$lblStatusValue.Location = New-Object System.Drawing.Point(10, 10)
$lblStatusValue.Size = New-Object System.Drawing.Size(200, 25)
$statusPanel.Controls.Add($lblStatusValue)
$lblPortValue = New-Object System.Windows.Forms.Label
$lblPortValue.Text = "Port: 7777"
$lblPortValue.Location = New-Object System.Drawing.Point(10, 35)
$lblPortValue.Size = New-Object System.Drawing.Size(200, 20)
$statusPanel.Controls.Add($lblPortValue)
$lblServerNameValue = New-Object System.Windows.Forms.Label
$lblServerNameValue.Font = New-Object System.Drawing.Font("Microsoft Sans Serif", 9, [System.Drawing.FontStyle]::Bold)
$lblServerNameValue.Location = New-Object System.Drawing.Point(250, 10)
$lblServerNameValue.Size = New-Object System.Drawing.Size(400, 25)
$statusPanel.Controls.Add($lblServerNameValue)
$lblPasswordValue = New-Object System.Windows.Forms.Label
$lblPasswordValue.Location = New-Object System.Drawing.Point(250, 35)
$lblPasswordValue.Size = New-Object System.Drawing.Size(200, 20)
$statusPanel.Controls.Add($lblPasswordValue)
$form.Controls.Add($statusPanel)
# Button Panel
$buttonPanel = New-Object System.Windows.Forms.Panel
$buttonPanel.Location = New-Object System.Drawing.Point(0, 85)
$buttonPanel.Size = New-Object System.Drawing.Size(850, 45)
$form.Controls.Add($buttonPanel)
$btnUpdate = New-Object System.Windows.Forms.Button
$btnUpdate.Text = "1. Update"
$btnUpdate.Size = New-Object System.Drawing.Size(100, 35)
$btnUpdate.Location = New-Object System.Drawing.Point(10, 5)
$btnUpdate.Add_Click({ Update-Server })
$buttonPanel.Controls.Add($btnUpdate)
$btnStart = New-Object System.Windows.Forms.Button
$btnStart.Text = "2. Start"
$btnStart.Size = New-Object System.Drawing.Size(100, 35)
$btnStart.Location = New-Object System.Drawing.Point(120, 5)
$btnStart.BackColor = "LightGreen"
$btnStart.Add_Click({ Start-Server })
$buttonPanel.Controls.Add($btnStart)
$btnStop = New-Object System.Windows.Forms.Button
$btnStop.Text = "3. Stop"
$btnStop.Size = New-Object System.Drawing.Size(100, 35)
$btnStop.Location = New-Object System.Drawing.Point(230, 5)
$btnStop.BackColor = "LightCoral"
$btnStop.Enabled = $false
$btnStop.Add_Click({ Stop-Server })
$buttonPanel.Controls.Add($btnStop)
$btnSave = New-Object System.Windows.Forms.Button
$btnSave.Text = "Save Config"
$btnSave.Size = New-Object System.Drawing.Size(80, 35)
$btnSave.Location = New-Object System.Drawing.Point(340, 5)
$btnSave.Add_Click({ Save-Config })
$buttonPanel.Controls.Add($btnSave)
# Settings Panel
$settingsPanel = New-Object System.Windows.Forms.Panel
$settingsPanel.Location = New-Object System.Drawing.Point(0, 135)
$settingsPanel.Size = New-Object System.Drawing.Size(850, 450)
$settingsPanel.AutoScroll = $true
$form.Controls.Add($settingsPanel)
# SteamCMD
$lbl1 = New-Object System.Windows.Forms.Label
$lbl1.Text = "SteamCMD Dir:"
$lbl1.Location = New-Object System.Drawing.Point(10, 10)
$lbl1.Size = New-Object System.Drawing.Size(100, 25)
$settingsPanel.Controls.Add($lbl1)
$txtSteamCMDDir = New-Object System.Windows.Forms.TextBox
$txtSteamCMDDir.Location = New-Object System.Drawing.Point(120, 8)
$txtSteamCMDDir.Size = New-Object System.Drawing.Size(500, 25)
$settingsPanel.Controls.Add($txtSteamCMDDir)
$btnBrowse1 = New-Object System.Windows.Forms.Button
$btnBrowse1.Text = "Browse"
$btnBrowse1.Location = New-Object System.Drawing.Point(630, 7)
$btnBrowse1.Size = New-Object System.Drawing.Size(70, 27)
$btnBrowse1.Add_Click({ Browse-Directory $txtSteamCMDDir })
$settingsPanel.Controls.Add($btnBrowse1)
$btnInstallSteamCMD = New-Object System.Windows.Forms.Button
$btnInstallSteamCMD.Location = New-Object System.Drawing.Point(710, 7)
$btnInstallSteamCMD.Size = New-Object System.Drawing.Size(100, 27)
$btnInstallSteamCMD.Add_Click({ Install-SteamCMD })
$settingsPanel.Controls.Add($btnInstallSteamCMD)
$lblStatus = New-Object System.Windows.Forms.Label
$lblStatus.Location = New-Object System.Drawing.Point(120, 40)
$lblStatus.Size = New-Object System.Drawing.Size(300, 25)
$settingsPanel.Controls.Add($lblStatus)
# Install Dir
$lbl2 = New-Object System.Windows.Forms.Label
$lbl2.Text = "Game Install Dir:"
$lbl2.Location = New-Object System.Drawing.Point(10, 75)
$lbl2.Size = New-Object System.Drawing.Size(100, 25)
$settingsPanel.Controls.Add($lbl2)
$txtInstallDir = New-Object System.Windows.Forms.TextBox
$txtInstallDir.Location = New-Object System.Drawing.Point(120, 73)
$txtInstallDir.Size = New-Object System.Drawing.Size(600, 25)
$settingsPanel.Controls.Add($txtInstallDir)
$btnBrowse2 = New-Object System.Windows.Forms.Button
$btnBrowse2.Text = "Browse"
$btnBrowse2.Location = New-Object System.Drawing.Point(730, 72)
$btnBrowse2.Size = New-Object System.Drawing.Size(70, 27)
$btnBrowse2.Add_Click({ Browse-Directory $txtInstallDir })
$settingsPanel.Controls.Add($btnBrowse2)
# Steam Login
$loginGroup = New-Object System.Windows.Forms.GroupBox
$loginGroup.Text = "Steam Login (optional)"
$loginGroup.Location = New-Object System.Drawing.Point(10, 115)
$loginGroup.Size = New-Object System.Drawing.Size(800, 80)
$settingsPanel.Controls.Add($loginGroup)
$chkUseLogin = New-Object System.Windows.Forms.CheckBox
$chkUseLogin.Text = "Use Login"
$chkUseLogin.Location = New-Object System.Drawing.Point(10, 20)
$chkUseLogin.Size = New-Object System.Drawing.Size(80, 25)
$chkUseLogin.Add_CheckStateChanged({
$txtSteamUser.Enabled = $chkUseLogin.Checked
$txtSteamPass.Enabled = $chkUseLogin.Checked
})
$loginGroup.Controls.Add($chkUseLogin)
$lblUser = New-Object System.Windows.Forms.Label
$lblUser.Text = "Username:"
$lblUser.Location = New-Object System.Drawing.Point(100, 23)
$lblUser.Size = New-Object System.Drawing.Size(70, 20)
$loginGroup.Controls.Add($lblUser)
$txtSteamUser = New-Object System.Windows.Forms.TextBox
$txtSteamUser.Location = New-Object System.Drawing.Point(175, 20)
$txtSteamUser.Size = New-Object System.Drawing.Size(150, 25)
$txtSteamUser.Enabled = $false
$loginGroup.Controls.Add($txtSteamUser)
$lblPass = New-Object System.Windows.Forms.Label
$lblPass.Text = "Password:"
$lblPass.Location = New-Object System.Drawing.Point(340, 23)
$lblPass.Size = New-Object System.Drawing.Size(70, 20)
$loginGroup.Controls.Add($lblPass)
$txtSteamPass = New-Object System.Windows.Forms.TextBox
$txtSteamPass.Location = New-Object System.Drawing.Point(415, 20)
$txtSteamPass.Size = New-Object System.Drawing.Size(150, 25)
$txtSteamPass.PasswordChar = '*'
$txtSteamPass.Enabled = $false
$loginGroup.Controls.Add($txtSteamPass)
# Server Config
$serverGroup = New-Object System.Windows.Forms.GroupBox
$serverGroup.Text = "Server Settings"
$serverGroup.Location = New-Object System.Drawing.Point(10, 210)
$serverGroup.Size = New-Object System.Drawing.Size(800, 160)
$settingsPanel.Controls.Add($serverGroup)
$lblMax = New-Object System.Windows.Forms.Label
$lblMax.Text = "Max Players:"
$lblMax.Location = New-Object System.Drawing.Point(15, 25)
$lblMax.Size = New-Object System.Drawing.Size(85, 25)
$serverGroup.Controls.Add($lblMax)
$txtMaxPlayers = New-Object System.Windows.Forms.TextBox
$txtMaxPlayers.Location = New-Object System.Drawing.Point(105, 23)
$txtMaxPlayers.Size = New-Object System.Drawing.Size(80, 25)
$serverGroup.Controls.Add($txtMaxPlayers)
$lblPort = New-Object System.Windows.Forms.Label
$lblPort.Text = "Game Port:"
$lblPort.Location = New-Object System.Drawing.Point(220, 25)
$lblPort.Size = New-Object System.Drawing.Size(70, 25)
$serverGroup.Controls.Add($lblPort)
$txtPort = New-Object System.Windows.Forms.TextBox
$txtPort.Location = New-Object System.Drawing.Point(295, 23)
$txtPort.Size = New-Object System.Drawing.Size(80, 25)
$serverGroup.Controls.Add($txtPort)
$lblQuery = New-Object System.Windows.Forms.Label
$lblQuery.Text = "Query Port:"
$lblQuery.Location = New-Object System.Drawing.Point(410, 25)
$lblQuery.Size = New-Object System.Drawing.Size(80, 25)
$serverGroup.Controls.Add($lblQuery)
$txtQueryPort = New-Object System.Windows.Forms.TextBox
$txtQueryPort.Location = New-Object System.Drawing.Point(495, 23)
$txtQueryPort.Size = New-Object System.Drawing.Size(80, 25)
$serverGroup.Controls.Add($txtQueryPort)
$lblPassServer = New-Object System.Windows.Forms.Label
$lblPassServer.Text = "Server PW:"
$lblPassServer.Location = New-Object System.Drawing.Point(15, 60)
$lblPassServer.Size = New-Object System.Drawing.Size(85, 25)
$serverGroup.Controls.Add($lblPassServer)
$txtServerPassword = New-Object System.Windows.Forms.TextBox
$txtServerPassword.Location = New-Object System.Drawing.Point(105, 58)
$txtServerPassword.Size = New-Object System.Drawing.Size(150, 25)
$serverGroup.Controls.Add($txtServerPassword)
$lblAdmin = New-Object System.Windows.Forms.Label
$lblAdmin.Text = "Admin PW:"
$lblAdmin.Location = New-Object System.Drawing.Point(280, 60)
$lblAdmin.Size = New-Object System.Drawing.Size(85, 25)
$serverGroup.Controls.Add($lblAdmin)
$txtAdminPassword = New-Object System.Windows.Forms.TextBox
$txtAdminPassword.Location = New-Object System.Drawing.Point(370, 58)
$txtAdminPassword.Size = New-Object System.Drawing.Size(150, 25)
$serverGroup.Controls.Add($txtAdminPassword)
$lblName = New-Object System.Windows.Forms.Label
$lblName.Text = "Server Name:"
$lblName.Location = New-Object System.Drawing.Point(15, 95)
$lblName.Size = New-Object System.Drawing.Size(85, 25)
$serverGroup.Controls.Add($lblName)
$txtServerName = New-Object System.Windows.Forms.TextBox
$txtServerName.Location = New-Object System.Drawing.Point(105, 93)
$txtServerName.Size = New-Object System.Drawing.Size(350, 25)
$serverGroup.Controls.Add($txtServerName)
# Event handlers
$txtPort.Add_TextChanged({
$lblPortValue.Text = "Port: $($txtPort.Text)"
Save-Config
})
$txtServerName.Add_TextChanged({
$lblServerNameValue.Text = $txtServerName.Text
Save-Config
})
$txtServerPassword.Add_TextChanged({
if ($txtServerPassword.Text) {
$lblPasswordValue.Text = "PW: Set"
} else {
$lblPasswordValue.Text = "PW: None"
}
Save-Config
})
$txtMaxPlayers.Add_TextChanged({ Save-Config })
$txtQueryPort.Add_TextChanged({ Save-Config })
$txtAdminPassword.Add_TextChanged({ Save-Config })
# Initialize display values
$lblPortValue.Text = "Port: $($txtPort.Text)"
$lblServerNameValue.Text = $txtServerName.Text
$lblPasswordValue.Text = "PW: None"
# Load config and show
Load-Config
$form.ShowDialog()