# script_scanner.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "../stdafx.h"
    00013 #include "../string_func.h"
    00014 #include "../fileio_func.h"
    00015 #include <sys/stat.h>
    00016
    00017 #include <squirrel.h>
    00018 #include "../script/squirrel.hpp"
    00019 #include "script_scanner.hpp"
    00020
    00021 void ScriptScanner::ScanDir(const char *dirname, const char *info_file_name)
    00022 {
    00023   extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
    00024   extern bool FiosIsHiddenFile(const struct dirent *ent);
    00025
    00026   char d_name[MAX_PATH];
    00027   char temp_script[1024];
    00028   struct stat sb;
    00029   struct dirent *dirent;
    00030   DIR *dir;
    00031
    00032   dir = ttd_opendir(dirname);
    00033   /* Dir not found, so do nothing */
    00034   if (dir == NULL) return;
    00035
    00036   /* Walk all dirs trying to find a dir in which 'main.nut' exists */
    00037   while ((dirent = readdir(dir)) != NULL) {
    00038     ttd_strlcpy(d_name, FS2OTTD(dirent->d_name), sizeof(d_name));
    00039
    00040     /* Valid file, not '.' or '..', not hidden */
    00041     if (!FiosIsValidFile(dirname, dirent, &sb)) continue;
    00042     if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue;
    00043     if (FiosIsHiddenFile(dirent) && strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) != 0) continue;
    00044
    00045     /* Create the full length dirname */
    00046     ttd_strlcpy(temp_script, dirname, sizeof(temp_script));
    00047     ttd_strlcat(temp_script, d_name,  sizeof(temp_script));
    00048
    00049     if (S_ISREG(sb.st_mode)) {
    00050       /* Only .tar files are allowed */
    00051       char *ext = strrchr(d_name, '.');
    00052       if (ext == NULL || strcasecmp(ext, ".tar") != 0) continue;
    00053
    00054       /* We always expect a directory in the TAR */
    00055       const char *first_dir = FioTarFirstDir(temp_script);
    00056       if (first_dir == NULL) continue;
    00057
    00058       ttd_strlcat(temp_script, PATHSEP, sizeof(temp_script));
    00059       ttd_strlcat(temp_script, first_dir, sizeof(temp_script));
    00060       FioTarAddLink(temp_script, first_dir);
    00061     } else if (!S_ISDIR(sb.st_mode)) {
    00062       /* Skip any other type of file */
    00063       continue;
    00064     }
    00065
    00066     /* Add an additional / where needed */
    00067     if (temp_script[strlen(temp_script) - 1] != PATHSEPCHAR) ttd_strlcat(temp_script, PATHSEP, sizeof(temp_script));
    00068
    00069     char info_script[MAX_PATH];
    00070     ttd_strlcpy(info_script, temp_script, sizeof(info_script));
    00071     ttd_strlcpy(main_script, temp_script, sizeof(main_script));
    00072
    00073     /* Every script should contain an 'info.nut' and 'main.nut' file; else it is not a valid script */
    00074     ttd_strlcat(info_script, info_file_name, sizeof(info_script));
    00075     ttd_strlcat(main_script, "main.nut", sizeof(main_script));
    00076     if (!FioCheckFileExists(info_script, NO_DIRECTORY) || !FioCheckFileExists(main_script, NO_DIRECTORY)) continue;
    00077
    00078     /* We don't care if one of the other scripts failed to load. */
    00079     this->engine->ResetCrashed();
    00080     this->engine->LoadScript(info_script);
    00081   }
    00082   closedir(dir);
    00083 }
    00084
    00085 void ScriptScanner::ScanScriptDir(const char *info_file_name, Subdirectory search_dir)
    00086 {
    00087   char buf[MAX_PATH];
    00088   Searchpath sp;
    00089
    00090   extern void ScanForTarFiles();
    00091   ScanForTarFiles();
    00092
    00093   FOR_ALL_SEARCHPATHS(sp) {
    00094     FioAppendDirectory(buf, MAX_PATH, sp, search_dir);
    00095     if (FileExists(buf)) this->ScanDir(buf, info_file_name);
    00096   }
    00097 }
    00098
    00099 ScriptScanner::ScriptScanner()
    00100 {
    00101   this->engine = new Squirrel();
    00102   this->main_script[0] = '\0';
    00103
    00104   /* Mark this class as global pointer */
    00105   this->engine->SetGlobalPointer(this);
    00106 }
    00107
    00108 ScriptScanner::~ScriptScanner()
    00109 {
    00110   delete this->engine;
    00111 }