Mercurial > hg > openttd
changeset 18629:a55c2ad0bf2c draft
(svn r23476) -Codechange: use the error queue to replace switch mode error strings, again making it possible to return multiple errors
author | rubidium <rubidium@openttd.org> |
---|---|
date | Sat, 10 Dec 2011 15:16:58 +0000 |
parents | 718c69c25a56 |
children | 0a134b29df46 |
files | src/error.h src/error_gui.cpp src/network/core/tcp_game.cpp src/network/network.cpp src/network/network_client.cpp src/openttd.cpp src/saveload/afterload.cpp src/town_cmd.cpp src/window.cpp |
diffstat | 9 files changed, 58 insertions(+), 42 deletions(-) [+] |
line wrap: on
line diff
--- a/src/error.h +++ b/src/error.h @@ -24,5 +24,7 @@ void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x = 0, int y = 0, uint textref_stack_size = 0, const uint32 *textref_stack = NULL); void ClearErrorMessages(); +void ShowFirstError(); +void UnshowCriticalError(); #endif /* ERROR_H */
--- a/src/error_gui.cpp +++ b/src/error_gui.cpp @@ -127,6 +127,8 @@ typedef std::list<ErrorMessageData> ErrorList; /** The actual queue with errors. */ ErrorList _errors; +/** Whether the window system is initialized or not. */ +bool _window_system_initialized = false; /** Window class for displaying an error message window. */ struct ErrmsgWindow : public Window, ErrorMessageData { @@ -261,11 +263,7 @@ ~ErrmsgWindow() { SetRedErrorSquare(INVALID_TILE); - - if (!_errors.empty()) { - new ErrmsgWindow(_errors.front()); - _errors.pop_front(); - } + if (_window_system_initialized) ShowFirstError(); } virtual EventState OnKeyPress(uint16 key, uint16 keycode) @@ -293,6 +291,30 @@ _errors.clear(); } +/** Show the first error of the queue. */ +void ShowFirstError() +{ + _window_system_initialized = true; + if (!_errors.empty()) { + new ErrmsgWindow(_errors.front()); + _errors.pop_front(); + } +} + +/** + * Unshow the critical error. This has to happen when a critical + * error is shown and we uninitialise the window system, i.e. + * remove all the windows. + */ +void UnshowCriticalError() +{ + ErrmsgWindow *w = (ErrmsgWindow*)FindWindowById(WC_ERRMSG, 0); + if (w != NULL) { + if (w->IsCritical()) _errors.push_front(*w); + _window_system_initialized = false; + } +} + /** * Display an error message in a window. * @param summary_msg General error message showed in first line. Must be valid.
--- a/src/network/core/tcp_game.cpp +++ b/src/network/core/tcp_game.cpp @@ -18,6 +18,7 @@ #include "../network.h" #include "../network_internal.h" #include "../../debug.h" +#include "../../error.h" #include "table/strings.h" @@ -46,8 +47,7 @@ if (!_network_server && _networking) { _switch_mode = SM_MENU; _networking = false; - extern StringID _switch_mode_errorstr; - _switch_mode_errorstr = STR_NETWORK_ERROR_LOSTCONNECTION; + ShowErrorMessage(STR_NETWORK_ERROR_LOSTCONNECTION, INVALID_STRING_ID, WL_CRITICAL); return NETWORK_RECV_STATUS_CONN_LOST; }
--- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -36,6 +36,7 @@ #include "../rev.h" #include "../core/pool_func.hpp" #include "../gfx_func.h" +#include "../error.h" #include "table/strings.h" #ifdef DEBUG_DUMP_COMMANDS @@ -287,8 +288,7 @@ void NetworkError(StringID error_string) { _switch_mode = SM_MENU; - extern StringID _switch_mode_errorstr; - _switch_mode_errorstr = error_string; + ShowErrorMessage(error_string, INVALID_STRING_ID, WL_CRITICAL); } /**
--- a/src/network/network_client.cpp +++ b/src/network/network_client.cpp @@ -526,12 +526,11 @@ ************/ extern bool SafeLoad(const char *filename, int mode, GameMode newgm, Subdirectory subdir, struct LoadFilter *lf = NULL); -extern StringID _switch_mode_errorstr; NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_FULL(Packet *p) { /* We try to join a server which is full */ - _switch_mode_errorstr = STR_NETWORK_ERROR_SERVER_FULL; + ShowErrorMessage(STR_NETWORK_ERROR_SERVER_FULL, INVALID_STRING_ID, WL_CRITICAL); DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0); return NETWORK_RECV_STATUS_SERVER_FULL; @@ -540,7 +539,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_BANNED(Packet *p) { /* We try to join a server where we are banned */ - _switch_mode_errorstr = STR_NETWORK_ERROR_SERVER_BANNED; + ShowErrorMessage(STR_NETWORK_ERROR_SERVER_BANNED, INVALID_STRING_ID, WL_CRITICAL); DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0); return NETWORK_RECV_STATUS_SERVER_BANNED; @@ -652,28 +651,28 @@ case NETWORK_ERROR_NOT_AUTHORIZED: case NETWORK_ERROR_NOT_EXPECTED: case NETWORK_ERROR_COMPANY_MISMATCH: - _switch_mode_errorstr = STR_NETWORK_ERROR_SERVER_ERROR; + ShowErrorMessage(STR_NETWORK_ERROR_SERVER_ERROR, INVALID_STRING_ID, WL_CRITICAL); break; case NETWORK_ERROR_FULL: - _switch_mode_errorstr = STR_NETWORK_ERROR_SERVER_FULL; + ShowErrorMessage(STR_NETWORK_ERROR_SERVER_FULL, INVALID_STRING_ID, WL_CRITICAL); break; case NETWORK_ERROR_WRONG_REVISION: - _switch_mode_errorstr = STR_NETWORK_ERROR_WRONG_REVISION; + ShowErrorMessage(STR_NETWORK_ERROR_WRONG_REVISION, INVALID_STRING_ID, WL_CRITICAL); break; case NETWORK_ERROR_WRONG_PASSWORD: - _switch_mode_errorstr = STR_NETWORK_ERROR_WRONG_PASSWORD; + ShowErrorMessage(STR_NETWORK_ERROR_WRONG_PASSWORD, INVALID_STRING_ID, WL_CRITICAL); break; case NETWORK_ERROR_KICKED: - _switch_mode_errorstr = STR_NETWORK_ERROR_KICKED; + ShowErrorMessage(STR_NETWORK_ERROR_KICKED, INVALID_STRING_ID, WL_CRITICAL); break; case NETWORK_ERROR_CHEATER: - _switch_mode_errorstr = STR_NETWORK_ERROR_CHEATER; + ShowErrorMessage(STR_NETWORK_ERROR_CHEATER, INVALID_STRING_ID, WL_CRITICAL); break; case NETWORK_ERROR_TOO_MANY_COMMANDS: - _switch_mode_errorstr = STR_NETWORK_ERROR_TOO_MANY_COMMANDS; + ShowErrorMessage(STR_NETWORK_ERROR_TOO_MANY_COMMANDS, INVALID_STRING_ID, WL_CRITICAL); break; default: - _switch_mode_errorstr = STR_NETWORK_ERROR_LOSTCONNECTION; + ShowErrorMessage(STR_NETWORK_ERROR_LOSTCONNECTION, INVALID_STRING_ID, WL_CRITICAL); } DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0); @@ -710,7 +709,7 @@ } /* NewGRF mismatch, bail out */ - _switch_mode_errorstr = STR_NETWORK_ERROR_NEWGRF_MISMATCH; + ShowErrorMessage(STR_NETWORK_ERROR_NEWGRF_MISMATCH, INVALID_STRING_ID, WL_CRITICAL); return ret; } @@ -849,7 +848,7 @@ if (!load_success) { DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0); - _switch_mode_errorstr = STR_NETWORK_ERROR_SAVEGAMEERROR; + ShowErrorMessage(STR_NETWORK_ERROR_SAVEGAMEERROR, INVALID_STRING_ID, WL_CRITICAL); return NETWORK_RECV_STATUS_SAVEGAME; } /* If the savegame has successfully loaded, ALL windows have been removed, @@ -1053,7 +1052,7 @@ /* Only when we're trying to join we really * care about the server shutting down. */ if (this->status >= STATUS_JOIN) { - _switch_mode_errorstr = STR_NETWORK_MESSAGE_SERVER_SHUTDOWN; + ShowErrorMessage(STR_NETWORK_MESSAGE_SERVER_SHUTDOWN, INVALID_STRING_ID, WL_CRITICAL); } return NETWORK_RECV_STATUS_SERVER_ERROR; @@ -1068,7 +1067,7 @@ * Client ID modulo 16. This way reconnects should be spread * out a bit. */ _network_reconnect = _network_own_client_id % 16; - _switch_mode_errorstr = STR_NETWORK_MESSAGE_SERVER_REBOOT; + ShowErrorMessage(STR_NETWORK_MESSAGE_SERVER_REBOOT, INVALID_STRING_ID, WL_CRITICAL); } return NETWORK_RECV_STATUS_SERVER_ERROR; @@ -1157,7 +1156,7 @@ * the server will forcefully disconnect you. */ if (lag > 20) { this->NetworkGameSocketHandler::CloseConnection(); - _switch_mode_errorstr = STR_NETWORK_ERROR_LOSTCONNECTION; + ShowErrorMessage(STR_NETWORK_ERROR_LOSTCONNECTION, INVALID_STRING_ID, WL_CRITICAL); return; }
--- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -71,9 +71,6 @@ #include "table/strings.h" -/** Error message to show when switching modes. */ -StringID _switch_mode_errorstr; - void CallLandscapeTick(); void IncreaseDate(); void DoPaletteAnimations(); @@ -524,7 +521,6 @@ _game_mode = GM_MENU; _switch_mode = SM_MENU; - _switch_mode_errorstr = INVALID_STRING_ID; _config_file = NULL; GetOptData mgo(argc - 1, argv + 1, _options); @@ -1103,11 +1099,6 @@ default: NOT_REACHED(); } - - if (_switch_mode_errorstr != INVALID_STRING_ID) { - ShowErrorMessage(_switch_mode_errorstr, INVALID_STRING_ID, WL_CRITICAL); - _switch_mode_errorstr = INVALID_STRING_ID; - } }
--- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -53,6 +53,7 @@ #include "../smallmap_gui.h" #include "../news_func.h" #include "../group.h" +#include "../error.h" #include "table/strings.h" @@ -60,7 +61,6 @@ #include <signal.h> -extern StringID _switch_mode_errorstr; extern Company *DoStartupNewCompany(bool is_ai, CompanyID company = INVALID_COMPANY); /** @@ -622,8 +622,8 @@ } switch (gcf_res) { - case GLC_COMPATIBLE: _switch_mode_errorstr = STR_NEWGRF_COMPATIBLE_LOAD_WARNING; break; - case GLC_NOT_FOUND: _switch_mode_errorstr = STR_NEWGRF_DISABLED_WARNING; _pause_mode = PM_PAUSED_ERROR; break; + case GLC_COMPATIBLE: ShowErrorMessage(STR_NEWGRF_COMPATIBLE_LOAD_WARNING, INVALID_STRING_ID, WL_CRITICAL); break; + case GLC_NOT_FOUND: ShowErrorMessage(STR_NEWGRF_DISABLED_WARNING, INVALID_STRING_ID, WL_CRITICAL); _pause_mode = PM_PAUSED_ERROR; break; default: break; } @@ -1756,9 +1756,7 @@ RoadVehicle *v; FOR_ALL_ROADVEHICLES(v) { if (v->First() == v && HasBit(EngInfo(v->engine_type)->misc_flags, EF_ROAD_TRAM)) { - if (_switch_mode_errorstr == INVALID_STRING_ID || _switch_mode_errorstr == STR_NEWGRF_COMPATIBLE_LOAD_WARNING) { - _switch_mode_errorstr = STR_WARNING_LOADGAME_REMOVED_TRAMS; - } + ShowErrorMessage(STR_WARNING_LOADGAME_REMOVED_TRAMS, INVALID_STRING_ID, WL_CRITICAL); delete v; } }
--- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -1896,8 +1896,7 @@ /* If there are no towns at all and we are generating new game, bail out */ if (Town::GetNumItems() == 0 && _game_mode != GM_EDITOR) { - extern StringID _switch_mode_errorstr; - _switch_mode_errorstr = STR_ERROR_COULD_NOT_CREATE_TOWN; + ShowErrorMessage(STR_ERROR_COULD_NOT_CREATE_TOWN, INVALID_STRING_ID, WL_CRITICAL); } return false; // we are still without a town? we failed, simply
--- a/src/window.cpp +++ b/src/window.cpp @@ -32,6 +32,7 @@ #include "hotkeys.h" #include "toolbar_gui.h" #include "statusbar_gui.h" +#include "error.h" static Point _drag_delta; ///< delta between mouse cursor and upper left corner of dragged window @@ -1448,6 +1449,8 @@ NWidgetLeaf::InvalidateDimensionCache(); // Reset cached sizes of several widgets. NWidgetScrollbar::InvalidateDimensionCache(); + + ShowFirstError(); } /** @@ -1455,6 +1458,8 @@ */ void UnInitWindowSystem() { + UnshowCriticalError(); + Window *w; FOR_ALL_WINDOWS_FROM_FRONT(w) delete w;