# 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 "../script/squirrel.hpp"
    00018 #include "script_scanner.hpp"
    00019
    00020 void ScriptScanner::ScanDir(const char *dirname, const char *info_file_name)
    00021 {
    00022   extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
    00023   extern bool FiosIsHiddenFile(const struct dirent *ent);
    00024
    00025   char d_name[MAX_PATH];
    00026   char temp_script[1024];
    00027   struct stat sb;
    00028   struct dirent *dirent;
    00029   DIR *dir;
    00030
    00031   dir = ttd_opendir(dirname);
    00032   /* Dir not found, so do nothing */
    00033   if (dir == NULL) return;
    00034
    00035   /* Walk all dirs trying to find a dir in which 'main.nut' exists */
    00036   while ((dirent = readdir(dir)) != NULL) {
    00037     ttd_strlcpy(d_name, FS2OTTD(dirent->d_name), sizeof(d_name));
    00038
    00039     /* Valid file, not '.' or '..', not hidden */
    00040     if (!FiosIsValidFile(dirname, dirent, &sb)) continue;
    00041     if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue;
    00042     if (FiosIsHiddenFile(dirent) && strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) != 0) continue;
    00043
    00044     /* Create the full length dirname */
    00045     ttd_strlcpy(temp_script, dirname, sizeof(temp_script));
    00046     ttd_strlcat(temp_script, d_name,  sizeof(temp_script));
    00047
    00048     if (S_ISREG(sb.st_mode)) {
    00049       /* Only .tar files are allowed */
    00050       char *ext = strrchr(d_name, '.');
    00051       if (ext == NULL || strcasecmp(ext, ".tar") != 0) continue;
    00052
    00053       /* We always expect a directory in the TAR */
    00054       const char *first_dir = FioTarFirstDir(temp_script);
    00055       if (first_dir == NULL) continue;
    00056
    00057       ttd_strlcat(temp_script, PATHSEP, sizeof(temp_script));
    00058       ttd_strlcat(temp_script, first_dir, sizeof(temp_script));
    00059       FioTarAddLink(temp_script, first_dir);
    00060     } else if (!S_ISDIR(sb.st_mode)) {
    00061       /* Skip any other type of file */
    00062       continue;
    00063     }
    00064
    00065     /* Add an additional / where needed */
    00066     if (temp_script[strlen(temp_script) - 1] != PATHSEPCHAR) ttd_strlcat(temp_script, PATHSEP, sizeof(temp_script));
    00067
    00068     char info_script[MAX_PATH];
    00069     ttd_strlcpy(info_script, temp_script, sizeof(info_script));
    00070     ttd_strlcpy(main_script, temp_script, sizeof(main_script));
    00071
    00072     /* Every script should contain an 'info.nut' and 'main.nut' file; else it is not a valid script */
    00073     ttd_strlcat(info_script, info_file_name, sizeof(info_script));
    00074     ttd_strlcat(main_script, "main.nut", sizeof(main_script));
    00075     if (!FioCheckFileExists(info_script, NO_DIRECTORY) || !FioCheckFileExists(main_script, NO_DIRECTORY)) continue;
    00076
    00077     /* We don't care if one of the other scripts failed to load. */
    00078     this->engine->ResetCrashed();
    00079     this->engine->LoadScript(info_script);
    00080   }
    00081   closedir(dir);
    00082 }
    00083
    00084 void ScriptScanner::ScanScriptDir(const char *info_file_name, Subdirectory search_dir)
    00085 {
    00086   char buf[MAX_PATH];
    00087   Searchpath sp;
    00088
    00089   FOR_ALL_SEARCHPATHS(sp) {
    00090     FioAppendDirectory(buf, MAX_PATH, sp, search_dir);
    00091     if (FileExists(buf)) this->ScanDir(buf, info_file_name);
    00092   }
    00093 }
    00094
    00095 ScriptScanner::ScriptScanner()
    00096 {
    00097   this->engine = new Squirrel();
    00098   this->main_script[0] = '\0';
    00099
    00100   /* Mark this class as global pointer */
    00101   this->engine->SetGlobalPointer(this);
    00102 }
    00103
    00104 ScriptScanner::~ScriptScanner()
    00105 {
    00106   delete this->engine;
    00107 }