Skip to content
View in the app

A better way to browse. Learn more.

Learn more

hltv-auto-recording/addons/amxmodx/scripting/olds_hltvrec.sma

olds_hltvrec.sma
260 lines · 8.4 KB · Pawn (AMX Mod X)
Raw
/*
 * HLTV Auto Recording (olds.ro workshop card LIB-0835)
 * Copyright (C) 2026 daemon (olds.ro)
 *
 * This program is free software: you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details: <https://www.gnu.org/licenses/>.
 *
 * olds_hltvrec_delay seconds after the first round of a map starts, the connected HLTV proxy
 * starts recording <prefix>_<map>_<YYYYmmdd_HHMM>; at the map's end (intermission, or the map
 * changing) it stops. mp_restartgame does not split the demo. Two ways to reach the proxy:
 *   olds_hltvrec_rcon_addr ""      server_cmd of olds_hltvrec_cmd_start / _stop (default
 *                                  tv_record / tv_stoprecord, for a proxy bridge that adds them)
 *   olds_hltvrec_rcon_addr ip:port rcon to the HLTV proxy itself: record <name> / stoprecording
 */
#include <amxmodx>
#include <reapi>
#include <sockets>

#define PLUGIN  "HLTV Auto Recording"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define TASK_START 83500

new Float:g_delay, g_prefix[32], g_minPlayers
new g_cmdStart[32], g_cmdStop[32], g_rconAddr[64], g_rconPass[64]

new bool:g_recording, bool:g_firstRoundSeen
new bool:g_warnedProxy, bool:g_warnedPlayers, bool:g_warnedRcon
new g_demo[96]

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)

    bind_pcvar_float(create_cvar("olds_hltvrec_delay", "15.0", FCVAR_NONE, "Seconds after the first round starts before recording", true, 0.0, true, 600.0), g_delay)
    bind_pcvar_string(create_cvar("olds_hltvrec_prefix", "match", FCVAR_NONE, "First part of the demo name"), g_prefix, charsmax(g_prefix))
    bind_pcvar_num(create_cvar("olds_hltvrec_minplayers", "4", FCVAR_NONE, "Players needed (HLTV not counted) to record", true, 0.0, true, 32.0), g_minPlayers)
    bind_pcvar_string(create_cvar("olds_hltvrec_cmd_start", "tv_record", FCVAR_NONE, "Server command that starts recording (bridge mode)"), g_cmdStart, charsmax(g_cmdStart))
    bind_pcvar_string(create_cvar("olds_hltvrec_cmd_stop", "tv_stoprecord", FCVAR_NONE, "Server command that stops recording (bridge mode)"), g_cmdStop, charsmax(g_cmdStop))
    bind_pcvar_string(create_cvar("olds_hltvrec_rcon_addr", "", FCVAR_PROTECTED, "ip:port of the HLTV proxy for rcon; empty = bridge mode"), g_rconAddr, charsmax(g_rconAddr))
    bind_pcvar_string(create_cvar("olds_hltvrec_rcon_pass", "", FCVAR_PROTECTED, "The HLTV proxy's rcon password"), g_rconPass, charsmax(g_rconPass))
    AutoExecConfig(true, "olds_hltvrec")

    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_post", true)
    RegisterHookChain(RG_CSGameRules_GoToIntermission, "on_intermission_post", true)
    register_concmd("amx_hltvrec_status", "cmd_status", ADMIN_RCON, "- shows the recording state")
}

public plugin_end()
{
    if (g_recording)
    {
        log_amx("Map ends: stopping the HLTV demo %s", g_demo)
        stop_recording(true)
    }
}

/* ------------------------------------------------------------------ talking to the proxy */

rcon_warn(const why[])
{
    if (!g_warnedRcon)
    {
        log_amx("HLTV rcon to %s failed: %s", g_rconAddr, why)
        g_warnedRcon = true
    }
}

