game_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 "game_info.hpp"
00020 #include "game_scanner.hpp"
00021 #include "../script/api/script_controller.hpp"
00022 
00023 
00024 void GameScannerInfo::Initialize()
00025 {
00026   ScriptScanner::Initialize("GSScanner");
00027 }
00028 
00029 void GameScannerInfo::GetScriptName(ScriptInfo *info, char *name, int len)
00030 {
00031   snprintf(name, len, "%s", info->GetName());
00032 }
00033 
00034 void GameScannerInfo::RegisterAPI(class Squirrel *engine)
00035 {
00036   GameInfo::RegisterAPI(engine);
00037 }
00038 
00039 GameInfo *GameScannerInfo::FindInfo(const char *nameParam, int versionParam, bool force_exact_match)
00040 {
00041   if (this->info_list.size() == 0) return NULL;
00042   if (nameParam == NULL) return NULL;
00043 
00044   char game_name[1024];
00045   ttd_strlcpy(game_name, nameParam, sizeof(game_name));
00046   strtolower(game_name);
00047 
00048   GameInfo *info = NULL;
00049   int version = -1;
00050 
00051   if (versionParam == -1) {
00052     /* We want to load the latest version of this Game script; so find it */
00053     if (this->info_single_list.find(game_name) != this->info_single_list.end()) return static_cast<GameInfo *>(this->info_single_list[game_name]);
00054 
00055     /* If we didn't find a match Game script, maybe the user included a version */
00056     char *e = strrchr(game_name, '.');
00057     if (e == NULL) return NULL;
00058     *e = '\0';
00059     e++;
00060     versionParam = atoi(e);
00061     /* FALL THROUGH, like we were calling this function with a version. */
00062   }
00063 
00064   if (force_exact_match) {
00065     /* Try to find a direct 'name.version' match */
00066     char game_name_tmp[1024];
00067     snprintf(game_name_tmp, sizeof(game_name_tmp), "%s.%d", game_name, versionParam);
00068     strtolower(game_name_tmp);
00069     if (this->info_list.find(game_name_tmp) != this->info_list.end()) return static_cast<GameInfo *>(this->info_list[game_name_tmp]);
00070   }
00071 
00072   /* See if there is a compatible Game script which goes by that name, with the highest
00073    *  version which allows loading the requested version */
00074   ScriptInfoList::iterator it = this->info_list.begin();
00075   for (; it != this->info_list.end(); it++) {
00076     GameInfo *i = static_cast<GameInfo *>((*it).second);
00077     if (strcasecmp(game_name, i->GetName()) == 0 && i->CanLoadFromVersion(versionParam) && (version == -1 || i->GetVersion() > version)) {
00078       version = (*it).second->GetVersion();
00079       info = i;
00080     }
00081   }
00082 
00083   return info;
00084 }
00085 
00086 
00087 void GameScannerLibrary::Initialize()
00088 {
00089   ScriptScanner::Initialize("GSScanner");
00090 }
00091 
00092 void GameScannerLibrary::GetScriptName(ScriptInfo *info, char *name, int len)
00093 {
00094   GameLibrary *library = static_cast<GameLibrary *>(info);
00095   snprintf(name, len, "%s.%s", library->GetCategory(), library->GetInstanceName());
00096 }
00097 
00098 void GameScannerLibrary::RegisterAPI(class Squirrel *engine)
00099 {
00100   GameLibrary::RegisterAPI(engine);
00101 }
00102 
00103 GameLibrary *GameScannerLibrary::FindLibrary(const char *library, int version)
00104 {
00105   /* Internally we store libraries as 'library.version' */
00106   char library_name[1024];
00107   snprintf(library_name, sizeof(library_name), "%s.%d", library, version);
00108   strtolower(library_name);
00109 
00110   /* Check if the library + version exists */
00111   ScriptInfoList::iterator iter = this->info_list.find(library_name);
00112   if (iter == this->info_list.end()) return NULL;
00113 
00114   return static_cast<GameLibrary *>((*iter).second);
00115 }