1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-20 02:42:42 +01:00

Codechange: Use std::unordered_map for NewGRF language_map.

NewGRFs only use a small subset of the available language IDs. Using an unordered_map allows only the reference languages to have space allocated.

This avoids manual new/delete of array.
This commit is contained in:
Peter Nelson
2024-03-10 19:48:44 +00:00
committed by Peter Nelson
parent fee73f3253
commit 66044472d7
2 changed files with 8 additions and 10 deletions

View File

@@ -2644,7 +2644,12 @@ static ChangeInfoResult TownHouseChangeInfo(uint hid, int numinfo, int prop, Byt
{
/* LanguageID "MAX_LANG", i.e. 7F is any. This language can't have a gender/case mapping, but has to be handled gracefully. */
const GRFFile *grffile = GetFileByGRFID(grfid);
return (grffile != nullptr && grffile->language_map != nullptr && language_id < MAX_LANG) ? &grffile->language_map[language_id] : nullptr;
if (grffile == nullptr) return nullptr;
auto it = grffile->language_map.find(language_id);
if (it == std::end(grffile->language_map)) return nullptr;
return &it->second;
}
/**
@@ -2859,8 +2864,6 @@ static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, By
break;
}
if (_cur.grffile->language_map == nullptr) _cur.grffile->language_map = new LanguageMap[MAX_LANG];
if (prop == 0x15) {
uint plural_form = buf->ReadByte();
if (plural_form >= LANGUAGE_MAX_PLURAL) {
@@ -8933,11 +8936,6 @@ GRFFile::GRFFile(const GRFConfig *config)
this->param_end = config->num_params;
}
GRFFile::~GRFFile()
{
delete[] this->language_map;
}
/**
* Find first cargo label that exists and is active from a list of cargo labels.
* @param labels List of cargo labels.