OpenTTD Source 20261112-master-gbcbec5e63f
newgrf_act8.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 "../string_func.h"
13#include "newgrf_bytereader.h"
14#include "newgrf_internal.h"
15#include "../newgrf_text.h"
16
17#include "table/strings.h"
18
19#include "../safeguards.h"
20
21/* Action 0x08 (GrfLoadingStage::FileScan) */
22static void ScanInfo(ByteReader &buf)
23{
24 uint8_t grf_version = buf.ReadByte();
25 GrfID grfid = buf.ReadLabel<GrfID>();
26 std::string_view name = buf.ReadString();
27
28 _cur_gps.grfconfig->ident.grfid = grfid;
29
30 if (grf_version < 2 || grf_version > 8) {
31 _cur_gps.grfconfig->flags.Set(GRFConfigFlag::Invalid);
32 Debug(Facility::Grf, Severity::Critical, "{}: NewGRF \"{}\" (GRFID {}) uses GRF version {}, which is incompatible with this version of OpenTTD.", _cur_gps.grfconfig->filename, StrMakeValid(name), FormatArrayAsHex(grfid), grf_version);
33 }
34
35 /* GRF IDs starting with 0xFF are reserved for internal TTDPatch use */
36 if (grfid[0] == 0xFF) _cur_gps.grfconfig->flags.Set(GRFConfigFlag::System);
37
38 AddGRFTextToList(_cur_gps.grfconfig->name, GRFLanguage::Unspecified, grfid, false, name);
39
40 if (buf.HasData()) {
41 std::string_view info = buf.ReadString();
42 AddGRFTextToList(_cur_gps.grfconfig->info, GRFLanguage::Unspecified, grfid, true, info);
43 }
44
45 /* GrfLoadingStage::FileScan only looks for the action 8, so we can skip the rest of the file */
46 _cur_gps.skip_sprites = -1;
47}
48
49/* Action 0x08 */
50static void GRFInfo(ByteReader &buf)
51{
52 /* <08> <version> <grf-id> <name> <info>
53 *
54 * B version newgrf version, currently 06
55 * 4*B grf-id globally unique ID of this .grf file
56 * S name name of this .grf set
57 * S info string describing the set, and e.g. author and copyright */
58
59 uint8_t version = buf.ReadByte();
60 GrfID grfid = buf.ReadLabel<GrfID>();
61 std::string_view name = buf.ReadString();
62
63 if (_cur_gps.stage < GrfLoadingStage::Reserve && _cur_gps.grfconfig->status != GRFStatus::Unknown) {
64 DisableGrf(STR_NEWGRF_ERROR_MULTIPLE_ACTION_8);
65 return;
66 }
67
68 if (_cur_gps.grffile->grfid != grfid) {
69 Debug(Facility::Grf, Severity::Critical, "GRFInfo: GRFID {} in FILESCAN stage does not match GRFID {} in INIT/RESERVE/ACTIVATION stage", FormatArrayAsHex(_cur_gps.grffile->grfid), FormatArrayAsHex(grfid));
70 _cur_gps.grffile->grfid = grfid;
71 }
72
73 _cur_gps.grffile->grf_version = version;
74 _cur_gps.grfconfig->status = _cur_gps.stage < GrfLoadingStage::Reserve ? GRFStatus::Initialised : GRFStatus::Activated;
75
76 /* Do swap the GRFID for displaying purposes since people expect that */
77 Debug(Facility::Grf, Severity::Error, "GRFInfo: Loaded GRFv{} set {} - {} (palette: {}, version: {})", version, FormatArrayAsHex(grfid), StrMakeValid(name), (_cur_gps.grfconfig->palette & GRFP_USE_MASK) ? "Windows" : "DOS", _cur_gps.grfconfig->version);
78}
79
81template <> void GrfActionHandler<0x08>::FileScan(ByteReader &buf) { ScanInfo(buf); }
87template <> void GrfActionHandler<0x08>::Init(ByteReader &buf) { GRFInfo(buf); }
89template <> void GrfActionHandler<0x08>::Reserve(ByteReader &buf) { GRFInfo(buf); }
91template <> void GrfActionHandler<0x08>::Activation(ByteReader &buf) { GRFInfo(buf); }
Class to read from a NewGRF file.
T ReadLabel()
Read a label.
std::string_view ReadString()
Read a NUL-terminated string.
uint8_t ReadByte()
Read a single byte (8 bits).
Functions related to debugging.
#define Debug(facility, severity, format_string,...)
Output a line of debugging information.
Definition debug.h:36
@ Grf
Grf message facility.
Definition debug_type.h:30
@ Critical
Critical, user should know about this.
Definition debug_type.h:15
@ Error
Error, but we are recovering.
Definition debug_type.h:16
GRFError * DisableGrf(StringID message, GRFConfig *config)
Disable a GRF.
Definition newgrf.cpp:139
@ Reserve
Third step of NewGRF loading; reserve features and GRMs.
Definition newgrf.h:62
NewGRF buffer reader definition.
@ Initialised
GRF file has been initialised.
@ Unknown
The status of this grf file is unknown.
@ Activated
GRF file has been activated.
@ Invalid
GRF is unusable with this version of OpenTTD.
@ System
GRF file is an openttd-internal system grf.
@ GRFP_USE_MASK
Bitmask to get only the use palette use states.
NewGRF internal processing state.
static void AddGRFTextToList(GRFTextList &list, GRFLanguage langid, std::string_view text_to_add)
Add a new text to a GRFText list.
Header of Action 04 "universal holder" structure and functions.
@ Unspecified
No specific language.
Label< struct GrfIDTag > GrfID
The unique identifier of a NewGRF.
Definition newgrf_type.h:17
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
std::string FormatArrayAsHex(std::span< const uint8_t > data)
Format a byte array into a continuous hex string.
Definition string.cpp:76
static void StrMakeValid(Builder &builder, StringConsumer &consumer, StringValidationSettings settings)
Copies the valid (UTF-8) characters from consumer to the builder.
Definition string.cpp:118
Functions related to low-level strings.
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.