Changeset - r27236:5b7ff986fc85
[Not reviewed]
master
0 12 0
Rubidium - 18 months ago 2023-04-27 20:18:53
rubidium@openttd.org
Codechange: make md5sumToString std::string compatible
12 files changed with 24 insertions and 54 deletions:
0 comments (0 inline, 0 general)
src/console_cmds.cpp
Show inline comments
 
@@ -1896,15 +1896,13 @@ static void OutputContentState(const Con
 
{
 
	static const char * const types[] = { "Base graphics", "NewGRF", "AI", "AI library", "Scenario", "Heightmap", "Base sound", "Base music", "Game script", "GS library" };
 
	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);
 
	IConsolePrint(state_to_colour[ci->state], "{}, {}, {}, {}, {:08X}, {}", ci->id, types[ci->type - 1], states[ci->state], ci->name, ci->unique_id, buf);
 
	IConsolePrint(state_to_colour[ci->state], "{}, {}, {}, {}, {:08X}, {}", ci->id, types[ci->type - 1], states[ci->state], ci->name, ci->unique_id, MD5SumToString(ci->md5sum));
 
}
 

	
 
DEF_CONSOLE_CMD(ConContent)
 
{
 
	static ContentCallback *cb = nullptr;
 
	if (cb == nullptr) {
src/gamelog.cpp
Show inline comments
 
@@ -122,15 +122,13 @@ void Gamelog::Reset()
 
 * @param gc GrfConfig, if known
 
 */
 
template <typename T>
 
static void AddGrfInfo(T &outputIterator, uint grfid, const uint8 *md5sum, const GRFConfig *gc)
 
{
 
	if (md5sum != nullptr) {
 
		char txt[40];
 
		md5sumToString(txt, lastof(txt), md5sum);
 
		fmt::format_to(outputIterator, "GRF ID {:08X}, checksum {}", BSWAP32(grfid), txt);
 
		fmt::format_to(outputIterator, "GRF ID {:08X}, checksum {}", BSWAP32(grfid), MD5SumToString(md5sum));
 
	} else {
 
		fmt::format_to(outputIterator, "GRF ID {:08X}", BSWAP32(grfid));
 
	}
 

	
 
	if (gc != nullptr) {
 
		fmt::format_to(outputIterator, ", filename: {} (md5sum matches)", gc->filename);
src/network/network_client.cpp
Show inline comments
 
@@ -663,15 +663,13 @@ NetworkRecvStatus ClientNetworkGameSocke
 
		DeserializeGRFIdentifier(p, &c);
 

	
 
		/* Check whether we know this GRF */
 
		const GRFConfig *f = FindGRFConfig(c.grfid, FGCM_EXACT, c.md5sum);
 
		if (f == nullptr) {
 
			/* We do not know this GRF, bail out of initialization */
 
			char buf[sizeof(c.md5sum) * 2 + 1];
 
			md5sumToString(buf, lastof(buf), c.md5sum);
 
			Debug(grf, 0, "NewGRF {:08X} not found; checksum {}", BSWAP32(c.grfid), buf);
 
			Debug(grf, 0, "NewGRF {:08X} not found; checksum {}", BSWAP32(c.grfid), MD5SumToString(c.md5sum));
 
			ret = NETWORK_RECV_STATUS_NEWGRF_MISMATCH;
 
		}
 
	}
 

	
 
	if (ret == NETWORK_RECV_STATUS_OKAY) {
 
		/* Start receiving the map */
src/network/network_content_gui.cpp
Show inline comments
 
@@ -362,15 +362,13 @@ class NetworkContentListWindow : public 
 
			for (const ContentInfo *ci : this->content) {
 
				if (ci->state != ContentInfo::DOES_NOT_EXIST) continue;
 

	
 
				if (!first) url.push_back(',');
 
				first = false;
 

	
 
				char buf[33];
 
				md5sumToString(buf, lastof(buf), ci->md5sum);
 
				fmt::format_to(std::back_inserter(url), "{:08X}:{}", ci->unique_id, buf);
 
				fmt::format_to(std::back_inserter(url), "{:08X}:{}", ci->unique_id, MD5SumToString(ci->md5sum));
 
			}
 
		} else {
 
			url += "do=searchtext&q=";
 

	
 
			/* Escape search term */
 
			for (const char *search = this->filter_editbox.text.buf; *search != '\0'; search++) {
src/newgrf_config.cpp
Show inline comments
 
@@ -514,34 +514,30 @@ GRFListCompatibility IsGoodGRFConfigList
 
{
 
	GRFListCompatibility res = GLC_ALL_GOOD;
 

	
 
	for (GRFConfig *c = grfconfig; c != nullptr; c = c->next) {
 
		const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, c->ident.md5sum);
 
		if (f == nullptr || HasBit(f->flags, GCF_INVALID)) {
 
			char buf[256];
 

	
 
			/* If we have not found the exactly matching GRF try to find one with the
 
			 * same grfid, as it most likely is compatible */
 
			f = FindGRFConfig(c->ident.grfid, FGCM_COMPATIBLE, nullptr, c->version);
 
			if (f != nullptr) {
 
				md5sumToString(buf, lastof(buf), c->ident.md5sum);
 
				Debug(grf, 1, "NewGRF {:08X} ({}) not found; checksum {}. Compatibility mode on", BSWAP32(c->ident.grfid), c->filename, buf);
 
				Debug(grf, 1, "NewGRF {:08X} ({}) not found; checksum {}. Compatibility mode on", BSWAP32(c->ident.grfid), c->filename, MD5SumToString(c->ident.md5sum));
 
				if (!HasBit(c->flags, GCF_COMPATIBLE)) {
 
					/* Preserve original_md5sum after it has been assigned */
 
					SetBit(c->flags, GCF_COMPATIBLE);
 
					memcpy(c->original_md5sum, c->ident.md5sum, sizeof(c->original_md5sum));
 
				}
 

	
 
				/* Non-found has precedence over compatibility load */
 
				if (res != GLC_NOT_FOUND) res = GLC_COMPATIBLE;
 
				goto compatible_grf;
 
			}
 

	
 
			/* No compatible grf was found, mark it as disabled */
 
			md5sumToString(buf, lastof(buf), c->ident.md5sum);
 
			Debug(grf, 0, "NewGRF {:08X} ({}) not found; checksum {}", BSWAP32(c->ident.grfid), c->filename, buf);
 
			Debug(grf, 0, "NewGRF {:08X} ({}) not found; checksum {}", BSWAP32(c->ident.grfid), c->filename, MD5SumToString(c->ident.md5sum));
 

	
 
			c->status = GCS_NOT_FOUND;
 
			res = GLC_NOT_FOUND;
 
		} else {
 
compatible_grf:
 
			Debug(grf, 1, "Loading GRF {:08X} from {}", BSWAP32(f->ident.grfid), f->filename);
src/newgrf_gui.cpp
Show inline comments
 
@@ -89,34 +89,34 @@ static void ShowNewGRFInfo(const GRFConf
 
	if (c->filename != nullptr) {
 
		SetDParamStr(0, c->filename);
 
		tr.top = DrawStringMultiLine(tr, STR_NEWGRF_SETTINGS_FILENAME);
 
	}
 

	
 
	/* Prepare and draw GRF ID */
 
	char buff[256];
 
	seprintf(buff, lastof(buff), "%08X", BSWAP32(c->ident.grfid));
 
	SetDParamStr(0, buff);
 
	std::string tmp = fmt::format("{:08X}", BSWAP32(c->ident.grfid));
 
	SetDParamStr(0, tmp);
 
	tr.top = DrawStringMultiLine(tr, STR_NEWGRF_SETTINGS_GRF_ID);
 

	
 
	if ((_settings_client.gui.newgrf_developer_tools || _settings_client.gui.newgrf_show_old_versions) && c->version != 0) {
 
		SetDParam(0, c->version);
 
		tr.top = DrawStringMultiLine(tr, STR_NEWGRF_SETTINGS_VERSION);
 
	}
 
	if ((_settings_client.gui.newgrf_developer_tools || _settings_client.gui.newgrf_show_old_versions) && c->min_loadable_version != 0) {
 
		SetDParam(0, c->min_loadable_version);
 
		tr.top = DrawStringMultiLine(tr, STR_NEWGRF_SETTINGS_MIN_VERSION);
 
	}
 

	
 
	/* Prepare and draw MD5 sum */
 
	md5sumToString(buff, lastof(buff), c->ident.md5sum);
 
	SetDParamStr(0, buff);
 
	tmp = MD5SumToString(c->ident.md5sum);
 
	SetDParamStr(0, tmp);
 
	tr.top = DrawStringMultiLine(tr, STR_NEWGRF_SETTINGS_MD5SUM);
 

	
 
	/* Show GRF parameter list */
 
	if (show_params) {
 
		if (c->num_params > 0) {
 
			char buff[256];
 
			GRFBuildParamList(buff, c, lastof(buff));
 
			SetDParam(0, STR_JUST_RAW_STRING);
 
			SetDParamStr(1, buff);
 
		} else {
 
			SetDParam(0, STR_NEWGRF_SETTINGS_PARAMETER_NONE);
 
		}
src/openttd.cpp
Show inline comments
 
@@ -254,15 +254,14 @@ static void WriteSavegameInfo(const char
 
		fmt::format_to(std::back_inserter(message), "NewGRFs have been removed\n");
 
	}
 

	
 
	message += "NewGRFs:\n";
 
	if (_load_check_data.HasNewGrfs()) {
 
		for (GRFConfig *c = _load_check_data.grfconfig; c != nullptr; c = c->next) {
 
			char md5sum[33];
 
			md5sumToString(md5sum, lastof(md5sum), HasBit(c->flags, GCF_COMPATIBLE) ? c->original_md5sum : c->ident.md5sum);
 
			fmt::format_to(std::back_inserter(message), "{:08X} {} {}\n", c->ident.grfid, md5sum, c->filename);
 
			fmt::format_to(std::back_inserter(message), "{:08X} {} {}\n", c->ident.grfid,
 
				MD5SumToString(HasBit(c->flags, GCF_COMPATIBLE) ? c->original_md5sum : c->ident.md5sum), c->filename);
 
		}
 
	}
 

	
 
	/* ShowInfo put output to stderr, but version information should go
 
	 * to stdout; this is the only exception */
 
#if !defined(_WIN32)
src/saveload/afterload.cpp
Show inline comments
 
@@ -385,22 +385,18 @@ static void CDECL HandleSavegameLoadCras
 
			"Please load the savegame with the appropriate NewGRFs installed.\n"
 
			"The missing/compatible NewGRFs are:\n";
 

	
 
		for (const GRFConfig *c = _grfconfig; c != nullptr; c = c->next) {
 
			if (HasBit(c->flags, GCF_COMPATIBLE)) {
 
				const GRFIdentifier *replaced = _gamelog.GetOverriddenIdentifier(c);
 
				char original_md5[40];
 
				char replaced_md5[40];
 
				md5sumToString(original_md5, lastof(original_md5), c->original_md5sum);
 
				md5sumToString(replaced_md5, lastof(replaced_md5), replaced->md5sum);
 
				fmt::format_to(std::back_inserter(message), "NewGRF {:08X} (checksum {}) not found.\n  Loaded NewGRF \"{}\" (checksum {}) with same GRF ID instead.\n", BSWAP32(c->ident.grfid), original_md5, c->filename, replaced_md5);
 
				fmt::format_to(std::back_inserter(message), "NewGRF {:08X} (checksum {}) not found.\n  Loaded NewGRF \"{}\" (checksum {}) with same GRF ID instead.\n",
 
						BSWAP32(c->ident.grfid), MD5SumToString(c->original_md5sum), c->filename, MD5SumToString(replaced->md5sum));
 
			}
 
			if (c->status == GCS_NOT_FOUND) {
 
				char buf[40];
 
				md5sumToString(buf, lastof(buf), c->ident.md5sum);
 
				fmt::format_to(std::back_inserter(message), "NewGRF {:08X} ({}) not found; checksum {}.\n", BSWAP32(c->ident.grfid), c->filename, buf);
 
				fmt::format_to(std::back_inserter(message), "NewGRF {:08X} ({}) not found; checksum {}.\n",
 
						BSWAP32(c->ident.grfid), c->filename, MD5SumToString(c->ident.md5sum));
 
			}
 
		}
 
	} else {
 
		message +=
 
			"This is probably caused by a corruption in the savegame.\n"
 
			"Please file a bug report and attach this savegame.\n";
src/screenshot.cpp
Show inline comments
 
@@ -317,15 +317,13 @@ static bool MakePNGImage(const char *nam
 

	
 
	std::string message;
 
	message.reserve(1024);
 
	fmt::format_to(std::back_inserter(message), "Graphics set: {} ({})\n", BaseGraphics::GetUsedSet()->name, BaseGraphics::GetUsedSet()->version);
 
	message += "NewGRFs:\n";
 
	for (const GRFConfig *c = _game_mode == GM_MENU ? nullptr : _grfconfig; c != nullptr; c = c->next) {
 
		char buf[33];
 
		md5sumToString(buf, lastof(buf), c->ident.md5sum);
 
		fmt::format_to(std::back_inserter(message), "{:08X} {} {}\n", BSWAP32(c->ident.grfid), buf, c->filename);
 
		fmt::format_to(std::back_inserter(message), "{:08X} {} {}\n", BSWAP32(c->ident.grfid), MD5SumToString(c->ident.md5sum), c->filename);
 
	}
 
	message += "\nCompanies:\n";
 
	for (const Company *c : Company::Iterate()) {
 
		if (c->ai_info == nullptr) {
 
			fmt::format_to(std::back_inserter(message), "{:2d}: Human\n", (int)c->index);
 
		} else {
src/settings.cpp
Show inline comments
 
@@ -1132,20 +1132,17 @@ static void GRFSaveConfig(IniFile &ini, 
 
{
 
	ini.RemoveGroup(grpname);
 
	IniGroup *group = ini.GetGroup(grpname);
 
	const GRFConfig *c;
 

	
 
	for (c = list; c != nullptr; c = c->next) {
 
		/* Hex grfid (4 bytes in nibbles), "|", hex md5sum (16 bytes in nibbles), "|", file system path. */
 
		char key[4 * 2 + 1 + 16 * 2 + 1 + MAX_PATH];
 
		char params[512];
 
		GRFBuildParamList(params, c, lastof(params));
 

	
 
		char *pos = key + seprintf(key, lastof(key), "%08X|", BSWAP32(c->ident.grfid));
 
		pos = md5sumToString(pos, lastof(key), c->ident.md5sum);
 
		seprintf(pos, lastof(key), "|%s", c->filename);
 
		std::string key = fmt::format("{:08X}|{}|{}", BSWAP32(c->ident.grfid),
 
				MD5SumToString(c->ident.md5sum), c->filename);
 
		group->GetItem(key, true)->SetValue(params);
 
	}
 
}
 

	
 
/* Common handler for saving/loading variables to the configuration file */
 
static void HandleSettingDescs(IniFile &generic_ini, IniFile &private_ini, IniFile &secrets_ini, SettingDescProc *proc, SettingDescProcList *proc_list, bool only_startup = false)
src/string.cpp
Show inline comments
 
@@ -609,26 +609,18 @@ int CDECL seprintf(char *str, const char
 
	return ret;
 
}
 

	
 

	
 
/**
 
 * Convert the md5sum to a hexadecimal string representation
 
 * @param buf buffer to put the md5sum into
 
 * @param last last character of buffer (usually lastof(buf))
 
 * @param md5sum the md5sum itself
 
 * @return a pointer to the next character after the md5sum
 
 * @return the string representation of the md5sum.
 
 */
 
char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16])
 
std::string MD5SumToString(const uint8 md5sum[16])
 
{
 
	char *p = buf;
 

	
 
	for (uint i = 0; i < 16; i++) {
 
		p += seprintf(p, last, "%02X", md5sum[i]);
 
	}
 

	
 
	return p;
 
	return FormatArrayAsHex({md5sum, 16});
 
}
 

	
 

	
 
/* UTF-8 handling routines */
 

	
 

	
src/string_func.h
Show inline comments
 
@@ -83,13 +83,13 @@ static inline size_t ttd_strnlen(const c
 
{
 
	const char *t;
 
	for (t = str; (size_t)(t - str) < maxlen && *t != '\0'; t++) {}
 
	return t - str;
 
}
 

	
 
char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16]);
 
std::string MD5SumToString(const uint8 md5sum[16]);
 

	
 
bool IsValidChar(WChar key, CharSetFilter afilter);
 

	
 
size_t Utf8Decode(WChar *c, const char *s);
 
size_t Utf8Encode(char *buf, WChar c);
 
size_t Utf8Encode(std::ostreambuf_iterator<char> &buf, WChar c);
0 comments (0 inline, 0 general)