2011-12-19 20:55:56 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file game_scanner.cpp allows scanning Game scripts */
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
|
|
|
|
#include "../script/squirrel_class.hpp"
|
|
|
|
#include "game_info.hpp"
|
|
|
|
#include "game_scanner.hpp"
|
2023-07-20 15:34:59 +00:00
|
|
|
#include "../core/format.hpp"
|
2023-05-05 12:00:00 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../safeguards.h"
|
|
|
|
|
2011-12-19 20:55:56 +00:00
|
|
|
|
|
|
|
void GameScannerInfo::Initialize()
|
|
|
|
{
|
|
|
|
ScriptScanner::Initialize("GSScanner");
|
|
|
|
}
|
|
|
|
|
2023-05-05 12:00:00 +00:00
|
|
|
std::string GameScannerInfo::GetScriptName(ScriptInfo *info)
|
2011-12-19 20:55:56 +00:00
|
|
|
{
|
2023-05-05 12:00:00 +00:00
|
|
|
return info->GetName();
|
2011-12-19 20:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GameScannerInfo::RegisterAPI(class Squirrel *engine)
|
|
|
|
{
|
|
|
|
GameInfo::RegisterAPI(engine);
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:53:34 +00:00
|
|
|
GameInfo *GameScannerInfo::FindInfo(const std::string &name, int version, bool force_exact_match)
|
2011-12-19 20:55:56 +00:00
|
|
|
{
|
2023-10-20 18:09:58 +00:00
|
|
|
if (this->info_list.empty()) return nullptr;
|
2023-05-05 07:53:34 +00:00
|
|
|
if (name.empty()) return nullptr;
|
2011-12-19 20:55:56 +00:00
|
|
|
|
2023-05-05 09:01:01 +00:00
|
|
|
if (version == -1) {
|
2011-12-19 20:55:56 +00:00
|
|
|
/* We want to load the latest version of this Game script; so find it */
|
2023-05-05 09:01:01 +00:00
|
|
|
auto it = this->info_single_list.find(name);
|
|
|
|
if (it != this->info_single_list.end()) return static_cast<GameInfo *>(it->second);
|
2020-12-25 14:22:40 +00:00
|
|
|
return nullptr;
|
2011-12-19 20:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (force_exact_match) {
|
|
|
|
/* Try to find a direct 'name.version' match */
|
2023-05-05 09:01:01 +00:00
|
|
|
std::string name_with_version = fmt::format("{}.{}", name, version);
|
|
|
|
auto it = this->info_list.find(name_with_version);
|
|
|
|
if (it != this->info_list.end()) return static_cast<GameInfo *>(it->second);
|
2020-12-25 14:22:40 +00:00
|
|
|
return nullptr;
|
2011-12-19 20:55:56 +00:00
|
|
|
}
|
|
|
|
|
2020-12-25 14:22:40 +00:00
|
|
|
GameInfo *info = nullptr;
|
2023-05-05 09:01:01 +00:00
|
|
|
int highest_version = -1;
|
2020-12-25 14:22:40 +00:00
|
|
|
|
2011-12-19 20:55:56 +00:00
|
|
|
/* See if there is a compatible Game script which goes by that name, with the highest
|
|
|
|
* version which allows loading the requested version */
|
2020-12-25 18:38:18 +00:00
|
|
|
for (const auto &item : this->info_list) {
|
|
|
|
GameInfo *i = static_cast<GameInfo *>(item.second);
|
2023-05-05 09:01:01 +00:00
|
|
|
if (StrEqualsIgnoreCase(name, i->GetName()) && i->CanLoadFromVersion(version) && (highest_version == -1 || i->GetVersion() > highest_version)) {
|
|
|
|
highest_version = item.second->GetVersion();
|
2011-12-19 20:55:56 +00:00
|
|
|
info = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
2011-12-19 20:56:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
void GameScannerLibrary::Initialize()
|
|
|
|
{
|
|
|
|
ScriptScanner::Initialize("GSScanner");
|
|
|
|
}
|
|
|
|
|
2023-05-05 12:00:00 +00:00
|
|
|
std::string GameScannerLibrary::GetScriptName(ScriptInfo *info)
|
2011-12-19 20:56:59 +00:00
|
|
|
{
|
|
|
|
GameLibrary *library = static_cast<GameLibrary *>(info);
|
2023-05-05 12:00:00 +00:00
|
|
|
return fmt::format("{}.{}", library->GetCategory(), library->GetInstanceName());
|
2011-12-19 20:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GameScannerLibrary::RegisterAPI(class Squirrel *engine)
|
|
|
|
{
|
|
|
|
GameLibrary::RegisterAPI(engine);
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:53:34 +00:00
|
|
|
GameLibrary *GameScannerLibrary::FindLibrary(const std::string &library, int version)
|
2011-12-19 20:56:59 +00:00
|
|
|
{
|
|
|
|
/* Internally we store libraries as 'library.version' */
|
2023-05-05 09:01:01 +00:00
|
|
|
std::string library_name = fmt::format("{}.{}", library, version);
|
2011-12-19 20:56:59 +00:00
|
|
|
|
|
|
|
/* Check if the library + version exists */
|
2020-12-25 18:38:18 +00:00
|
|
|
ScriptInfoList::iterator it = this->info_list.find(library_name);
|
|
|
|
if (it == this->info_list.end()) return nullptr;
|
2011-12-19 20:56:59 +00:00
|
|
|
|
2020-12-25 18:38:18 +00:00
|
|
|
return static_cast<GameLibrary *>((*it).second);
|
2011-12-19 20:56:59 +00:00
|
|
|
}
|