Codechange: [ContentInfo] Use a vector for dependencies instead of custom allocation

This commit is contained in:
rubidium42 2021-05-30 12:53:42 +02:00 committed by rubidium42
parent 9c424ab741
commit df181bb641
4 changed files with 16 additions and 21 deletions

View File

@ -24,7 +24,7 @@
ContentInfo::ContentInfo() ContentInfo::ContentInfo()
: /* Temporary... will be removed later in the PR. */ : /* Temporary... will be removed later in the PR. */
type((ContentType)0), id((ContentID)0), filesize(0), filename(""), name(""), version(""), type((ContentType)0), id((ContentID)0), filesize(0), filename(""), name(""), version(""),
url(""), description(""), unique_id(0), md5sum(""), dependency_count(0), dependencies(nullptr), url(""), description(""), unique_id(0), md5sum(""),
state((State)0), upgrade(false) state((State)0), upgrade(false)
{ {
} }
@ -32,7 +32,6 @@ ContentInfo::ContentInfo()
/** Free everything allocated */ /** Free everything allocated */
ContentInfo::~ContentInfo() ContentInfo::~ContentInfo()
{ {
free(this->dependencies);
} }
/** /**
@ -42,9 +41,8 @@ ContentInfo::~ContentInfo()
void ContentInfo::TransferFrom(ContentInfo *other) void ContentInfo::TransferFrom(ContentInfo *other)
{ {
if (other != this) { if (other != this) {
free(this->dependencies);
*this = *other; *this = *other;
other->dependencies = nullptr; other->dependencies.clear();
other->tags.clear(); other->tags.clear();
} }
} }

View File

@ -67,8 +67,7 @@ struct ContentInfo {
char description[512]; ///< Description of the content char description[512]; ///< Description of the content
uint32 unique_id; ///< Unique ID; either GRF ID or shortname uint32 unique_id; ///< Unique ID; either GRF ID or shortname
byte md5sum[16]; ///< The MD5 checksum byte md5sum[16]; ///< The MD5 checksum
uint8 dependency_count; ///< Number of dependencies std::vector<ContentID> dependencies; ///< The dependencies (unique server side ids)
ContentID *dependencies; ///< Malloced array of dependencies (unique server side ids)
StringList tags; ///< Tags associated with the content StringList tags; ///< Tags associated with the content
State state; ///< Whether the content info is selected (for download) State state; ///< Whether the content info is selected (for download)
bool upgrade; ///< This item is an upgrade bool upgrade; ///< This item is an upgrade

View File

@ -66,9 +66,9 @@ bool ClientNetworkContentSocketHandler::Receive_SERVER_INFO(Packet *p)
ci->md5sum[j] = p->Recv_uint8(); ci->md5sum[j] = p->Recv_uint8();
} }
ci->dependency_count = p->Recv_uint8(); uint dependency_count = p->Recv_uint8();
ci->dependencies = MallocT<ContentID>(ci->dependency_count); ci->dependencies.reserve(dependency_count);
for (uint i = 0; i < ci->dependency_count; i++) ci->dependencies[i] = (ContentID)p->Recv_uint32(); for (uint i = 0; i < dependency_count; i++) ci->dependencies.push_back((ContentID)p->Recv_uint32());
uint tag_count = p->Recv_uint8(); uint tag_count = p->Recv_uint8();
ci->tags.reserve(tag_count); ci->tags.reserve(tag_count);
@ -927,8 +927,8 @@ void ClientNetworkContentSocketHandler::ReverseLookupDependency(ConstContentVect
for (const ContentInfo *ci : this->infos) { for (const ContentInfo *ci : this->infos) {
if (ci == child) continue; if (ci == child) continue;
for (uint i = 0; i < ci->dependency_count; i++) { for (auto &dependency : ci->dependencies) {
if (ci->dependencies[i] == child->id) { if (dependency == child->id) {
parents.push_back(ci); parents.push_back(ci);
break; break;
} }
@ -969,10 +969,10 @@ void ClientNetworkContentSocketHandler::CheckDependencyState(ContentInfo *ci)
/* Selection is easy; just walk all children and set the /* Selection is easy; just walk all children and set the
* autoselected state. That way we can see what we automatically * autoselected state. That way we can see what we automatically
* selected and thus can unselect when a dependency is removed. */ * selected and thus can unselect when a dependency is removed. */
for (uint i = 0; i < ci->dependency_count; i++) { for (auto &dependency : ci->dependencies) {
ContentInfo *c = this->GetContent(ci->dependencies[i]); ContentInfo *c = this->GetContent(dependency);
if (c == nullptr) { if (c == nullptr) {
this->DownloadContentInfo(ci->dependencies[i]); this->DownloadContentInfo(dependency);
} else if (c->state == ContentInfo::UNSELECTED) { } else if (c->state == ContentInfo::UNSELECTED) {
c->state = ContentInfo::AUTOSELECTED; c->state = ContentInfo::AUTOSELECTED;
this->CheckDependencyState(c); this->CheckDependencyState(c);
@ -995,10 +995,10 @@ void ClientNetworkContentSocketHandler::CheckDependencyState(ContentInfo *ci)
this->Unselect(c->id); this->Unselect(c->id);
} }
for (uint i = 0; i < ci->dependency_count; i++) { for (auto &dependency : ci->dependencies) {
const ContentInfo *c = this->GetContent(ci->dependencies[i]); const ContentInfo *c = this->GetContent(dependency);
if (c == nullptr) { if (c == nullptr) {
DownloadContentInfo(ci->dependencies[i]); DownloadContentInfo(dependency);
continue; continue;
} }
if (c->state != ContentInfo::AUTOSELECTED) continue; if (c->state != ContentInfo::AUTOSELECTED) continue;

View File

@ -725,13 +725,11 @@ public:
SetDParam(0, this->selected->filesize); SetDParam(0, this->selected->filesize);
y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_FILESIZE); y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_FILESIZE);
if (this->selected->dependency_count != 0) { if (!this->selected->dependencies.empty()) {
/* List dependencies */ /* List dependencies */
char buf[DRAW_STRING_BUFFER] = ""; char buf[DRAW_STRING_BUFFER] = "";
char *p = buf; char *p = buf;
for (uint i = 0; i < this->selected->dependency_count; i++) { for (auto &cid : this->selected->dependencies) {
ContentID cid = this->selected->dependencies[i];
/* Try to find the dependency */ /* Try to find the dependency */
ConstContentIterator iter = _network_content_client.Begin(); ConstContentIterator iter = _network_content_client.Begin();
for (; iter != _network_content_client.End(); iter++) { for (; iter != _network_content_client.End(); iter++) {