1
0
Fork 0

Codechange: Use find_if to get default writeable savegame format.

This removes the last of lastof.
pull/12849/head
Peter Nelson 2024-07-09 00:57:30 +01:00
parent 56b0eac2e9
commit 8ed33d1e6f
No known key found for this signature in database
GPG Key ID: 8EF8F0A467DF75ED
1 changed files with 12 additions and 11 deletions

View File

@ -2680,14 +2680,15 @@ static const SaveLoadFormat _saveload_formats[] = {
* uncompressed, or another type * uncompressed, or another type
* @param full_name Name of the savegame format. If empty it picks the first available one * @param full_name Name of the savegame format. If empty it picks the first available one
* @param compression_level Output for telling what compression level we want. * @param compression_level Output for telling what compression level we want.
* @return Pointer to SaveLoadFormat struct giving all characteristics of this type of savegame * @return Reference to SaveLoadFormat struct giving all characteristics of this type of savegame
*/ */
static const SaveLoadFormat *GetSavegameFormat(const std::string &full_name, uint8_t *compression_level) static const SaveLoadFormat &GetSavegameFormat(const std::string &full_name, uint8_t *compression_level)
{ {
const SaveLoadFormat *def = lastof(_saveload_formats); /* Find default savegame format, the highest one with which files can be written. */
auto it = std::find_if(std::rbegin(_saveload_formats), std::rend(_saveload_formats), [](const auto &slf) { return slf.init_write != nullptr; });
if (it == std::rend(_saveload_formats)) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "no writeable savegame formats");
/* find default savegame format, the highest one with which files can be written */ const SaveLoadFormat &def = *it;
while (!def->init_write) def--;
if (!full_name.empty()) { if (!full_name.empty()) {
/* Get the ":..." of the compression level out of the way */ /* Get the ":..." of the compression level out of the way */
@ -2711,15 +2712,15 @@ static const SaveLoadFormat *GetSavegameFormat(const std::string &full_name, uin
*compression_level = level; *compression_level = level;
} }
} }
return &slf; return slf;
} }
} }
SetDParamStr(0, name); SetDParamStr(0, name);
SetDParamStr(1, def->name); SetDParamStr(1, def.name);
ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_ALGORITHM, WL_CRITICAL); ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_ALGORITHM, WL_CRITICAL);
} }
*compression_level = def->default_compression; *compression_level = def.default_compression;
return def; return def;
} }
@ -2805,13 +2806,13 @@ static SaveOrLoadResult SaveFileToDisk(bool threaded)
{ {
try { try {
uint8_t compression; uint8_t compression;
const SaveLoadFormat *fmt = GetSavegameFormat(_savegame_format, &compression); const SaveLoadFormat &fmt = GetSavegameFormat(_savegame_format, &compression);
/* We have written our stuff to memory, now write it to file! */ /* We have written our stuff to memory, now write it to file! */
uint32_t hdr[2] = { fmt->tag, TO_BE32(SAVEGAME_VERSION << 16) }; uint32_t hdr[2] = { fmt.tag, TO_BE32(SAVEGAME_VERSION << 16) };
_sl.sf->Write((uint8_t*)hdr, sizeof(hdr)); _sl.sf->Write((uint8_t*)hdr, sizeof(hdr));
_sl.sf = fmt->init_write(_sl.sf, compression); _sl.sf = fmt.init_write(_sl.sf, compression);
_sl.dumper->Flush(_sl.sf); _sl.dumper->Flush(_sl.sf);
ClearSaveLoadState(); ClearSaveLoadState();