1
0
Fork 0

Codechange: Use cached name for all station/industry/town name formatting. (#10634)

This reuses an existing name caching mechanism to avoid "recalculating" names every time.
pull/10639/head
PeterN 2023-04-11 21:50:22 +01:00 committed by GitHub
parent d7f0c5d6d8
commit 3b2eb11fe8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 3 deletions

View File

@ -35,6 +35,7 @@
#include "game/game_text.hpp" #include "game/game_text.hpp"
#include "network/network_content_gui.h" #include "network/network_content_gui.h"
#include "newgrf_engine.h" #include "newgrf_engine.h"
#include "core/backup_type.hpp"
#include <stack> #include <stack>
#include "table/strings.h" #include "table/strings.h"
@ -1456,7 +1457,11 @@ static char *FormatString(char *buff, const char *str_arg, StringParameters *arg
const Industry *i = Industry::GetIfValid(args->GetInt32(SCC_INDUSTRY_NAME)); const Industry *i = Industry::GetIfValid(args->GetInt32(SCC_INDUSTRY_NAME));
if (i == nullptr) break; if (i == nullptr) break;
if (_scan_for_gender_data) { static bool use_cache = true;
if (use_cache) { // Use cached version if first call
AutoRestoreBackup cache_backup(use_cache, false);
buff = strecpy(buff, i->GetCachedName(), last);
} else if (_scan_for_gender_data) {
/* Gender is defined by the industry type. /* Gender is defined by the industry type.
* STR_FORMAT_INDUSTRY_NAME may have the town first, so it would result in the gender of the town name */ * STR_FORMAT_INDUSTRY_NAME may have the town first, so it would result in the gender of the town name */
StringParameters tmp_params(nullptr, 0, nullptr); StringParameters tmp_params(nullptr, 0, nullptr);
@ -1501,7 +1506,11 @@ static char *FormatString(char *buff, const char *str_arg, StringParameters *arg
break; break;
} }
if (!st->name.empty()) { static bool use_cache = true;
if (use_cache) { // Use cached version if first call
AutoRestoreBackup cache_backup(use_cache, false);
buff = strecpy(buff, st->GetCachedName(), last);
} else if (!st->name.empty()) {
int64 args_array[] = {(int64)(size_t)st->name.c_str()}; int64 args_array[] = {(int64)(size_t)st->name.c_str()};
StringParameters tmp_params(args_array); StringParameters tmp_params(args_array);
buff = GetStringWithArgs(buff, STR_JUST_RAW_STRING, &tmp_params, last); buff = GetStringWithArgs(buff, STR_JUST_RAW_STRING, &tmp_params, last);
@ -1531,7 +1540,11 @@ static char *FormatString(char *buff, const char *str_arg, StringParameters *arg
const Town *t = Town::GetIfValid(args->GetInt32(SCC_TOWN_NAME)); const Town *t = Town::GetIfValid(args->GetInt32(SCC_TOWN_NAME));
if (t == nullptr) break; if (t == nullptr) break;
if (!t->name.empty()) { static bool use_cache = true;
if (use_cache) { // Use cached version if first call
AutoRestoreBackup cache_backup(use_cache, false);
buff = strecpy(buff, t->GetCachedName(), last);
} else if (!t->name.empty()) {
int64 args_array[] = {(int64)(size_t)t->name.c_str()}; int64 args_array[] = {(int64)(size_t)t->name.c_str()};
StringParameters tmp_params(args_array); StringParameters tmp_params(args_array);
buff = GetStringWithArgs(buff, STR_JUST_RAW_STRING, &tmp_params, last); buff = GetStringWithArgs(buff, STR_JUST_RAW_STRING, &tmp_params, last);