diff --git a/src/industry.h b/src/industry.h index 4822976f24..662e9fdec7 100644 --- a/src/industry.h +++ b/src/industry.h @@ -64,6 +64,7 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> { PartOfSubsidyByte part_of_subsidy; ///< NOSAVE: is this industry a source/destination of a subsidy? StationList stations_near; ///< NOSAVE: List of nearby stations. + std::unique_ptr cached_name; ///< NOSAVE: Cache of the resolved name of the industry OwnerByte founder; ///< Founder of the industry Date construction_date; ///< Date of the construction of the industry @@ -159,10 +160,21 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> { memset(&counts, 0, sizeof(counts)); } + inline const char *GetCachedName() const + { + if (!this->cached_name) const_cast(this)->FillCachedName(); + return this->cached_name.get(); + } + +private: + void FillCachedName(); + protected: static uint16 counts[NUM_INDUSTRYTYPES]; ///< Number of industries per type ingame }; +void ClearAllIndustryCachedNames(); + void PlantRandomFarmField(const Industry *i); void ReleaseDisastersTargetingIndustry(IndustryID); diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp index b41888feaa..8d67a3c133 100644 --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -2280,6 +2280,25 @@ void Industry::RecomputeProductionMultipliers() } } +void Industry::FillCachedName() +{ + char buf[256]; + int64 args_array[] = { this->index }; + StringParameters tmp_params(args_array); + char *end = GetStringWithArgs(buf, STR_INDUSTRY_NAME, &tmp_params, lastof(buf)); + char *alloced = MallocT(end - buf + 1); + memcpy(alloced, buf, end - buf + 1); + this->cached_name.reset(alloced); +} + +void ClearAllIndustryCachedNames() +{ + Industry *ind; + + FOR_ALL_INDUSTRIES(ind) { + ind->cached_name.reset(); + } +} /** * Set the #probability and #min_number fields for the industry type \a it for a running game. diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index ce1192c7ef..3f03fd7c97 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -1253,19 +1253,7 @@ protected: /** Sort industries by name */ static bool IndustryNameSorter(const Industry * const &a, const Industry * const &b) { - static char buf_cache[96]; - static char buf[96]; - - SetDParam(0, a->index); - GetString(buf, STR_INDUSTRY_NAME, lastof(buf)); - - if (b != last_industry) { - last_industry = b; - SetDParam(0, b->index); - GetString(buf_cache, STR_INDUSTRY_NAME, lastof(buf_cache)); - } - - return strnatcmp(buf, buf_cache) < 0; // Sort by name (natural sorting). + return strnatcmp(a->GetCachedName(), b->GetCachedName()) < 0; // Sort by name (natural sorting). } /** Sort industries by type and name */ diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 83f38bbfc3..d4f9406edc 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -236,6 +236,7 @@ void ClearAllCachedNames() { ClearAllStationCachedNames(); ClearAllTownCachedNames(); + ClearAllIndustryCachedNames(); } /** diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index a58ec7250d..7d41c8c73e 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -2888,6 +2888,7 @@ CommandCost CmdRenameTown(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 t->UpdateVirtCoord(); InvalidateWindowData(WC_TOWN_DIRECTORY, 0, 1); ClearAllStationCachedNames(); + ClearAllIndustryCachedNames(); UpdateAllStationVirtCoords(); } return CommandCost();