q3rcon-ps/lib/Q3Rcon.psm1

89 lines
1.9 KiB
PowerShell
Raw Permalink Normal View History

try {
. (Join-Path $PSScriptRoot packet.ps1)
. (Join-Path $PSScriptRoot base.ps1)
}
catch {
throw "unable to dot source module files"
}
2023-11-29 16:18:03 +00:00
class Rcon {
[Object]$base
Rcon ([string]$hostname, [int]$port, [string]$passwd) {
$this.base = New-Base -hostname $hostname -port $port -passwd $passwd
}
[Rcon] _login() {
$resp = $this.Send("login")
2023-11-29 16:18:03 +00:00
if ($resp -in @("Bad rcon", "Bad rconpassword.", "Invalid password.")) {
throw "invalid rcon password"
}
$this.base.ToString() | Write-Debug
return $this
}
[string] Send([string]$msg) {
2023-11-29 16:18:03 +00:00
return $this.base._send($msg)
}
[string] Send([string]$msg, [int]$timeout) {
return $this.base._send($msg, $timeout)
}
2023-11-29 16:18:03 +00:00
[void] Say($msg) {
$this.Send("say $msg")
2023-11-29 16:18:03 +00:00
}
[void] FastRestart() {
$this.Send("fast_restart", 2000)
2023-11-29 16:18:03 +00:00
}
[void] MapRotate() {
$this.Send("map_rotate", 2000)
2023-11-29 16:18:03 +00:00
}
[void] MapRestart() {
$this.Send("map_restart", 2000)
2023-11-29 16:18:03 +00:00
}
[string] Map() {
return $this.Send("mapname")
2023-11-29 16:18:03 +00:00
}
[void] SetMap($mapname) {
$this.Send("map mp_" + $mapname.TrimStart("mp_"), 2000)
2023-11-29 16:18:03 +00:00
}
[string] Gametype() {
return $this.Send("g_gametype")
2023-11-29 16:18:03 +00:00
}
[void] SetGametype($gametype) {
$this.Send("g_gametype $gametype")
2023-11-29 16:18:03 +00:00
}
[string] HostName() {
return $this.Send("sv_hostname")
2023-11-29 16:18:03 +00:00
}
[void] SetHostName($hostname) {
$this.Send("sv_hostname $hostname")
2023-11-29 16:18:03 +00:00
}
}
Function Connect-Rcon {
param([string]$hostname, [int]$port, [string]$passwd)
[Rcon]::new($hostname, $port, $passwd)._login()
2023-11-29 16:18:03 +00:00
}
Function Disconnect-Rcon {
param([Rcon]$rcon)
$rcon.base._close()
"Disconnected from {0}:{1}" -f $rcon.base.hostname, $rcon.base.port | Write-Debug
}
Export-ModuleMember -Function Connect-Rcon, Disconnect-Rcon