1
0
Fork 0

Codechange: Remove redundant NewsStringData data. (#12983)

Since SetDParamStr() always owns a copy of the string, there is no need to make another copy of it to keep it around while the news item exists.

This also fixes a leak in `CmdIndustrySetProduction` as the allocated data wasn't passed to AddIndustryNewsItem.
pull/12985/head
Peter Nelson 2024-10-08 19:48:55 +01:00 committed by GitHub
parent d53b681cf7
commit 446db2c826
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 14 additions and 23 deletions

View File

@ -2207,10 +2207,8 @@ CommandCost CmdIndustrySetProduction(DoCommandFlag flags, IndustryID ind_id, uin
}
/* Set parameters of news string */
NewsAllocatedData *data = nullptr;
if (str == STR_NEWS_CUSTOM_ITEM) {
NewsStringData *news = new NewsStringData(custom_news);
SetDParamStr(0, news->string);
SetDParamStr(0, custom_news);
} else if (str > STR_LAST_STRINGID) {
SetDParam(0, STR_TOWN_NAME);
SetDParam(1, ind->town->index);
@ -2218,7 +2216,7 @@ CommandCost CmdIndustrySetProduction(DoCommandFlag flags, IndustryID ind_id, uin
} else {
SetDParam(0, ind->index);
}
AddIndustryNewsItem(str, nt, ind->index, data);
AddIndustryNewsItem(str, nt, ind->index);
}
}

View File

@ -33,6 +33,7 @@
#include "group_gui.h"
#include "zoom_func.h"
#include "news_cmd.h"
#include "news_func.h"
#include "timer/timer.h"
#include "timer/timer_window.h"
#include "timer/timer_game_calendar.h"
@ -890,9 +891,8 @@ CommandCost CmdCustomNewsItem(DoCommandFlag flags, NewsType type, NewsReferenceT
if (company != INVALID_OWNER && company != _local_company) return CommandCost();
if (flags & DC_EXEC) {
NewsStringData *news = new NewsStringData(text);
SetDParamStr(0, news->string);
AddNewsItem(STR_NEWS_CUSTOM_ITEM, type, NF_NORMAL, reftype1, reference, NR_NONE, UINT32_MAX, news);
SetDParamStr(0, text);
AddNewsItem(STR_NEWS_CUSTOM_ITEM, type, NF_NORMAL, reftype1, reference, NR_NONE, UINT32_MAX);
}
return CommandCost();

View File

@ -144,12 +144,6 @@ struct NewsItem {
NewsItem(StringID string_id, NewsType type, NewsFlag flags, NewsReferenceType reftype1, uint32_t ref1, NewsReferenceType reftype2, uint32_t ref2, const NewsAllocatedData *data);
};
/** Container for a single string to be passed as NewsAllocatedData. */
struct NewsStringData : NewsAllocatedData {
std::string string; ///< The string to retain.
NewsStringData(const std::string &str) : string(str) {}
};
/**
* Data that needs to be stored for company news messages.
* The problem with company news messages are the custom name

View File

@ -48,17 +48,16 @@ void Subsidy::AwardTo(CompanyID company)
this->remaining = _settings_game.difficulty.subsidy_duration * CalendarTime::MONTHS_IN_YEAR;
SetDParam(0, company);
NewsStringData *company_name = new NewsStringData(GetString(STR_COMPANY_NAME));
std::string company_name = GetString(STR_COMPANY_NAME);
/* Add a news item */
std::pair<NewsReferenceType, NewsReferenceType> reftype = SetupSubsidyDecodeParam(this, SubsidyDecodeParamType::NewsAwarded, 1);
SetDParamStr(0, company_name->string);
SetDParamStr(0, company_name);
AddNewsItem(
STR_NEWS_SERVICE_SUBSIDY_AWARDED_HALF + _settings_game.difficulty.subsidy_multiplier,
NT_SUBSIDIES, NF_NORMAL,
reftype.first, this->src, reftype.second, this->dst,
company_name
reftype.first, this->src, reftype.second, this->dst
);
AI::BroadcastNewEvent(new ScriptEventSubsidyAwarded(this->index));
Game::NewEvent(new ScriptEventSubsidyAwarded(this->index));

View File

@ -2192,12 +2192,12 @@ std::tuple<CommandCost, Money, TownID> CmdFoundTown(DoCommandFlag flags, TileInd
AddTileNewsItem(STR_NEWS_NEW_TOWN_UNSPONSORED, NT_INDUSTRY_OPEN, tile);
} else {
SetDParam(0, _current_company);
NewsStringData *company_name = new NewsStringData(GetString(STR_COMPANY_NAME));
std::string company_name = GetString(STR_COMPANY_NAME);
SetDParamStr(0, company_name->string);
SetDParamStr(0, company_name);
SetDParam(1, t->index);
AddTileNewsItem(STR_NEWS_NEW_TOWN, NT_INDUSTRY_OPEN, tile, company_name);
AddTileNewsItem(STR_NEWS_NEW_TOWN, NT_INDUSTRY_OPEN, tile);
}
AI::BroadcastNewEvent(new ScriptEventTownFounded(t->index));
Game::NewEvent(new ScriptEventTownFounded(t->index));
@ -3342,14 +3342,14 @@ static CommandCost TownActionRoadRebuild(Town *t, DoCommandFlag flags)
t->road_build_months = 6;
SetDParam(0, _current_company);
NewsStringData *company_name = new NewsStringData(GetString(STR_COMPANY_NAME));
std::string company_name = GetString(STR_COMPANY_NAME);
SetDParam(0, t->index);
SetDParamStr(1, company_name->string);
SetDParamStr(1, company_name);
AddNewsItem(
TimerGameEconomy::UsingWallclockUnits() ? STR_NEWS_ROAD_REBUILDING_MINUTES : STR_NEWS_ROAD_REBUILDING_MONTHS,
NT_GENERAL, NF_NORMAL, NR_TOWN, t->index, NR_NONE, UINT32_MAX, company_name);
NT_GENERAL, NF_NORMAL, NR_TOWN, t->index, NR_NONE, UINT32_MAX);
AI::BroadcastNewEvent(new ScriptEventRoadReconstruction((ScriptCompany::CompanyID)(Owner)_current_company, t->index));
Game::NewEvent(new ScriptEventRoadReconstruction((ScriptCompany::CompanyID)(Owner)_current_company, t->index));
}