From 66044472d7b34bc358ccea744f98804376c7ea76 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sun, 10 Mar 2024 19:48:44 +0000 Subject: [PATCH] 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. --- src/newgrf.cpp | 14 ++++++-------- src/newgrf.h | 4 ++-- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 92ccd79b3b..d547674e68 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -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. diff --git a/src/newgrf.h b/src/newgrf.h index 18d643a14b..f67f802320 100644 --- a/src/newgrf.h +++ b/src/newgrf.h @@ -14,6 +14,7 @@ #include "rail_type.h" #include "road_type.h" #include "fileio_type.h" +#include "newgrf_text_type.h" #include "core/bitmath_func.hpp" #include "core/alloc_type.hpp" #include "core/mem_func.hpp" @@ -140,7 +141,7 @@ struct GRFFile : ZeroedMemoryAllocator { CanalProperties canal_local_properties[CF_END]; ///< Canal properties as set by this NewGRF - struct LanguageMap *language_map; ///< Mappings related to the languages. + std::unordered_map language_map; ///< Mappings related to the languages. int traininfo_vehicle_pitch; ///< Vertical offset for drawing train images in depot GUI and vehicle details uint traininfo_vehicle_width; ///< Width (in pixels) of a 8/8 train vehicle in depot GUI and vehicle details @@ -149,7 +150,6 @@ struct GRFFile : ZeroedMemoryAllocator { PriceMultipliers price_base_multipliers; ///< Price base multipliers as set by the grf. GRFFile(const struct GRFConfig *config); - ~GRFFile(); /** Get GRF Parameter with range checking */ uint32_t GetParam(uint number) const