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/>.
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 "../3rdparty/fmt/format.h"
18 #include "../safeguards.h"
21 void GameScannerInfo::Initialize()
23 ScriptScanner::Initialize("GSScanner");
26 std::string
GameScannerInfo::GetScriptName(ScriptInfo
*info
)
28 return info
->GetName();
31 void GameScannerInfo::RegisterAPI(class Squirrel
*engine
)
33 GameInfo::RegisterAPI(engine
);
36 GameInfo
*GameScannerInfo::FindInfo(const std::string
&name
, int version
, bool force_exact_match
)
38 if (this->info_list
.empty()) return nullptr;
39 if (name
.empty()) return nullptr;
42 /* We want to load the latest version of this Game script; so find it */
43 auto it
= this->info_single_list
.find(name
);
44 if (it
!= this->info_single_list
.end()) return static_cast<GameInfo
*>(it
->second
);
48 if (force_exact_match
) {
49 /* Try to find a direct 'name.version' match */
50 std::string name_with_version
= fmt::format("{}.{}", name
, version
);
51 auto it
= this->info_list
.find(name_with_version
);
52 if (it
!= this->info_list
.end()) return static_cast<GameInfo
*>(it
->second
);
56 GameInfo
*info
= nullptr;
57 int highest_version
= -1;
59 /* See if there is a compatible Game script which goes by that name, with the highest
60 * version which allows loading the requested version */
61 for (const auto &item
: this->info_list
) {
62 GameInfo
*i
= static_cast<GameInfo
*>(item
.second
);
63 if (StrEqualsIgnoreCase(name
, i
->GetName()) && i
->CanLoadFromVersion(version
) && (highest_version
== -1 || i
->GetVersion() > highest_version
)) {
64 highest_version
= item
.second
->GetVersion();
73 void GameScannerLibrary::Initialize()
75 ScriptScanner::Initialize("GSScanner");
78 std::string
GameScannerLibrary::GetScriptName(ScriptInfo
*info
)
80 GameLibrary
*library
= static_cast<GameLibrary
*>(info
);
81 return fmt::format("{}.{}", library
->GetCategory(), library
->GetInstanceName());
84 void GameScannerLibrary::RegisterAPI(class Squirrel
*engine
)
86 GameLibrary::RegisterAPI(engine
);
89 GameLibrary
*GameScannerLibrary::FindLibrary(const std::string
&library
, int version
)
91 /* Internally we store libraries as 'library.version' */
92 std::string library_name
= fmt::format("{}.{}", library
, version
);
94 /* Check if the library + version exists */
95 ScriptInfoList::iterator it
= this->info_list
.find(library_name
);
96 if (it
== this->info_list
.end()) return nullptr;
98 return static_cast<GameLibrary
*>((*it
).second
);