bool:rcon_proxy(const cmd[])
{
    new host[64], portText[8]
    strtok2(g_rconAddr, host, charsmax(host), portText, charsmax(portText), ':', TRIM_FULL)
    new port = str_to_num(portText)
    if (!host[0] || port < 1 || port > 65535)
    {
        rcon_warn("olds_hltvrec_rcon_addr must be ip:port")
        return false
    }
    new error
    new sock = socket_open(host, port, SOCKET_UDP, error)
    if (sock <= 0 || error)
    {
        rcon_warn("cannot open a UDP socket")
        return false
    }
    new packet[256], reply[128]
    oob_packet(packet, charsmax(packet), "challenge rcon^n")
    socket_send2(sock, packet, 4 + strlen(packet[4]))
    new got
    if (socket_is_readable(sock, 300000))
        got = socket_recv(sock, reply, charsmax(reply))
    new challenge[16]
    if (got > 19)
    {
        /* 4 x 0xFF, then "challenge rcon <number>" */
        new pos = contain(reply[4], "challenge rcon ")
        if (pos >= 0)
        {
            copy(challenge, charsmax(challenge), reply[4 + pos + 15])
            trim(challenge)
        }
    }
    if (!challenge[0] || !is_str_num(challenge))
    {
        socket_close(sock)
        rcon_warn("the proxy gave no rcon challenge")
        return false
    }
    new body[220]
    formatex(body, charsmax(body), "rcon %s ^"%s^" %s^n", challenge, g_rconPass, cmd)
    oob_packet(packet, charsmax(packet), body)
    socket_send2(sock, packet, 4 + strlen(packet[4]))
    socket_close(sock)
    return true
}

oob_packet(packet[], len, const text[])
{
    packet[0] = packet[1] = packet[2] = packet[3] = 0xFF
    copy(packet[4], len - 4, text)
}

bool:send_proxy(bool:start)
{
    if (!g_rconAddr[0])
    {
        if (start)
            server_cmd("%s %s", g_cmdStart, g_demo)
        else
            server_cmd("%s", g_cmdStop)
        return true
    }
    new cmd[128]
    if (start)
        formatex(cmd, charsmax(cmd), "record %s", g_demo)
    else
        copy(cmd, charsmax(cmd), "stoprecording")
    return rcon_proxy(cmd)
}

/* ------------------------------------------------------------------ round flow */

public on_restart_post()
{
    /* The first round of the map, and every round after it while nothing records (players or
       the proxy may come later). mp_restartgame while recording changes nothing. */
    if (g_recording || task_exists(TASK_START))
        return
    g_firstRoundSeen = true
    set_task(g_delay, "try_start", TASK_START)
}

public on_intermission_post()
{
    if (g_recording)
    {
        log_amx("Final round over: stopping the HLTV demo %s", g_demo)
        stop_recording(false)
    }
}

find_proxy()
{
    for (new id = 1; id <= MaxClients; id++)
        if (is_user_connected(id) && is_user_hltv(id))
            return id
    return 0
}

count_players()
{
    new n
    for (new id = 1; id <= MaxClients; id++)
        if (is_user_connected(id) && !is_user_hltv(id))
            n++
    return n
}

public try_start()
{
    if (g_recording)
        return
    new players = count_players()
    if (players < g_minPlayers)
    {
        if (!g_warnedPlayers)
        {
            log_amx("Not recording: %d player(s) on the server, olds_hltvrec_minplayers is %d", players, g_minPlayers)
            g_warnedPlayers = true
        }
        return
    }
    if (!find_proxy())
    {
        if (!g_warnedProxy)
        {
            log_amx("Not recording: no HLTV proxy is connected to this server")
            g_warnedProxy = true
        }
        return
    }
    new map[32], stamp[20]
    get_mapname(map, charsmax(map))
    get_time("%Y%m%d_%H%M", stamp, charsmax(stamp))
    formatex(g_demo, charsmax(g_demo), "%s_%s_%s", g_prefix, map, stamp)
    clean_name(g_demo)
    if (send_proxy(true))
    {
        g_recording = true
        log_amx("HLTV demo started: %s", g_demo)
    }
}

/* Only letters, digits, - and _ reach the command line. */
clean_name(name[])
{
    for (new i = 0; name[i]; i++)
    {
        new c = name[i]
        if (!(('a' <= c <= 'z') || ('A' <= c <= 'Z') || ('0' <= c <= '9') || c == '-' || c == '_'))
            name[i] = '_'
    }
}

stop_recording(bool:now)
{
    send_proxy(false)
    if (now)
        server_exec()
    g_recording = false
}

/* ------------------------------------------------------------------ admin */

public cmd_status(id, level, cid)
{
    if (id && !(get_user_flags(id) & level))
    {
        console_print(id, "You have no access to that command.")
        return PLUGIN_HANDLED
    }
    new proxy = find_proxy()
    if (g_recording)
        console_print(id, "[HLTVRec] recording %s", g_demo)
    else if (task_exists(TASK_START))
        console_print(id, "[HLTVRec] waiting to start (first round seen %d)", g_firstRoundSeen)
    else
        console_print(id, "[HLTVRec] not recording")
    new mode[16]
    copy(mode, charsmax(mode), "server command")
    if (g_rconAddr[0])
        copy(mode, charsmax(mode), "rcon")
    console_print(id, "[HLTVRec] players %d (need %d), proxy #%d, mode %s", count_players(), g_minPlayers, proxy, mode)
    return PLUGIN_HANDLED
}

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.