OpenTTD Source 20250331-master-g3c15e0c889
newgrf_actb.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 <http://www.gnu.org/licenses/>.
6 */
7
10#include "../stdafx.h"
11#include "../debug.h"
12#include "newgrf_bytereader.h"
13#include "newgrf_internal.h"
14
15#include "table/strings.h"
16
17#include "../safeguards.h"
18
19/* Action 0x0B */
20static void GRFLoadError(ByteReader &buf)
21{
22 /* <0B> <severity> <language-id> <message-id> [<message...> 00] [<data...>] 00 [<parnum>]
23 *
24 * B severity 00: notice, continue loading grf file
25 * 01: warning, continue loading grf file
26 * 02: error, but continue loading grf file, and attempt
27 * loading grf again when loading or starting next game
28 * 03: error, abort loading and prevent loading again in
29 * the future (only when restarting the patch)
30 * B language-id see action 4, use 1F for built-in error messages
31 * B message-id message to show, see below
32 * S message for custom messages (message-id FF), text of the message
33 * not present for built-in messages.
34 * V data additional data for built-in (or custom) messages
35 * B parnum parameter numbers to be shown in the message (maximum of 2) */
36
37 static const StringID msgstr[] = {
38 STR_NEWGRF_ERROR_VERSION_NUMBER,
39 STR_NEWGRF_ERROR_DOS_OR_WINDOWS,
40 STR_NEWGRF_ERROR_UNSET_SWITCH,
41 STR_NEWGRF_ERROR_INVALID_PARAMETER,
42 STR_NEWGRF_ERROR_LOAD_BEFORE,
43 STR_NEWGRF_ERROR_LOAD_AFTER,
44 STR_NEWGRF_ERROR_OTTD_VERSION_NUMBER,
45 };
46
47 static const StringID sevstr[] = {
48 STR_NEWGRF_ERROR_MSG_INFO,
49 STR_NEWGRF_ERROR_MSG_WARNING,
50 STR_NEWGRF_ERROR_MSG_ERROR,
51 STR_NEWGRF_ERROR_MSG_FATAL
52 };
53
54 uint8_t severity = buf.ReadByte();
55 uint8_t lang = buf.ReadByte();
56 uint8_t message_id = buf.ReadByte();
57
58 /* Skip the error if it isn't valid for the current language. */
59 if (!CheckGrfLangID(lang, _cur.grffile->grf_version)) return;
60
61 /* Skip the error until the activation stage unless bit 7 of the severity
62 * is set. */
63 if (!HasBit(severity, 7) && _cur.stage == GLS_INIT) {
64 GrfMsg(7, "GRFLoadError: Skipping non-fatal GRFLoadError in stage {}", _cur.stage);
65 return;
66 }
67 ClrBit(severity, 7);
68
69 if (severity >= lengthof(sevstr)) {
70 GrfMsg(7, "GRFLoadError: Invalid severity id {}. Setting to 2 (non-fatal error).", severity);
71 severity = 2;
72 } else if (severity == 3) {
73 /* This is a fatal error, so make sure the GRF is deactivated and no
74 * more of it gets loaded. */
75 DisableGrf();
76
77 /* Make sure we show fatal errors, instead of silly infos from before */
78 _cur.grfconfig->error.reset();
79 }
80
81 if (message_id >= lengthof(msgstr) && message_id != 0xFF) {
82 GrfMsg(7, "GRFLoadError: Invalid message id.");
83 return;
84 }
85
86 if (buf.Remaining() <= 1) {
87 GrfMsg(7, "GRFLoadError: No message data supplied.");
88 return;
89 }
90
91 /* For now we can only show one message per newgrf file. */
92 if (_cur.grfconfig->error.has_value()) return;
93
94 _cur.grfconfig->error = {sevstr[severity]};
95 GRFError *error = &_cur.grfconfig->error.value();
96
97 if (message_id == 0xFF) {
98 /* This is a custom error message. */
99 if (buf.HasData()) {
100 std::string_view message = buf.ReadString();
101
102 error->custom_message = TranslateTTDPatchCodes(_cur.grffile->grfid, lang, true, message, SCC_RAW_STRING_POINTER);
103 } else {
104 GrfMsg(7, "GRFLoadError: No custom message supplied.");
105 error->custom_message.clear();
106 }
107 } else {
108 error->message = msgstr[message_id];
109 }
110
111 if (buf.HasData()) {
112 std::string_view data = buf.ReadString();
113
114 error->data = TranslateTTDPatchCodes(_cur.grffile->grfid, lang, true, data);
115 } else {
116 GrfMsg(7, "GRFLoadError: No message data supplied.");
117 error->data.clear();
118 }
119
120 /* Only two parameter numbers can be used in the string. */
121 for (uint i = 0; i < error->param_value.size() && buf.HasData(); i++) {
122 uint param_number = buf.ReadByte();
123 error->param_value[i] = _cur.grffile->GetParam(param_number);
124 }
125}
126
127template <> void GrfActionHandler<0x0B>::FileScan(ByteReader &) { }
129template <> void GrfActionHandler<0x0B>::LabelScan(ByteReader &) { }
130template <> void GrfActionHandler<0x0B>::Init(ByteReader &buf) { GRFLoadError(buf); }
131template <> void GrfActionHandler<0x0B>::Reserve(ByteReader &buf) { GRFLoadError(buf); }
132template <> void GrfActionHandler<0x0B>::Activation(ByteReader &buf) { GRFLoadError(buf); }
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr T ClrBit(T &x, const uint8_t y)
Clears a bit in a variable.
Class to read from a NewGRF file.
std::string_view ReadString()
Read a string.
uint8_t ReadByte()
Read a single byte (8 bits).
GRFError * DisableGrf(StringID message, GRFConfig *config)
Disable a GRF.
Definition newgrf.cpp:131
NewGRF buffer reader definition.
NewGRF internal processing state.
std::string TranslateTTDPatchCodes(uint32_t grfid, uint8_t language_id, bool allow_newlines, std::string_view str, StringControlCode byte80)
Translate TTDPatch string codes into something OpenTTD can handle (better).
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:277
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
std::optional< GRFError > error
NOSAVE: Error/Warning during GRF loading (Action 0x0B)
Information about why GRF had problems during initialisation.
std::array< uint32_t, 2 > param_value
Values of GRF parameters to show for message and custom_message.
StringID message
Default message.
std::string custom_message
Custom message (if present)
std::string data
Additional data for message and custom_message.
uint32_t GetParam(uint number) const
Get GRF Parameter with range checking.
Definition newgrf.h:162
GRF action handler.
GRFFile * grffile
Currently processed GRF file.
GRFConfig * grfconfig
Config of the currently processed GRF file.
GrfLoadingStage stage
Current loading stage.