Changeset - r25618:b5050eb6e9cd
[Not reviewed]
master
0 6 0
rubidium42 - 3 years ago 2021-05-30 11:02:44
rubidium@openttd.org
Codechange: [ContentInfo] Use std::string instead of string buffers
6 files changed with 30 insertions and 25 deletions:
0 comments (0 inline, 0 general)
src/console_cmds.cpp
Show inline comments
 
@@ -1809,13 +1809,13 @@ static void OutputContentState(const Con
 
	static_assert(lengthof(types) == CONTENT_TYPE_END - CONTENT_TYPE_BEGIN);
 
	static const char * const states[] = { "Not selected", "Selected", "Dep Selected", "Installed", "Unknown" };
 
	static const TextColour state_to_colour[] = { CC_COMMAND, CC_INFO, CC_INFO, CC_WHITE, CC_ERROR };
 

	
 
	char buf[sizeof(ci->md5sum) * 2 + 1];
 
	md5sumToString(buf, lastof(buf), ci->md5sum);
 
	IConsolePrintF(state_to_colour[ci->state], "%d, %s, %s, %s, %08X, %s", ci->id, types[ci->type - 1], states[ci->state], ci->name, ci->unique_id, buf);
 
	IConsolePrintF(state_to_colour[ci->state], "%d, %s, %s, %s, %08X, %s", ci->id, types[ci->type - 1], states[ci->state], ci->name.c_str(), ci->unique_id, buf);
 
}
 

	
 
DEF_CONSOLE_CMD(ConContent)
 
