OpenTTD Source 20261112-master-gbcbec5e63f
newgrf_act11.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_house.h"
13#include "../newgrf_sound.h"
14#include "../spritecache.h"
15#include "../string_func.h"
16#include "newgrf_bytereader.h"
17#include "newgrf_internal.h"
18
19#include "../safeguards.h"
20
25static void ImportGRFSound(SoundEntry *sound)
26{
27 const GRFFile *file;
28 GrfID grfid = UnflattenNewGRFLabel<GrfID>(_cur_gps.file->ReadDword());
29 SoundID sound_id = _cur_gps.file->ReadWord();
30
31 file = GetFileByGRFID(grfid);
32 if (file == nullptr || file->sound_offset == 0) {
33 GrfMsg(Severity::Error, "ImportGRFSound: Source file not available");
34 return;
35 }
36
37 if (sound_id >= file->num_sounds) {
38 GrfMsg(Severity::Error, "ImportGRFSound: Sound effect {} is invalid", sound_id);
39 return;
40 }
41
42 GrfMsg(Severity::Warning, "ImportGRFSound: Copying sound {} ({}) from file {}", sound_id, file->sound_offset + sound_id, FormatArrayAsHex(grfid));
43
44 *sound = *GetSound(file->sound_offset + sound_id);
45
46 /* Reset volume, which TTDPatch doesn't copy */
47 sound->volume = SOUND_EFFECT_MAX_VOLUME;
48}
49
55static void LoadGRFSound(size_t offs, SoundEntry *sound)
56{
57 /* Set default volume */
58 sound->volume = SOUND_EFFECT_MAX_VOLUME;
59
60 if (offs != SIZE_MAX) {
61 /* Sound is present in the NewGRF. */
62 sound->file = _cur_gps.file;
63 sound->file_offset = offs;
64 sound->source = SoundSource::NewGRF;
65 sound->grf_container_ver = _cur_gps.file->GetContainerVersion();
66 }
67}
68
69/* Action 0x11 */
70static void GRFSound(ByteReader &buf)
71{
72 /* <11> <num>
73 *
74 * W num Number of sound files that follow */
75
76 uint16_t num = buf.ReadWord();
77 if (num == 0) return;
78
79 SoundEntry *sound;
80 if (_cur_gps.grffile->sound_offset == 0) {
81 _cur_gps.grffile->sound_offset = GetNumSounds();
82 _cur_gps.grffile->num_sounds = num;
83 sound = AllocateSound(num);
84 } else {
85 sound = GetSound(_cur_gps.grffile->sound_offset);
86 }
87
88 SpriteFile &file = *_cur_gps.file;
89 uint8_t grf_container_version = file.GetContainerVersion();
90 for (int i = 0; i < num; i++) {
91 _cur_gps.nfo_line++;
92
93 /* Check whether the index is in range. This might happen if multiple action 11 are present.
94 * While this is invalid, we do not check for this. But we should prevent it from causing bigger trouble */
95 bool invalid = i >= _cur_gps.grffile->num_sounds;
96
97 size_t offs = file.GetPos();
98
99 uint32_t len = grf_container_version >= 2 ? file.ReadDword() : file.ReadWord();
100 uint8_t type = file.ReadByte();
101
102 if (grf_container_version >= 2 && type == 0xFD) {
103 /* Reference to sprite section. */
104 if (invalid) {
105 GrfMsg(Severity::Error, "GRFSound: Sound index out of range (multiple Action 11?)");
106 file.SkipBytes(len);
107 } else if (len != 4) {
108 GrfMsg(Severity::Error, "GRFSound: Invalid sprite section import");
109 file.SkipBytes(len);
110 } else {
111 uint32_t id = file.ReadDword();
112 if (_cur_gps.stage == GrfLoadingStage::Init) LoadGRFSound(GetGRFSpriteOffset(id), sound + i);
113 }
114 continue;
115 }
116
117 if (type != 0xFF) {
118 GrfMsg(Severity::Error, "GRFSound: Unexpected RealSprite found, skipping");
119 file.SkipBytes(7);
120 SkipSpriteData(*_cur_gps.file, type, len - 8);
121 continue;
122 }
123
124 if (invalid) {
125 GrfMsg(Severity::Error, "GRFSound: Sound index out of range (multiple Action 11?)");
126 file.SkipBytes(len);
127 }
128
129 uint8_t action = file.ReadByte();
130 switch (action) {
131 case 0xFF:
132 /* Allocate sound only in init stage. */
133 if (_cur_gps.stage == GrfLoadingStage::Init) {
134 if (grf_container_version >= 2) {
135 GrfMsg(Severity::Error, "GRFSound: Inline sounds are not supported for container version >= 2");
136 } else {
137 LoadGRFSound(offs, sound + i);
138 }
139 }
140 file.SkipBytes(len - 1); // already read <action>
141 break;
142
143 case 0xFE:
144 if (_cur_gps.stage == GrfLoadingStage::Activation) {
145 /* XXX 'Action 0xFE' isn't really specified. It is only mentioned for
146 * importing sounds, so this is probably all wrong... */
147 if (file.ReadByte() != 0) GrfMsg(Severity::Error, "GRFSound: Import type mismatch");
148 ImportGRFSound(sound + i);
149 } else {
150 file.SkipBytes(len - 1); // already read <action>
151 }
152 break;
153
154 default:
155 GrfMsg(Severity::Error, "GRFSound: Unexpected Action {:x} found, skipping", action);
156 file.SkipBytes(len - 1); // already read <action>
157 break;
158 }
159 }
160}
161
162/* Action 0x11 (SKIP) */
163static void SkipAct11(ByteReader &buf)
164{
165 /* <11> <num>
166 *
167 * W num Number of sound files that follow */
168
169 _cur_gps.skip_sprites = buf.ReadWord();
170
171 GrfMsg(Severity::Notice, "SkipAct11: Skipping {} sprites", _cur_gps.skip_sprites);
172}
173
175template <> void GrfActionHandler<0x11>::FileScan(ByteReader &buf) { SkipAct11(buf); }
179template <> void GrfActionHandler<0x11>::LabelScan(ByteReader &buf) { SkipAct11(buf); }
181template <> void GrfActionHandler<0x11>::Init(ByteReader &buf) { GRFSound(buf); }
183template <> void GrfActionHandler<0x11>::Reserve(ByteReader &buf) { SkipAct11(buf); }
185template <> void GrfActionHandler<0x11>::Activation(ByteReader &buf) { GRFSound(buf); }
Class to read from a NewGRF file.
uint16_t ReadWord()
Read a single Word (16 bits).
size_t GetPos() const
Get position in the file.
uint8_t ReadByte()
Read a byte from the file.
uint32_t ReadDword()
Read a double word (32 bits) from the file (in low endian format).
void SkipBytes(size_t n)
Skip n bytes ahead in the file.
uint16_t ReadWord()
Read a word (16 bits) from the file (in low endian format).
RandomAccessFile with some extra information specific for sprite files.
uint8_t GetContainerVersion() const
Get the version number of container type used by the file.
Functions related to debugging.
@ Warning
Warning, wrong but okay if you don't know.
Definition debug_type.h:17
@ Notice
Notice.
Definition debug_type.h:18
@ Error
Error, but we are recovering.
Definition debug_type.h:16
void GRFUnsafe(ByteReader &)
Set the current NewGRF as unsafe for static use.
Definition newgrf.cpp:378
GRFFile * GetFileByGRFID(GrfID grfid)
Obtain a NewGRF file by its grfID.
Definition newgrf.cpp:105
@ Init
Second step of NewGRF loading; load all actions into memory.
Definition newgrf.h:61
@ Activation
Forth step of NewGRF loading; activate the features.
Definition newgrf.h:63
constexpr T UnflattenNewGRFLabel(uint32_t value)
Unflatten a NewGRF related label from a 32 bits integer.
Definition newgrf.h:270
static void LoadGRFSound(size_t offs, SoundEntry *sound)
Load a sound from a file.
static void ImportGRFSound(SoundEntry *sound)
Process a sound import from another GRF file.
NewGRF buffer reader definition.
Functions related to NewGRF houses.
NewGRF internal processing state.
SoundEntry * AllocateSound(uint num)
Allocate sound slots.
Functions related to NewGRF provided sounds.
Label< struct GrfIDTag > GrfID
The unique identifier of a NewGRF.
Definition newgrf_type.h:17
A number of safeguards to prevent using unsafe methods.
@ NewGRF
Contained within a NewGRF.
Definition sound_type.h:17
size_t GetGRFSpriteOffset(uint32_t id)
Get the file offset for a specific sprite in the sprite section of a GRF.
bool SkipSpriteData(SpriteFile &file, uint8_t type, uint16_t num)
Skip the given amount of sprite graphics data.
Functions to cache sprites in memory.
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
Functions related to low-level strings.
Dynamic data of a loaded NewGRF.
Definition newgrf.h:126
uint sound_offset
Index of first sound.
Definition newgrf.h:131
uint16_t num_sounds
Number of sounds.
Definition newgrf.h:132
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.
uint8_t grf_container_ver
NewGRF container version if the sound is from a NewGRF.
Definition sound_type.h:30