1
0
mirror of https://github.com/OpenTTD/OpenTTD.git synced 2025-09-01 02:49:10 +00:00

Codechange: use std::string as std::map key, instead of stredup string

This commit is contained in:
Rubidium
2023-05-05 11:01:01 +02:00
committed by rubidium42
parent 72082aa7d3
commit bc389a86c9
10 changed files with 54 additions and 111 deletions

View File

@@ -92,39 +92,35 @@ AIInfo *AIScannerInfo::SelectRandomAI() const
#undef GetAIInfo
}
AIInfo *AIScannerInfo::FindInfo(const char *nameParam, int versionParam, bool force_exact_match)
AIInfo *AIScannerInfo::FindInfo(const char *name, int version, bool force_exact_match)
{
if (this->info_list.size() == 0) return nullptr;
if (nameParam == nullptr) return nullptr;
if (name == nullptr) return nullptr;
char ai_name[1024];
strecpy(ai_name, nameParam, lastof(ai_name));
strtolower(ai_name);
if (versionParam == -1) {
if (version == -1) {
/* We want to load the latest version of this AI; so find it */
if (this->info_single_list.find(ai_name) != this->info_single_list.end()) return static_cast<AIInfo *>(this->info_single_list[ai_name]);
auto it = this->info_single_list.find(name);
if (it != this->info_single_list.end()) return static_cast<AIInfo *>(it->second);
return nullptr;
}
if (force_exact_match) {
/* Try to find a direct 'name.version' match */
char ai_name_tmp[1024];
seprintf(ai_name_tmp, lastof(ai_name_tmp), "%s.%d", ai_name, versionParam);
strtolower(ai_name_tmp);
if (this->info_list.find(ai_name_tmp) != this->info_list.end()) return static_cast<AIInfo *>(this->info_list[ai_name_tmp]);
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<AIInfo *>(it->second);
return nullptr;
}
AIInfo *info = nullptr;
int version = -1;
int highest_version = -1;
/* See if there is a compatible AI which goes by that name, with the highest
* version which allows loading the requested version */
for (const auto &item : this->info_list) {
AIInfo *i = static_cast<AIInfo *>(item.second);
if (StrEqualsIgnoreCase(ai_name, i->GetName()) && i->CanLoadFromVersion(versionParam) && (version == -1 || i->GetVersion() > version)) {
version = item.second->GetVersion();
if (StrEqualsIgnoreCase(name, i->GetName()) && i->CanLoadFromVersion(version) && (highest_version == -1 || i->GetVersion() > highest_version)) {
highest_version = item.second->GetVersion();
info = i;
}
}
@@ -152,9 +148,7 @@ void AIScannerLibrary::RegisterAPI(class Squirrel *engine)
AILibrary *AIScannerLibrary::FindLibrary(const char *library, int version)
{
/* Internally we store libraries as 'library.version' */
char library_name[1024];
seprintf(library_name, lastof(library_name), "%s.%d", library, version);
strtolower(library_name);
std::string library_name = fmt::format("{}.{}", library, version);
/* Check if the library + version exists */
ScriptInfoList::iterator it = this->info_list.find(library_name);

View File

@@ -27,12 +27,12 @@ public:
/**
* Check if we have an AI by name and version available in our list.
* @param nameParam The name of the AI.
* @param versionParam The version of the AI, or -1 if you want the latest.
* @param name The name of the AI.
* @param version The version of the AI, or -1 if you want the latest.
* @param force_exact_match Only match name+version, never latest.
* @return nullptr if no match found, otherwise the AI that matched.
*/
class AIInfo *FindInfo(const char *nameParam, int versionParam, bool force_exact_match);
class AIInfo *FindInfo(const char *name, int version, bool force_exact_match);
/**
* Set the Dummy AI.