OpenTTD Source 20241224-master-gf74b0cf984
game_scanner.cpp
Go to the documentation of this file.
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
10#include "../stdafx.h"
11
12#include "../script/squirrel_class.hpp"
13#include "game_info.hpp"
14#include "game_scanner.hpp"
15
16#include "../3rdparty/fmt/format.h"
17
18#include "../safeguards.h"
19
20
21void GameScannerInfo::Initialize()
22{
23 ScriptScanner::Initialize("GSScanner");
24}
25
27{
28 return info->GetName();
29}
30
35
36GameInfo *GameScannerInfo::FindInfo(const std::string &name, int version, bool force_exact_match)
37{
38 if (this->info_list.empty()) return nullptr;
39 if (name.empty()) return nullptr;
40
41 if (version == -1) {
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);
45 return nullptr;
46 }
47
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);
53 return nullptr;
54 }
55
56 GameInfo *info = nullptr;
57 int highest_version = -1;
58
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();
65 info = i;
66 }
67 }
68
69 return info;
70}
71
72
73void GameScannerLibrary::Initialize()
74{
75 ScriptScanner::Initialize("GSScanner");
76}
77
79{
80 GameLibrary *library = static_cast<GameLibrary *>(info);
81 return fmt::format("{}.{}", library->GetCategory(), library->GetInstanceName());
82}
83
88
89GameLibrary *GameScannerLibrary::FindLibrary(const std::string &library, int version)
90{
91 /* Internally we store libraries as 'library.version' */
92 std::string library_name = fmt::format("{}.{}", library, version);
93
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;
97
98 return static_cast<GameLibrary *>((*it).second);
99}
All static information from an Game like name, version, etc.
Definition game_info.hpp:16
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
Definition game_info.cpp:34
bool CanLoadFromVersion(int version) const
Check if we can start this Game.
Definition game_info.cpp:94
All static information from an Game library like name, version, etc.
Definition game_info.hpp:49
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
const std::string & GetCategory() const
Get the category this library is in.
Definition game_info.hpp:66
void RegisterAPI(class Squirrel *engine) override
Register the API for this ScriptInfo.
std::string GetScriptName(ScriptInfo *info) override
Get the script name how to store the script in memory.
class GameInfo * FindInfo(const std::string &name, int version, bool force_exact_match)
Check if we have a game by name and version available in our list.
class GameLibrary * FindLibrary(const std::string &library, int version)
Find a library in the pool.
void RegisterAPI(class Squirrel *engine) override
Register the API for this ScriptInfo.
std::string GetScriptName(ScriptInfo *info) override
Get the script name how to store the script in memory.
All static information from an Script like name, version, etc.
const std::string & GetInstanceName() const
Get the name of the instance of the script to create.
const std::string & GetName() const
Get the Name of the script.
int GetVersion() const
Get the version of the script.
class Squirrel * engine
The engine we're scanning with.
ScriptInfoList info_list
The list of all script.
ScriptInfoList info_single_list
The list of all unique script. The best script (highest version) is shown.
GameInfo keeps track of all information of an Game, like Author, Description, ...
declarations of the class for Game scanner
bool StrEqualsIgnoreCase(const std::string_view str1, const std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.
Definition string.cpp:347