{
 
	static ContentCallback *cb = nullptr;
 
	if (cb == nullptr) {
 
@@ -1879,13 +1879,13 @@ DEF_CONSOLE_CMD(ConContent)
 
		return true;
 
	}
 

	
 
	if (strcasecmp(argv[1], "state") == 0) {
 
		IConsolePrintF(CC_WHITE, "id, type, state, name");
 
		for (ConstContentIterator iter = _network_content_client.Begin(); iter != _network_content_client.End(); iter++) {
 
			if (argc > 2 && strcasestr((*iter)->name, argv[2]) == nullptr) continue;
 
			if (argc > 2 && strcasestr((*iter)->name.c_str(), argv[2]) == nullptr) continue;
 
			OutputContentState(*iter);
 
		}
 
		return true;
 
	}
 

	
 
	if (strcasecmp(argv[1], "download") == 0) {
src/network/core/config.h
Show inline comments
 
@@ -62,12 +62,17 @@ static const uint NETWORK_REVISION_LENGT
 
static const uint NETWORK_PASSWORD_LENGTH         =   33;         ///< The maximum length of the password, in bytes including '\0' (must be >= NETWORK_SERVER_ID_LENGTH)
 
static const uint NETWORK_CLIENTS_LENGTH          =  200;         ///< The maximum length for the list of clients that controls a company, in bytes including '\0'
 
static const uint NETWORK_CLIENT_NAME_LENGTH      =   25;         ///< The maximum length of a client's name, in bytes including '\0'
 
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_FILENAME_LENGTH =   48;         ///< The maximum length of a content's filename, in bytes including '\0'.
 
static const uint NETWORK_CONTENT_NAME_LENGTH     =   32;         ///< The maximum length of a content's name, in bytes including '\0'.
 
static const uint NETWORK_CONTENT_VERSION_LENGTH  =   16;         ///< The maximum length of a content's version, in bytes including '\0'.
 
static const uint NETWORK_CONTENT_URL_LENGTH      =   96;         ///< The maximum length of a content's url, in bytes including '\0'.
 
static const uint NETWORK_CONTENT_DESC_LENGTH     =  512;         ///< The maximum length of a content's description, 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
 

	
 
/**
 
 * Maximum number of GRFs that can be sent.
src/network/core/tcp_content_type.h
Show inline comments
 
@@ -57,17 +57,17 @@ struct ContentInfo {
 
		INVALID,        ///< The content's invalid
 
	};
 

	
 
	ContentType type;        ///< Type of content
 
	ContentID id;            ///< Unique (server side) ID for the content
 
	uint32 filesize;         ///< Size of the file
 
	char filename[48];       ///< Filename (for the .tar.gz; only valid on download)
 
	char name[32];           ///< Name of the content
 
	char version[16];        ///< Version of the content
 
	char url[96];            ///< URL related to the content
 
	char description[512];   ///< Description of the content
 
	std::string filename;    ///< Filename (for the .tar.gz; only valid on download)
 
	std::string name;        ///< Name of the content
 
	std::string version;     ///< Version of the content
 
	std::string url;         ///< URL related to the content
 
	std::string description; ///< Description of the content
 
	uint32 unique_id;        ///< Unique ID; either GRF ID or shortname
 
	byte md5sum[16];         ///< The MD5 checksum
 
	std::vector<ContentID> dependencies; ///< The dependencies (unique server side ids)
 
	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
 
@@ -53,16 +53,16 @@ bool ClientNetworkContentSocketHandler::
 
{
 
	ContentInfo *ci = new ContentInfo();
 
	ci->type     = (ContentType)p->Recv_uint8();
 
	ci->id       = (ContentID)p->Recv_uint32();
 
	ci->filesize = p->Recv_uint32();
 

	
 
	p->Recv_string(ci->name, lengthof(ci->name));
 
	p->Recv_string(ci->version, lengthof(ci->version));
 
	p->Recv_string(ci->url, lengthof(ci->url));
 
	p->Recv_string(ci->description, lengthof(ci->description), SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE);
 
	ci->name        = p->Recv_string(NETWORK_CONTENT_NAME_LENGTH);
 
	ci->version     = p->Recv_string(NETWORK_CONTENT_VERSION_LENGTH);
 
	ci->url         = p->Recv_string(NETWORK_CONTENT_URL_LENGTH);
 
	ci->description = p->Recv_string(NETWORK_CONTENT_DESC_LENGTH, SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE);
 

	
 
	ci->unique_id = p->Recv_uint32();
 
	for (uint j = 0; j < sizeof(ci->md5sum); j++) {
 
		ci->md5sum[j] = p->Recv_uint8();
 
	}
 

	
 
@@ -140,13 +140,13 @@ bool ClientNetworkContentSocketHandler::
 

	
 
	/* Do we already have a stub for this? */
 
	for (ContentInfo *ici : this->infos) {
 
		if (ici->type == ci->type && ici->unique_id == ci->unique_id &&
 
				memcmp(ci->md5sum, ici->md5sum, sizeof(ci->md5sum)) == 0) {
 
			/* Preserve the name if possible */
 
			if (StrEmpty(ci->name)) strecpy(ci->name, ici->name, lastof(ci->name));
 
			if (ci->name.empty()) ci->name = ici->name;
 
			if (ici->IsSelected()) ci->state = ici->state;
 

	
 
			/*
 
			 * As ici might be selected by the content window we cannot delete that.
 
			 * However, we want to keep most of the values of ci, except the values
 
			 * we (just) already preserved.
 
@@ -482,13 +482,13 @@ bool ClientNetworkContentSocketHandler::
 
		delete this->curInfo;
 
		/* When we haven't opened a file this must be our first packet with metadata. */
 
		this->curInfo = new ContentInfo;
 
		this->curInfo->type     = (ContentType)p->Recv_uint8();
 
		this->curInfo->id       = (ContentID)p->Recv_uint32();
 
		this->curInfo->filesize = p->Recv_uint32();
 
		p->Recv_string(this->curInfo->filename, lengthof(this->curInfo->filename));
 
		this->curInfo->filename = p->Recv_string(NETWORK_CONTENT_FILENAME_LENGTH);
 

	
 
		if (!this->BeforeDownload()) {
 
			this->CloseConnection();
 
			return false;
 
		}
 
	} else {
 
@@ -701,13 +701,13 @@ void ClientNetworkContentSocketHandler::
 
		for (uint i = 0; i < 2; i++) {
 
			p = strrchr(tmp, '.');
 
			check_and_terminate(p);
 
		}
 

	
 
		/* Copy the string, without extension, to the filename. */
 
		strecpy(this->curInfo->filename, tmp, lastof(this->curInfo->filename));
 
		this->curInfo->filename = tmp;
 

	
 
		/* Request the next file. */
 
		if (!this->BeforeDownload()) {
 
			this->OnFailure();
 
			return;
 
		}
src/network/network_content_gui.cpp
Show inline comments
 
@@ -144,13 +144,13 @@ void BaseNetworkContentDownloadStatusWin
 
	DrawStringMultiLine(r.left + 2, r.right - 2, y, y + FONT_HEIGHT_NORMAL * 2, str, TC_FROMSTRING, SA_CENTER);
 
}
 

	
 
void BaseNetworkContentDownloadStatusWindow::OnDownloadProgress(const ContentInfo *ci, int bytes)
 
{
 
	if (ci->id != this->cur_id) {
 
		strecpy(this->name, ci->filename, lastof(this->name));
 
		strecpy(this->name, ci->filename.c_str(), lastof(this->name));
 
		this->cur_id = ci->id;
 
		this->downloaded_files++;
 
	}
 

	
 
	this->downloaded_bytes += bytes;
 
	this->SetDirty();
 
@@ -405,13 +405,13 @@ class NetworkContentListWindow : public 
 
		this->ScrollToSelected();
 
	}
 

	
 
	/** Sort content by name. */
 
	static bool NameSorter(const ContentInfo * const &a, const ContentInfo * const &b)
 
	{
 
		return strnatcmp(a->name, b->name, true) < 0; // Sort by name (natural sorting).
 
		return strnatcmp(a->name.c_str(), b->name.c_str(), true) < 0; // Sort by name (natural sorting).
 
	}
 

	
 
	/** Sort content by type. */
 
	static bool TypeSorter(const ContentInfo * const &a, const ContentInfo * const &b)
 
	{
 
		int r = 0;
 
@@ -442,13 +442,13 @@ class NetworkContentListWindow : public 
 
	/** Filter content by tags/name */
 
	static bool CDECL TagNameFilter(const ContentInfo * const *a, ContentListFilterData &filter)
 
	{
 
		filter.string_filter.ResetState();
 
		for (auto &tag : (*a)->tags) filter.string_filter.AddLine(tag.c_str());
 

	
 
		filter.string_filter.AddLine((*a)->name);
 
		filter.string_filter.AddLine((*a)->name.c_str());
 
		return filter.string_filter.GetState();
 
	}
 

	
 
	/** Filter content by type, but still show content selected for download. */
 
	static bool CDECL TypeOrSelectedFilter(const ContentInfo * const *a, ContentListFilterData &filter)
 
	{
 
@@ -700,23 +700,23 @@ public:
 
			y += WD_PAR_VSEP_WIDE;
 
		}
 

	
 
		SetDParamStr(0, this->selected->name);
 
		y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_NAME);
 

	
 
		if (!StrEmpty(this->selected->version)) {
 
		if (!this->selected->version.empty()) {
 
			SetDParamStr(0, this->selected->version);
 
			y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_VERSION);
 
		}
 

	
 
		if (!StrEmpty(this->selected->description)) {
 
		if (!this->selected->description.empty()) {
 
			SetDParamStr(0, this->selected->description);
 
			y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_DESCRIPTION);
 
		}
 

	
 
		if (!StrEmpty(this->selected->url)) {
 
		if (!this->selected->url.empty()) {
 
			SetDParamStr(0, this->selected->url);
 
			y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_URL);
 
		}
 

	
 
		SetDParam(0, STR_CONTENT_TYPE_BASE_GRAPHICS + this->selected->type - CONTENT_TYPE_BASE_GRAPHICS);
 
		y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_TYPE);
 
@@ -733,13 +733,13 @@ public:
 
				/* Try to find the dependency */
 
				ConstContentIterator iter = _network_content_client.Begin();
 
				for (; iter != _network_content_client.End(); iter++) {
 
					const ContentInfo *ci = *iter;
 
					if (ci->id != cid) continue;
 

	
 
					p += seprintf(p, lastof(buf), p == buf ? "%s" : ", %s", (*iter)->name);
 
					p += seprintf(p, lastof(buf), p == buf ? "%s" : ", %s", (*iter)->name.c_str());
 
					break;
 
				}
 
			}
 
			SetDParamStr(0, buf);
 
			y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_DEPENDENCIES);
 
		}
 
