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