Browse Source

Configs are now discovered and executed.

First proper release!
nosoop 6 years ago
parent
commit
4fa4a2416a
1 changed files with 70 additions and 13 deletions
  1. 70 13
      scripting/plugin_configs.sp

+ 70 - 13
scripting/plugin_configs.sp

@@ -6,36 +6,57 @@
 
 #pragma newdecls required
 
-#define PLUGIN_VERSION "0.0.1"
+#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.",
+    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 CONFIG_DIRECTORY "cfg/sourcemod/pluginconfigs"
+#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 fileName[PLATFORM_MAX_PATH];
+	char baseFileName[PLATFORM_MAX_PATH], pluginFileName[PLATFORM_MAX_PATH];
 	for (int i = 0; i < g_PluginNames.Length; i++) {
-		g_PluginNames.GetString(i, fileName, sizeof(fileName));
-		Handle plugin = FindPluginByFile(fileName);
+		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) {
-			char filePath[PLATFORM_MAX_PATH];
-			strcopy(filePath, sizeof(filePath), CONFIG_DIRECTORY);
-			StrCat(filePath, sizeof(filePath), fileName);
-			// ExecuteConfig() ...
+			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);
+			}
 		}
 	}
 }
@@ -43,22 +64,58 @@ public void OnAutoConfigsBuffered() {
 void ReloadPluginConfigList() {
 	g_PluginNames.Clear();
 	
-	if (!DirExists(CONFIG_DIRECTORY, false)) {
+	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("cfg/sourcemod/pluginconfigs", false);
+	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);
 			
-			// TODO make sure this is basename
+			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;
 }