ai_scanner.cpp

Go to the documentation of this file.
00001 /* $Id: ai_scanner.cpp 15718 2009-03-15 00:32:18Z 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   strcpy(this->main_script, "%_dummy");
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   if (this->info_single_list.size() == 0) {
00349     DEBUG(ai, 0, "No suitable AI found, loading 'dummy' AI.");
00350     return this->info_dummy;
00351   }
00352 
00353   /* Find a random AI */
00354   uint pos;
00355   if (_networking) pos = InteractiveRandomRange((uint16)this->info_single_list.size());
00356   else             pos =            RandomRange((uint16)this->info_single_list.size());
00357 
00358   /* Find the Nth item from the array */
00359   AIInfoList::iterator it = this->info_single_list.begin();
00360   for (; pos > 0; pos--) it++;
00361   AIInfoList::iterator first_it = it;
00362   return (*it).second;
00363 }
00364 
00365 AIInfo *AIScanner::FindInfo(const char *nameParam, int versionParam)
00366 {
00367   if (this->info_list.size() == 0) return NULL;
00368   if (nameParam == NULL) return NULL;
00369 
00370   char ai_name[1024];
00371   ttd_strlcpy(ai_name, nameParam, sizeof(ai_name));
00372   strtolower(ai_name);
00373 
00374   AIInfo *info = NULL;
00375   int version = -1;
00376 
00377   if (versionParam == -1) {
00378     /* We want to load the latest version of this AI; so find it */
00379     if (this->info_single_list.find(ai_name) != this->info_single_list.end()) return this->info_single_list[ai_name];
00380 
00381     /* If we didn't find a match AI, maybe the user included a version */
00382     char *e = strrchr(ai_name, '.');
00383     if (e == NULL) return NULL;
00384     *e = '\0';
00385     e++;
00386     versionParam = atoi(e);
00387     /* Fall-through, like we were calling this function with a version */
00388   }
00389 
00390   /* Try to find a direct 'name.version' match */
00391   char ai_name_tmp[1024];
00392   snprintf(ai_name_tmp, sizeof(ai_name_tmp), "%s.%d", ai_name, versionParam);
00393   strtolower(ai_name_tmp);
00394   if (this->info_list.find(ai_name_tmp) != this->info_list.end()) return this->info_list[ai_name_tmp];
00395 
00396   /* See if there is a compatible AI which goes by that name, with the highest
00397    *  version which allows loading the requested version */
00398   AIInfoList::iterator it = this->info_list.begin();
00399   for (; it != this->info_list.end(); it++) {
00400     char ai_name_compare[1024];
00401     snprintf(ai_name_compare, sizeof(ai_name_compare), "%s", (*it).second->GetName());
00402     strtolower(ai_name_compare);
00403 
00404     if (strcasecmp(ai_name, ai_name_compare) == 0 && (*it).second->CanLoadFromVersion(versionParam)) {
00405       version = (*it).second->GetVersion();
00406       info = (*it).second;
00407     }
00408   }
00409 
00410   return info;
00411 }
00412 
00413 char *AIScanner::GetAIConsoleList(char *p, const char *last)
00414 {
00415   p += seprintf(p, last, "List of AIs:\n");
00416   AIInfoList::iterator it = this->info_list.begin();
00417   for (; it != this->info_list.end(); it++) {
00418     AIInfo *i = (*it).second;
00419     p += seprintf(p, last, "%10s (v%d): %s\n", i->GetName(), i->GetVersion(), i->GetDescription());
00420   }
00421   p += seprintf(p, last, "\n");
00422 
00423   return p;
00424 }
00425 
00426 #if defined(ENABLE_NETWORK)
00427 #include "../network/network_content.h"
00428 #include "../md5.h"
00429 #include "../tar_type.h"
00430 
00432 struct AIFileChecksumCreator : FileScanner {
00433   byte md5sum[16]; 
00434 
00439   AIFileChecksumCreator()
00440   {
00441     memset(this->md5sum, 0, sizeof(this->md5sum));
00442   }
00443 
00444   /* Add the file and calculate the md5 sum. */
00445   virtual bool AddFile(const char *filename, size_t basepath_length)
00446   {
00447     Md5 checksum;
00448     uint8 buffer[1024];
00449     size_t len, size;
00450     byte tmp_md5sum[16];
00451 
00452     /* Open the file ... */
00453     FILE *f = FioFOpenFile(filename, "rb", DATA_DIR, &size);
00454     if (f == NULL) return false;
00455 
00456     /* ... calculate md5sum... */
00457     while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
00458       size -= len;
00459       checksum.Append(buffer, len);
00460     }
00461     checksum.Finish(tmp_md5sum);
00462 
00463     FioFCloseFile(f);
00464 
00465     /* ... and xor it to the overall md5sum. */
00466     for (uint i = 0; i < sizeof(md5sum); i++) this->md5sum[i] ^= tmp_md5sum[i];
00467 
00468     return true;
00469   }
00470 };
00471 
00480 static bool IsSameAI(const ContentInfo *ci, bool md5sum, AIFileInfo *info)
00481 {
00482   uint32 id = 0;
00483   const char *str = info->GetShortName();
00484   for (int j = 0; j < 4 && *str != '\0'; j++, str++) id |= *str << (8 * j);
00485 
00486   if (id != ci->unique_id) return false;
00487   if (!md5sum) return true;
00488 
00489   AIFileChecksumCreator checksum;
00490   char path[MAX_PATH];
00491   strecpy(path, info->GetMainScript(), lastof(path));
00492   /* There'll always be at least 2 path separator characters in an AI's
00493    * main script name as the search algorithm requires the main script to
00494    * be in a subdirectory of the AI directory; so ai/<path>/main.nut. */
00495   *strrchr(path, PATHSEPCHAR) = '\0';
00496   *strrchr(path, PATHSEPCHAR) = '\0';
00497   TarList::iterator iter = _tar_list.find(path);
00498 
00499   if (iter != _tar_list.end()) {
00500     /* The main script is in a tar file, so find all files that
00501      * are in the same tar and add them to the MD5 checksumming. */
00502     TarFileList::iterator tar;
00503     FOR_ALL_TARS(tar) {
00504       /* Not in the same tar. */
00505       if (tar->second.tar_filename != iter->first) continue;
00506 
00507       /* Check the extension. */
00508       const char *ext = strrchr(tar->first.c_str(), '.');
00509       if (ext == NULL || strcasecmp(ext, ".nut") != 0) continue;
00510 
00511       /* Create the full path name, */
00512       seprintf(path, lastof(path), "%s%c%s", tar->second.tar_filename, PATHSEPCHAR, tar->first.c_str());
00513       checksum.AddFile(path, 0);
00514     }
00515   } else {
00516     /* Add the path sep char back when searching a directory, so we are
00517      * in the actual directory. */
00518     path[strlen(path)] = PATHSEPCHAR;
00519     checksum.Scan(".nut", path);
00520   }
00521 
00522   return memcmp(ci->md5sum, checksum.md5sum, sizeof(ci->md5sum)) == 0;
00523 }
00524 
00531 bool AIScanner::HasAI(const ContentInfo *ci, bool md5sum)
00532 {
00533   switch (ci->type) {
00534     case CONTENT_TYPE_AI:
00535       for (AIInfoList::iterator it = this->info_list.begin(); it != this->info_list.end(); it++) {
00536         if (IsSameAI(ci, md5sum, (*it).second)) return true;
00537       }
00538       return false;
00539 
00540     case CONTENT_TYPE_AI_LIBRARY:
00541       for (AILibraryList::iterator it = this->library_list.begin(); it != this->library_list.end(); it++) {
00542         if (IsSameAI(ci, md5sum, (*it).second)) return true;
00543       }
00544       return false;
00545 
00546     default:
00547       NOT_REACHED();
00548   }
00549 }
00550 
00557 /* static */ bool AI::HasAI(const ContentInfo *ci, bool md5sum)
00558 {
00559   return AI::ai_scanner->HasAI(ci, md5sum);
00560 }
00561 
00562 #endif /* ENABLE_NETWORK */

Generated on Wed Apr 1 14:38:04 2009 for OpenTTD by  doxygen 1.5.6