diff --git a/src/core/pool_func.hpp b/src/core/pool_func.hpp index 0857a5142b..ea4b7b860a 100644 --- a/src/core/pool_func.hpp +++ b/src/core/pool_func.hpp @@ -14,7 +14,7 @@ #include "mem_func.hpp" #include "pool_type.hpp" -extern void NORETURN SlErrorCorruptFmt(const char *format, ...) WARN_FORMAT(1, 2); +#include "../saveload/saveload_error.hpp" // SlErrorCorruptFmt /** * Helper for defining the method's signature. @@ -157,13 +157,13 @@ DEFINE_POOL_METHOD(void *)::GetNew(size_t size) DEFINE_POOL_METHOD(void *)::GetNew(size_t size, size_t index) { if (index >= Tmax_size) { - SlErrorCorruptFmt("%s index " PRINTF_SIZE " out of range (" PRINTF_SIZE ")", this->name, index, Tmax_size); + SlErrorCorruptFmt("{} index {} out of range ({})", this->name, index, Tmax_size); } if (index >= this->size) this->ResizeFor(index); if (this->data[index] != nullptr) { - SlErrorCorruptFmt("%s index " PRINTF_SIZE " already in use", this->name, index); + SlErrorCorruptFmt("{} index {} already in use", this->name, index); } return this->AllocateItem(size, index); diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index 604c6292e1..5ede15b4ab 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -315,7 +315,7 @@ static void SlNullPointers() _sl_version = SAVEGAME_VERSION; for (const ChunkHandler &ch : ChunkHandlers()) { - Debug(sl, 3, "Nulling pointers for {:c}{:c}{:c}{:c}", ch.id >> 24, ch.id >> 16, ch.id >> 8, ch.id); + Debug(sl, 3, "Nulling pointers for {}", ch.GetName()); ch.FixPointers(); } @@ -367,27 +367,6 @@ void NORETURN SlErrorCorrupt(const char *msg) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, msg); } -void NORETURN SlErrorCorruptFmt(const char *format, ...) WARN_FORMAT(1, 2); - -/** - * Issue an SlErrorCorrupt with a format string. - * @param format format string - * @param ... arguments to format string - * @note This function does never return as it throws an exception to - * break out of all the saveload code. - */ -void NORETURN SlErrorCorruptFmt(const char *format, ...) -{ - va_list ap; - char msg[256]; - - va_start(ap, format); - vseprintf(msg, lastof(msg), format, ap); - va_end(ap); - - SlErrorCorrupt(msg); -} - typedef void (*AsyncSaveFinishProc)(); ///< Callback for when the savegame loading is finished. static std::atomic _async_save_finish; ///< Callback to call when the savegame loading is finished. @@ -2242,7 +2221,7 @@ static void SlSaveChunk(const ChunkHandler &ch) if (ch.type == CH_READONLY) return; SlWriteUint32(ch.id); - Debug(sl, 2, "Saving chunk {:c}{:c}{:c}{:c}", ch.id >> 24, ch.id >> 16, ch.id >> 8, ch.id); + Debug(sl, 2, "Saving chunk {}", ch.GetName()); _sl.block_mode = ch.type; _sl.expect_table_header = (_sl.block_mode == CH_TABLE || _sl.block_mode == CH_SPARSE_TABLE); @@ -2331,7 +2310,7 @@ static void SlFixPointers() _sl.action = SLA_PTRS; for (const ChunkHandler &ch : ChunkHandlers()) { - Debug(sl, 3, "Fixing pointers for {:c}{:c}{:c}{:c}", ch.id >> 24, ch.id >> 16, ch.id >> 8, ch.id); + Debug(sl, 3, "Fixing pointers for {}", ch.GetName()); ch.FixPointers(); } diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h index 539992f5bb..0cc9cbaaa3 100644 --- a/src/saveload/saveload.h +++ b/src/saveload/saveload.h @@ -10,9 +10,9 @@ #ifndef SAVELOAD_H #define SAVELOAD_H +#include "saveload_error.hpp" #include "../fileio_type.h" #include "../fios.h" -#include "../strings_type.h" #include "../core/span_type.hpp" #include #include @@ -449,6 +449,15 @@ struct ChunkHandler { * @param len Number of bytes to skip. */ virtual void LoadCheck(size_t len = 0) const; + + std::string GetName() const + { + return std::string() + + static_cast(this->id >> 24) + + static_cast(this->id >> 16) + + static_cast(this->id >> 8) + + static_cast(this->id); + } }; /** A reference to ChunkHandler. */ @@ -1164,8 +1173,6 @@ void SlCopy(void *object, size_t length, VarType conv); std::vector SlTableHeader(const SaveLoadTable &slt); std::vector SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct); void SlObject(void *object, const SaveLoadTable &slt); -void NORETURN SlError(StringID string, const char *extra_msg = nullptr); -void NORETURN SlErrorCorrupt(const char *msg); bool SaveloadCrashWithMissingNewGRFs(); diff --git a/src/saveload/saveload_error.hpp b/src/saveload/saveload_error.hpp new file mode 100644 index 0000000000..77025f5a33 --- /dev/null +++ b/src/saveload/saveload_error.hpp @@ -0,0 +1,34 @@ +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file saveload.h Functions/types related to errors from savegames. */ + +#ifndef SAVELOAD_ERROR_HPP +#define SAVELOAD_ERROR_HPP + +#include "../3rdparty/fmt/format.h" +#include "../strings_type.h" + +void NORETURN SlError(StringID string, const char *extra_msg = nullptr); +void NORETURN SlErrorCorrupt(const char *msg); + +/** + * Issue an SlErrorCorrupt with a format string. + * @param format_string The formatting string to tell what to do with the remaining arguments. + * @param fmt_args The arguments to be passed to fmt. + * @tparam T The type of formatting parameter. + * @tparam Args The types of the fmt arguments. + * @note This function does never return as it throws an exception to + * break out of all the saveload code. + */ +template +static inline void NORETURN SlErrorCorruptFmt(const T &format, Args&&... fmt_args) +{ + SlErrorCorrupt(fmt::format(format, fmt_args...).c_str()); +} + +#endif /* SAVELOAD_ERROR_HPP */