Skip to content
View in the app

A better way to browse. Learn more.

Learn more

custom-game-description/addons/amxmodx/scripting/olds_gamename.sma

olds_gamename.sma
175 lines · 4.8 KB · Pawn (AMX Mod X)
Raw
/*
 * Custom Game Description (olds.ro workshop card LIB-1522)
 *
 * Replaces the game name that the server browser shows for this server
 * ("Counter-Strike") with the server's own text, for example
 * "olds.ro CS 1.6". Query tools that read the server info see the same text.
 *
 * Cvar (saved with its description in configs/plugins/olds_gamename.cfg the
 * first time the plugin starts):
 *   olds_gamename "olds.ro CS 1.6"   The game name in the server list.
 *                                    Empty = the game's own name.
 * The value is read at every map start, after all configs have run, so a
 * change shows from the next map on. The server info has room for 31 bytes:
 * a longer name is cut there, never in the middle of a UTF-8 character, and
 * the cut is written to the AMXX log. Control characters are removed.
 *
 * The plugin shows no text to players, so it has no dictionary file.
 *
 * Requires AMX Mod X 1.10 or newer with the fakemeta module. ReAPI is not
 * used.
 *
 * 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 <fakemeta>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Custom Game Description";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

// Longest game name the server info holds, without the terminating zero.
const MAX_DESCRIPTION = 31;

// Longest cvar value read; anything past it is cut anyway.
const CVAR_LENGTH = 128;

new g_pcvarName;

// The name given to the server browser; empty = the game's own name.
new g_description[MAX_DESCRIPTION + 1];

public plugin_init()
{
	register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);

	g_pcvarName = create_cvar("olds_gamename", "olds.ro CS 1.6", FCVAR_NONE,
		"Game name in the server browser, at most 31 bytes; empty = the game's own name; read at map start");

	AutoExecConfig(true, "olds_gamename");

	register_forward(FM_GetGameDescription, "fw_get_game_description");
}

public plugin_cfg()
{
	// The plugin is loaded again on every map, so the name starts out empty.
	// Until the configs have run, use the value the cvar kept from the last
	// map; without this the list would show "Counter-Strike" for a few
	// seconds after each map change.
	load_description(false);
}

/**
 * Called once per map after server.cfg, amxx.cfg, the plugin configs and the
 * per-map configs have all run, so the cvar holds this map's value.
 * (plugin_cfg can run before configs/plugins/olds_gamename.cfg, which would
 * leave the name one map behind.)
 */
public OnConfigsExecuted()
{
	load_description(true);
}

/**
 * Reads the cvar into g_description: control characters removed, spaces
 * trimmed, at most MAX_DESCRIPTION bytes. A cut is logged when report is true.
 */
load_description(bool:report)
{
	new text[CVAR_LENGTH];
	get_pcvar_string(g_pcvarName, text, charsmax(text));

	remove_control_characters(text);
	trim(text);

	new length = strlen(text);

	if (length > MAX_DESCRIPTION)
	{
		utf8_cut(text, MAX_DESCRIPTION);
		trim(text);

		if (report)
		{
			log_amx("olds_gamename is %d bytes long; the server list has room for %d, so it shows ^"%s^".",
				length, MAX_DESCRIPTION, text);
		}
	}

	copy(g_description, charsmax(g_description), text);
}

/**
 * The engine asks for the game name whenever it answers a server info query.
 */
public fw_get_game_description()
{
	if (!g_description[0])
		return FMRES_IGNORED;

	forward_return(FMV_STRING, g_description);
	return FMRES_SUPERCEDE;
}

/**
 * Drops bytes below 32 (tabs, line breaks and the like), which have no place
 * in a one-line game name.
 */
remove_control_characters(text[])
{
	new to;

	for (new from = 0; text[from]; from++)
	{
		if ((text[from] & 0xFF) >= ' ')
			text[to++] = text[from];
	}

	text[to] = EOS;
}

/**
 * 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;
}

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.