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")) {
$cmd = $line -replace '(?:^|_)(\p{L})', { $_.Groups[1].Value.ToUpper() }
$rcon.$cmd()
}
elseif ($line.StartsWith("map mp_")) {
$mapname = $line.Split()[1]
$rcon.SetMap($mapname)
$resp = $rcon.$cmd()
}
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
$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
@ -58,13 +57,6 @@ 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() })
@ -73,25 +65,7 @@ Function FinalizeForm($form) {
Function SendRconCommand() {
param($rcon)
$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
}
$rcon.Send($OTB.Text)
$OTB.Text = ""
}
@ -111,7 +85,6 @@ Function Main {
AddOkButton -form $form -rcon $rcon
AddCloseButton($form)
AddLabel($form)
AddResponseLabel($form)
AddTextBox -form $form -rcon $rcon
FinalizeForm($form)

View File

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