@@ -762,13 +762,13 @@ public:
 

	
 
			char buf[DRAW_STRING_BUFFER] = "";
 
			char *p = buf;
 
			for (const ContentInfo *ci : tree) {
 
				if (ci == this->selected || ci->state != ContentInfo::SELECTED) continue;
 

	
 
				p += seprintf(p, lastof(buf), buf == p ? "%s" : ", %s", ci->name);
 
				p += seprintf(p, lastof(buf), buf == p ? "%s" : ", %s", ci->name.c_str());
 
			}
 
			if (p != buf) {
 
				SetDParamStr(0, buf);
 
				y = DrawStringMultiLine(r.left + DETAIL_LEFT, r.right - DETAIL_RIGHT, y, max_y, STR_CONTENT_DETAIL_SELECTED_BECAUSE_OF);
 
			}
 
		}
 
@@ -839,13 +839,13 @@ public:
 
				this->Close();
 
				break;
 

	
 
			case WID_NCL_OPEN_URL:
 
				if (this->selected != nullptr) {
 
					extern void OpenBrowser(const char *url);
 
					OpenBrowser(this->selected->url);
 
					OpenBrowser(this->selected->url.c_str());
 
				}
 
				break;
 

	
 
			case WID_NCL_DOWNLOAD:
 
				if (BringWindowToFrontById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD) == nullptr) new NetworkContentDownloadStatusWindow();
 
				break;
 
