OpenTTD Source 20260206-master-g4d4e37dbf1
newgrf_act0_badges.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_badge.h"
14#include "newgrf_bytereader.h"
15#include "newgrf_internal.h"
16
17#include "../safeguards.h"
18
19static ChangeInfoResult BadgeChangeInfo(uint first, uint last, int prop, ByteReader &buf)
20{
22
23 if (last >= UINT16_MAX) {
24 GrfMsg(1, "BadgeChangeInfo: Tag {} is invalid, max {}, ignoring", last, UINT16_MAX - 1);
25 return CIR_INVALID_ID;
26 }
27
28 for (uint id = first; id < last; ++id) {
29 auto it = _cur_gps.grffile->badge_map.find(id);
30 if (prop != 0x08 && it == std::end(_cur_gps.grffile->badge_map)) {
31 GrfMsg(1, "BadgeChangeInfo: Attempt to modify undefined tag {}, ignoring", id);
32 return CIR_INVALID_ID;
33 }
34
35 Badge *badge = nullptr;
36 if (prop != 0x08) badge = GetBadge(it->second);
37
38 switch (prop) {
39 case 0x08: { // Label
40 std::string_view label = buf.ReadString();
41 _cur_gps.grffile->badge_map[id] = GetOrCreateBadge(label).index;
42 break;
43 }
44
45 case 0x09: // Flags
46 badge->flags = static_cast<BadgeFlags>(buf.ReadDWord());
47 break;
48
49 default:
50 ret = CIR_UNKNOWN;
51 break;
52 }
53 }
54
55 return ret;
56}
57
58template <> ChangeInfoResult GrfChangeInfoHandler<GSF_BADGES>::Reserve(uint, uint, int, ByteReader &) { return CIR_UNHANDLED; }
59template <> ChangeInfoResult GrfChangeInfoHandler<GSF_BADGES>::Activation(uint first, uint last, int prop, ByteReader &buf) { return BadgeChangeInfo(first, last, prop, buf); }
BadgeID index
Index assigned to badge.
BadgeFlags flags
Display flags.
Class to read from a NewGRF file.
uint32_t ReadDWord()
Read a single DWord (32 bits).
std::string_view ReadString()
Read a NUL-terminated string.
Functions related to debugging.
Badge * GetBadge(BadgeID index)
Get a badge if it exists.
Badge & GetOrCreateBadge(std::string_view label)
Register a badge label and return its global index.
Functions related to NewGRF badges.
Types related to NewGRF badges.
NewGRF buffer reader definition.
NewGRF internal processing state.
ChangeInfoResult
Possible return values for the GrfChangeInfoHandler functions.
@ CIR_INVALID_ID
Attempt to modify an invalid ID.
@ CIR_UNKNOWN
Variable is unknown.
@ CIR_UNHANDLED
Variable was parsed but unread.
@ CIR_SUCCESS
Variable was parsed and read.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.