plugin_configs.sp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * Sourcemod 1.7 Plugin Template
  3. */
  4. #pragma semicolon 1
  5. #include <sourcemod>
  6. #pragma newdecls required
  7. #define PLUGIN_VERSION "0.1.0"
  8. public Plugin myinfo = {
  9. name = "[ANY] Plugin Configs",
  10. author = "nosoop",
  11. description = "For those of us that are too lazy to recompile plugins with " ...
  12. "AutoExecConfig. Executes configurations corresponding to running plugins by " ...
  13. "file name.",
  14. version = PLUGIN_VERSION,
  15. url = "https://github.com/nosoop/"
  16. }
  17. #define SERVER_OUTPUT_PREFIX "[plcfg] "
  18. #define EXEC_DIRECTORY "pluginconfs"
  19. #define CONFIG_DIRECTORY "cfg/" ... EXEC_DIRECTORY
  20. ArrayList g_PluginNames;
  21. public void OnPluginStart() {
  22. g_PluginNames = new ArrayList(PLATFORM_MAX_PATH);
  23. RegAdminCmd("sm_plugincfg_reload", AdminCmd_ReloadPluginConf, ADMFLAG_CONFIG,
  24. "Rebuilds the plugin configuration list and executes them.");
  25. ReloadPluginConfigList();
  26. }
  27. public Action AdminCmd_ReloadPluginConf(int client, int argc) {
  28. ReloadPluginConfigList();
  29. PrintToServer("%d configuration files added to list.", g_PluginNames.Length);
  30. return Plugin_Handled;
  31. }
  32. public void OnAutoConfigsBuffered() {
  33. char baseFileName[PLATFORM_MAX_PATH], pluginFileName[PLATFORM_MAX_PATH];
  34. for (int i = 0; i < g_PluginNames.Length; i++) {
  35. g_PluginNames.GetString(i, baseFileName, sizeof(baseFileName));
  36. strcopy(pluginFileName, sizeof(pluginFileName), baseFileName);
  37. StrCat(pluginFileName, sizeof(pluginFileName), ".smx");
  38. Handle plugin = FindPluginByFile(pluginFileName);
  39. if (plugin != INVALID_HANDLE && GetPluginStatus(plugin) == Plugin_Running) {
  40. if (ExecuteConfigFile(EXEC_DIRECTORY ... "/%s.cfg", baseFileName)) {
  41. PrintToServer(SERVER_OUTPUT_PREFIX ... "Executed config file "
  42. ... EXEC_DIRECTORY ... "/%s.cfg", baseFileName);
  43. } else {
  44. PrintToServer(SERVER_OUTPUT_PREFIX ... "Config file "
  45. ... EXEC_DIRECTORY ... "/%s.cfg does not exist", baseFileName);
  46. }
  47. }
  48. }
  49. }
  50. void ReloadPluginConfigList() {
  51. g_PluginNames.Clear();
  52. if (!DirExists(CONFIG_DIRECTORY, true)) {
  53. CreateDirectory(CONFIG_DIRECTORY,
  54. FPERM_U_READ + FPERM_U_WRITE + FPERM_U_EXEC + FPERM_G_READ + FPERM_G_EXEC +
  55. FPERM_O_READ + FPERM_O_EXEC);
  56. PrintToServer("Looks like this is the first time running. Generated directory %s, "
  57. ... "go put config files there.", CONFIG_DIRECTORY);
  58. }
  59. DirectoryListing configDirectory = OpenDirectory(CONFIG_DIRECTORY, true);
  60. char buffer[PLATFORM_MAX_PATH];
  61. FileType fileType;
  62. while (configDirectory.GetNext(buffer, sizeof(buffer), fileType)) {
  63. if (fileType == FileType_File) {
  64. GetFileBaseName(buffer, ".cfg", buffer, sizeof(buffer));
  65. g_PluginNames.PushString(buffer);
  66. PrintToServer(SERVER_OUTPUT_PREFIX ... "%s added to config execution list.",
  67. buffer);
  68. } else {
  69. // TODO recurse directories
  70. }
  71. }
  72. delete configDirectory;
  73. }
  74. stock bool ExecuteConfigFile(const char[] fmt, any ...) {
  75. char configExecPath[PLATFORM_MAX_PATH], configFilePath[PLATFORM_MAX_PATH];
  76. VFormat(configExecPath, sizeof(configExecPath), fmt, 2);
  77. Format(configFilePath, sizeof(configFilePath), "cfg/", configExecPath);
  78. if (FileExists(configFilePath, true)) {
  79. ServerCommand("exec %s", configExecPath);
  80. return true;
  81. }
  82. return false;
  83. }
  84. bool GetFileBaseName(const char[] path, const char[] ext = "", char[] buffer, int maxlen) {
  85. int pos = -1;
  86. if (strlen(ext) == 0) {
  87. pos = FindCharInString(path, '.', true);
  88. } else {
  89. pos = StrContains(path, ext, false);
  90. }
  91. if (pos != -1) {
  92. maxlen = pos <= maxlen? pos + 1 : maxlen;
  93. strcopy(buffer, maxlen, path);
  94. return pos < maxlen;
  95. }
  96. strcopy(buffer, maxlen, path);
  97. return false;
  98. }