mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Use find_if to get default writeable saveload format. (#12849)
* Codechange: Use find_if to get default writeable savegame format. This removes the last of lastof, and so the lastof macro is removed.pull/12850/head
parent
56b0eac2e9
commit
100dd7b6d1
|
@ -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();
|
||||||
|
|
|
@ -285,14 +285,6 @@ char (&ArraySizeHelper(T (&array)[N]))[N];
|
||||||
*/
|
*/
|
||||||
#define lengthof(array) (sizeof(ArraySizeHelper(array)))
|
#define lengthof(array) (sizeof(ArraySizeHelper(array)))
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the last element of an fixed size array.
|
|
||||||
*
|
|
||||||
* @param x The pointer to the first element of the array
|
|
||||||
* @return The pointer to the last element of the array
|
|
||||||
*/
|
|
||||||
#define lastof(x) (&x[lengthof(x) - 1])
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the size of a variable within a class.
|
* Gets the size of a variable within a class.
|
||||||
* @param base The class the variable is in.
|
* @param base The class the variable is in.
|
||||||
|
|
|
@ -1420,7 +1420,7 @@ public:
|
||||||
uint spacer_i = 0;
|
uint spacer_i = 0;
|
||||||
uint button_i = 0;
|
uint button_i = 0;
|
||||||
|
|
||||||
/* Index into the arrangement indices. The macro lastof cannot be used here! */
|
/* Index into the arrangement indices. */
|
||||||
const WidgetID *slotp = rtl ? &arrangement[arrangable_count - 1] : arrangement;
|
const WidgetID *slotp = rtl ? &arrangement[arrangable_count - 1] : arrangement;
|
||||||
for (uint i = 0; i < arrangable_count; i++) {
|
for (uint i = 0; i < arrangable_count; i++) {
|
||||||
uint slot = lookup[*slotp];
|
uint slot = lookup[*slotp];
|
||||||
|
|
Loading…
Reference in New Issue