OpenTTD Source: src/newgrf/newgrf_act13.cpp Source File

OpenTTD Source 15.0-RC2

newgrf_act13.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
10#include "../stdafx.h"
11#include "../debug.h"
12#include "../newgrf_text.h"
13#include "../strings_func.h"
14#include "newgrf_bytereader.h"
15#include "newgrf_internal.h"
16
17#include "table/strings.h"
18
19#include "../safeguards.h"
20
22 static void TranslateGRFStrings(ByteReader &buf)
23{
24 /* <13> <grfid> <num-ent> <offset> <text...>
25 *
26 * 4*B grfid The GRFID of the file whose texts are to be translated
27 * B num-ent Number of strings
28 * W offset First text ID
29 * S text... Zero-terminated strings */
30
31 uint32_t grfid = buf.ReadDWord();
32 const GRFConfig *c = GetGRFConfig(grfid);
33 if (c == nullptr || (c->status != GCS_INITIALISED && c->status != GCS_ACTIVATED)) {
34 GrfMsg(7, "TranslateGRFStrings: GRFID 0x{:08X} unknown, skipping action 13", std::byteswap(grfid));
35 return;
36 }
37
38 if (c->status == GCS_INITIALISED) {
39 /* If the file is not active but will be activated later, give an error
40 * and disable this file. */
41 GRFError *error = DisableGrf(STR_NEWGRF_ERROR_LOAD_AFTER);
42
43 error->data = GetString(STR_NEWGRF_ERROR_AFTER_TRANSLATED_FILE);
44
45 return;
46 }
47
48 /* Since no language id is supplied for with version 7 and lower NewGRFs, this string has
49 * to be added as a generic string, thus the language id of 0x7F. For this to work
50 * new_scheme has to be true as well, which will also be implicitly the case for version 8
51 * and higher. A language id of 0x7F will be overridden by a non-generic id, so this will
52 * not change anything if a string has been provided specifically for this language. */
53 uint8_t language = _cur_gps.grffile->grf_version >= 8 ? buf.ReadByte() : 0x7F;
54 uint8_t num_strings = buf.ReadByte();
55 uint16_t first_id = buf.ReadWord();
56
57 if (!((first_id >= 0xD000 && first_id + num_strings <= 0xD400) || (first_id >= 0xD800 && first_id + num_strings <= 0xE000))) {
58 GrfMsg(7, "TranslateGRFStrings: Attempting to set out-of-range string IDs in action 13 (first: 0x{:04X}, number: 0x{:02X})", first_id, num_strings);
59 return;
60 }
61
62 for (uint i = 0; i < num_strings && buf.HasData(); i++) {
63 std::string_view string = buf.ReadString();
64
65 if (string.empty()) {
66 GrfMsg(7, "TranslateGRFString: Ignoring empty string.");
67 continue;
68 }
69
70 AddGRFString(grfid, GRFStringID(first_id + i), language, true, true, string, STR_UNDEFINED);
71 }
72}
73
74template <> void GrfActionHandler<0x13>::FileScan(ByteReader &) { }
75template <> void GrfActionHandler<0x13>::SafetyScan(ByteReader &) { }
76template <> void GrfActionHandler<0x13>::LabelScan(ByteReader &) { }
77template <> void GrfActionHandler<0x13>::Init(ByteReader &) { }
78template <> void GrfActionHandler<0x13>::Reserve(ByteReader &) { }
79template <> void GrfActionHandler<0x13>::Activation(ByteReader &buf) { TranslateGRFStrings(buf); }
ByteReader
Class to read from a NewGRF file.
Definition newgrf_bytereader.h:18
ByteReader::ReadDWord
uint32_t ReadDWord()
Read a single DWord (32 bits).
Definition newgrf_bytereader.h:66
ByteReader::ReadWord
uint16_t ReadWord()
Read a single Word (16 bits).
Definition newgrf_bytereader.h:45
ByteReader::ReadString
std::string_view ReadString()
Read a NUL-terminated string.
Definition newgrf_bytereader.h:91
ByteReader::ReadByte
uint8_t ReadByte()
Read a single byte (8 bits).
Definition newgrf_bytereader.h:34
DisableGrf
GRFError * DisableGrf(StringID message, GRFConfig *config)
Disable a GRF.
Definition newgrf.cpp:135
TranslateGRFStrings
static void TranslateGRFStrings(ByteReader &buf)
Action 0x13.
Definition newgrf_act13.cpp:22
newgrf_bytereader.h
NewGRF buffer reader definition.
GetGRFConfig
GRFConfig * GetGRFConfig(uint32_t grfid, uint32_t mask)
Retrieve a NewGRF from the current config by its grfid.
Definition newgrf_config.cpp:622
GCS_INITIALISED
@ GCS_INITIALISED
GRF file has been initialised.
Definition newgrf_config.h:37
GCS_ACTIVATED
@ GCS_ACTIVATED
GRF file has been activated.
Definition newgrf_config.h:38
newgrf_internal.h
NewGRF internal processing state.
AddGRFString
StringID AddGRFString(uint32_t grfid, GRFStringID stringid, uint8_t langid_to_add, bool new_scheme, bool allow_newlines, std::string_view text_to_add, StringID def_string)
Add the new read string into our structure.
Definition newgrf_text.cpp:536
GRFStringID
StrongType::Typedef< uint32_t, struct GRFStringIDTag, StrongType::Compare, StrongType::Integer > GRFStringID
Type for GRF-internal string IDs.
Definition newgrf_text_type.h:16
GetString
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:424
GRFConfig
Information about GRF, used in the game and (part of it) in savegames.
Definition newgrf_config.h:158
GRFConfig::status
GRFStatus status
NOSAVE: GRFStatus, enum.
Definition newgrf_config.h:179
GRFError
Information about why GRF had problems during initialisation.
Definition newgrf_config.h:109
GRFError::data
std::string data
Additional data for message and custom_message.
Definition newgrf_config.h:114
GrfActionHandler
GRF action handler.
Definition newgrf_internal.h:37
GrfProcessingState::grffile
GRFFile * grffile
Currently processed GRF file.
Definition newgrf_internal.h:67