Codechange: Use std::vector for GRFConfig lists. (#10835)

This replaces the C-style custom managed linked-list and allows use of iterators etc.
This commit is contained in:
Peter Nelson
2025-01-31 17:09:09 +00:00
committed by GitHub
parent 40aeedeade
commit 5664b1e2f6
23 changed files with 248 additions and 384 deletions
+7 -14
View File
@@ -129,7 +129,7 @@ void CheckGameCompatibility(NetworkGameInfo &ngi)
ngi.compatible = ngi.version_compatible;
/* Check if we have all the GRFs on the client-system too. */
for (const GRFConfig *c = ngi.grfconfig; c != nullptr; c = c->next) {
for (const auto &c : ngi.grfconfig) {
if (c->status == GCS_NOT_FOUND) ngi.compatible = false;
}
}
@@ -148,7 +148,7 @@ void FillStaticNetworkServerGameInfo()
_network_game_info.map_height = Map::SizeY();
_network_game_info.landscape = _settings_game.game_creation.landscape;
_network_game_info.dedicated = _network_dedicated;
_network_game_info.grfconfig = _grfconfig;
CopyGRFConfigList(_network_game_info.grfconfig, _grfconfig, false);
_network_game_info.server_name = _settings_client.network.server_name;
_network_game_info.server_revision = GetNetworkRevisionString();
@@ -230,17 +230,11 @@ void SerializeNetworkGameInfo(Packet &p, const NetworkServerGameInfo &info, bool
* the GRFs that are needed, i.e. the ones that the server has
* selected in the NewGRF GUI and not the ones that are used due
* to the fact that they are in [newgrf-static] in openttd.cfg */
const GRFConfig *c;
uint count = 0;
/* Count number of GRFs to send information about */
for (c = info.grfconfig; c != nullptr; c = c->next) {
if (!HasBit(c->flags, GCF_STATIC)) count++;
}
uint count = std::ranges::count_if(info.grfconfig, [](const auto &c) { return !HasBit(c->flags, GCF_STATIC); });
p.Send_uint8 (count); // Send number of GRFs
/* Send actual GRF Identifications */
for (c = info.grfconfig; c != nullptr; c = c->next) {
for (const auto &c : info.grfconfig) {
if (HasBit(c->flags, GCF_STATIC)) continue;
SerializeGRFIdentifier(p, c->ident);
@@ -311,7 +305,7 @@ void DeserializeNetworkGameInfo(Packet &p, NetworkGameInfo &info, const GameInfo
static_assert(std::numeric_limits<uint8_t>::max() == NETWORK_MAX_GRF_COUNT);
uint num_grfs = p.Recv_uint8();
GRFConfig **dst = &info.grfconfig;
GRFConfigList &dst = info.grfconfig;
for (uint i = 0; i < num_grfs; i++) {
NamedGRFIdentifier grf;
switch (newgrf_serialisation) {
@@ -335,13 +329,12 @@ void DeserializeNetworkGameInfo(Packet &p, NetworkGameInfo &info, const GameInfo
NOT_REACHED();
}
GRFConfig *c = new GRFConfig();
auto c = std::make_unique<GRFConfig>();
c->ident = grf.ident;
HandleIncomingNetworkGameInfoGRFConfig(*c, grf.name);
/* Append GRFConfig to the list */
*dst = c;
dst = &c->next;
dst.push_back(std::move(c));
}
[[fallthrough]];
}
+2 -6
View File
@@ -243,17 +243,13 @@ bool ClientNetworkCoordinatorSocketHandler::Receive_GC_LISTING(Packet &p)
for (; servers > 0; servers--) {
std::string connection_string = p.Recv_string(NETWORK_HOSTNAME_PORT_LENGTH);
/* Read the NetworkGameInfo from the packet. */
NetworkGameInfo ngi = {};
DeserializeNetworkGameInfo(p, ngi, &this->newgrf_lookup_table);
/* Now we know the connection string, we can add it to our list. */
NetworkGameList *item = NetworkGameListAddItem(connection_string);
/* Clear any existing GRFConfig chain. */
ClearGRFConfigList(item->info.grfconfig);
/* Copy the new NetworkGameInfo info. */
item->info = ngi;
/* Read the NetworkGameInfo from the packet. */
DeserializeNetworkGameInfo(p, item->info, &this->newgrf_lookup_table);
/* Check for compatability with the client. */
CheckGameCompatibility(item->info);
/* Mark server as online. */
+1 -5
View File
@@ -72,8 +72,6 @@ void NetworkGameListRemoveItem(NetworkGameList *remove)
prev_item->next = remove->next;
}
/* Remove GRFConfig information */
ClearGRFConfigList(remove->info.grfconfig);
delete remove;
NetworkRebuildHostList();
@@ -99,8 +97,6 @@ void NetworkGameListRemoveExpired()
item = item->next;
*prev_item = item;
/* Remove GRFConfig information */
ClearGRFConfigList(remove->info.grfconfig);
delete remove;
} else {
prev_item = &item->next;
@@ -121,7 +117,7 @@ void NetworkAfterNewGRFScan()
/* Reset compatibility state */
item->info.compatible = item->info.version_compatible;
for (GRFConfig *c = item->info.grfconfig; c != nullptr; c = c->next) {
for (auto &c : item->info.grfconfig) {
assert(HasBit(c->flags, GCF_COPY));
const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, &c->ident.md5sum);
+2 -2
View File
@@ -585,8 +585,8 @@ public:
/* 'NewGRF Settings' button invisible if no NewGRF is used */
bool changed = false;
changed |= this->GetWidget<NWidgetStacked>(WID_NG_NEWGRF_SEL)->SetDisplayedPlane(sel == nullptr || sel->status != NGLS_ONLINE || sel->info.grfconfig == nullptr ? SZSP_NONE : 0);
changed |= this->GetWidget<NWidgetStacked>(WID_NG_NEWGRF_MISSING_SEL)->SetDisplayedPlane(sel == nullptr || sel->status != NGLS_ONLINE || sel->info.grfconfig == nullptr || !sel->info.version_compatible || sel->info.compatible ? SZSP_NONE : 0);
changed |= this->GetWidget<NWidgetStacked>(WID_NG_NEWGRF_SEL)->SetDisplayedPlane(sel == nullptr || sel->status != NGLS_ONLINE || sel->info.grfconfig.empty() ? SZSP_NONE : 0);
changed |= this->GetWidget<NWidgetStacked>(WID_NG_NEWGRF_MISSING_SEL)->SetDisplayedPlane(sel == nullptr || sel->status != NGLS_ONLINE || sel->info.grfconfig.empty() || !sel->info.version_compatible || sel->info.compatible ? SZSP_NONE : 0);
if (changed) {
this->ReInit();
return;
+4 -8
View File
@@ -411,21 +411,17 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::SendNewGRFCheck()
Debug(net, 9, "client[{}] status = NEWGRFS_CHECK", this->client_id);
this->status = STATUS_NEWGRFS_CHECK;
if (_grfconfig == nullptr) {
if (_grfconfig.empty()) {
/* There are no NewGRFs, so they're welcome. */
return this->SendWelcome();
}
auto p = std::make_unique<Packet>(this, PACKET_SERVER_CHECK_NEWGRFS, TCP_MTU);
const GRFConfig *c;
uint grf_count = 0;
for (c = _grfconfig; c != nullptr; c = c->next) {
if (!HasBit(c->flags, GCF_STATIC)) grf_count++;
}
uint grf_count = std::ranges::count_if(_grfconfig, [](const auto &c){ return !HasBit(c->flags, GCF_STATIC); });
p->Send_uint8 (grf_count);
for (c = _grfconfig; c != nullptr; c = c->next) {
for (const auto &c : _grfconfig) {
if (!HasBit(c->flags, GCF_STATIC)) SerializeGRFIdentifier(*p, c->ident);
}