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.
pull/741/head
Peter Nelson 7 months ago committed by Peter Nelson
parent fee73f3253
commit 66044472d7

@ -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.

@ -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<uint8_t, LanguageMap> 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

Loading…
Cancel
Save