q3rcon-ps/examples/gui.ps1

129 lines
3.5 KiB
PowerShell
Raw Normal View History

2023-11-29 22:11:58 +00:00
[cmdletbinding()]
param()
Import-Module ../lib/Q3Rcon.psm1
2023-11-29 19:21:43 +00:00
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$OKB = New-Object System.Windows.Forms.Button
$OTB = New-Object System.Windows.Forms.TextBox
$CAB = New-Object System.Windows.Forms.Button
$Lbl = New-Object System.Windows.Forms.Label
2023-11-30 10:31:25 +00:00
$RLbl = New-Object System.Windows.Forms.Label
2023-11-29 19:21:43 +00:00
2023-12-01 09:35:37 +00:00
Function New-Form {
2023-11-29 19:21:43 +00:00
$form = New-Object System.Windows.Forms.Form
$form.Text = "Q3Rcon Client"
$form.Size = New-Object System.Drawing.Size(275, 200)
2023-11-29 19:21:43 +00:00
$form.StartPosition = "CenterScreen"
return $form
}
2023-12-01 09:35:37 +00:00
Function Add-OkButton {
2023-11-29 19:21:43 +00:00
param($form, $rcon)
$OKB.Location = New-Object System.Drawing.Size(65, 100)
$OKB.Size = New-Object System.Drawing.Size(65, 23)
$OKB.Text = "Send"
2023-12-01 09:35:37 +00:00
$OKB.Add_Click({ Send-RconCommand -rcon $rcon })
2023-11-29 19:21:43 +00:00
$form.Controls.Add($OKB)
}
2023-12-01 09:35:37 +00:00
Function Add-CloseButton($form) {
2023-11-29 19:21:43 +00:00
$CAB.Location = New-Object System.Drawing.Size(140, 100)
$CAB.Size = New-Object System.Drawing.Size(65, 23)
$CAB.Text = "Close"
$CAB.Add_Click({ Write-Host "Disconnecting from Rcon" -ForegroundColor Green; $form.Close() })
$form.Controls.Add($CAB)
}
2023-12-01 09:35:37 +00:00
Function Add-Label($form) {
2023-11-29 19:21:43 +00:00
$Lbl.Location = New-Object System.Drawing.Size(10, 20)
$Lbl.Size = New-Object System.Drawing.Size(260, 20)
$Lbl.Text = "Input Rcon Command:"
$form.Controls.Add($Lbl)
}
2023-12-01 09:35:37 +00:00
Function Add-TextBox {
param($form, $rcon)
2023-11-29 19:21:43 +00:00
$OTB.Location = New-Object System.Drawing.Size(10, 50)
$OTB.Size = New-Object System.Drawing.Size(240, 20)
$OTB.Add_KeyDown({
if ($_.KeyCode -eq [System.Windows.Forms.Keys]::Enter) {
2023-12-01 09:35:37 +00:00
Send-RconCommand -rcon $rcon
}
})
2023-11-29 19:21:43 +00:00
$form.Controls.Add($OTB)
}
2023-12-01 09:35:37 +00:00
Function Add-ResponseLabel($form) {
2023-11-30 10:31:25 +00:00
$RLbl.Location = New-Object System.Drawing.Size(10, 75)
$RLbl.Size = New-Object System.Drawing.Size(260, 20)
$RLbl.Text = ""
$form.Controls.Add($RLbl)
}
2023-12-01 09:35:37 +00:00
Function Start-Form($form) {
2023-11-29 19:21:43 +00:00
$form.Topmost = $true
2023-12-01 09:35:37 +00:00
$form.Add_Shown({ $form.Activate() })
[void] $form.ShowDialog()
2023-11-29 19:21:43 +00:00
}
2023-12-01 09:35:37 +00:00
Function Send-RconCommand() {
param($rcon)
2023-11-30 10:31:25 +00:00
$line = $OTB.Text
$line | Write-Debug
if ($line -in @("fast_restart", "map_rotate", "map_restart")) {
$RLbl.Text = ""
$cmd = $line -replace '(?:^|_)(\p{L})', { $_.Groups[1].Value.ToUpper() }
$rcon.$cmd()
}
elseif ($line.StartsWith("map mp_")) {
$RLbl.Text = ""
$mapname = $line.Split()[1]
$rcon.SetMap($mapname)
}
else {
$resp = $rcon.Send($line)
}
if ($resp -match '^["](?<name>[a-z_]+)["]\sis[:]\s["](?<value>.*?)\^7["]\s') {
2023-11-30 10:31:25 +00:00
$RLbl.Text = $Matches.name + ": " + $Matches.value
}
$OTB.Text = ""
}
2023-11-29 19:21:43 +00:00
Function Get-ConnFromPSD1 {
$configpath = Join-Path $PSScriptRoot "config.psd1"
return Import-PowerShellDataFile -Path $configpath
}
2023-11-29 22:54:52 +00:00
Function Main {
2023-12-01 09:35:37 +00:00
BEGIN {
2023-11-29 22:54:52 +00:00
$conn = Get-ConnFromPSD1
$rcon = Connect-Rcon -hostname $conn.host -port $conn.port -passwd $conn.passwd
Write-Host $rcon.base.ToString() -ForegroundColor Green
2023-12-01 09:35:37 +00:00
$form = New-Form
Add-OkButton -form $form -rcon $rcon
Add-CloseButton($form)
Add-Label($form)
Add-ResponseLabel($form)
Add-TextBox -form $form -rcon $rcon
}
PROCESS {
Start-Form($form)
2023-11-29 22:54:52 +00:00
}
2023-12-01 09:35:37 +00:00
END {
2023-11-29 22:54:52 +00:00
Disconnect-Rcon -rcon $rcon
}
2023-11-29 19:21:43 +00:00
}
2023-11-29 22:54:52 +00:00
if ($MyInvocation.InvocationName -ne '.') {
Main
}