Codechange: make md5sumToString std::string compatible

This commit is contained in:
Rubidium 2023-04-27 22:18:53 +02:00 committed by rubidium42
parent 51c6b8c1e4
commit a312a6c1b2
12 changed files with 24 additions and 54 deletions

View File

@ -1899,9 +1899,7 @@ static void OutputContentState(const ContentInfo *const ci)
static const char * const states[] = { "Not selected", "Selected", "Dep Selected", "Installed", "Unknown" }; 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 }; static const TextColour state_to_colour[] = { CC_COMMAND, CC_INFO, CC_INFO, CC_WHITE, CC_ERROR };
char buf[sizeof(ci->md5sum) * 2 + 1]; IConsolePrint(state_to_colour[ci->state], "{}, {}, {}, {}, {:08X}, {}", ci->id, types[ci->type - 1], states[ci->state], ci->name, ci->unique_id, MD5SumToString(ci->md5sum));
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);
} }
DEF_CONSOLE_CMD(ConContent) DEF_CONSOLE_CMD(ConContent)

View File

@ -125,9 +125,7 @@ template <typename T>
static void AddGrfInfo(T &outputIterator, uint grfid, const uint8 *md5sum, const GRFConfig *gc) static void AddGrfInfo(T &outputIterator, uint grfid, const uint8 *md5sum, const GRFConfig *gc)
{ {
if (md5sum != nullptr) { if (md5sum != nullptr) {
char txt[40]; fmt::format_to(outputIterator, "GRF ID {:08X}, checksum {}", BSWAP32(grfid), MD5SumToString(md5sum));
md5sumToString(txt, lastof(txt), md5sum);
fmt::format_to(outputIterator, "GRF ID {:08X}, checksum {}", BSWAP32(grfid), txt);
} else { } else {
fmt::format_to(outputIterator, "GRF ID {:08X}", BSWAP32(grfid)); fmt::format_to(outputIterator, "GRF ID {:08X}", BSWAP32(grfid));
} }

View File

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

View File

@ -365,9 +365,7 @@ class NetworkContentListWindow : public Window, ContentCallback {
if (!first) url.push_back(','); if (!first) url.push_back(',');
first = false; first = false;
char buf[33]; fmt::format_to(std::back_inserter(url), "{:08X}:{}", ci->unique_id, MD5SumToString(ci->md5sum));
md5sumToString(buf, lastof(buf), ci->md5sum);
fmt::format_to(std::back_inserter(url), "{:08X}:{}", ci->unique_id, buf);
} }
} else { } else {
url += "do=searchtext&q="; url += "do=searchtext&q=";

View File

@ -517,14 +517,11 @@ GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig)
for (GRFConfig *c = grfconfig; c != nullptr; c = c->next) { for (GRFConfig *c = grfconfig; c != nullptr; c = c->next) {
const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, c->ident.md5sum); const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, c->ident.md5sum);
if (f == nullptr || HasBit(f->flags, GCF_INVALID)) { 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 /* If we have not found the exactly matching GRF try to find one with the
* same grfid, as it most likely is compatible */ * same grfid, as it most likely is compatible */
f = FindGRFConfig(c->ident.grfid, FGCM_COMPATIBLE, nullptr, c->version); f = FindGRFConfig(c->ident.grfid, FGCM_COMPATIBLE, nullptr, c->version);
if (f != nullptr) { 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, MD5SumToString(c->ident.md5sum));
Debug(grf, 1, "NewGRF {:08X} ({}) not found; checksum {}. Compatibility mode on", BSWAP32(c->ident.grfid), c->filename, buf);
if (!HasBit(c->flags, GCF_COMPATIBLE)) { if (!HasBit(c->flags, GCF_COMPATIBLE)) {
/* Preserve original_md5sum after it has been assigned */ /* Preserve original_md5sum after it has been assigned */
SetBit(c->flags, GCF_COMPATIBLE); SetBit(c->flags, GCF_COMPATIBLE);
@ -537,8 +534,7 @@ GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig)
} }
/* No compatible grf was found, mark it as disabled */ /* 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, MD5SumToString(c->ident.md5sum));
Debug(grf, 0, "NewGRF {:08X} ({}) not found; checksum {}", BSWAP32(c->ident.grfid), c->filename, buf);
c->status = GCS_NOT_FOUND; c->status = GCS_NOT_FOUND;
res = GLC_NOT_FOUND; res = GLC_NOT_FOUND;

View File

@ -92,9 +92,8 @@ static void ShowNewGRFInfo(const GRFConfig *c, const Rect &r, bool show_params)
} }
/* Prepare and draw GRF ID */ /* Prepare and draw GRF ID */
char buff[256]; std::string tmp = fmt::format("{:08X}", BSWAP32(c->ident.grfid));
seprintf(buff, lastof(buff), "%08X", BSWAP32(c->ident.grfid)); SetDParamStr(0, tmp);
SetDParamStr(0, buff);
tr.top = DrawStringMultiLine(tr, STR_NEWGRF_SETTINGS_GRF_ID); 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) { if ((_settings_client.gui.newgrf_developer_tools || _settings_client.gui.newgrf_show_old_versions) && c->version != 0) {
@ -107,13 +106,14 @@ static void ShowNewGRFInfo(const GRFConfig *c, const Rect &r, bool show_params)
} }
/* Prepare and draw MD5 sum */ /* Prepare and draw MD5 sum */
md5sumToString(buff, lastof(buff), c->ident.md5sum); tmp = MD5SumToString(c->ident.md5sum);
SetDParamStr(0, buff); SetDParamStr(0, tmp);
tr.top = DrawStringMultiLine(tr, STR_NEWGRF_SETTINGS_MD5SUM); tr.top = DrawStringMultiLine(tr, STR_NEWGRF_SETTINGS_MD5SUM);
/* Show GRF parameter list */ /* Show GRF parameter list */
if (show_params) { if (show_params) {
if (c->num_params > 0) { if (c->num_params > 0) {
char buff[256];
GRFBuildParamList(buff, c, lastof(buff)); GRFBuildParamList(buff, c, lastof(buff));
SetDParam(0, STR_JUST_RAW_STRING); SetDParam(0, STR_JUST_RAW_STRING);
SetDParamStr(1, buff); SetDParamStr(1, buff);

View File

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

View File

@ -388,16 +388,12 @@ static void CDECL HandleSavegameLoadCrash(int signum)
for (const GRFConfig *c = _grfconfig; c != nullptr; c = c->next) { for (const GRFConfig *c = _grfconfig; c != nullptr; c = c->next) {
if (HasBit(c->flags, GCF_COMPATIBLE)) { if (HasBit(c->flags, GCF_COMPATIBLE)) {
const GRFIdentifier *replaced = _gamelog.GetOverriddenIdentifier(c); const GRFIdentifier *replaced = _gamelog.GetOverriddenIdentifier(c);
char original_md5[40]; fmt::format_to(std::back_inserter(message), "NewGRF {:08X} (checksum {}) not found.\n Loaded NewGRF \"{}\" (checksum {}) with same GRF ID instead.\n",
char replaced_md5[40]; BSWAP32(c->ident.grfid), MD5SumToString(c->original_md5sum), c->filename, MD5SumToString(replaced->md5sum));
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);
} }
if (c->status == GCS_NOT_FOUND) { if (c->status == GCS_NOT_FOUND) {
char buf[40]; fmt::format_to(std::back_inserter(message), "NewGRF {:08X} ({}) not found; checksum {}.\n",
md5sumToString(buf, lastof(buf), c->ident.md5sum); BSWAP32(c->ident.grfid), c->filename, MD5SumToString(c->ident.md5sum));
fmt::format_to(std::back_inserter(message), "NewGRF {:08X} ({}) not found; checksum {}.\n", BSWAP32(c->ident.grfid), c->filename, buf);
} }
} }
} else { } else {

View File

@ -320,9 +320,7 @@ static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *user
fmt::format_to(std::back_inserter(message), "Graphics set: {} ({})\n", BaseGraphics::GetUsedSet()->name, BaseGraphics::GetUsedSet()->version); fmt::format_to(std::back_inserter(message), "Graphics set: {} ({})\n", BaseGraphics::GetUsedSet()->name, BaseGraphics::GetUsedSet()->version);
message += "NewGRFs:\n"; message += "NewGRFs:\n";
for (const GRFConfig *c = _game_mode == GM_MENU ? nullptr : _grfconfig; c != nullptr; c = c->next) { for (const GRFConfig *c = _game_mode == GM_MENU ? nullptr : _grfconfig; c != nullptr; c = c->next) {
char buf[33]; fmt::format_to(std::back_inserter(message), "{:08X} {} {}\n", BSWAP32(c->ident.grfid), MD5SumToString(c->ident.md5sum), c->filename);
md5sumToString(buf, lastof(buf), c->ident.md5sum);
fmt::format_to(std::back_inserter(message), "{:08X} {} {}\n", BSWAP32(c->ident.grfid), buf, c->filename);
} }
message += "\nCompanies:\n"; message += "\nCompanies:\n";
for (const Company *c : Company::Iterate()) { for (const Company *c : Company::Iterate()) {

View File

@ -1135,14 +1135,11 @@ static void GRFSaveConfig(IniFile &ini, const char *grpname, const GRFConfig *li
const GRFConfig *c; const GRFConfig *c;
for (c = list; c != nullptr; c = c->next) { 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]; char params[512];
GRFBuildParamList(params, c, lastof(params)); GRFBuildParamList(params, c, lastof(params));
char *pos = key + seprintf(key, lastof(key), "%08X|", BSWAP32(c->ident.grfid)); std::string key = fmt::format("{:08X}|{}|{}", BSWAP32(c->ident.grfid),
pos = md5sumToString(pos, lastof(key), c->ident.md5sum); MD5SumToString(c->ident.md5sum), c->filename);
seprintf(pos, lastof(key), "|%s", c->filename);
group->GetItem(key, true)->SetValue(params); group->GetItem(key, true)->SetValue(params);
} }
} }

View File

@ -612,20 +612,12 @@ int CDECL seprintf(char *str, const char *last, const char *format, ...)
/** /**
* Convert the md5sum to a hexadecimal string representation * 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 * @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; return FormatArrayAsHex({md5sum, 16});
for (uint i = 0; i < 16; i++) {
p += seprintf(p, last, "%02X", md5sum[i]);
}
return p;
} }

View File

@ -86,7 +86,7 @@ static inline size_t ttd_strnlen(const char *str, size_t maxlen)
return t - str; 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); bool IsValidChar(WChar key, CharSetFilter afilter);