1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
8 /** @file game_scanner.cpp allows scanning Game scripts */
9
10 #include "../stdafx.h"
11
12 #include "../script/squirrel_class.hpp"
13 #include "game_info.hpp"
14 #include "game_scanner.hpp"
15
16 #include "../safeguards.h"
17
18
Initialize()19 void GameScannerInfo::Initialize()
20 {
21 ScriptScanner::Initialize("GSScanner");
22 }
23
GetScriptName(ScriptInfo * info,char * name,const char * last)24 void GameScannerInfo::GetScriptName(ScriptInfo *info, char *name, const char *last)
25 {
26 seprintf(name, last, "%s", info->GetName());
27 }
28
RegisterAPI(class Squirrel * engine)29 void GameScannerInfo::RegisterAPI(class Squirrel *engine)
30 {
31 GameInfo::RegisterAPI(engine);
32 }
33
FindInfo(const char * nameParam,int versionParam,bool force_exact_match)34 GameInfo *GameScannerInfo::FindInfo(const char *nameParam, int versionParam, bool force_exact_match)
35 {
36 if (this->info_list.size() == 0) return nullptr;
37 if (nameParam == nullptr) return nullptr;
38
39 char game_name[1024];
40 strecpy(game_name, nameParam, lastof(game_name));
41 strtolower(game_name);
42
43 if (versionParam == -1) {
44 /* We want to load the latest version of this Game script; so find it */
45 if (this->info_single_list.find(game_name) != this->info_single_list.end()) return static_cast<GameInfo *>(this->info_single_list[game_name]);
46 return nullptr;
47 }
48
49 if (force_exact_match) {
50 /* Try to find a direct 'name.version' match */
51 char game_name_tmp[1024];
52 seprintf(game_name_tmp, lastof(game_name_tmp), "%s.%d", game_name, versionParam);
53 strtolower(game_name_tmp);
54 if (this->info_list.find(game_name_tmp) != this->info_list.end()) return static_cast<GameInfo *>(this->info_list[game_name_tmp]);
55 return nullptr;
56 }
57
58 GameInfo *info = nullptr;
59 int version = -1;
60
61 /* See if there is a compatible Game script which goes by that name, with the highest
62 * version which allows loading the requested version */
63 for (const auto &item : this->info_list) {
64 GameInfo *i = static_cast<GameInfo *>(item.second);
65 if (strcasecmp(game_name, i->GetName()) == 0 && i->CanLoadFromVersion(versionParam) && (version == -1 || i->GetVersion() > version)) {
66 version = item.second->GetVersion();
67 info = i;
68 }
69 }
70
71 return info;
72 }
73
74
Initialize()75 void GameScannerLibrary::Initialize()
76 {
77 ScriptScanner::Initialize("GSScanner");
78 }
79
GetScriptName(ScriptInfo * info,char * name,const char * last)80 void GameScannerLibrary::GetScriptName(ScriptInfo *info, char *name, const char *last)
81 {
82 GameLibrary *library = static_cast<GameLibrary *>(info);
83 seprintf(name, last, "%s.%s", library->GetCategory(), library->GetInstanceName());
84 }
85
RegisterAPI(class Squirrel * engine)86 void GameScannerLibrary::RegisterAPI(class Squirrel *engine)
87 {
88 GameLibrary::RegisterAPI(engine);
89 }
90
FindLibrary(const char * library,int version)91 GameLibrary *GameScannerLibrary::FindLibrary(const char *library, int version)
92 {
93 /* Internally we store libraries as 'library.version' */
94 char library_name[1024];
95 seprintf(library_name, lastof(library_name), "%s.%d", library, version);
96 strtolower(library_name);
97
98 /* Check if the library + version exists */
99 ScriptInfoList::iterator it = this->info_list.find(library_name);
100 if (it == this->info_list.end()) return nullptr;
101
102 return static_cast<GameLibrary *>((*it).second);
103 }
104