mirror of https://github.com/OpenTTD/OpenTTD
Codechange: use std::string instead of stredup for saveload error messages
parent
8665404fe0
commit
1f3b7e2efd
|
@ -32,7 +32,7 @@ typedef SmallMap<uint, CompanyProperties *> CompanyPropertiesMap;
|
||||||
struct LoadCheckData {
|
struct LoadCheckData {
|
||||||
bool checkable; ///< True if the savegame could be checked by SL_LOAD_CHECK. (Old savegames are not checkable.)
|
bool checkable; ///< True if the savegame could be checked by SL_LOAD_CHECK. (Old savegames are not checkable.)
|
||||||
StringID error; ///< Error message from loading. INVALID_STRING_ID if no error.
|
StringID error; ///< Error message from loading. INVALID_STRING_ID if no error.
|
||||||
char *error_data; ///< Data to pass to SetDParamStr when displaying #error.
|
std::string error_msg; ///< Data to pass to SetDParamStr when displaying #error.
|
||||||
|
|
||||||
uint32 map_size_x, map_size_y;
|
uint32 map_size_x, map_size_y;
|
||||||
TimerGameCalendar::Date current_date;
|
TimerGameCalendar::Date current_date;
|
||||||
|
@ -47,7 +47,7 @@ struct LoadCheckData {
|
||||||
struct LoggedAction *gamelog_action; ///< Gamelog actions
|
struct LoggedAction *gamelog_action; ///< Gamelog actions
|
||||||
uint gamelog_actions; ///< Number of gamelog actions
|
uint gamelog_actions; ///< Number of gamelog actions
|
||||||
|
|
||||||
LoadCheckData() : error_data(nullptr), grfconfig(nullptr),
|
LoadCheckData() : grfconfig(nullptr),
|
||||||
grf_compatibility(GLC_NOT_FOUND), gamelog_action(nullptr), gamelog_actions(0)
|
grf_compatibility(GLC_NOT_FOUND), gamelog_action(nullptr), gamelog_actions(0)
|
||||||
{
|
{
|
||||||
this->Clear();
|
this->Clear();
|
||||||
|
|
|
@ -49,8 +49,7 @@ void LoadCheckData::Clear()
|
||||||
{
|
{
|
||||||
this->checkable = false;
|
this->checkable = false;
|
||||||
this->error = INVALID_STRING_ID;
|
this->error = INVALID_STRING_ID;
|
||||||
free(this->error_data);
|
this->error_msg.clear();
|
||||||
this->error_data = nullptr;
|
|
||||||
|
|
||||||
this->map_size_x = this->map_size_y = 256; // Default for old savegames which do not store mapsize.
|
this->map_size_x = this->map_size_y = 256; // Default for old savegames which do not store mapsize.
|
||||||
this->current_date = 0;
|
this->current_date = 0;
|
||||||
|
@ -500,7 +499,7 @@ public:
|
||||||
tr.top += FONT_HEIGHT_NORMAL;
|
tr.top += FONT_HEIGHT_NORMAL;
|
||||||
} else if (_load_check_data.error != INVALID_STRING_ID) {
|
} else if (_load_check_data.error != INVALID_STRING_ID) {
|
||||||
/* Incompatible / broken savegame */
|
/* Incompatible / broken savegame */
|
||||||
SetDParamStr(0, _load_check_data.error_data);
|
SetDParamStr(0, _load_check_data.error_msg);
|
||||||
tr.top = DrawStringMultiLine(tr, _load_check_data.error, TC_RED);
|
tr.top = DrawStringMultiLine(tr, _load_check_data.error, TC_RED);
|
||||||
} else {
|
} else {
|
||||||
/* Mapsize */
|
/* Mapsize */
|
||||||
|
|
|
@ -623,7 +623,7 @@ int openttd_main(int argc, char *argv[])
|
||||||
if (_load_check_data.HasErrors()) {
|
if (_load_check_data.HasErrors()) {
|
||||||
InitializeLanguagePacks(); // A language pack is needed for GetString()
|
InitializeLanguagePacks(); // A language pack is needed for GetString()
|
||||||
char buf[256];
|
char buf[256];
|
||||||
SetDParamStr(0, _load_check_data.error_data);
|
SetDParamStr(0, _load_check_data.error_msg);
|
||||||
GetString(buf, _load_check_data.error, lastof(buf));
|
GetString(buf, _load_check_data.error, lastof(buf));
|
||||||
fprintf(stderr, "%s\n", buf);
|
fprintf(stderr, "%s\n", buf);
|
||||||
}
|
}
|
||||||
|
|
|
@ -208,7 +208,7 @@ struct SaveLoadParams {
|
||||||
LoadFilter *lf; ///< Filter to read the savegame from.
|
LoadFilter *lf; ///< Filter to read the savegame from.
|
||||||
|
|
||||||
StringID error_str; ///< the translatable error message to show
|
StringID error_str; ///< the translatable error message to show
|
||||||
char *extra_msg; ///< the error message
|
std::string extra_msg; ///< the error message
|
||||||
|
|
||||||
bool saveinprogress; ///< Whether there is currently a save in progress.
|
bool saveinprogress; ///< Whether there is currently a save in progress.
|
||||||
};
|
};
|
||||||
|
@ -330,17 +330,15 @@ static void SlNullPointers()
|
||||||
* @note This function does never return as it throws an exception to
|
* @note This function does never return as it throws an exception to
|
||||||
* break out of all the saveload code.
|
* break out of all the saveload code.
|
||||||
*/
|
*/
|
||||||
void NORETURN SlError(StringID string, const char *extra_msg)
|
void NORETURN SlError(StringID string, const std::string &extra_msg)
|
||||||
{
|
{
|
||||||
/* Distinguish between loading into _load_check_data vs. normal save/load. */
|
/* Distinguish between loading into _load_check_data vs. normal save/load. */
|
||||||
if (_sl.action == SLA_LOAD_CHECK) {
|
if (_sl.action == SLA_LOAD_CHECK) {
|
||||||
_load_check_data.error = string;
|
_load_check_data.error = string;
|
||||||
free(_load_check_data.error_data);
|
_load_check_data.error_msg = extra_msg;
|
||||||
_load_check_data.error_data = (extra_msg == nullptr) ? nullptr : stredup(extra_msg);
|
|
||||||
} else {
|
} else {
|
||||||
_sl.error_str = string;
|
_sl.error_str = string;
|
||||||
free(_sl.extra_msg);
|
_sl.extra_msg = extra_msg;
|
||||||
_sl.extra_msg = (extra_msg == nullptr) ? nullptr : stredup(extra_msg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* We have to nullptr all pointers here; we might be in a state where
|
/* We have to nullptr all pointers here; we might be in a state where
|
||||||
|
@ -362,7 +360,7 @@ void NORETURN SlError(StringID string, const char *extra_msg)
|
||||||
* @note This function does never return as it throws an exception to
|
* @note This function does never return as it throws an exception to
|
||||||
* break out of all the saveload code.
|
* break out of all the saveload code.
|
||||||
*/
|
*/
|
||||||
void NORETURN SlErrorCorrupt(const char *msg)
|
void NORETURN SlErrorCorrupt(const std::string &msg)
|
||||||
{
|
{
|
||||||
SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, msg);
|
SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME, msg);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,8 +13,8 @@
|
||||||
#include "../3rdparty/fmt/format.h"
|
#include "../3rdparty/fmt/format.h"
|
||||||
#include "../strings_type.h"
|
#include "../strings_type.h"
|
||||||
|
|
||||||
void NORETURN SlError(StringID string, const char *extra_msg = nullptr);
|
void NORETURN SlError(StringID string, const std::string &extra_msg = {});
|
||||||
void NORETURN SlErrorCorrupt(const char *msg);
|
void NORETURN SlErrorCorrupt(const std::string &msg);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Issue an SlErrorCorrupt with a format string.
|
* Issue an SlErrorCorrupt with a format string.
|
||||||
|
@ -28,7 +28,7 @@ void NORETURN SlErrorCorrupt(const char *msg);
|
||||||
template <typename T, typename ... Args>
|
template <typename T, typename ... Args>
|
||||||
static inline void NORETURN SlErrorCorruptFmt(const T &format, Args&&... fmt_args)
|
static inline void NORETURN SlErrorCorruptFmt(const T &format, Args&&... fmt_args)
|
||||||
{
|
{
|
||||||
SlErrorCorrupt(fmt::format(format, fmt_args...).c_str());
|
SlErrorCorrupt(fmt::format(format, fmt_args...));
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* SAVELOAD_ERROR_HPP */
|
#endif /* SAVELOAD_ERROR_HPP */
|
||||||
|
|
Loading…
Reference in New Issue