Compare commits

..

No commits in common. "b712768f9764dcbd686feabb72d589cd80d1d0fc" and "61f7afb9f7e296f4831aa49b73c20722793c9e98" have entirely different histories.

4 changed files with 34 additions and 68 deletions

View File

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

View File

@ -10,7 +10,6 @@ $OKB = New-Object System.Windows.Forms.Button
$OTB = New-Object System.Windows.Forms.TextBox $OTB = New-Object System.Windows.Forms.TextBox
$CAB = New-Object System.Windows.Forms.Button $CAB = New-Object System.Windows.Forms.Button
$Lbl = New-Object System.Windows.Forms.Label $Lbl = New-Object System.Windows.Forms.Label
$RLbl = New-Object System.Windows.Forms.Label
Function InitForm { Function InitForm {
$form = New-Object System.Windows.Forms.Form $form = New-Object System.Windows.Forms.Form
@ -58,13 +57,6 @@ Function AddTextBox {
$form.Controls.Add($OTB) $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) { Function FinalizeForm($form) {
$form.Topmost = $true $form.Topmost = $true
$form.Add_Shown({ $form.Activate() }) $form.Add_Shown({ $form.Activate() })
@ -73,25 +65,7 @@ Function FinalizeForm($form) {
Function SendRconCommand() { Function SendRconCommand() {
param($rcon) param($rcon)
$line = $OTB.Text $rcon.Send($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 = "" $OTB.Text = ""
} }
@ -111,7 +85,6 @@ Function Main {
AddOkButton -form $form -rcon $rcon AddOkButton -form $form -rcon $rcon
AddCloseButton($form) AddCloseButton($form)
AddLabel($form) AddLabel($form)
AddResponseLabel($form)
AddTextBox -form $form -rcon $rcon AddTextBox -form $form -rcon $rcon
FinalizeForm($form) FinalizeForm($form)

View File

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

View File

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