ai_scanner.cpp

Go to the documentation of this file.
00001 /* $Id: ai_scanner.cpp 16704 2009-06-30 20:11:36Z rubidium $ */
00002 
00005 #include "../stdafx.h"
00006 #include "../debug.h"
00007 #include "../string_func.h"
00008 #include "../fileio_func.h"
00009 #include "../fios.h"
00010 #include "../network/network.h"
00011 #include "../core/random_func.hpp"
00012 #include <sys/stat.h>
00013 
00014 #include <squirrel.h>
00015 #include "../script/squirrel.hpp"
00016 #include "../script/squirrel_helper.hpp"
00017 #include "../script/squirrel_class.hpp"
00018 #include "ai.hpp"
00019 #include "ai_info.hpp"
00020 #include "ai_scanner.hpp"
00021 #include "api/ai_controller.hpp"
00022 
00023 void AIScanner::ScanDir(const char *dirname, bool library_scan)
00024 {
00025   extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
00026   extern bool FiosIsHiddenFile(const struct dirent *ent);
00027 
00028   char d_name[MAX_PATH];
00029   char temp_script[1024];
00030   struct stat sb;
00031   struct dirent *dirent;
00032   DIR *dir;
00033 
00034   dir = ttd_opendir(dirname);
00035   /* Dir not found, so do nothing */
00036   if (dir == NULL) return;
00037 
00038   /* Walk all dirs trying to find a dir in which 'main.nut' exists */
00039   while ((dirent = readdir(dir)) != NULL) {
00040     ttd_strlcpy(d_name, FS2OTTD(dirent->d_name), sizeof(d_name));
00041 
00042     /* Valid file, not '.' or '..', not hidden */
00043     if (!FiosIsValidFile(dirname, dirent, &sb)) continue;
00044     if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue;
00045     if (FiosIsHiddenFile(dirent) && strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) != 0) continue;
00046 
00047     /* Create the full length dirname */
00048     ttd_strlcpy(temp_script, dirname, sizeof(temp_script));
00049     ttd_strlcat(temp_script, d_name,  sizeof(temp_script));
00050 
00051     if (S_ISREG(sb.st_mode)) {
00052       /* Only .tar files are allowed */
00053       char *ext = strrchr(d_name, '.');
00054       if (ext == NULL || strcasecmp(ext, ".tar") != 0) continue;
00055 
00056       /* We always expect a directory in the TAR */
00057       const char *first_dir = FioTarFirstDir(temp_script);
00058       if (first_dir == NULL) continue;
00059 
00060       ttd_strlcat(temp_script, PATHSEP, sizeof(temp_script));
00061       ttd_strlcat(temp_script, first_dir, sizeof(temp_script));
00062       FioTarAddLink(temp_script, first_dir);
00063     } else if (!S_ISDIR(sb.st_mode)) {
00064       /* Skip any other type of file */
00065       continue;
00066     }
00067 
00068     /* Add an additional / where needed */
00069     if (temp_script[strlen(temp_script) - 1] != PATHSEPCHAR) ttd_strlcat(temp_script, PATHSEP, sizeof(temp_script));
00070 
00071     if (!library_scan) {
00072       char info_script[MAX_PATH];
00073       ttd_strlcpy(info_script, temp_script, sizeof(info_script));
00074       ttd_strlcpy(main_script, temp_script, sizeof(main_script));
00075 
00076       /* Every AI should contain an 'info.nut' and 'main.nut' file; else it is not a valid AI */
00077       ttd_strlcat(info_script, "info.nut", sizeof(info_script));
00078       ttd_strlcat(main_script, "main.nut", sizeof(main_script));
00079       if (!FioCheckFileExists(info_script, AI_DIR) || !FioCheckFileExists(main_script, AI_DIR)) continue;
00080 
00081       DEBUG(ai, 6, "Loading AI at location '%s'", main_script);
00082       /* We don't care if one of the other scripts failed to load. */
00083       this->engine->ResetCrashed();
00084       this->engine->LoadScript(info_script);
00085     } else {
00086       char library_script[MAX_PATH];
00087       ttd_strlcpy(library_script, temp_script, sizeof(library_script));
00088       ttd_strlcpy(main_script, temp_script, sizeof(main_script));
00089 
00090       /* Every library should contain an 'library.nut' and 'main.nut' file; else it is not a valid library */
00091       ttd_strlcat(library_script, "library.nut", sizeof(library_script));
00092       ttd_strlcat(main_script, "main.nut", sizeof(main_script));
00093       if (!FioCheckFileExists(library_script, AI_LIBRARY_DIR) || !FioCheckFileExists(main_script, AI_LIBRARY_DIR)) continue;
00094 
00095       DEBUG(ai, 6, "Loading AI Library at location '%s'", main_script);
00096       /* We don't care if one of the other scripst failed to load. */
00097       this->engine->ResetCrashed();
00098       this->engine->LoadScript(library_script);
00099     }
00100   }
00101   closedir(dir);
00102 }
00103 
00104 void AIScanner::ScanAIDir()
00105 {
00106   char buf[MAX_PATH];
00107   Searchpath sp;
00108 
00109   extern void ScanForTarFiles();
00110   ScanForTarFiles();
00111 
00112   FOR_ALL_SEARCHPATHS(sp) {
00113     FioAppendDirectory(buf, MAX_PATH, sp, AI_DIR);
00114     if (FileExists(buf)) this->ScanDir(buf, false);
00115     FioAppendDirectory(buf, MAX_PATH, sp, AI_LIBRARY_DIR);
00116     if (FileExists(buf)) this->ScanDir(buf, true);
00117   }
00118 }
00119 
00120 void AIScanner::RescanAIDir()
00121 {
00122   this->ScanAIDir();
00123 }
00124 
00125 AIScanner::AIScanner() :
00126   info_dummy(NULL)
00127 {
00128   this->engine = new Squirrel();
00129   this->main_script[0] = '\0';
00130 
00131   /* Create the AIInfo class, and add the RegisterAI function */
00132   DefSQClass <AIInfo> SQAIInfo("AIInfo");
00133   SQAIInfo.PreRegister(engine);
00134   SQAIInfo.AddConstructor<void (AIInfo::*)(), 1>(engine, "x");
00135   SQAIInfo.DefSQAdvancedMethod(this->engine, &AIInfo::AddSetting, "AddSetting");
00136   SQAIInfo.DefSQAdvancedMethod(this->engine, &AIInfo::AddLabels, "AddLabels");
00137   SQAIInfo.DefSQConst(engine, AICONFIG_RANDOM, "AICONFIG_RANDOM");
00138   SQAIInfo.DefSQConst(engine, AICONFIG_BOOLEAN, "AICONFIG_BOOLEAN");
00139   SQAIInfo.PostRegister(engine);
00140   this->engine->AddMethod("RegisterAI", &AIInfo::Constructor, 2, "tx");
00141   this->engine->AddMethod("RegisterDummyAI", &AIInfo::DummyConstructor, 2, "tx");
00142 
00143   /* Create the AILibrary class, and add the RegisterLibrary function */
00144   this->engine->AddClassBegin("AILibrary");
00145   this->engine->AddClassEnd();
00146   this->engine->AddMethod("RegisterLibrary", &AILibrary::Constructor, 2, "tx");
00147 
00148   /* Mark this class as global pointer */
00149   this->engine->SetGlobalPointer(this);
00150 
00151   /* Scan the AI dir for scripts */
00152   this->ScanAIDir();
00153 
00154   /* Create the dummy AI */
00155   this->engine->ResetCrashed();
00156   strecpy(this->main_script, "%_dummy", lastof(this->main_script));
00157   extern void AI_CreateAIInfoDummy(HSQUIRRELVM vm);
00158   AI_CreateAIInfoDummy(this->engine->GetVM());
00159 }
00160 
00161 AIScanner::~AIScanner()
00162 {
00163   AIInfoList::iterator it = this->info_list.begin();
00164   for (; it != this->info_list.end(); it++) {
00165     free((void *)(*it).first);
00166     delete (*it).second;
00167   }
00168   it = this->info_single_list.begin();
00169   for (; it != this->info_single_list.end(); it++) {
00170     free((void *)(*it).first);
00171   }
00172   AILibraryList::iterator lit = this->library_list.begin();
00173   for (; lit != this->library_list.end(); lit++) {
00174     free((void *)(*lit).first);
00175     delete (*lit).second;
00176   }
00177 
00178   delete this->info_dummy;
00179   delete this->engine;
00180 }
00181 
00182 bool AIScanner::ImportLibrary(const char *library, const char *class_name, int version, HSQUIRRELVM vm, AIController *controller)
00183 {
00184   /* Internally we store libraries as 'library.version' */
00185   char library_name[1024];
00186   snprintf(library_name, sizeof(library_name), "%s.%d", library, version);
00187   strtolower(library_name);
00188 
00189   /* Check if the library + version exists */
00190   AILibraryList::iterator iter = this->library_list.find(library_name);
00191   if (iter == this->library_list.end()) {
00192     char error[1024];
00193 
00194     /* Now see if the version doesn't exist, or the library */
00195     iter = this->library_list.find(library);
00196     if (iter == this->library_list.end()) {
00197       snprintf(error, sizeof(error), "couldn't find library '%s'", library);
00198     } else {
00199       snprintf(error, sizeof(error), "couldn't find library '%s' version %d. The latest version available is %d", library, version, (*iter).second->GetVersion());
00200     }
00201     sq_throwerror(vm, OTTD2FS(error));
00202     return false;
00203   }
00204 
00205   /* Get the current table/class we belong to */
00206   HSQOBJECT parent;
00207   sq_getstackobj(vm, 1, &parent);
00208 
00209   char fake_class[1024];
00210   int next_number;
00211 
00212   if (!controller->LoadedLibrary(library_name, &next_number, &fake_class[0], sizeof(fake_class))) {
00213     /* Create a new fake internal name */
00214     snprintf(fake_class, sizeof(fake_class), "_internalNA%d", next_number);
00215 
00216     /* Load the library in a 'fake' namespace, so we can link it to the name the user requested */
00217     sq_pushroottable(vm);
00218     sq_pushstring(vm, OTTD2FS(fake_class), -1);
00219     sq_newclass(vm, SQFalse);
00220     /* Load the library */
00221     if (!Squirrel::LoadScript(vm, (*iter).second->GetMainScript(), false)) {
00222       char error[1024];
00223       snprintf(error, sizeof(error), "there was a compile error when importing '%s' version %d", library, version);
00224       sq_throwerror(vm, OTTD2FS(error));
00225       return false;
00226     }
00227     /* Create the fake class */
00228     sq_newslot(vm, -3, SQFalse);
00229     sq_pop(vm, 1);
00230 
00231     controller->AddLoadedLibrary(library_name, fake_class);
00232   }
00233 
00234   /* Find the real class inside the fake class (like 'sets.Vector') */
00235   sq_pushroottable(vm);
00236   sq_pushstring(vm, OTTD2FS(fake_class), -1);
00237   if (SQ_FAILED(sq_get(vm, -2))) {
00238     sq_throwerror(vm, _SC("internal error assigning library class"));
00239     return false;
00240   }
00241   sq_pushstring(vm, OTTD2FS((*iter).second->GetInstanceName()), -1);
00242   if (SQ_FAILED(sq_get(vm, -2))) {
00243     char error[1024];
00244     snprintf(error, sizeof(error), "unable to find class '%s' in the library '%s' version %d", (*iter).second->GetInstanceName(), library, version);
00245     sq_throwerror(vm, OTTD2FS(error));
00246     return false;
00247   }
00248   HSQOBJECT obj;
00249   sq_getstackobj(vm, -1, &obj);
00250   sq_pop(vm, 3);
00251 
00252   if (StrEmpty(class_name)) {
00253     sq_pushobject(vm, obj);
00254     return true;
00255   }
00256 
00257   /* Now link the name the user wanted to our 'fake' class */
00258   sq_pushobject(vm, parent);
00259   sq_pushstring(vm, OTTD2FS(class_name), -1);
00260   sq_pushobject(vm, obj);
00261   sq_newclass(vm, SQTrue);
00262   sq_newslot(vm, -3, SQFalse);
00263   sq_pop(vm, 1);
00264 
00265   sq_pushobject(vm, obj);
00266   return true;
00267 }
00268 
00269 void AIScanner::RegisterLibrary(AILibrary *library)
00270 {
00271   char library_name[1024];
00272   snprintf(library_name, sizeof(library_name), "%s.%s.%d", library->GetCategory(), library->GetInstanceName(), library->GetVersion());
00273   strtolower(library_name);
00274 
00275   if (this->library_list.find(library_name) != this->library_list.end()) {
00276     /* This AI was already registered */
00277 #ifdef WIN32
00278     /* Windows doesn't care about the case */
00279     if (strcasecmp(this->library_list[library_name]->GetMainScript(), library->GetMainScript()) == 0) {
00280 #else
00281     if (strcmp(this->library_list[library_name]->GetMainScript(), library->GetMainScript()) == 0) {
00282 #endif
00283       delete library;
00284       return;
00285     }
00286 
00287     DEBUG(ai, 0, "Registering two libraries with the same name and version");
00288     DEBUG(ai, 0, "  1: %s", this->library_list[library_name]->GetMainScript());
00289     DEBUG(ai, 0, "  2: %s", library->GetMainScript());
00290     DEBUG(ai, 0, "The first is taking precedence.");
00291 
00292     delete library;
00293     return;
00294   }
00295 
00296   this->library_list[strdup(library_name)] = library;
00297 }
00298 
00299 void AIScanner::RegisterAI(AIInfo *info)
00300 {
00301   char ai_name[1024];
00302   snprintf(ai_name, sizeof(ai_name), "%s.%d", info->GetName(), info->GetVersion());
00303   strtolower(ai_name);
00304 
00305   /* Check if GetShortName follows the rules */
00306   if (strlen(info->GetShortName()) != 4) {
00307     DEBUG(ai, 0, "The AI '%s' returned a string from GetShortName() which is not four characaters. Unable to load the AI.", info->GetName());
00308     delete info;
00309     return;
00310   }
00311 
00312   if (this->info_list.find(ai_name) != this->info_list.end()) {
00313     /* This AI was already registered */
00314 #ifdef WIN32
00315     /* Windows doesn't care about the case */
00316     if (strcasecmp(this->info_list[ai_name]->GetMainScript(), info->GetMainScript()) == 0) {
00317 #else
00318     if (strcmp(this->info_list[ai_name]->GetMainScript(), info->GetMainScript()) == 0) {
00319 #endif
00320       delete info;
00321       return;
00322     }
00323 
00324     DEBUG(ai, 0, "Registering two AIs with the same name and version");
00325     DEBUG(ai, 0, "  1: %s", this->info_list[ai_name]->GetMainScript());
00326     DEBUG(ai, 0, "  2: %s", info->GetMainScript());
00327     DEBUG(ai, 0, "The first is taking precedence.");
00328 
00329     delete info;
00330     return;
00331   }
00332 
00333   this->info_list[strdup(ai_name)] = info;
00334 
00335   /* Add the AI to the 'unique' AI list, where only the highest version of the
00336    *  AI is registered. */
00337   snprintf(ai_name, sizeof(ai_name), "%s", info->GetName());
00338   strtolower(ai_name);
00339   if (this->info_single_list.find(ai_name) == this->info_single_list.end()) {
00340     this->info_single_list[strdup(ai_name)] = info;
00341   } else if (this->info_single_list[ai_name]->GetVersion() < info->GetVersion()) {
00342     this->info_single_list[ai_name] = info;
00343   }
00344 }
00345 
00346 AIInfo *AIScanner::SelectRandomAI()
00347 {
00348   uint num_random_ais = 0;
00349   for (AIInfoList::iterator it = this->info_single_list.begin(); it != this->info_single_list.end(); it++) {
00350     if (it->second->UseAsRandomAI()) num_random_ais++;
00351   }
00352 
00353   if (num_random_ais == 0) {
00354     DEBUG(ai, 0, "No suitable AI found, loading 'dummy' AI.");
00355     return this->info_dummy;
00356   }
00357 
00358   /* Find a random AI */
00359   uint pos;
00360   if (_networking) {
00361     pos = InteractiveRandomRange(num_random_ais);
00362   } else {
00363     pos = RandomRange(num_random_ais);
00364   }
00365 
00366   /* Find the Nth item from the array */
00367   AIInfoList::iterator it = this->info_single_list.begin();
00368   while (!it->second->UseAsRandomAI()) it++;
00369   for (; pos > 0; pos--) {
00370     it++;
00371     while (!it->second->UseAsRandomAI()) it++;
00372   }
00373   return (*it).second;
00374 }
00375 
00376 AIInfo *AIScanner::FindInfo(const char *nameParam, int versionParam)
00377 {
00378   if (this->info_list.size() == 0) return NULL;
00379   if (nameParam == NULL) return NULL;
00380 
00381   char ai_name[1024];
00382   ttd_strlcpy(ai_name, nameParam, sizeof(ai_name));
00383   strtolower(ai_name);
00384 
00385   AIInfo *info = NULL;
00386   int version = -1;
00387 
00388   if (versionParam == -1) {
00389     /* We want to load the latest version of this AI; so find it */
00390     if (this->info_single_list.find(ai_name) != this->info_single_list.end()) return this->info_single_list[ai_name];
00391 
00392     /* If we didn't find a match AI, maybe the user included a version */
00393     char *e = strrchr(ai_name, '.');
00394     if (e == NULL) return NULL;
00395     *e = '\0';
00396     e++;
00397     versionParam = atoi(e);
00398     /* Fall-through, like we were calling this function with a version */
00399   }
00400 
00401   /* Try to find a direct 'name.version' match */
00402   char ai_name_tmp[1024];
00403   snprintf(ai_name_tmp, sizeof(ai_name_tmp), "%s.%d", ai_name, versionParam);
00404   strtolower(ai_name_tmp);
00405   if (this->info_list.find(ai_name_tmp) != this->info_list.end()) return this->info_list[ai_name_tmp];
00406 
00407   /* See if there is a compatible AI which goes by that name, with the highest
00408    *  version which allows loading the requested version */
00409   AIInfoList::iterator it = this->info_list.begin();
00410   for (; it != this->info_list.end(); it++) {
00411     char ai_name_compare[1024];
00412     snprintf(ai_name_compare, sizeof(ai_name_compare), "%s", (*it).second->GetName());
00413     strtolower(ai_name_compare);
00414 
00415     if (strcasecmp(ai_name, ai_name_compare) == 0 && (*it).second->CanLoadFromVersion(versionParam) && (version == -1 || (*it).second->GetVersion() > version)) {
00416       version = (*it).second->GetVersion();
00417       info = (*it).second;
00418     }
00419   }
00420 
00421   return info;
00422 }
00423 
00424 char *AIScanner::GetAIConsoleList(char *p, const char *last)
00425 {
00426   p += seprintf(p, last, "List of AIs:\n");
00427   AIInfoList::iterator it = this->info_list.begin();
00428   for (; it != this->info_list.end(); it++) {
00429     AIInfo *i = (*it).second;
00430     p += seprintf(p, last, "%10s (v%d): %s\n", i->GetName(), i->GetVersion(), i->GetDescription());
00431   }
00432   p += seprintf(p, last, "\n");
00433 
00434   return p;
00435 }
00436 
00437 #if defined(ENABLE_NETWORK)
00438 #include "../network/network_content.h"
00439 #include "../md5.h"
00440 #include "../tar_type.h"
00441 
00443 struct AIFileChecksumCreator : FileScanner {
00444   byte md5sum[16]; 
00445 
00450   AIFileChecksumCreator()
00451   {
00452     memset(this->md5sum, 0, sizeof(this->md5sum));
00453   }
00454 
00455   /* Add the file and calculate the md5 sum. */
00456   virtual bool AddFile(const char *filename, size_t basepath_length)
00457   {
00458     Md5 checksum;
00459     uint8 buffer[1024];
00460     size_t len, size;
00461     byte tmp_md5sum[16];
00462 
00463     /* Open the file ... */
00464     FILE *f = FioFOpenFile(filename, "rb", DATA_DIR, &size);
00465     if (f == NULL) return false;
00466 
00467     /* ... calculate md5sum... */
00468     while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
00469       size -= len;
00470       checksum.Append(buffer, len);
00471     }
00472     checksum.Finish(tmp_md5sum);
00473 
00474     FioFCloseFile(f);
00475 
00476     /* ... and xor it to the overall md5sum. */
00477     for (uint i = 0; i < sizeof(md5sum); i++) this->md5sum[i] ^= tmp_md5sum[i];
00478 
00479     return true;
00480   }
00481 };
00482 
00491 static bool IsSameAI(const ContentInfo *ci, bool md5sum, AIFileInfo *info)
00492 {
00493   uint32 id = 0;
00494   const char *str = info->GetShortName();
00495   for (int j = 0; j < 4 && *str != '\0'; j++, str++) id |= *str << (8 * j);
00496 
00497   if (id != ci->unique_id) return false;
00498   if (!md5sum) return true;
00499 
00500   AIFileChecksumCreator checksum;
00501   char path[MAX_PATH];
00502   strecpy(path, info->GetMainScript(), lastof(path));
00503   /* There'll always be at least 2 path separator characters in an AI's
00504    * main script name as the search algorithm requires the main script to
00505    * be in a subdirectory of the AI directory; so ai/<path>/main.nut. */
00506   *strrchr(path, PATHSEPCHAR) = '\0';
00507   *strrchr(path, PATHSEPCHAR) = '\0';
00508   TarList::iterator iter = _tar_list.find(path);
00509 
00510   if (iter != _tar_list.end()) {
00511     /* The main script is in a tar file, so find all files that
00512      * are in the same tar and add them to the MD5 checksumming. */
00513     TarFileList::iterator tar;
00514     FOR_ALL_TARS(tar) {
00515       /* Not in the same tar. */
00516       if (tar->second.tar_filename != iter->first) continue;
00517 
00518       /* Check the extension. */
00519       const char *ext = strrchr(tar->first.c_str(), '.');
00520       if (ext == NULL || strcasecmp(ext, ".nut") != 0) continue;
00521 
00522       /* Create the full path name, */
00523       seprintf(path, lastof(path), "%s%c%s", tar->second.tar_filename, PATHSEPCHAR, tar->first.c_str());
00524       checksum.AddFile(path, 0);
00525     }
00526   } else {
00527     /* Add the path sep char back when searching a directory, so we are
00528      * in the actual directory. */
00529     path[strlen(path)] = PATHSEPCHAR;
00530     checksum.Scan(".nut", path);
00531   }
00532 
00533   return memcmp(ci->md5sum, checksum.md5sum, sizeof(ci->md5sum)) == 0;
00534 }
00535 
00542 bool AIScanner::HasAI(const ContentInfo *ci, bool md5sum)
00543 {
00544   switch (ci->type) {
00545     case CONTENT_TYPE_AI:
00546       for (AIInfoList::iterator it = this->info_list.begin(); it != this->info_list.end(); it++) {
00547         if (IsSameAI(ci, md5sum, (*it).second)) return true;
00548       }
00549       return false;
00550 
00551     case CONTENT_TYPE_AI_LIBRARY:
00552       for (AILibraryList::iterator it = this->library_list.begin(); it != this->library_list.end(); it++) {
00553         if (IsSameAI(ci, md5sum, (*it).second)) return true;
00554       }
00555       return false;
00556 
00557     default:
00558       NOT_REACHED();
00559   }
00560 }
00561 
00568 /* static */ bool AI::HasAI(const ContentInfo *ci, bool md5sum)
00569 {
00570   return AI::ai_scanner->HasAI(ci, md5sum);
00571 }
00572 
00573 #endif /* ENABLE_NETWORK */

Generated on Thu Sep 24 19:35:00 2009 for OpenTTD by  doxygen 1.5.6