Changeset - r25616:f6fb8fecc5a4
[Not reviewed]
master
0 5 0
rubidium42 - 3 years ago 2021-05-30 10:47:50
rubidium@openttd.org
Codechange: [ContentInfo] Use StringList for tags instead of custom allocations
5 files changed with 12 insertions and 15 deletions:
0 comments (0 inline, 0 general)
src/network/core/config.h
Show inline comments
 
@@ -65,6 +65,7 @@ static const uint NETWORK_CLIENT_NAME_LE
 
static const uint NETWORK_RCONCOMMAND_LENGTH      =  500;         ///< The maximum length of a rconsole command, in bytes including '\0'
 
static const uint NETWORK_GAMESCRIPT_JSON_LENGTH  = COMPAT_MTU-3; ///< The maximum length of a gamescript json string, in bytes including '\0'. Must not be longer than COMPAT_MTU including header (3 bytes)
 
static const uint NETWORK_CHAT_LENGTH             =  900;         ///< The maximum length of a chat message, in bytes including '\0'
 
static const uint NETWORK_CONTENT_TAG_LENGTH      =   32;         ///< The maximum length of a content's tag, in bytes including '\0'.
 

	
 
static const uint NETWORK_GRF_NAME_LENGTH         =   80;         ///< Maximum length of the name of a GRF
 

	
src/network/core/tcp_content.cpp
Show inline comments
 
@@ -25,7 +25,7 @@ ContentInfo::ContentInfo()
 
	: /* Temporary... will be removed later in the PR. */
 
	type((ContentType)0), id((ContentID)0), filesize(0), filename(""), name(""), version(""),
 
	url(""), description(""), unique_id(0), md5sum(""), dependency_count(0), dependencies(nullptr),
 
	tag_count(0), tags(nullptr), state((State)0), upgrade(false)
 
	state((State)0), upgrade(false)
 
{
 
}
 

	
 
@@ -33,7 +33,6 @@ ContentInfo::ContentInfo()
 
ContentInfo::~ContentInfo()
 
{
 
	free(this->dependencies);
 
	free(this->tags);
 
}
 

	
 
/**
 
@@ -44,10 +43,9 @@ void ContentInfo::TransferFrom(ContentIn
 
{
 
	if (other != this) {
 
		free(this->dependencies);
 
		free(this->tags);
 
		*this = *other;
 
		other->dependencies = nullptr;
 
		other->tags = nullptr;
 
		other->tags.clear();
 
	}
 
}
 

	
src/network/core/tcp_content_type.h
Show inline comments
 
@@ -69,8 +69,7 @@ struct ContentInfo {
 
	byte md5sum[16];         ///< The MD5 checksum
 
	uint8 dependency_count;  ///< Number of dependencies
 
	ContentID *dependencies; ///< Malloced array of dependencies (unique server side ids)
 
	uint8 tag_count;         ///< Number of tags
 
	char (*tags)[32];        ///< Malloced array of tags (strings)
 
	StringList tags;         ///< Tags associated with the content
 
	State state;             ///< Whether the content info is selected (for download)
 
	bool upgrade;            ///< This item is an upgrade
 

	
src/network/network_content.cpp
Show inline comments
 
@@ -70,9 +70,9 @@ bool ClientNetworkContentSocketHandler::
 
	ci->dependencies = MallocT<ContentID>(ci->dependency_count);
 
	for (uint i = 0; i < ci->dependency_count; i++) ci->dependencies[i] = (ContentID)p->Recv_uint32();
 

	
 
	ci->tag_count = p->Recv_uint8();
 
	ci->tags = MallocT<char[32]>(ci->tag_count);
 
	for (uint i = 0; i < ci->tag_count; i++) p->Recv_string(ci->tags[i], lengthof(*ci->tags));
 
	uint tag_count = p->Recv_uint8();
 
	ci->tags.reserve(tag_count);
 
	for (uint i = 0; i < tag_count; i++) ci->tags.push_back(p->Recv_string(NETWORK_CONTENT_TAG_LENGTH));
 

	
 
	if (!ci->IsValid()) {
 
		delete ci;
src/network/network_content_gui.cpp
Show inline comments
 
@@ -443,9 +443,8 @@ class NetworkContentListWindow : public 
 
	static bool CDECL TagNameFilter(const ContentInfo * const *a, ContentListFilterData &filter)
 
	{
 
		filter.string_filter.ResetState();
 
		for (int i = 0; i < (*a)->tag_count; i++) {
 
			filter.string_filter.AddLine((*a)->tags[i]);
 
		}
 
		for (auto &tag : (*a)->tags) filter.string_filter.AddLine(tag.c_str());
 

	
 
		filter.string_filter.AddLine((*a)->name);
 
		return filter.string_filter.GetState();
 
	}
 
@@ -747,12 +746,12 @@ public:
 
			y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_DEPENDENCIES);
 
		}
 

	
 
		if (this->selected->tag_count != 0) {
 
		if (!this->selected->tags.empty()) {
 
			/* List all tags */
 
			char buf[DRAW_STRING_BUFFER] = "";
 
			char *p = buf;
 
			for (uint i = 0; i < this->selected->tag_count; i++) {
 
				p += seprintf(p, lastof(buf), i == 0 ? "%s" : ", %s", this->selected->tags[i]);
 
			for (auto &tag : this->selected->tags) {
 
				p += seprintf(p, lastof(buf), p == buf ? "%s" : ", %s", tag.c_str());
 
			}
 
			SetDParamStr(0, buf);
 
			y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_TAGS);
0 comments (0 inline, 0 general)