/*
* Config Reload Command (olds.ro workshop card LIB-1502)
*
* An admin runs server.cfg, or another config in the game folder, again
* without restarting the server or changing the map. The new settings are
* live as soon as the command returns.
*
* Console command (admins with the ADMIN_RCON flag, and the server console):
* amx_execcfg runs server.cfg
* amx_execcfg <file> runs another config, for example practice.cfg
* Only plain names are accepted: lower-case letters, digits and _ followed by
* .cfg. Paths ("../", "/"), other extensions and other characters are
* refused, so the command can only reach config files in the game folder.
* A file that does not exist is reported and nothing is executed.
*
* Cvar (saved with its description in configs/plugins/olds_execcfg.cfg the
* first time the plugin starts):
* olds_execcfg_log 1 1 = write who ran which config to the AMXX log.
*
* Every text an admin reads comes from data/lang/olds_execcfg.txt (English).
* The AMXX log line stays in English for the server owner.
*
* Requires AMX Mod X 1.10 or newer. No other modules; 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 <amxmisc>
#pragma semicolon 1
new const PLUGIN_NAME[] = "Config Reload Command";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";
new const DICTIONARY[] = "olds_execcfg.txt";
new const COMMAND_NAME[] = "amx_execcfg";
new const DEFAULT_FILE[] = "server.cfg";
new const CFG_EXTENSION[] = ".cfg";
// Longest file name accepted, ".cfg" included.
const MAX_FILE_LENGTH = 63;
new g_log; // olds_execcfg_log
// A config started by this command is being executed right now. A config
// that runs amx_execcfg itself would otherwise call it again and again.
new bool:g_executing;
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
register_dictionary(DICTIONARY);
// The help line shown by amx_help comes from the dictionary.
register_concmd(COMMAND_NAME, "cmd_execcfg", ADMIN_RCON, "OLDS_EXECCFG_HELP", -1, true);
bind_pcvar_num(create_cvar("olds_execcfg_log", "1", FCVAR_NONE,
"1 = write who ran amx_execcfg, and which config, to the AMXX log",
true, 0.0, true, 1.0), g_log);
AutoExecConfig(true, "olds_execcfg");
}
/**
* amx_execcfg [file]
*
* The amxmisc helper cmd_access is not used because it prints through the
* common.txt dictionary, which has texts in many other languages.
*/
public cmd_execcfg(id, level, cid)
{
if (!has_command_access(id, level))
{
console_print(id, "%L", id, "OLDS_EXECCFG_NO_ACCESS", COMMAND_NAME);
return PLUGIN_HANDLED;
}
if (g_executing)
{
console_print(id, "%L", id, "OLDS_EXECCFG_NESTED", COMMAND_NAME, COMMAND_NAME);
return PLUGIN_HANDLED;
}
if (read_argc() > 2)
{
print_usage(id);
return PLUGIN_HANDLED;
}
// One cell more than the longest accepted name, so a longer name is seen
// as too long instead of being cut to an allowed one.
new file[MAX_FILE_LENGTH + 2];
if (read_argc() == 2)
{
read_argv(1, file, charsmax(file));
if (!is_allowed_name(file))
{
console_print(id, "%L", id, "OLDS_EXECCFG_BAD_NAME", file);
print_usage(id);
return PLUGIN_HANDLED;
}
}
else
{
copy(file, charsmax(file), DEFAULT_FILE);
}
// Checked through the game's file system, so it finds the file in the
// same places the exec command looks.
if (!file_exists(file, true))
{
console_print(id, "%L", id, "OLDS_EXECCFG_MISSING", file);
return PLUGIN_HANDLED;
}
// server_exec runs the config now, so its values are live when this
// command returns and the confirmation below is true.
g_executing = true;
server_cmd("exec %s", file);
server_exec();
g_executing = false;
console_print(id, "%L", id, "OLDS_EXECCFG_DONE", file);
if (g_log)
log_execution(id, file);
return PLUGIN_HANDLED;
}
/**
* A plain config name: one or more of a-z, 0-9 and _, then ".cfg", and not
* longer than MAX_FILE_LENGTH. Nothing else (no dots, slashes, spaces or
* capitals) is allowed.
*/
bool:is_allowed_name(const file[])
{
new length = strlen(file);
new stemLength = length - (sizeof(CFG_EXTENSION) - 1);
if (length > MAX_FILE_LENGTH || stemLength < 1)
return false;
if (!equal(file[stemLength], CFG_EXTENSION))
return false;
for (new i = 0; i < stemLength; i++)
{
new c = file[i];
if (!(('a' <= c <= 'z') || ('0' <= c <= '9') || c == '_'))
return false;
}
return true;
}
/**
* The server console (id 0) always has access, and so has the host of a
* listen server (id 1). Anyone else needs the flag in level: the one the
* command was registered with, or the one set for it in cmdaccess.ini.
*/
bool:has_command_access(id, level)
{
if (id == 0 || (id == 1 && !is_dedicated_server()))
return true;
if (level == ADMIN_ALL)
return true;
if (level == ADMIN_ADMIN)
return is_user_admin(id) != 0;
return (get_user_flags(id) & level) != 0;
}
print_usage(id)
{
console_print(id, "%L", id, "OLDS_EXECCFG_USAGE", COMMAND_NAME, DEFAULT_FILE);
}
/**
* One line in the AMXX log, in the same form as the stock admin commands.
*/
log_execution(id, const file[])
{
if (id)
{
new name[MAX_NAME_LENGTH], authid[MAX_AUTHID_LENGTH];
get_user_name(id, name, charsmax(name));
get_user_authid(id, authid, charsmax(authid));
log_amx("Cmd: ^"%s<%d><%s><>^" executed config ^"%s^"", name, get_user_userid(id), authid, file);
}
else
{
log_amx("Cmd: Server executed config ^"%s^"", file);
}
}