(svn r13442) [NoAI] -Add: added 'rescan_ai' console command. Use it to rescan the ai dir when you add AIs or libraries without needing to exit and restart openttd. noai
authorglx
Tue, 10 Jun 2008 14:40:32 +0000
branchnoai
changeset 10891 5ebb6f9068d0
parent 10890 1d8f2baff829
child 10896 6bb839ed0002
(svn r13442) [NoAI] -Add: added 'rescan_ai' console command. Use it to rescan the ai dir when you add AIs or libraries without needing to exit and restart openttd.
src/ai/ai.cpp
src/ai/ai.h
src/ai/ai_squirrel.cpp
src/ai/ai_squirrel.hpp
src/console_cmds.cpp
--- a/src/ai/ai.cpp	Tue Jun 10 14:24:14 2008 +0000
+++ b/src/ai/ai.cpp	Tue Jun 10 14:40:32 2008 +0000
@@ -221,6 +221,11 @@
 	return _ai_squirrel->ImportLibrary(library, class_name, version, vm);
 }
 
+void AI_Rescan()
+{
+	_ai_squirrel->RescanAIDir();
+}
+
 #else /* NO_THREADS */
 /* Stub functions for when we do not have threads. */
 void AI_StartNewAI(PlayerID player) { DEBUG(ai, 0, "Threading is disabled and therefor the AI too."); }
@@ -235,4 +240,5 @@
 AIInfo *AI_GetPlayerInfo(PlayerID player) { return NULL; }
 bool AI_ImportLibrary(const char *library, const char *class_name, int version, HSQUIRRELVM vm) { return false; }
 void CcAI(bool success, TileIndex tile, uint32 p1, uint32 p2) {}
+void AI_Rescan() {}
 #endif /* NO_THREADS */
--- a/src/ai/ai.h	Tue Jun 10 14:24:14 2008 +0000
+++ b/src/ai/ai.h	Tue Jun 10 14:40:32 2008 +0000
@@ -18,6 +18,7 @@
 void AI_Event(PlayerID player, AIEvent *event);
 char *AI_GetConsoleList(char *p, const char *last);
 bool AI_ImportLibrary(const char *library, const char *class_name, int version, HSQUIRRELVM vm);
+void AI_Rescan();
 
 void CcAI(bool success, TileIndex tile, uint32 p1, uint32 p2);
 
--- a/src/ai/ai_squirrel.cpp	Tue Jun 10 14:24:14 2008 +0000
+++ b/src/ai/ai_squirrel.cpp	Tue Jun 10 14:40:32 2008 +0000
@@ -110,6 +110,26 @@
 	closedir(dir);
 }
 
+void AISquirrel::ScanAIDir()
+{
+	char buf[MAX_PATH];
+	Searchpath sp;
+
+	FOR_ALL_SEARCHPATHS(sp) {
+		FioAppendDirectory(buf, MAX_PATH, sp, AI_DIR);
+		if (FileExists(buf)) this->ScanDir(buf, false);
+		ttd_strlcat(buf, "library" PATHSEP, MAX_PATH);
+		if (FileExists(buf)) this->ScanDir(buf, true);
+	}
+}
+
+void AISquirrel::RescanAIDir()
+{
+	extern void ScanForTarFiles();
+	ScanForTarFiles();
+	this->ScanAIDir();
+}
+
 AISquirrel::AISquirrel()
 {
 	this->library_instance_count = 0;
@@ -128,15 +148,7 @@
 	this->engine->SetGlobalPointer(this);
 
 	/* Scan the AI dir for scripts */
-	char buf[MAX_PATH];
-	Searchpath sp;
-
-	FOR_ALL_SEARCHPATHS(sp) {
-		FioAppendDirectory(buf, MAX_PATH, sp, AI_DIR);
-		if (FileExists(buf)) this->ScanDir(buf, false);
-		ttd_strlcat(buf, "library" PATHSEP, MAX_PATH);
-		if (FileExists(buf)) this->ScanDir(buf, true);
-	}
+	this->ScanAIDir();
 }
 
 AISquirrel::~AISquirrel()
--- a/src/ai/ai_squirrel.hpp	Tue Jun 10 14:24:14 2008 +0000
+++ b/src/ai/ai_squirrel.hpp	Tue Jun 10 14:40:32 2008 +0000
@@ -63,11 +63,21 @@
 	 */
 	const char *GetCurrentDirName() { return this->current_dir; }
 
+	/**
+	 * Rescan the AI dir for scripts.
+	 */
+	void RescanAIDir();
+
 private:
 	typedef std::map<const char *, class AIInfo *, ltstr> AIInfoList;
 	typedef std::map<const char *, class AILibrary *, ltstr> AILibraryList;
 
 	/**
+	 * Scan the AI dir for scripts.
+	 */
+	void ScanAIDir();
+
+	/**
 	 * Scan a dir for AIs.
 	 *  For non-library-scan, if an AI is found, AIInfo is created, and the AI
 	 *    is registered to the central system.
--- a/src/console_cmds.cpp	Tue Jun 10 14:24:14 2008 +0000
+++ b/src/console_cmds.cpp	Tue Jun 10 14:40:32 2008 +0000
@@ -903,6 +903,23 @@
 	return true;
 }
 
+DEF_CONSOLE_CMD(ConRescanAI)
+{
+	if (argc == 0) {
+		IConsoleHelp("Rescan the AI dir for scripts. Usage: 'rescan_ai'");
+		return true;
+	}
+
+	if (_networking && !_network_server) {
+		IConsoleWarning("Only the server can rescan the AI dir for scripts.");
+		return true;
+	}
+
+	AI_Rescan();
+
+	return true;
+}
+
 DEF_CONSOLE_CMD(ConGetSeed)
 {
 	if (argc == 0) {
@@ -1416,6 +1433,7 @@
 	IConsoleCmdRegister("getseed",      ConGetSeed);
 	IConsoleCmdRegister("getdate",      ConGetDate);
 	IConsoleCmdRegister("quit",         ConExit);
+	IConsoleCmdRegister("rescan_ai",    ConRescanAI);
 	IConsoleCmdRegister("resetengines", ConResetEngines);
 	IConsoleCmdRegister("return",       ConReturn);
 	IConsoleCmdRegister("screenshot",   ConScreenShot);