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