/** * Sourcemod 1.7 Plugin Template */ #pragma semicolon 1 #include #pragma newdecls required #define PLUGIN_VERSION "0.1.0" public Plugin myinfo = { name = "[ANY] Plugin Configs", author = "nosoop", description = "For those of us that are too lazy to recompile plugins with " ... "AutoExecConfig. Executes configurations corresponding to running plugins by " ... "file name.", version = PLUGIN_VERSION, url = "https://github.com/nosoop/" } #define SERVER_OUTPUT_PREFIX "[plcfg] " #define EXEC_DIRECTORY "pluginconfs" #define CONFIG_DIRECTORY "cfg/" ... EXEC_DIRECTORY ArrayList g_PluginNames; public void OnPluginStart() { g_PluginNames = new ArrayList(PLATFORM_MAX_PATH); RegAdminCmd("sm_plugincfg_reload", AdminCmd_ReloadPluginConf, ADMFLAG_CONFIG, "Rebuilds the plugin configuration list and executes them."); ReloadPluginConfigList(); } public Action AdminCmd_ReloadPluginConf(int client, int argc) { ReloadPluginConfigList(); PrintToServer("%d configuration files added to list.", g_PluginNames.Length); return Plugin_Handled; } public void OnAutoConfigsBuffered() { char baseFileName[PLATFORM_MAX_PATH], pluginFileName[PLATFORM_MAX_PATH]; for (int i = 0; i < g_PluginNames.Length; i++) { g_PluginNames.GetString(i, baseFileName, sizeof(baseFileName)); strcopy(pluginFileName, sizeof(pluginFileName), baseFileName); StrCat(pluginFileName, sizeof(pluginFileName), ".smx"); Handle plugin = FindPluginByFile(pluginFileName); if (plugin != INVALID_HANDLE && GetPluginStatus(plugin) == Plugin_Running) { if (ExecuteConfigFile(EXEC_DIRECTORY ... "/%s.cfg", baseFileName)) { PrintToServer(SERVER_OUTPUT_PREFIX ... "Executed config file " ... EXEC_DIRECTORY ... "/%s.cfg", baseFileName); } else { PrintToServer(SERVER_OUTPUT_PREFIX ... "Config file " ... EXEC_DIRECTORY ... "/%s.cfg does not exist", baseFileName); } } } } void ReloadPluginConfigList() { g_PluginNames.Clear(); if (!DirExists(CONFIG_DIRECTORY, true)) { CreateDirectory(CONFIG_DIRECTORY, FPERM_U_READ + FPERM_U_WRITE + FPERM_U_EXEC + FPERM_G_READ + FPERM_G_EXEC + FPERM_O_READ + FPERM_O_EXEC); PrintToServer("Looks like this is the first time running. Generated directory %s, " ... "go put config files there.", CONFIG_DIRECTORY); } DirectoryListing configDirectory = OpenDirectory(CONFIG_DIRECTORY, true); char buffer[PLATFORM_MAX_PATH]; FileType fileType; while (configDirectory.GetNext(buffer, sizeof(buffer), fileType)) { if (fileType == FileType_File) { GetFileBaseName(buffer, ".cfg", buffer, sizeof(buffer)); g_PluginNames.PushString(buffer); PrintToServer(SERVER_OUTPUT_PREFIX ... "%s added to config execution list.", buffer); } else { // TODO recurse directories } } delete configDirectory; } stock bool ExecuteConfigFile(const char[] fmt, any ...) { char configExecPath[PLATFORM_MAX_PATH], configFilePath[PLATFORM_MAX_PATH]; VFormat(configExecPath, sizeof(configExecPath), fmt, 2); Format(configFilePath, sizeof(configFilePath), "cfg/", configExecPath); if (FileExists(configFilePath, true)) { ServerCommand("exec %s", configExecPath); return true; } return false; } bool GetFileBaseName(const char[] path, const char[] ext = "", char[] buffer, int maxlen) { int pos = -1; if (strlen(ext) == 0) { pos = FindCharInString(path, '.', true); } else { pos = StrContains(path, ext, false); } if (pos != -1) { maxlen = pos <= maxlen? pos + 1 : maxlen; strcopy(buffer, maxlen, path); return pos < maxlen; } strcopy(buffer, maxlen, path); return false; }