OpenTTD Source 20261112-master-gbcbec5e63f
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "../stdafx.h"
11#include "../debug.h"
12#include "newgrf_bytereader.h"
13#include "newgrf_internal.h"
14#include "../newgrf_text.h"
15#include "../language.h"
16
17#include "table/strings.h"
18
19#include "../safeguards.h"
20
21/* Action 0x0B */
22static void GRFLoadError(ByteReader &buf)
23{
24 /* <0B> <severity> <language-id> <message-id> [<message...> 00] [<data...>] 00 [<parnum>]
25 *
26 * B severity 00: notice, continue loading grf file
27 * 01: warning, continue loading grf file
28 * 02: error, but continue loading grf file, and attempt
29 * loading grf again when loading or starting next game
30 * 03: error, abort loading and prevent loading again in
31 * the future (only when restarting the patch)
32 * B language-id see action 4, use 1F for built-in error messages
33 * B message-id message to show, see below
34 * S message for custom messages (message-id FF), text of the message
35 * not present for built-in messages.
36 * V data additional data for built-in (or custom) messages
37 * B parnum parameter numbers to be shown in the message (maximum of 2) */
38
39 static const StringID msgstr[] = {
40 STR_NEWGRF_ERROR_VERSION_NUMBER,
41 STR_NEWGRF_ERROR_DOS_OR_WINDOWS,
42 STR_NEWGRF_ERROR_UNSET_SWITCH,
43 STR_NEWGRF_ERROR_INVALID_PARAMETER,
44 STR_NEWGRF_ERROR_LOAD_BEFORE,
45 STR_NEWGRF_ERROR_LOAD_AFTER,
46 STR_NEWGRF_ERROR_OTTD_VERSION_NUMBER,
47 };
48
49 static const StringID sevstr[] = {
50 STR_NEWGRF_ERROR_MSG_INFO,
51 STR_NEWGRF_ERROR_MSG_WARNING,
52 STR_NEWGRF_ERROR_MSG_ERROR,
53 STR_NEWGRF_ERROR_MSG_FATAL
54 };
55
56 uint8_t severity = buf.ReadByte();
57 uint8_t lang = buf.ReadByte();
58 uint8_t message_id = buf.ReadByte();
59
60 /* Skip the error if it isn't valid for the current language. */
61 if (!CheckGrfLangID(lang, _cur_gps.grffile->grf_version)) return;
62
63 /* Skip the error until the activation stage unless bit 7 of the severity
64 * is set. */
65 if (!HasBit(severity, 7) && _cur_gps.stage == GrfLoadingStage::Init) {
66 GrfMsg(Severity::Trace1, "GRFLoadError: Skipping non-fatal GRFLoadError in stage {}", _cur_gps.stage);
67 return;
68 }
69 ClrBit(severity, 7);
70
71 if (severity >= lengthof(sevstr)) {
72 GrfMsg(Severity::Trace1, "GRFLoadError: Invalid severity id {}. Setting to 2 (non-fatal error).", severity);
73 severity = 2;
74 } else if (severity == 3) {
75 /* This is a fatal error, so make sure the GRF is deactivated and no
76 * more of it gets loaded. */
77 DisableGrf();
78 }
79
80 if (message_id >= lengthof(msgstr) && message_id != 0xFF) {
81 GrfMsg(Severity::Trace1, "GRFLoadError: Invalid message id.");
82 return;
83 }
84
85 if (buf.Remaining() <= 1) {
86 GrfMsg(Severity::Trace1, "GRFLoadError: No message data supplied.");
87 return;
88 }
89
90 /* An error may be emitted multiple times in different loading stages. Re-use if so. */
91 auto it = std::ranges::find(_cur_gps.grfconfig->errors, _cur_gps.nfo_line, &GRFError::nfo_line);
92 if (it == std::end(_cur_gps.grfconfig->errors)) {
93 it = _cur_gps.grfconfig->errors.emplace(it, sevstr[severity], _cur_gps.nfo_line);
94 }
95
96 GRFError &error = *it;
97
98 if (message_id == 0xFF) {
99 /* This is a custom error message. */
100 if (buf.HasData()) {
101 std::string_view message = buf.ReadString();
102
103 error.custom_message = TranslateTTDPatchCodes(_cur_gps.grffile->grfid, _current_language->newgrflangid, true, message, SCC_RAW_STRING_POINTER);
104 } else {
105 GrfMsg(Severity::Trace1, "GRFLoadError: No custom message supplied.");
106 error.custom_message.clear();
107 }
108 } else {
109 error.message = msgstr[message_id];
110 }
111
112 if (buf.HasData()) {
113 std::string_view data = buf.ReadString();
114
115 error.data = TranslateTTDPatchCodes(_cur_gps.grffile->grfid, _current_language->newgrflangid, true, data);
116 } else {
117 GrfMsg(Severity::Trace1, "GRFLoadError: No message data supplied.");
118 error.data.clear();
119 }
120
121 /* Only two parameter numbers can be used in the string. */
122 for (uint i = 0; i < error.param_value.size() && buf.HasData(); i++) {
123 uint param_number = buf.ReadByte();
124 error.param_value[i] = _cur_gps.grffile->GetParam(param_number);
125 }
126}
127
135template <> void GrfActionHandler<0x0B>::Init(ByteReader &buf) { GRFLoadError(buf); }
137template <> void GrfActionHandler<0x0B>::Reserve(ByteReader &buf) { GRFLoadError(buf); }
139template <> void GrfActionHandler<0x0B>::Activation(ByteReader &buf) { GRFLoadError(buf); }
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 NUL-terminated string.
uint8_t ReadByte()
Read a single byte (8 bits).
Functions related to debugging.
@ Trace1
Trace information #1.
Definition debug_type.h:22
Information about languages and their files.
const LanguageMetadata * _current_language
The currently loaded language.
Definition strings.cpp:53
GRFError * DisableGrf(StringID message, GRFConfig *config)
Disable a GRF.
Definition newgrf.cpp:139
@ Init
Second step of NewGRF loading; load all actions into memory.
Definition newgrf.h:61
NewGRF buffer reader definition.
NewGRF internal processing state.
std::string TranslateTTDPatchCodes(GrfID grfid, GRFLanguage language_id, bool allow_newlines, std::string_view str, StringControlCode byte80)
Translate TTDPatch string codes into something OpenTTD can handle (better).
Header of Action 04 "universal holder" structure and functions.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:261
StrongType::Typedef< uint32_t, struct StringIDTag, StrongType::Compare, StrongType::Integer > StringID
Numeric value that represents a string, independent of the selected language.
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.
uint32_t nfo_line
Line within NewGRF of error.
StringID message
Default message.
std::string custom_message
Custom message (if present).
std::string data
Additional data for message and custom_message.
static void FileScan(ByteReader &buf)
Implementation of the GrfLoadingStage::FileScan stage of this action.
static void SafetyScan(ByteReader &buf)
Implementation of the GrfLoadingStage::SafetyScan stage of this action.
static void Reserve(ByteReader &buf)
Implementation of the GrfLoadingStage::Reserve stage of this action.
static void Activation(ByteReader &buf)
Implementation of the GrfLoadingStage::Activation stage of this action.
static void Init(ByteReader &buf)
Implementation of the GrfLoadingStage::Init stage of this action.
static void LabelScan(ByteReader &buf)
Implementation of the GrfLoadingStage::LabelScan stage of this action.