Fix #8316: Make sort industries by production and transported with a cargo filter...
[openttd-github.git] / src / game / game_scanner.cpp
blob1935b78137155238fed96951ef0e761138e0e27d
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 */
8 /** @file game_scanner.cpp allows scanning Game scripts */
10 #include "../stdafx.h"
12 #include "../script/squirrel_class.hpp"
13 #include "game_info.hpp"
14 #include "game_scanner.hpp"
16 #include "../safeguards.h"
19 void GameScannerInfo::Initialize()
21 ScriptScanner::Initialize("GSScanner");
24 void GameScannerInfo::GetScriptName(ScriptInfo *info, char *name, const char *last)
26 seprintf(name, last, "%s", info->GetName());
29 void GameScannerInfo::RegisterAPI(class Squirrel *engine)
31 GameInfo::RegisterAPI(engine);
34 GameInfo *GameScannerInfo::FindInfo(const char *nameParam, int versionParam, bool force_exact_match)
36 if (this->info_list.size() == 0) return nullptr;
37 if (nameParam == nullptr) return nullptr;
39 char game_name[1024];
40 strecpy(game_name, nameParam, lastof(game_name));
41 strtolower(game_name);
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;
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;
58 GameInfo *info = nullptr;
59 int version = -1;
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;
71 return info;
75 void GameScannerLibrary::Initialize()
77 ScriptScanner::Initialize("GSScanner");
80 void GameScannerLibrary::GetScriptName(ScriptInfo *info, char *name, const char *last)
82 GameLibrary *library = static_cast<GameLibrary *>(info);
83 seprintf(name, last, "%s.%s", library->GetCategory(), library->GetInstanceName());
86 void GameScannerLibrary::RegisterAPI(class Squirrel *engine)
88 GameLibrary::RegisterAPI(engine);
91 GameLibrary *GameScannerLibrary::FindLibrary(const char *library, int version)
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);
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;
102 return static_cast<GameLibrary *>((*it).second);