(svn r15736) -Codechange: Split AIScanner/AIFileInfo to the more generic classes ScriptScanner/ScriptFileInfo.
This commit is contained in:
@@ -20,114 +20,16 @@
|
||||
#include "ai_scanner.hpp"
|
||||
#include "api/ai_controller.hpp"
|
||||
|
||||
void AIScanner::ScanDir(const char *dirname, bool library_scan)
|
||||
{
|
||||
extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
|
||||
extern bool FiosIsHiddenFile(const struct dirent *ent);
|
||||
|
||||
char d_name[MAX_PATH];
|
||||
char temp_script[1024];
|
||||
struct stat sb;
|
||||
struct dirent *dirent;
|
||||
DIR *dir;
|
||||
|
||||
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));
|
||||
|
||||
/* Valid file, not '.' or '..', not hidden */
|
||||
if (!FiosIsValidFile(dirname, dirent, &sb)) continue;
|
||||
if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue;
|
||||
if (FiosIsHiddenFile(dirent) && strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) != 0) continue;
|
||||
|
||||
/* Create the full length dirname */
|
||||
ttd_strlcpy(temp_script, dirname, sizeof(temp_script));
|
||||
ttd_strlcat(temp_script, d_name, sizeof(temp_script));
|
||||
|
||||
if (S_ISREG(sb.st_mode)) {
|
||||
/* Only .tar files are allowed */
|
||||
char *ext = strrchr(d_name, '.');
|
||||
if (ext == NULL || strcasecmp(ext, ".tar") != 0) continue;
|
||||
|
||||
/* We always expect a directory in the TAR */
|
||||
const char *first_dir = FioTarFirstDir(temp_script);
|
||||
if (first_dir == NULL) continue;
|
||||
|
||||
ttd_strlcat(temp_script, PATHSEP, sizeof(temp_script));
|
||||
ttd_strlcat(temp_script, first_dir, sizeof(temp_script));
|
||||
FioTarAddLink(temp_script, first_dir);
|
||||
} else if (!S_ISDIR(sb.st_mode)) {
|
||||
/* Skip any other type of file */
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Add an additional / where needed */
|
||||
if (temp_script[strlen(temp_script) - 1] != PATHSEPCHAR) ttd_strlcat(temp_script, PATHSEP, sizeof(temp_script));
|
||||
|
||||
if (!library_scan) {
|
||||
char info_script[MAX_PATH];
|
||||
ttd_strlcpy(info_script, temp_script, sizeof(info_script));
|
||||
ttd_strlcpy(main_script, temp_script, sizeof(main_script));
|
||||
|
||||
/* Every AI should contain an 'info.nut' and 'main.nut' file; else it is not a valid AI */
|
||||
ttd_strlcat(info_script, "info.nut", sizeof(info_script));
|
||||
ttd_strlcat(main_script, "main.nut", sizeof(main_script));
|
||||
if (!FioCheckFileExists(info_script, AI_DIR) || !FioCheckFileExists(main_script, AI_DIR)) continue;
|
||||
|
||||
DEBUG(ai, 6, "Loading AI at location '%s'", main_script);
|
||||
/* We don't care if one of the other scripts failed to load. */
|
||||
this->engine->ResetCrashed();
|
||||
this->engine->LoadScript(info_script);
|
||||
} else {
|
||||
char library_script[MAX_PATH];
|
||||
ttd_strlcpy(library_script, temp_script, sizeof(library_script));
|
||||
ttd_strlcpy(main_script, temp_script, sizeof(main_script));
|
||||
|
||||
/* Every library should contain an 'library.nut' and 'main.nut' file; else it is not a valid library */
|
||||
ttd_strlcat(library_script, "library.nut", sizeof(library_script));
|
||||
ttd_strlcat(main_script, "main.nut", sizeof(main_script));
|
||||
if (!FioCheckFileExists(library_script, AI_LIBRARY_DIR) || !FioCheckFileExists(main_script, AI_LIBRARY_DIR)) continue;
|
||||
|
||||
DEBUG(ai, 6, "Loading AI Library at location '%s'", main_script);
|
||||
/* We don't care if one of the other scripst failed to load. */
|
||||
this->engine->ResetCrashed();
|
||||
this->engine->LoadScript(library_script);
|
||||
}
|
||||
}
|
||||
closedir(dir);
|
||||
}
|
||||
|
||||
void AIScanner::ScanAIDir()
|
||||
{
|
||||
char buf[MAX_PATH];
|
||||
Searchpath sp;
|
||||
|
||||
extern void ScanForTarFiles();
|
||||
ScanForTarFiles();
|
||||
|
||||
FOR_ALL_SEARCHPATHS(sp) {
|
||||
FioAppendDirectory(buf, MAX_PATH, sp, AI_DIR);
|
||||
if (FileExists(buf)) this->ScanDir(buf, false);
|
||||
FioAppendDirectory(buf, MAX_PATH, sp, AI_LIBRARY_DIR);
|
||||
if (FileExists(buf)) this->ScanDir(buf, true);
|
||||
}
|
||||
}
|
||||
|
||||
void AIScanner::RescanAIDir()
|
||||
{
|
||||
this->ScanAIDir();
|
||||
this->ScanScriptDir("info.nut", AI_DIR);
|
||||
this->ScanScriptDir("library.nut", AI_LIBRARY_DIR);
|
||||
}
|
||||
|
||||
AIScanner::AIScanner() :
|
||||
ScriptScanner(),
|
||||
info_dummy(NULL)
|
||||
{
|
||||
this->engine = new Squirrel();
|
||||
this->main_script[0] = '\0';
|
||||
|
||||
/* Create the AIInfo class, and add the RegisterAI function */
|
||||
DefSQClass <AIInfo> SQAIInfo("AIInfo");
|
||||
SQAIInfo.PreRegister(engine);
|
||||
@@ -145,11 +47,8 @@ AIScanner::AIScanner() :
|
||||
this->engine->AddClassEnd();
|
||||
this->engine->AddMethod("RegisterLibrary", &AILibrary::Constructor, 2, "tx");
|
||||
|
||||
/* Mark this class as global pointer */
|
||||
this->engine->SetGlobalPointer(this);
|
||||
|
||||
/* Scan the AI dir for scripts */
|
||||
this->ScanAIDir();
|
||||
this->RescanAIDir();
|
||||
|
||||
/* Create the dummy AI */
|
||||
this->engine->ResetCrashed();
|
||||
@@ -176,7 +75,6 @@ AIScanner::~AIScanner()
|
||||
}
|
||||
|
||||
delete this->info_dummy;
|
||||
delete this->engine;
|
||||
}
|
||||
|
||||
bool AIScanner::ImportLibrary(const char *library, const char *class_name, int version, HSQUIRRELVM vm, AIController *controller)
|
||||
|
Reference in New Issue
Block a user