# ai_scanner.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_scanner.cpp 18953 2010-01-29 21:38:55Z yexo $ */
    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 "../debug.h"
    00014 #include "../fileio_func.h"
    00015 #include "../network/network.h"
    00016 #include "../core/random_func.hpp"
    00017
    00018 #include <squirrel.h>
    00019 #include "../script/squirrel.hpp"
    00020 #include "../script/squirrel_helper.hpp"
    00021 #include "../script/squirrel_class.hpp"
    00022 #include "ai_info.hpp"
    00023 #include "ai_scanner.hpp"
    00024 #include "api/ai_controller.hpp"
    00025
    00026 void AIScanner::RescanAIDir()
    00027 {
    00028   this->ScanScriptDir("info.nut", AI_DIR);
    00029   this->ScanScriptDir("library.nut", AI_LIBRARY_DIR);
    00030 }
    00031
    00032 AIScanner::AIScanner() :
    00033   ScriptScanner(),
    00034   info_dummy(NULL)
    00035 {
    00036   /* Create the AIInfo class, and add the RegisterAI function */
    00037   DefSQClass <AIInfo> SQAIInfo("AIInfo");
    00038   SQAIInfo.PreRegister(engine);
    00039   SQAIInfo.AddConstructor<void (AIInfo::*)(), 1>(engine, "x");
    00040   SQAIInfo.DefSQAdvancedMethod(this->engine, &AIInfo::AddSetting, "AddSetting");
    00041   SQAIInfo.DefSQAdvancedMethod(this->engine, &AIInfo::AddLabels, "AddLabels");
    00042   SQAIInfo.DefSQConst(engine, AICONFIG_NONE, "AICONFIG_NONE");
    00043   SQAIInfo.DefSQConst(engine, AICONFIG_RANDOM, "AICONFIG_RANDOM");
    00044   SQAIInfo.DefSQConst(engine, AICONFIG_BOOLEAN, "AICONFIG_BOOLEAN");
    00045   SQAIInfo.DefSQConst(engine, AICONFIG_INGAME, "AICONFIG_INGAME");
    00046   SQAIInfo.PostRegister(engine);
    00047   this->engine->AddMethod("RegisterAI", &AIInfo::Constructor, 2, "tx");
    00048   this->engine->AddMethod("RegisterDummyAI", &AIInfo::DummyConstructor, 2, "tx");
    00049
    00050   /* Create the AILibrary class, and add the RegisterLibrary function */
    00051   this->engine->AddClassBegin("AILibrary");
    00052   this->engine->AddClassEnd();
    00053   this->engine->AddMethod("RegisterLibrary", &AILibrary::Constructor, 2, "tx");
    00054
    00055   /* Scan the AI dir for scripts */
    00056   this->RescanAIDir();
    00057
    00058   /* Create the dummy AI */
    00059   this->engine->ResetCrashed();
    00060   strecpy(this->main_script, "%_dummy", lastof(this->main_script));
    00061   extern void AI_CreateAIInfoDummy(HSQUIRRELVM vm);
    00062   AI_CreateAIInfoDummy(this->engine->GetVM());
    00063 }
    00064
    00065 AIScanner::~AIScanner()
    00066 {
    00067   AIInfoList::iterator it = this->info_list.begin();
    00068   for (; it != this->info_list.end(); it++) {
    00069     free((void *)(*it).first);
    00070     delete (*it).second;
    00071   }
    00072   it = this->info_single_list.begin();
    00073   for (; it != this->info_single_list.end(); it++) {
    00074     free((void *)(*it).first);
    00075   }
    00076   AILibraryList::iterator lit = this->library_list.begin();
    00077   for (; lit != this->library_list.end(); lit++) {
    00078     free((void *)(*lit).first);
    00079     delete (*lit).second;
    00080   }
    00081
    00082   delete this->info_dummy;
    00083 }
    00084
    00085 bool AIScanner::ImportLibrary(const char *library, const char *class_name, int version, HSQUIRRELVM vm, AIController *controller)
    00086 {
    00087   /* Internally we store libraries as 'library.version' */
    00088   char library_name[1024];
    00089   snprintf(library_name, sizeof(library_name), "%s.%d", library, version);
    00090   strtolower(library_name);
    00091
    00092   /* Check if the library + version exists */
    00093   AILibraryList::iterator iter = this->library_list.find(library_name);
    00094   if (iter == this->library_list.end()) {
    00095     char error[1024];
    00096
    00097     /* Now see if the version doesn't exist, or the library */
    00098     iter = this->library_list.find(library);
    00099     if (iter == this->library_list.end()) {
    00100       snprintf(error, sizeof(error), "couldn't find library '%s'", library);
    00101     } else {
    00102       snprintf(error, sizeof(error), "couldn't find library '%s' version %d. The latest version available is %d", library, version, (*iter).second->GetVersion());
    00103     }
    00104     sq_throwerror(vm, OTTD2SQ(error));
    00105     return false;
    00106   }
    00107
    00108   /* Get the current table/class we belong to */
    00109   HSQOBJECT parent;
    00110   sq_getstackobj(vm, 1, &parent);
    00111
    00112   char fake_class[1024];
    00113   int next_number;
    00114
    00115   if (!controller->LoadedLibrary(library_name, &next_number, &fake_class[0], sizeof(fake_class))) {
    00116     /* Create a new fake internal name */
    00117     snprintf(fake_class, sizeof(fake_class), "_internalNA%d", next_number);
    00118
    00119     /* Load the library in a 'fake' namespace, so we can link it to the name the user requested */
    00120     sq_pushroottable(vm);
    00121     sq_pushstring(vm, OTTD2SQ(fake_class), -1);
    00122     sq_newclass(vm, SQFalse);
    00123     /* Load the library */
    00124     if (!Squirrel::LoadScript(vm, (*iter).second->GetMainScript(), false)) {
    00125       char error[1024];
    00126       snprintf(error, sizeof(error), "there was a compile error when importing '%s' version %d", library, version);
    00127       sq_throwerror(vm, OTTD2SQ(error));
    00128       return false;
    00129     }
    00130     /* Create the fake class */
    00131     sq_newslot(vm, -3, SQFalse);
    00132     sq_pop(vm, 1);
    00133
    00134     controller->AddLoadedLibrary(library_name, fake_class);
    00135   }
    00136
    00137   /* Find the real class inside the fake class (like 'sets.Vector') */
    00138   sq_pushroottable(vm);
    00139   sq_pushstring(vm, OTTD2SQ(fake_class), -1);
    00140   if (SQ_FAILED(sq_get(vm, -2))) {
    00141     sq_throwerror(vm, _SC("internal error assigning library class"));
    00142     return false;
    00143   }
    00144   sq_pushstring(vm, OTTD2SQ((*iter).second->GetInstanceName()), -1);
    00145   if (SQ_FAILED(sq_get(vm, -2))) {
    00146     char error[1024];
    00147     snprintf(error, sizeof(error), "unable to find class '%s' in the library '%s' version %d", (*iter).second->GetInstanceName(), library, version);
    00148     sq_throwerror(vm, OTTD2SQ(error));
    00149     return false;
    00150   }
    00151   HSQOBJECT obj;
    00152   sq_getstackobj(vm, -1, &obj);
    00153   sq_pop(vm, 3);
    00154
    00155   if (StrEmpty(class_name)) {
    00156     sq_pushobject(vm, obj);
    00157     return true;
    00158   }
    00159
    00160   /* Now link the name the user wanted to our 'fake' class */
    00161   sq_pushobject(vm, parent);
    00162   sq_pushstring(vm, OTTD2SQ(class_name), -1);
    00163   sq_pushobject(vm, obj);
    00164   sq_newclass(vm, SQTrue);
    00165   sq_newslot(vm, -3, SQFalse);
    00166   sq_pop(vm, 1);
    00167
    00168   sq_pushobject(vm, obj);
    00169   return true;
    00170 }
    00171
    00172 void AIScanner::RegisterLibrary(AILibrary *library)
    00173 {
    00174   char library_name[1024];
    00175   snprintf(library_name, sizeof(library_name), "%s.%s.%d", library->GetCategory(), library->GetInstanceName(), library->GetVersion());
    00176   strtolower(library_name);
    00177
    00178   if (this->library_list.find(library_name) != this->library_list.end()) {
    00179     /* This AI was already registered */
    00180 #ifdef WIN32
    00181     /* Windows doesn't care about the case */
    00182     if (strcasecmp(this->library_list[library_name]->GetMainScript(), library->GetMainScript()) == 0) {
    00183 #else
    00184     if (strcmp(this->library_list[library_name]->GetMainScript(), library->GetMainScript()) == 0) {
    00185 #endif
    00186       delete library;
    00187       return;
    00188     }
    00189
    00190     DEBUG(ai, 0, "Registering two libraries with the same name and version");
    00191     DEBUG(ai, 0, "  1: %s", this->library_list[library_name]->GetMainScript());
    00192     DEBUG(ai, 0, "  2: %s", library->GetMainScript());
    00193     DEBUG(ai, 0, "The first is taking precedence.");
    00194
    00195     delete library;
    00196     return;
    00197   }
    00198
    00199   this->library_list[strdup(library_name)] = library;
    00200 }
    00201
    00202 void AIScanner::RegisterAI(AIInfo *info)
    00203 {
    00204   char ai_name[1024];
    00205   snprintf(ai_name, sizeof(ai_name), "%s.%d", info->GetName(), info->GetVersion());
    00206   strtolower(ai_name);
    00207
    00208   /* Check if GetShortName follows the rules */
    00209   if (strlen(info->GetShortName()) != 4) {
    00210     DEBUG(ai, 0, "The AI '%s' returned a string from GetShortName() which is not four characaters. Unable to load the AI.", info->GetName());
    00211     delete info;
    00212     return;
    00213   }
    00214
    00215   if (this->info_list.find(ai_name) != this->info_list.end()) {
    00216     /* This AI was already registered */
    00217 #ifdef WIN32
    00218     /* Windows doesn't care about the case */
    00219     if (strcasecmp(this->info_list[ai_name]->GetMainScript(), info->GetMainScript()) == 0) {
    00220 #else
    00221     if (strcmp(this->info_list[ai_name]->GetMainScript(), info->GetMainScript()) == 0) {
    00222 #endif
    00223       delete info;
    00224       return;
    00225     }
    00226
    00227     DEBUG(ai, 0, "Registering two AIs with the same name and version");
    00228     DEBUG(ai, 0, "  1: %s", this->info_list[ai_name]->GetMainScript());
    00229     DEBUG(ai, 0, "  2: %s", info->GetMainScript());
    00230     DEBUG(ai, 0, "The first is taking precedence.");
    00231
    00232     delete info;
    00233     return;
    00234   }
    00235
    00236   this->info_list[strdup(ai_name)] = info;
    00237
    00238   /* Add the AI to the 'unique' AI list, where only the highest version of the
    00239    *  AI is registered. */
    00240   snprintf(ai_name, sizeof(ai_name), "%s", info->GetName());
    00241   strtolower(ai_name);
    00242   if (this->info_single_list.find(ai_name) == this->info_single_list.end()) {
    00243     this->info_single_list[strdup(ai_name)] = info;
    00244   } else if (this->info_single_list[ai_name]->GetVersion() < info->GetVersion()) {
    00245     this->info_single_list[ai_name] = info;
    00246   }
    00247 }
    00248
    00249 AIInfo *AIScanner::SelectRandomAI() const
    00250 {
    00251   uint num_random_ais = 0;
    00252   for (AIInfoList::const_iterator it = this->info_single_list.begin(); it != this->info_single_list.end(); it++) {
    00253     if (it->second->UseAsRandomAI()) num_random_ais++;
    00254   }
    00255
    00256   if (num_random_ais == 0) {
    00257     DEBUG(ai, 0, "No suitable AI found, loading 'dummy' AI.");
    00258     return this->info_dummy;
    00259   }
    00260
    00261   /* Find a random AI */
    00262   uint pos;
    00263   if (_networking) {
    00264     pos = InteractiveRandomRange(num_random_ais);
    00265   } else {
    00266     pos = RandomRange(num_random_ais);
    00267   }
    00268
    00269   /* Find the Nth item from the array */
    00270   AIInfoList::const_iterator it = this->info_single_list.begin();
    00271   while (!it->second->UseAsRandomAI()) it++;
    00272   for (; pos > 0; pos--) {
    00273     it++;
    00274     while (!it->second->UseAsRandomAI()) it++;
    00275   }
    00276   return (*it).second;
    00277 }
    00278
    00279 AIInfo *AIScanner::FindInfo(const char *nameParam, int versionParam, bool force_exact_match)
    00280 {
    00281   if (this->info_list.size() == 0) return NULL;
    00282   if (nameParam == NULL) return NULL;
    00283
    00284   char ai_name[1024];
    00285   ttd_strlcpy(ai_name, nameParam, sizeof(ai_name));
    00286   strtolower(ai_name);
    00287
    00288   AIInfo *info = NULL;
    00289   int version = -1;
    00290
    00291   if (versionParam == -1) {
    00292     /* We want to load the latest version of this AI; so find it */
    00293     if (this->info_single_list.find(ai_name) != this->info_single_list.end()) return this->info_single_list[ai_name];
    00294
    00295     /* If we didn't find a match AI, maybe the user included a version */
    00296     char *e = strrchr(ai_name, '.');
    00297     if (e == NULL) return NULL;
    00298     *e = '\0';
    00299     e++;
    00300     versionParam = atoi(e);
    00301     /* Fall-through, like we were calling this function with a version */
    00302   }
    00303
    00304   if (force_exact_match) {
    00305     /* Try to find a direct 'name.version' match */
    00306     char ai_name_tmp[1024];
    00307     snprintf(ai_name_tmp, sizeof(ai_name_tmp), "%s.%d", ai_name, versionParam);
    00308     strtolower(ai_name_tmp);
    00309     if (this->info_list.find(ai_name_tmp) != this->info_list.end()) return this->info_list[ai_name_tmp];
    00310   }
    00311
    00312   /* See if there is a compatible AI which goes by that name, with the highest
    00313    *  version which allows loading the requested version */
    00314   AIInfoList::iterator it = this->info_list.begin();
    00315   for (; it != this->info_list.end(); it++) {
    00316     if (strcasecmp(ai_name, (*it).second->GetName()) == 0 && (*it).second->CanLoadFromVersion(versionParam) && (version == -1 || (*it).second->GetVersion() > version)) {
    00317       version = (*it).second->GetVersion();
    00318       info = (*it).second;
    00319     }
    00320   }
    00321
    00322   return info;
    00323 }
    00324
    00325 char *AIScanner::GetAIConsoleList(char *p, const char *last) const
    00326 {
    00327   p += seprintf(p, last, "List of AIs:\n");
    00328   AIInfoList::const_iterator it = this->info_list.begin();
    00329   for (; it != this->info_list.end(); it++) {
    00330     AIInfo *i = (*it).second;
    00331     p += seprintf(p, last, "%10s (v%d): %s\n", i->GetName(), i->GetVersion(), i->GetDescription());
    00332   }
    00333   p += seprintf(p, last, "\n");
    00334
    00335   return p;
    00336 }
    00337
    00338 #if defined(ENABLE_NETWORK)
    00339 #include "../network/network_content.h"
    00340 #include "../3rdparty/md5/md5.h"
    00341 #include "../tar_type.h"
    00342
    00344 struct AIFileChecksumCreator : FileScanner {
    00345   byte md5sum[16];
    00346
    00351   AIFileChecksumCreator()
    00352   {
    00353     memset(this->md5sum, 0, sizeof(this->md5sum));
    00354   }
    00355
    00356   /* Add the file and calculate the md5 sum. */
    00357   virtual bool AddFile(const char *filename, size_t basepath_length)
    00358   {
    00359     Md5 checksum;
    00360     uint8 buffer[1024];
    00361     size_t len, size;
    00362     byte tmp_md5sum[16];
    00363
    00364     /* Open the file ... */
    00365     FILE *f = FioFOpenFile(filename, "rb", DATA_DIR, &size);
    00366     if (f == NULL) return false;
    00367
    00368     /* ... calculate md5sum... */
    00369     while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
    00370       size -= len;
    00371       checksum.Append(buffer, len);
    00372     }
    00373     checksum.Finish(tmp_md5sum);
    00374
    00375     FioFCloseFile(f);
    00376
    00377     /* ... and xor it to the overall md5sum. */
    00378     for (uint i = 0; i < sizeof(md5sum); i++) this->md5sum[i] ^= tmp_md5sum[i];
    00379
    00380     return true;
    00381   }
    00382 };
    00383
    00392 static bool IsSameAI(const ContentInfo *ci, bool md5sum, AIFileInfo *info)
    00393 {
    00394   uint32 id = 0;
    00395   const char *str = info->GetShortName();
    00396   for (int j = 0; j < 4 && *str != '\0'; j++, str++) id |= *str << (8 * j);
    00397
    00398   if (id != ci->unique_id) return false;
    00399   if (!md5sum) return true;
    00400
    00401   AIFileChecksumCreator checksum;
    00402   char path[MAX_PATH];
    00403   strecpy(path, info->GetMainScript(), lastof(path));
    00404   /* There'll always be at least 2 path separator characters in an AI's
    00405    * main script name as the search algorithm requires the main script to
    00406    * be in a subdirectory of the AI directory; so ai/<path>/main.nut. */
    00407   *strrchr(path, PATHSEPCHAR) = '\0';
    00408   *strrchr(path, PATHSEPCHAR) = '\0';
    00409   TarList::iterator iter = _tar_list.find(path);
    00410
    00411   if (iter != _tar_list.end()) {
    00412     /* The main script is in a tar file, so find all files that
    00413      * are in the same tar and add them to the MD5 checksumming. */
    00414     TarFileList::iterator tar;
    00415     FOR_ALL_TARS(tar) {
    00416       /* Not in the same tar. */
    00417       if (tar->second.tar_filename != iter->first) continue;
    00418
    00419       /* Check the extension. */
    00420       const char *ext = strrchr(tar->first.c_str(), '.');
    00421       if (ext == NULL || strcasecmp(ext, ".nut") != 0) continue;
    00422
    00423       /* Create the full path name, */
    00424       seprintf(path, lastof(path), "%s%c%s", tar->second.tar_filename, PATHSEPCHAR, tar->first.c_str());
    00425       checksum.AddFile(path, 0);
    00426     }
    00427   } else {
    00428     /* Add the path sep char back when searching a directory, so we are
    00429      * in the actual directory. */
    00430     path[strlen(path)] = PATHSEPCHAR;
    00431     checksum.Scan(".nut", path);
    00432   }
    00433
    00434   return memcmp(ci->md5sum, checksum.md5sum, sizeof(ci->md5sum)) == 0;
    00435 }
    00436
    00443 bool AIScanner::HasAI(const ContentInfo *ci, bool md5sum)
    00444 {
    00445   switch (ci->type) {
    00446     case CONTENT_TYPE_AI:
    00447       for (AIInfoList::iterator it = this->info_list.begin(); it != this->info_list.end(); it++) {
    00448         if (IsSameAI(ci, md5sum, (*it).second)) return true;
    00449       }
    00450       return false;
    00451
    00452     case CONTENT_TYPE_AI_LIBRARY:
    00453       for (AILibraryList::iterator it = this->library_list.begin(); it != this->library_list.end(); it++) {
    00454         if (IsSameAI(ci, md5sum, (*it).second)) return true;
    00455       }
    00456       return false;
    00457
    00458     default:
    00459       NOT_REACHED();
    00460   }
    00461 }
    00462
    00469 /* static */ bool AI::HasAI(const ContentInfo *ci, bool md5sum)
    00470 {
    00471   return AI::ai_scanner->HasAI(ci, md5sum);
    00472 }
    00473
    00474 #endif /* ENABLE_NETWORK */