(svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts noai
authortruelight
Thu, 15 Mar 2007 15:20:26 +0000
branchnoai
changeset 9411 29c9dfcdb8e9
parent 9410 286fcd8edc64
child 9412 af09fe15860d
(svn r9209) [NoAI] -Add: added ScanDir(), which scans the 'ai' dir for main.nut scripts
src/ai/squirrel/squirrel.cpp
src/ai/squirrel/squirrel.hpp
--- a/src/ai/squirrel/squirrel.cpp	Thu Mar 15 14:50:03 2007 +0000
+++ b/src/ai/squirrel/squirrel.cpp	Thu Mar 15 15:20:26 2007 +0000
@@ -4,6 +4,10 @@
 
 #include "squirrel.hpp"
 #include "../../debug.h"
+#include "../../string.h"
+#include "../../fios.h"
+#include <sys/types.h>
+#include <sys/stat.h>
 
 #define SQUIRREL_CLASS
 #include "convert.hpp"
@@ -87,6 +91,46 @@
 	return 0;
 }
 
+extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
+extern bool FiosIsHiddenFile(const struct dirent *ent);
+
+
+void FSquirrel::ScanDir(const char *dirname)
+{
+	struct stat sb;
+	struct dirent *dirent;
+	DIR *dir;
+	char d_name[256];
+	char script_name[256];
+	dir = ttd_opendir(dirname);
+	/* Dir not found, so do nothing */
+	if (dir == NULL) return;
+
+	/* Walk all dirs trying to find a dir in which 'main.nut' exists */
+	while ((dirent = readdir(dir)) != NULL) {
+		ttd_strlcpy(d_name, FS2OTTD(dirent->d_name), sizeof(d_name));
+
+		/* Found file must be directory, but not '.' or '..' */
+		if (FiosIsValidFile("ai", dirent, &sb) && (sb.st_mode & S_IFDIR) &&
+				(!FiosIsHiddenFile(dirent) || strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) == 0) &&
+				strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) {
+			/* Create the full-length script-name */
+			strcpy(script_name, dirname);
+			strcat(script_name, PATHSEP);
+			strcat(script_name, d_name);
+			strcat(script_name, PATHSEP);
+			strcat(script_name, "main.nut");
+			/* If it exists, load it up */
+			if (FileExists(script_name)) {
+				DEBUG(ai, 6, "[squirrel] Loading script '%s' for AI handling", script_name);
+				this->current_script = script_name;
+				this->engine->LoadScript(this->current_script);
+			}
+		}
+	}
+	closedir(dir);
+}
+
 void FSquirrel::Initializer()
 {
 	this->engine = new SquirrelEngine();
@@ -102,9 +146,8 @@
 	/* Mark this class as global pointer */
 	this->engine->SetGlobalPointer(this);
 
-	/* Now load all scripts we can find */
-	this->current_script = "ai/SQNoAI/main.nut";
-	this->engine->LoadScript(this->current_script);
+	/* Scan the AI dir for scripts */
+	this->ScanDir("ai");
 }
 
 FSquirrel::~FSquirrel()
--- a/src/ai/squirrel/squirrel.hpp	Thu Mar 15 14:50:03 2007 +0000
+++ b/src/ai/squirrel/squirrel.hpp	Thu Mar 15 15:20:26 2007 +0000
@@ -40,8 +40,17 @@
 	SquirrelEngine *engine; ///< The SquirrelEngine
 	const char *current_script; ///< Temporary variable to know which script defines which class
 
+	/**
+	 * The constructor for when a script makes an instance of the Factory class.
+	 */
 	static SQInteger FactoryConstructor(HSQUIRRELVM vm);
 
+	/**
+	 * Scans a dir to see if there are dirs in it which have a file called 'main.nut'.
+	 *  If found it loads the script.
+	 */
+	void ScanDir(const char *dirname);
+
 public:
 	~FSquirrel();