mirror of https://github.com/OpenTTD/OpenTTD
(svn r22836) -Codechange: simplify the scanning of (AI) scripts
parent
672df52e0c
commit
d839aa0475
|
@ -24,8 +24,8 @@ void AIScanner::RescanAIDir()
|
||||||
{
|
{
|
||||||
/* Get rid of information of old AIs. */
|
/* Get rid of information of old AIs. */
|
||||||
this->Reset();
|
this->Reset();
|
||||||
this->ScanScriptDir("info.nut", AI_DIR);
|
this->Scan(PATHSEP "info.nut", AI_DIR);
|
||||||
this->ScanScriptDir("library.nut", AI_LIBRARY_DIR);
|
this->Scan(PATHSEP "library.nut", AI_LIBRARY_DIR);
|
||||||
}
|
}
|
||||||
|
|
||||||
AIScanner::AIScanner() :
|
AIScanner::AIScanner() :
|
||||||
|
@ -56,7 +56,9 @@ AIScanner::AIScanner() :
|
||||||
|
|
||||||
/* Create the dummy AI */
|
/* Create the dummy AI */
|
||||||
this->engine->ResetCrashed();
|
this->engine->ResetCrashed();
|
||||||
strecpy(this->main_script, "%_dummy", lastof(this->main_script));
|
|
||||||
|
free(this->main_script);
|
||||||
|
this->main_script = strdup("%_dummy");
|
||||||
extern void AI_CreateAIInfoDummy(HSQUIRRELVM vm);
|
extern void AI_CreateAIInfoDummy(HSQUIRRELVM vm);
|
||||||
AI_CreateAIInfoDummy(this->engine->GetVM());
|
AI_CreateAIInfoDummy(this->engine->GetVM());
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,91 +17,42 @@
|
||||||
#include "../script/squirrel.hpp"
|
#include "../script/squirrel.hpp"
|
||||||
#include "script_scanner.hpp"
|
#include "script_scanner.hpp"
|
||||||
|
|
||||||
void ScriptScanner::ScanDir(const char *dirname, const char *info_file_name)
|
bool ScriptScanner::AddFile(const char *filename, size_t basepath_length)
|
||||||
{
|
{
|
||||||
extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
|
free(this->main_script);
|
||||||
extern bool FiosIsHiddenFile(const struct dirent *ent);
|
this->main_script = strdup(filename);
|
||||||
|
if (this->main_script == NULL) return false;
|
||||||
|
|
||||||
char d_name[MAX_PATH];
|
const char *end = this->main_script + strlen(this->main_script) + 1;
|
||||||
char temp_script[1024];
|
char *p = strrchr(this->main_script, PATHSEPCHAR);
|
||||||
struct stat sb;
|
if (p == NULL) {
|
||||||
struct dirent *dirent;
|
p = this->main_script;
|
||||||
DIR *dir;
|
} else {
|
||||||
|
/* Skip over the path separator character. We don't need that. */
|
||||||
dir = ttd_opendir(dirname);
|
p++;
|
||||||
/* 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));
|
|
||||||
|
|
||||||
char info_script[MAX_PATH];
|
|
||||||
ttd_strlcpy(info_script, temp_script, sizeof(info_script));
|
|
||||||
ttd_strlcpy(main_script, temp_script, sizeof(main_script));
|
|
||||||
|
|
||||||
/* Every script should contain an 'info.nut' and 'main.nut' file; else it is not a valid script */
|
|
||||||
ttd_strlcat(info_script, info_file_name, sizeof(info_script));
|
|
||||||
ttd_strlcat(main_script, "main.nut", sizeof(main_script));
|
|
||||||
if (!FioCheckFileExists(info_script, NO_DIRECTORY) || !FioCheckFileExists(main_script, NO_DIRECTORY)) continue;
|
|
||||||
|
|
||||||
/* We don't care if one of the other scripts failed to load. */
|
|
||||||
this->engine->ResetCrashed();
|
|
||||||
this->engine->LoadScript(info_script);
|
|
||||||
}
|
}
|
||||||
closedir(dir);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ScriptScanner::ScanScriptDir(const char *info_file_name, Subdirectory search_dir)
|
strecpy(p, "main.nut", end);
|
||||||
{
|
|
||||||
char buf[MAX_PATH];
|
|
||||||
Searchpath sp;
|
|
||||||
|
|
||||||
FOR_ALL_SEARCHPATHS(sp) {
|
if (!FioCheckFileExists(filename, this->subdir) || !FioCheckFileExists(this->main_script, this->subdir)) return false;
|
||||||
FioAppendDirectory(buf, MAX_PATH, sp, search_dir);
|
|
||||||
if (FileExists(buf)) this->ScanDir(buf, info_file_name);
|
/* We don't care if one of the other scripts failed to load. */
|
||||||
}
|
this->engine->ResetCrashed();
|
||||||
|
this->engine->LoadScript(filename);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptScanner::ScriptScanner()
|
ScriptScanner::ScriptScanner()
|
||||||
{
|
{
|
||||||
this->engine = new Squirrel();
|
this->engine = new Squirrel();
|
||||||
this->main_script[0] = '\0';
|
|
||||||
|
|
||||||
/* Mark this class as global pointer */
|
/* Mark this class as global pointer */
|
||||||
this->engine->SetGlobalPointer(this);
|
this->engine->SetGlobalPointer(this);
|
||||||
|
this->main_script = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptScanner::~ScriptScanner()
|
ScriptScanner::~ScriptScanner()
|
||||||
{
|
{
|
||||||
|
free(this->main_script);
|
||||||
delete this->engine;
|
delete this->engine;
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,10 +12,10 @@
|
||||||
#ifndef SCRIPT_SCANNER_HPP
|
#ifndef SCRIPT_SCANNER_HPP
|
||||||
#define SCRIPT_SCANNER_HPP
|
#define SCRIPT_SCANNER_HPP
|
||||||
|
|
||||||
#include "../fileio_type.h"
|
#include "../fileio_func.h"
|
||||||
|
|
||||||
/** Scanner to help finding scripts. */
|
/** Scanner to help finding scripts. */
|
||||||
class ScriptScanner {
|
class ScriptScanner : public FileScanner {
|
||||||
public:
|
public:
|
||||||
ScriptScanner();
|
ScriptScanner();
|
||||||
~ScriptScanner();
|
~ScriptScanner();
|
||||||
|
@ -30,22 +30,11 @@ public:
|
||||||
*/
|
*/
|
||||||
const char *GetMainScript() { return this->main_script; }
|
const char *GetMainScript() { return this->main_script; }
|
||||||
|
|
||||||
/**
|
/* virtual */ bool AddFile(const char *filename, size_t basepath_length);
|
||||||
* Rescan for scripts.
|
|
||||||
* @param info_file_name The name of the 'info.nut' file.
|
|
||||||
* @param search_dir The subdirecotry to search for scripts.
|
|
||||||
*/
|
|
||||||
void ScanScriptDir(const char *info_file_name, Subdirectory search_dir);
|
|
||||||
|
|
||||||
private:
|
|
||||||
/**
|
|
||||||
* Scan a dir for scripts.
|
|
||||||
*/
|
|
||||||
void ScanDir(const char *dirname, const char *info_file_name);
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
class Squirrel *engine; ///< The engine we're scanning with.
|
class Squirrel *engine; ///< The engine we're scanning with.
|
||||||
char main_script[1024]; ///< The name of the current main script.
|
char *main_script; ///< The name of the current main script.
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* SCRIPT_SCANNER_HPP */
|
#endif /* SCRIPT_SCANNER_HPP */
|
||||||
|
|
Loading…
Reference in New Issue