/*
* Dynamic Hostname (olds.ro workshop card LIB-1570)
*
* Changes the server name (hostname) every few seconds, so the server list
* shows a name that moves: either the time left on the map, or a series of
* texts ("frames") read from a file.
*
* Cvars (saved with their descriptions in configs/plugins/olds_hostname.cfg
* the first time the plugin starts):
* olds_hn_base "olds.ro CS" the server name; empty = "olds.ro CS"
* (the log says so once per map)
* olds_hn_mode 1 0 = off, the plugin leaves the hostname
* alone
* 1 = base name and the time left (mm:ss)
* 2 = the frames of configs/olds_hostname.txt
* one after another, in file order
* olds_hn_interval 5 seconds between updates (1 - 300)
*
* Frames file configs/olds_hostname.txt: one server name per line, at most
* 63 bytes each (longer ones are cut), up to 32 lines; empty lines and lines
* starting with ; or // are skipped. It is read at every map start and by
* olds_hn_reload. Without frames, mode 2 shows the base name.
*
* The time left is worked out at every update from mp_timelimit, so after a
* map change it is the new map's time. Without a time limit the base name is
* shown alone. The hostname is only set when the text changed.
*
* Server commands:
* olds_hn_status prints the settings, the frames and the hostname now
* olds_hn_reload reads configs/olds_hostname.txt again
*
* Requires AMX Mod X 1.10 or newer. No extra modules and no ReAPI.
*
* Copyright (C) 2026 alexit (olds.ro workshop)
* SPDX-License-Identifier: GPL-3.0-or-later
*
* 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/>.
*/
#include <amxmodx>
#include <amxmisc>
#pragma semicolon 1
new const PLUGIN_NAME[] = "Dynamic Hostname";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";
new const DICTIONARY[] = "olds_hostname.txt";
new const FRAMES_FILE[] = "olds_hostname.txt";
new const DEFAULT_BASE[] = "olds.ro CS";
// The server list shows at most 63 bytes of the hostname.
const MAX_HOSTNAME = 63;
const MAX_FRAMES = 32;
const TASK_UPDATE = 15701;
new g_base[MAX_HOSTNAME + 1]; // olds_hn_base
new g_mode; // olds_hn_mode
new g_interval; // olds_hn_interval
new g_frames[MAX_FRAMES][MAX_HOSTNAME + 1];
new g_frameCount;
new g_nextFrame;
new g_updates;
new g_skipped;
new bool:g_warnedBase;
new bool:g_warnedFrames;
new g_pcvarTimelimit;
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
register_dictionary(DICTIONARY);
bind_pcvar_string(create_cvar("olds_hn_base", DEFAULT_BASE, FCVAR_NONE,
"Server name; mode 1 adds the time left to it; empty = olds.ro CS"),
g_base, charsmax(g_base));
bind_pcvar_num(create_cvar("olds_hn_mode", "1", FCVAR_NONE,
"0 = off, 1 = base name and time left, 2 = the frames of configs/olds_hostname.txt in turn",
true, 0.0, true, 2.0), g_mode);
new pcvar = create_cvar("olds_hn_interval", "5", FCVAR_NONE,
"Seconds between hostname updates",
true, 1.0, true, 300.0);
bind_pcvar_num(pcvar, g_interval);
hook_cvar_change(pcvar, "on_interval_change");
AutoExecConfig(true, "olds_hostname");
register_srvcmd("olds_hn_status", "cmd_status");
register_srvcmd("olds_hn_reload", "cmd_reload");
g_pcvarTimelimit = get_cvar_pointer("mp_timelimit");
}
/**
* Once per map, after server.cfg, the plugin config and the map configs, so
* mp_timelimit is this map's.
*/
public OnConfigsExecuted()
{
load_frames();
task_update();
schedule(g_interval);
}
/**
* Before the map changes: the plain base name, so the list shows no stale
* time until the next map's first update.
*/
public plugin_end()
{
if (g_mode && g_updates)
{
new base[MAX_HOSTNAME + 1];
get_base(base, charsmax(base), false);
set_cvar_string("hostname", base);
}
}
public on_interval_change(pcvar, const oldValue[], const newValue[])
{
schedule(str_to_num(newValue));
}
schedule(seconds)
{
remove_task(TASK_UPDATE);
set_task(float(clamp(seconds, 1, 300)), "task_update", TASK_UPDATE, _, _, "b");
}
/**
* Every olds_hn_interval seconds.
*/
public task_update()
{
if (!g_mode)
return;
new text[MAX_HOSTNAME + 1];
build_name(text, charsmax(text));
new current[128];
get_cvar_string("hostname", current, charsmax(current));
if (equal(current, text))
{
g_skipped++;
return;
}
set_cvar_string("hostname", text);
g_updates++;
}
build_name(text[], maxLength)
{
if (g_mode == 2)
{
if (g_frameCount)
{
if (g_nextFrame >= g_frameCount)
g_nextFrame = 0;
copy(text, maxLength, g_frames[g_nextFrame]);
g_nextFrame = (g_nextFrame + 1) % g_frameCount;
return;
}
if (!g_warnedFrames)
{
g_warnedFrames = true;
log_amx("olds_hn_mode is 2, but configs/%s has no frames; the base name is shown.", FRAMES_FILE);
}
}
new base[MAX_HOSTNAME + 1];
get_base(base, charsmax(base), true);
if (g_mode != 1 || get_pcvar_float(g_pcvarTimelimit) <= 0.0)
{
copy(text, maxLength, base);
return;
}
new left = max(get_timeleft(), 0);
new clock[16], full[160];
formatex(clock, charsmax(clock), "%02d:%02d", left / 60, left % 60);
formatex(full, charsmax(full), "%L", LANG_SERVER, "OLDS_HN_TIMELEFT", base, clock);
utf8_cut(full, MAX_HOSTNAME);
copy(text, maxLength, full);
}
/**
* olds_hn_base without spaces around it, or the default name when it is
* empty (logged once per map when warn is true).
*/
get_base(out[], maxLength, bool:warn)
{
copy(out, maxLength, g_base);
trim(out);
if (out[0])
return;
copy(out, maxLength, DEFAULT_BASE);
if (warn && !g_warnedBase)
{
g_warnedBase = true;
log_amx("olds_hn_base is empty; ^"%s^" is used as the server name.", DEFAULT_BASE);
}
}
/**
* Reads configs/olds_hostname.txt: one frame per line.
*/
load_frames()
{
g_frameCount = 0;
g_nextFrame = 0;
g_warnedFrames = false;
new path[PLATFORM_MAX_PATH];
get_configsdir(path, charsmax(path));
format(path, charsmax(path), "%s/%s", path, FRAMES_FILE);
new file = fopen(path, "rt");
if (!file)
return;
new line[256];
while (g_frameCount < MAX_FRAMES && fgets(file, line, charsmax(line)))
{
trim(line);
if (!line[0] || line[0] == ';' || (line[0] == '/' && line[1] == '/'))
continue;
utf8_cut(line, MAX_HOSTNAME);
copy(g_frames[g_frameCount], MAX_HOSTNAME, line);
g_frameCount++;
}
fclose(file);
}
public cmd_reload()
{
load_frames();
server_print("[olds_hostname] %d frame(s) read from configs/%s", g_frameCount, FRAMES_FILE);
return PLUGIN_HANDLED;
}
public cmd_status()
{
new current[128];
get_cvar_string("hostname", current, charsmax(current));
server_print("[olds_hostname] mode %d, every %d s, base ^"%s^", %d frame(s)",
g_mode, g_interval, g_base, g_frameCount);
for (new i = 0; i < g_frameCount; i++)
server_print("[olds_hostname] frame %d: %s", i + 1, g_frames[i]);
server_print("[olds_hostname] hostname now ^"%s^" (%d update(s), %d skipped as unchanged)",
current, g_updates, g_skipped);
return PLUGIN_HANDLED;
}
/**
* Shortens text to at most maxBytes bytes without leaving half of a UTF-8
* character at the end.
*/
utf8_cut(text[], maxBytes)
{
if (strlen(text) <= maxBytes)
return;
text[maxBytes] = EOS;
// Find where the last character starts: continuation bytes are 10xxxxxx.
new start = maxBytes - 1;
while (start > 0 && (text[start] & 0xC0) == 0x80)
start--;
new lead = text[start] & 0xFF;
new needed = 1;
if (lead >= 0xF0)
needed = 4;
else if (lead >= 0xE0)
needed = 3;
else if (lead >= 0xC0)
needed = 2;
if (maxBytes - start < needed)
text[start] = EOS;
}