@@ -980,13 +980,13 @@ public:
 

	
 
		/* If data == 2 then the status window caused this OnInvalidate */
 
		this->SetWidgetDisabledState(WID_NCL_DOWNLOAD, this->filesize_sum == 0 || (FindWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD) != nullptr && data != 2));
 
		this->SetWidgetDisabledState(WID_NCL_UNSELECT, this->filesize_sum == 0);
 
		this->SetWidgetDisabledState(WID_NCL_SELECT_ALL, !show_select_all);
 
		this->SetWidgetDisabledState(WID_NCL_SELECT_UPDATE, !show_select_upgrade);
 
		this->SetWidgetDisabledState(WID_NCL_OPEN_URL, this->selected == nullptr || StrEmpty(this->selected->url));
 
		this->SetWidgetDisabledState(WID_NCL_OPEN_URL, this->selected == nullptr || this->selected->url.empty());
 
		for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
 
			this->SetWidgetDisabledState(WID_NCL_TEXTFILE + tft, this->selected == nullptr || this->selected->state != ContentInfo::ALREADY_HERE || this->selected->GetTextfile(tft) == nullptr);
 
		}
 

	
 
		this->GetWidget<NWidgetCore>(WID_NCL_CANCEL)->widget_data = this->filesize_sum == 0 ? STR_AI_SETTINGS_CLOSE : STR_AI_LIST_CANCEL;
 
	}
src/newgrf_gui.cpp
Show inline comments
 
@@ -1547,13 +1547,13 @@ void ShowMissingContentWindow(const GRFC
 
	for (const GRFConfig *c = list; c != nullptr; c = c->next) {
 
		if (c->status != GCS_NOT_FOUND && !HasBit(c->flags, GCF_COMPATIBLE)) continue;
 

	
 
		ContentInfo *ci = new ContentInfo();
 
		ci->type = CONTENT_TYPE_NEWGRF;
 
		ci->state = ContentInfo::DOES_NOT_EXIST;
 
		strecpy(ci->name, c->GetName(), lastof(ci->name));
 
		ci->name = c->GetName();
 
		ci->unique_id = BSWAP32(c->ident.grfid);
 
		memcpy(ci->md5sum, HasBit(c->flags, GCF_COMPATIBLE) ? c->original_md5sum : c->ident.md5sum, sizeof(ci->md5sum));
 
		cv.push_back(ci);
 
	}
 
	ShowNetworkContentListWindow(cv.size() == 0 ? nullptr : &cv, CONTENT_TYPE_NEWGRF);
 
}
0 comments (0 inline, 0 general)