Compare commits

...

4 Commits

Author SHA1 Message Date
b712768f97 add ability to query single var values 2023-11-30 10:31:25 +00:00
fdf83415d4 if we're changing maps, use SetMap() 2023-11-30 09:44:34 +00:00
7683665c7c lowercase login method to indicate it as internal method
added method overload for {Rcon}.Send
2023-11-30 09:44:05 +00:00
e446a45a6c upd {base}.send method.
If only a single fragment expected, don't use string array
2023-11-30 09:42:58 +00:00
4 changed files with 68 additions and 34 deletions

View File

@ -14,12 +14,15 @@ Function Read-HostUntilEmpty {
if ($line -in @("fast_restart", "map_rotate", "map_restart")) {
$cmd = $line -replace '(?:^|_)(\p{L})', { $_.Groups[1].Value.ToUpper() }
$resp = $rcon.$cmd()
$rcon.$cmd()
}
elseif ($line.StartsWith("map mp_")) {
$mapname = $line.Split()[1]
$rcon.SetMap($mapname)
}
else {
$resp = $rcon.Send($line)
$rcon.Send($line)
}
$resp | Write-Host
}
}

View File

@ -10,6 +10,7 @@ $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
$RLbl = New-Object System.Windows.Forms.Label
Function InitForm {
$form = New-Object System.Windows.Forms.Form
@ -57,6 +58,13 @@ Function AddTextBox {
$form.Controls.Add($OTB)
}
Function AddResponseLabel($form) {
$RLbl.Location = New-Object System.Drawing.Size(10, 75)
$RLbl.Size = New-Object System.Drawing.Size(260, 20)
$RLbl.Text = ""
$form.Controls.Add($RLbl)
}
Function FinalizeForm($form) {
$form.Topmost = $true
$form.Add_Shown({ $form.Activate() })
@ -65,7 +73,25 @@ Function FinalizeForm($form) {
Function SendRconCommand() {
param($rcon)
$rcon.Send($OTB.Text)
$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>[A-Za-z_]+)\^7["]\s') {
$RLbl.Text = $Matches.name + ": " + $Matches.value
}
$OTB.Text = ""
}
@ -85,6 +111,7 @@ Function Main {
AddOkButton -form $form -rcon $rcon
AddCloseButton($form)
AddLabel($form)
AddResponseLabel($form)
AddTextBox -form $form -rcon $rcon
FinalizeForm($form)

View File

@ -1,5 +1,10 @@
. $PSScriptRoot\packet.ps1
. $PSScriptRoot\base.ps1
try {
. (Join-Path $PSScriptRoot packet.ps1)
. (Join-Path $PSScriptRoot base.ps1)
}
catch {
throw "unable to dot source module files"
}
class Rcon {
@ -9,8 +14,8 @@ class Rcon {
$this.base = New-Base -hostname $hostname -port $port -passwd $passwd
}
[Rcon] Login() {
$resp = $this.base._send("login")
[Rcon] _login() {
$resp = $this.Send("login")
if ($resp -in @("Bad rcon", "Bad rconpassword.", "Invalid password.")) {
throw "invalid rcon password"
}
@ -18,55 +23,59 @@ class Rcon {
return $this
}
[string] Send($msg) {
[string] Send([string]$msg) {
return $this.base._send($msg)
}
[string] Send([string]$msg, [int]$timeout) {
return $this.base._send($msg, $timeout)
}
[void] Say($msg) {
$this.base._send($msg)
$this.Send("say $msg")
}
[void] FastRestart() {
$this.base._send("fast_restart", 2000)
$this.Send("fast_restart", 2000)
}
[void] MapRotate() {
$this.base._send("map_rotate", 2000)
$this.Send("map_rotate", 2000)
}
[void] MapRestart() {
$this.base._send("map_restart", 2000)
$this.Send("map_restart", 2000)
}
[string] Map() {
return $this.base._send("mapname")
return $this.Send("mapname")
}
[void] SetMap($mapname) {
$this.base._send("map mp_$mapname")
$this.Send("map mp_" + $mapname.TrimStart("mp_"), 2000)
}
[string] Gametype() {
return $this.base._send("g_gametype")
return $this.Send("g_gametype")
}
[void] SetGametype($gametype) {
$this.base._send("g_gametype $gametype")
$this.Send("g_gametype $gametype")
}
[string] HostName() {
return $this.base._send("sv_hostname")
return $this.Send("sv_hostname")
}
[void] SetHostName($hostname) {
$this.base._send("sv_hostname $hostname")
$this.Send("sv_hostname $hostname")
}
}
Function Connect-Rcon {
param([string]$hostname, [int]$port, [string]$passwd)
[Rcon]::new($hostname, $port, $passwd).Login()
[Rcon]::new($hostname, $port, $passwd)._login()
}
Function Disconnect-Rcon {

View File

@ -38,22 +38,16 @@ class Base {
[string] _send([string]$msg) {
$this._socket.Send($this.request.Payload($msg))
[string[]]$data = @()
$sw = [Diagnostics.Stopwatch]::StartNew()
While ($sw.ElapsedMilliseconds -lt 50) {
try {
$buf = New-Object System.Byte[] 4096
$this._socket.Receive($buf)
$data += [System.Text.Encoding]::ASCII.GetString($($buf | Select-Object -Skip $($this.response.Header().Length - 1)))
}
catch [System.Net.Sockets.SocketException] {
if ( $_.Exception.SocketErrorCode -eq 'TimedOut' ) {
"finished waiting for fragment" | Write-Debug
}
$buf = New-Object System.Byte[] 4096
try {
$this._socket.Receive($buf)
}
catch [System.Net.Sockets.SocketException] {
if ( $_.Exception.SocketErrorCode -eq 'TimedOut' ) {
"finished waiting for fragment" | Write-Debug
}
}
$sw.Stop()
return [string]::Join("", $data)
return [System.Text.Encoding]::ASCII.GetString($($buf | Select-Object -Skip $($this.response.Header().Length - 1)))
}
[string] _send([string]$msg, [int]$timeout) {
@ -73,6 +67,7 @@ class Base {
}
}
}
$sw.Stop()
return [string]::Join("", $data)
}