OpenTTD Source 20260512-master-g20b387b91f
strings_type.h
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#ifndef STRINGS_TYPE_H
11#define STRINGS_TYPE_H
12
14#include "core/enum_type.hpp"
16
20typedef uint32_t StringID;
21static const StringID INVALID_STRING_ID = 0xFFFF;
22static const int MAX_CHAR_LENGTH = 4;
23static const uint MAX_LANG = 0x7F;
24
26enum TextDirection : uint8_t {
29};
30
32enum StringTab : uint8_t {
33 /* Tabs 0..1 for regular strings */
34 TEXT_TAB_TOWN = 4,
35 TEXT_TAB_INDUSTRY = 9,
36 TEXT_TAB_STATION = 12,
37 TEXT_TAB_SPECIAL = 14,
38 TEXT_TAB_OLD_CUSTOM = 15,
39 TEXT_TAB_VEHICLE = 16,
40 /* Tab 17 for regular strings */
41 TEXT_TAB_OLD_NEWGRF = 26,
45};
46
49
51static const uint TAB_SIZE_BITS = 11;
53static const uint TAB_SIZE = 1 << TAB_SIZE_BITS;
54
56static const uint TAB_SIZE_GAMESCRIPT = TAB_SIZE * 32;
57
59static const uint TAB_SIZE_NEWGRF = TAB_SIZE * 256;
60
62static constexpr uint32_t BUILTIN_TOWNNAME_GENERATOR_COUNT = 21;
63
65static constexpr StringID SPECSTR_TOWNNAME_START = 0x20C0;
66static constexpr StringID SPECSTR_TOWNNAME_END = SPECSTR_TOWNNAME_START + BUILTIN_TOWNNAME_GENERATOR_COUNT;
67
69static constexpr StringID SPECSTR_COMPANY_NAME_START = 0x70EA;
70static constexpr StringID SPECSTR_COMPANY_NAME_END = SPECSTR_COMPANY_NAME_START + BUILTIN_TOWNNAME_GENERATOR_COUNT;
71
72static constexpr StringID SPECSTR_SILLY_NAME = 0x70E5;
73static constexpr StringID SPECSTR_ANDCO_NAME = 0x70E6;
74static constexpr StringID SPECSTR_PRESIDENT_NAME = 0x70E7;
75
76using StringParameterData = std::variant<std::monostate, uint64_t, std::string>;
77
79struct StringParameter {
80 StringParameterData data;
81 char32_t type;
82
83 StringParameter() = default;
84 inline StringParameter(StringParameterData &&data) : data(std::move(data)), type(0) {}
85
86 inline StringParameter(const std::monostate &data) : data(data), type(0) {}
87 inline StringParameter(uint64_t data) : data(data), type(0) {}
88
89 inline StringParameter(std::string_view data) : data(std::string{data}), type(0) {}
90 inline StringParameter(std::string &&data) : data(std::move(data)), type(0) {}
91 inline StringParameter(const std::string &data) : data(data), type(0) {}
92
93 inline StringParameter(const ConvertibleThroughBase auto &data) : data(static_cast<uint64_t>(data.base())), type(0) {}
94
100 template <typename T> requires is_scoped_enum_v<T>
101 inline StringParameter(const T &data) : data(static_cast<uint64_t>(to_underlying(data))), type(0) {}
102};
103
107class EncodedString {
108public:
109 EncodedString() = default;
110
111 auto operator<=>(const EncodedString &) const = default;
112
113 std::string GetDecodedString() const;
114 EncodedString ReplaceParam(size_t param, StringParameter &&value) const;
115
116 inline void clear() { this->string.clear(); }
117 inline bool empty() const { return this->string.empty(); }
118
119private:
120 std::string string;
121
122 /* An EncodedString can only be created by GetEncodedStringWithArgs(). */
123 explicit EncodedString(std::string &&string) : string(std::move(string)) {}
124
125 friend EncodedString GetEncodedStringWithArgs(StringID str, std::span<const StringParameter> params);
126
127 template <typename Tcont, typename Titer>
128 friend class EndianBufferWriter;
129 friend class EndianBufferReader;
130 friend class ScriptText;
131};
132
133#endif /* STRINGS_TYPE_H */
Container for an encoded string, created by GetEncodedString.
friend EncodedString GetEncodedStringWithArgs(StringID str, std::span< const StringParameter > params)
Encode a string with its parameters into an encoded string.
Definition strings.cpp:102
std::string string
The encoded string.
std::string GetDecodedString() const
Decode the encoded string.
Definition strings.cpp:207
EncodedString ReplaceParam(size_t param, StringParameter &&value) const
Replace a parameter of this EncodedString.
Definition strings.cpp:150
Endian-aware buffer adapter that always reads values in little endian order.
Endian-aware buffer adapter that always writes values in little endian order.
Concept for unifying the convert through 'base()' behaviour of several 'strong' types.
#define T
Climate temperate.
Definition engines.h:91
Type (helpers) for enums.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
constexpr bool is_scoped_enum_v
Implementation of std::is_scoped_enum_v (from C++23).
Definition enum_type.hpp:24
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
StrongType::Typedef< uint32_t, struct StringIndexInTabTag, StrongType::Compare, StrongType::Integer > StringIndexInTab
The index/offset of a string within a StringTab.
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames).
static constexpr StringID SPECSTR_COMPANY_NAME_START
Special strings for company names on the form "TownName transport".
static constexpr StringID SPECSTR_SILLY_NAME
Special string for silly company names.
static const uint TAB_SIZE_NEWGRF
Number of strings for NewGRFs.
static const uint TAB_SIZE_GAMESCRIPT
Number of strings for GameScripts.
static const uint MAX_LANG
Maximum number of languages supported by the game, and the NewGRF specs.
static const uint TAB_SIZE_BITS
Number of bits for the StringIndex within a StringTab.
static constexpr uint32_t BUILTIN_TOWNNAME_GENERATOR_COUNT
The number of builtin generators for town names.
static constexpr StringID SPECSTR_ANDCO_NAME
Special string for Surname & Co company names.
static constexpr StringID SPECSTR_PRESIDENT_NAME
Special string for the president's name.
static constexpr StringID SPECSTR_TOWNNAME_START
Special strings for town names.
static const uint TAB_SIZE
Number of strings per StringTab.
StringTab
StringTabs to group StringIDs.
@ TEXT_TAB_NEWGRF_START
Start of NewGRF supplied strings.
@ TEXT_TAB_GAMESCRIPT_START
Start of GameScript supplied strings.
@ TEXT_TAB_END
End of language files.
TextDirection
Directions a text can go to.
@ TD_LTR
Text is written left-to-right by default.
@ TD_RTL
Text is written right-to-left by default.
static const int MAX_CHAR_LENGTH
Max. length of UTF-8 encoded unicode character.
Type (helpers) for making a strong typedef that is a distinct type.
The data required to format and validate a single parameter of a string.
StringParameter(const T &data)
Create a StringParameter from a scoped enum.
StringParameterData data
The data of the parameter.
char32_t type
The StringControlCode to interpret this data with when it's the first parameter, otherwise '\0'.
Templated helper to make a type-safe 'typedef' representing a single POD value.