mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Replace GetSavegameFormat's compression output pointer with std::pair return. (#12850)
This avoids using an unchecked pointer as an out-parameter.pull/12852/head
parent
100dd7b6d1
commit
b4bcb330c7
|
@ -2679,10 +2679,9 @@ static const SaveLoadFormat _saveload_formats[] = {
|
||||||
* Return the savegameformat of the game. Whether it was created with ZLIB compression
|
* Return the savegameformat of the game. Whether it was created with ZLIB compression
|
||||||
* 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.
|
* @return Pair containing reference to SaveLoadFormat struct giving all characteristics of this type of savegame, and a compression level to use.
|
||||||
* @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 std::pair<const SaveLoadFormat &, uint8_t> GetSavegameFormat(const std::string &full_name)
|
||||||
{
|
{
|
||||||
/* Find default savegame format, the highest one with which files can be written. */
|
/* 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; });
|
auto it = std::find_if(std::rbegin(_saveload_formats), std::rend(_saveload_formats), [](const auto &slf) { return slf.init_write != nullptr; });
|
||||||
|
@ -2698,7 +2697,6 @@ static const SaveLoadFormat &GetSavegameFormat(const std::string &full_name, uin
|
||||||
|
|
||||||
for (const auto &slf : _saveload_formats) {
|
for (const auto &slf : _saveload_formats) {
|
||||||
if (slf.init_write != nullptr && name.compare(slf.name) == 0) {
|
if (slf.init_write != nullptr && name.compare(slf.name) == 0) {
|
||||||
*compression_level = slf.default_compression;
|
|
||||||
if (has_comp_level) {
|
if (has_comp_level) {
|
||||||
const std::string complevel(full_name, separator + 1);
|
const std::string complevel(full_name, separator + 1);
|
||||||
|
|
||||||
|
@ -2709,10 +2707,10 @@ static const SaveLoadFormat &GetSavegameFormat(const std::string &full_name, uin
|
||||||
SetDParamStr(0, complevel);
|
SetDParamStr(0, complevel);
|
||||||
ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_LEVEL, WL_CRITICAL);
|
ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_SAVEGAME_COMPRESSION_LEVEL, WL_CRITICAL);
|
||||||
} else {
|
} else {
|
||||||
*compression_level = level;
|
return {slf, ClampTo<uint8_t>(level)};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return slf;
|
return {slf, slf.default_compression};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2720,8 +2718,7 @@ static const SaveLoadFormat &GetSavegameFormat(const std::string &full_name, uin
|
||||||
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;
|
return {def, def.default_compression};
|
||||||
return def;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* actual loader/saver function */
|
/* actual loader/saver function */
|
||||||
|
@ -2805,8 +2802,7 @@ static void SaveFileError()
|
||||||
static SaveOrLoadResult SaveFileToDisk(bool threaded)
|
static SaveOrLoadResult SaveFileToDisk(bool threaded)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
uint8_t compression;
|
auto [fmt, compression] = GetSavegameFormat(_savegame_format);
|
||||||
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) };
|
||||||
|
|
Loading…
Reference in New Issue