OpenTTD Source 20261112-master-gbcbec5e63f
newgrf_text.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
17
18#include "stdafx.h"
19
20#include "debug.h"
21#include "newgrf.h"
22#include "strings_func.h"
23#include "strings_internal.h"
24#include "newgrf_text.h"
25#include "newgrf_cargo.h"
26#include "string_func.h"
30#include "language.h"
31#include <ranges>
32
33#include "table/strings.h"
34#include "table/control_codes.h"
35
36#include "safeguards.h"
37
40
41
48 GRFTextList textholder;
49 StringID def_string;
50 GrfID grfid;
51 GRFStringID stringid;
52};
53
54
56
63int LanguageMap::GetMapping(int newgrf_id, bool gender) const
64{
65 const std::vector<Mapping> &map = gender ? this->gender_map : this->case_map;
66 for (const Mapping &m : map) {
67 if (m.newgrf_id == newgrf_id) return m.openttd_id;
68 }
69 return -1;
70}
71
78int LanguageMap::GetReverseMapping(int openttd_id, bool gender) const
79{
80 const std::vector<Mapping> &map = gender ? this->gender_map : this->case_map;
81 for (const Mapping &m : map) {
82 if (m.openttd_id == openttd_id) return m.newgrf_id;
83 }
84 return -1;
85}
86
98
100 int offset;
101
103 std::map<int, std::string> strings;
104
110 void Flush(const LanguageMap *lm, std::string &dest)
111 {
112 if (this->strings.find(0) == this->strings.end()) {
113 /* In case of a (broken) NewGRF without a default,
114 * assume an empty string. */
115 GrfMsg(Severity::Error, "choice list misses default value");
116 this->strings[0] = std::string();
117 }
118
119 StringBuilder builder(dest);
120
121 if (lm == nullptr) {
122 /* In case there is no mapping, just ignore everything but the default.
123 * A probable cause for this happening is when the language file has
124 * been removed by the user and as such no mapping could be made. */
125 builder += this->strings[0];
126 return;
127 }
128
129 builder.PutUtf8(this->type);
130
131 if (this->type == SCC_SWITCH_CASE) {
132 /*
133 * Format for case switch:
134 * <NUM CASES> <CASE1> <LEN1> <STRING1> <CASE2> <LEN2> <STRING2> <CASE3> <LEN3> <STRING3> <LENDEFAULT> <STRINGDEFAULT>
135 * Each LEN is printed using 2 bytes in big endian order.
136 */
137
138 /* "<NUM CASES>" */
139 int count = 0;
140 for (uint8_t i = 0; i < _current_language->num_cases; i++) {
141 /* Count the ones we have a mapped string for. */
142 if (this->strings.find(lm->GetReverseMapping(i, false)) != this->strings.end()) count++;
143 }
144 builder.PutUint8(count);
145
146 auto add_case = [&](std::string_view str) {
147 /* "<LENn>" */
148 uint16_t len = ClampTo<uint16_t>(str.size());
149 builder.PutUint16LE(len);
150
151 /* "<STRINGn>" */
152 builder += str.substr(0, len);
153 };
154
155 for (uint8_t i = 0; i < _current_language->num_cases; i++) {
156 /* Resolve the string we're looking for. */
157 int idx = lm->GetReverseMapping(i, false);
158 if (this->strings.find(idx) == this->strings.end()) continue;
159 auto &str = this->strings[idx];
160
161 /* "<CASEn>" */
162 builder.PutUint8(i + 1);
163
164 add_case(str);
165 }
166
167 /* "<STRINGDEFAULT>" */
168 add_case(this->strings[0]);
169 } else {
170 if (this->type == SCC_PLURAL_LIST) {
171 builder.PutUint8(lm->plural_form);
172 }
173
174 /*
175 * Format for choice list:
176 * <OFFSET> <NUM CHOICES> <LENs> <STRINGs>
177 */
178
179 /* "<OFFSET>" */
180 builder.PutUint8(this->offset - 0x80);
181
182 /* "<NUM CHOICES>" */
183 int count = (this->type == SCC_GENDER_LIST ? _current_language->num_genders : LANGUAGE_MAX_PLURAL_FORMS);
184 builder.PutUint8(count);
185
186 /* "<LENs>" */
187 for (int i = 0; i < count; i++) {
188 int idx = (this->type == SCC_GENDER_LIST ? lm->GetReverseMapping(i, true) : i + 1);
189 const auto &str = this->strings[this->strings.find(idx) != this->strings.end() ? idx : 0];
190 size_t len = str.size();
191 if (len > UINT8_MAX) GrfMsg(Severity::Error, "choice list string is too long");
192 builder.PutUint8(ClampTo<uint8_t>(len));
193 }
194
195 /* "<STRINGs>" */
196 for (int i = 0; i < count; i++) {
197 int idx = (this->type == SCC_GENDER_LIST ? lm->GetReverseMapping(i, true) : i + 1);
198 const auto &str = this->strings[this->strings.find(idx) != this->strings.end() ? idx : 0];
199 uint8_t len = ClampTo<uint8_t>(str.size());
200 builder += str.substr(0, len);
201 }
202 }
203 }
204};
205
215std::string TranslateTTDPatchCodes(GrfID grfid, GRFLanguage language_id, bool allow_newlines, std::string_view str, StringControlCode byte80)
216{
217 /* Empty input string? Nothing to do here. */
218 if (str.empty()) return {};
219
220 StringConsumer consumer(str);
221
222 /* Is this an unicode string? */
223 bool unicode = consumer.ReadUtf8If(NFO_UTF8_IDENTIFIER);
224
225 /* Helper variable for a possible (string) mapping of plural/gender and cases. */
226 std::optional<UnmappedChoiceList> mapping_pg, mapping_c;
227 std::optional<std::reference_wrapper<std::string>> dest_c;
228
229 std::string dest;
230 StringBuilder builder(dest);
231 while (consumer.AnyBytesLeft()) {
232 char32_t c;
233 if (auto u = unicode ? consumer.TryReadUtf8() : std::nullopt; u.has_value()) {
234 c = *u;
235 /* 'Magic' range of control codes. */
236 if (0xE000 <= c && c <= 0xE0FF) {
237 c -= 0xE000;
238 } else if (c >= 0x20) {
239 if (!IsValidChar(c, CS_ALPHANUMERAL)) c = '?';
240 builder.PutUtf8(c);
241 continue;
242 }
243 } else {
244 c = consumer.ReadUint8(); // read as unsigned, otherwise integer promotion breaks it
245 }
246 assert(c <= 0xFF);
247 if (c == '\0') break;
248
249 switch (c) {
250 case 0x01:
251 consumer.SkipUint8();
252 builder.PutChar(' ');
253 break;
254 case 0x0A: break;
255 case 0x0D:
256 if (allow_newlines) {
257 builder.PutChar(0x0A);
258 } else {
259 GrfMsg(Severity::Error, "Detected newline in string that does not allow one");
260 }
261 break;
262 case 0x0E: builder.PutUtf8(SCC_TINYFONT); break;
263 case 0x0F: builder.PutUtf8(SCC_BIGFONT); break;
264 case 0x1F:
265 consumer.SkipUint8();
266 consumer.SkipUint8();
267 builder.PutChar(' ');
268 break;
269 case 0x7B:
270 case 0x7C:
271 case 0x7D:
272 case 0x7E:
273 case 0x7F: builder.PutUtf8(SCC_NEWGRF_PRINT_DWORD_SIGNED + c - 0x7B); break;
274 case 0x80: builder.PutUtf8(byte80); break;
275 case 0x81:
276 {
277 uint16_t string = consumer.ReadUint16LE();
278 builder.PutUtf8(SCC_NEWGRF_STRINL);
279 builder.PutUtf8(MapGRFStringID(grfid, GRFStringID{string}).base());
280 break;
281 }
282 case 0x82:
283 case 0x83:
284 case 0x84: builder.PutUtf8(SCC_NEWGRF_PRINT_WORD_DATE_LONG + c - 0x82); break;
285 case 0x85: builder.PutUtf8(SCC_NEWGRF_DISCARD_WORD); break;
286 case 0x86: builder.PutUtf8(SCC_NEWGRF_ROTATE_TOP_4_WORDS); break;
287 case 0x87: builder.PutUtf8(SCC_NEWGRF_PRINT_WORD_VOLUME_LONG); break;
288 case 0x88: builder.PutUtf8(SCC_BLUE); break;
289 case 0x89: builder.PutUtf8(SCC_SILVER); break;
290 case 0x8A: builder.PutUtf8(SCC_GOLD); break;
291 case 0x8B: builder.PutUtf8(SCC_RED); break;
292 case 0x8C: builder.PutUtf8(SCC_PURPLE); break;
293 case 0x8D: builder.PutUtf8(SCC_LTBROWN); break;
294 case 0x8E: builder.PutUtf8(SCC_ORANGE); break;
295 case 0x8F: builder.PutUtf8(SCC_GREEN); break;
296 case 0x90: builder.PutUtf8(SCC_YELLOW); break;
297 case 0x91: builder.PutUtf8(SCC_DKGREEN); break;
298 case 0x92: builder.PutUtf8(SCC_CREAM); break;
299 case 0x93: builder.PutUtf8(SCC_BROWN); break;
300 case 0x94: builder.PutUtf8(SCC_WHITE); break;
301 case 0x95: builder.PutUtf8(SCC_LTBLUE); break;
302 case 0x96: builder.PutUtf8(SCC_GRAY); break;
303 case 0x97: builder.PutUtf8(SCC_DKBLUE); break;
304 case 0x98: builder.PutUtf8(SCC_BLACK); break;
305 case 0x9A:
306 {
307 uint8_t code = consumer.ReadUint8();
308 switch (code) {
309 case 0x00: goto string_end;
310 case 0x01: builder.PutUtf8(SCC_NEWGRF_PRINT_QWORD_CURRENCY); break;
311 /* 0x02: ignore next colour byte is not supported. It works on the final
312 * string and as such hooks into the string drawing routine. At that
313 * point many things already happened, such as splitting up of strings
314 * when drawn over multiple lines or right-to-left translations, which
315 * make the behaviour peculiar, e.g. only happening at specific width
316 * of windows. Or we need to add another pass over the string to just
317 * support this. As such it is not implemented in OpenTTD. */
318 case 0x03:
319 {
320 uint16_t tmp = consumer.ReadUint16LE();
322 builder.PutUtf8(tmp);
323 break;
324 }
325 case 0x06: builder.PutUtf8(SCC_NEWGRF_PRINT_BYTE_HEX); break;
326 case 0x07: builder.PutUtf8(SCC_NEWGRF_PRINT_WORD_HEX); break;
327 case 0x08: builder.PutUtf8(SCC_NEWGRF_PRINT_DWORD_HEX); break;
328 /* 0x09, 0x0A are TTDPatch internal use only string codes. */
329 case 0x0B: builder.PutUtf8(SCC_NEWGRF_PRINT_QWORD_HEX); break;
330 case 0x0C: builder.PutUtf8(SCC_NEWGRF_PRINT_WORD_STATION_NAME); break;
331 case 0x0D: builder.PutUtf8(SCC_NEWGRF_PRINT_WORD_WEIGHT_LONG); break;
332 case 0x0E:
333 case 0x0F:
334 {
335 const LanguageMap *lm = LanguageMap::GetLanguageMap(grfid, language_id);
336 int index = consumer.ReadUint8();
337 int mapped = lm != nullptr ? lm->GetMapping(index, code == 0x0E) : -1;
338 if (mapped >= 0) {
339 builder.PutUtf8(code == 0x0E ? SCC_GENDER_INDEX : SCC_SET_CASE);
340 builder.PutUtf8(code == 0x0E ? mapped : mapped + 1);
341 }
342 break;
343 }
344
345 case 0x10:
346 case 0x11:
347 if (!mapping_pg.has_value() && !mapping_c.has_value()) {
348 if (code == 0x10) consumer.SkipUint8(); // Skip the index
349 GrfMsg(Severity::Error, "choice list {} marker found when not expected", code == 0x10 ? "next" : "default");
350 break;
351 } else {
352 auto &mapping = mapping_pg ? mapping_pg : mapping_c;
353 int index = (code == 0x10 ? consumer.ReadUint8() : 0);
354 if (mapping->strings.find(index) != mapping->strings.end()) {
355 GrfMsg(Severity::Error, "duplicate choice list string, ignoring");
356 } else {
357 builder = StringBuilder(mapping->strings[index]);
358 if (!mapping_pg) dest_c = mapping->strings[index];
359 }
360 }
361 break;
362
363 case 0x12:
364 if (!mapping_pg.has_value() && !mapping_c.has_value()) {
365 GrfMsg(Severity::Error, "choice list end marker found when not expected");
366 } else {
367 auto &mapping = mapping_pg ? mapping_pg : mapping_c;
368 auto &new_dest = mapping_pg && dest_c ? dest_c->get() : dest;
369 /* Now we can start flushing everything and clean everything up. */
370 mapping->Flush(LanguageMap::GetLanguageMap(grfid, language_id), new_dest);
371 if (!mapping_pg) dest_c.reset();
372 mapping.reset();
373
374 builder = StringBuilder(new_dest);
375 }
376 break;
377
378 case 0x13:
379 case 0x14:
380 case 0x15: {
381 auto &mapping = code == 0x14 ? mapping_c : mapping_pg;
382 /* Case mapping can have nested plural/gender mapping. Otherwise nesting is invalid. */
383 if (mapping.has_value() || mapping_pg.has_value()) {
384 GrfMsg(Severity::Error, "choice lists can't be stacked, it's going to get messy now...");
385 if (code != 0x14) consumer.SkipUint8();
386 } else {
387 static const StringControlCode mp[] = { SCC_GENDER_LIST, SCC_SWITCH_CASE, SCC_PLURAL_LIST };
388 mapping.emplace(mp[code - 0x13], code == 0x14 ? 0 : consumer.ReadUint8());
389 }
390 break;
391 }
392
393 case 0x16:
394 case 0x17:
395 case 0x18:
396 case 0x19:
397 case 0x1A:
398 case 0x1B:
399 case 0x1C:
400 case 0x1D:
401 case 0x1E:
402 builder.PutUtf8(SCC_NEWGRF_PRINT_DWORD_DATE_LONG + code - 0x16);
403 break;
404
405 case 0x1F: builder.PutUtf8(SCC_PUSH_COLOUR); break;
406 case 0x20: builder.PutUtf8(SCC_POP_COLOUR); break;
407
408 case 0x21: builder.PutUtf8(SCC_NEWGRF_PRINT_DWORD_FORCE); break;
409
410 default:
411 GrfMsg(Severity::Error, "missing handler for extended format code");
412 break;
413 }
414 break;
415 }
416
417 case 0x9B: builder.PutUtf8(SCC_TOWN); break;
418 case 0x9C: builder.PutUtf8(SCC_CITY); break;
419 case 0x9E: builder.PutUtf8(0x20AC); break; // Euro
420 case 0x9F: builder.PutUtf8(0x0178); break; // Y with diaeresis
421 case 0xA0: builder.PutUtf8(SCC_UP_ARROW); break;
422 case 0xAA: builder.PutUtf8(SCC_DOWN_ARROW); break;
423 case 0xAC: builder.PutUtf8(SCC_CHECKMARK); break;
424 case 0xAD: builder.PutUtf8(SCC_CROSS); break;
425 case 0xAF: builder.PutUtf8(SCC_RIGHT_ARROW); break;
426 case 0xB4: builder.PutUtf8(SCC_TRAIN); break;
427 case 0xB5: builder.PutUtf8(SCC_LORRY); break;
428 case 0xB6: builder.PutUtf8(SCC_BUS); break;
429 case 0xB7: builder.PutUtf8(SCC_PLANE); break;
430 case 0xB8: builder.PutUtf8(SCC_SHIP); break;
431 case 0xB9: builder.PutUtf8(SCC_SUPERSCRIPT_M1); break;
432 case 0xBC: builder.PutUtf8(SCC_SMALL_UP_ARROW); break;
433 case 0xBD: builder.PutUtf8(SCC_SMALL_DOWN_ARROW); break;
434 default:
435 /* Validate any unhandled character */
436 if (!IsValidChar(c, CS_ALPHANUMERAL)) c = '?';
437 builder.PutUtf8(c);
438 break;
439 }
440 }
441
442string_end:
443 if (mapping_pg.has_value() || mapping_c.has_value()) {
444 GrfMsg(Severity::Error, "choice list was incomplete, the whole list is ignored");
445 }
446
447 return dest;
448}
449
456static void AddGRFTextToList(GRFTextList &list, GRFLanguage langid, std::string_view text_to_add)
457{
458 /* Loop through all languages and see if we can replace a string */
459 for (auto &text : list) {
460 if (text.langid == langid) {
461 text.text = text_to_add;
462 return;
463 }
464 }
465
466 /* If a string wasn't replaced, then we must append the new string */
467 list.emplace_back(langid, std::string{text_to_add});
468}
469
479void AddGRFTextToList(GRFTextList &list, GRFLanguage langid, GrfID grfid, bool allow_newlines, std::string_view text_to_add)
480{
481 AddGRFTextToList(list, langid, TranslateTTDPatchCodes(grfid, langid, allow_newlines, text_to_add));
482}
483
493void AddGRFTextToList(GRFTextWrapper &list, GRFLanguage langid, GrfID grfid, bool allow_newlines, std::string_view text_to_add)
494{
495 if (list == nullptr) list = std::make_shared<GRFTextList>();
496 AddGRFTextToList(*list, langid, grfid, allow_newlines, text_to_add);
497}
498
505void AddGRFTextToList(GRFTextWrapper &list, std::string_view text_to_add)
506{
507 if (list == nullptr) list = std::make_shared<GRFTextList>();
508 AddGRFTextToList(*list, GRFLanguage::Unspecified, text_to_add);
509}
510
521static StringID AddGRFString(GrfID grfid, GRFStringID stringid, GRFLanguage langid_to_add, bool allow_newlines, std::string_view text_to_add, StringID def_string)
522{
523 auto it = std::ranges::find_if(_grf_text, [&grfid, &stringid](const GRFTextEntry &grf_text) { return grf_text.grfid == grfid && grf_text.stringid == stringid; });
524 if (it == std::end(_grf_text)) {
525 /* Too many strings allocated, return empty. */
526 if (_grf_text.size() == TAB_SIZE_NEWGRF) return STR_EMPTY;
527
528 /* We didn't find our stringid and grfid in the list, allocate a new id. */
529 it = _grf_text.emplace(std::end(_grf_text));
530 it->grfid = grfid;
531 it->stringid = stringid;
532 it->def_string = def_string;
533 }
534 StringIndexInTab id(it - std::begin(_grf_text));
535
536 std::string newtext = TranslateTTDPatchCodes(grfid, langid_to_add, allow_newlines, text_to_add);
537 AddGRFTextToList(it->textholder, langid_to_add, newtext);
538
539 GrfMsg(Severity::Notice, "Added 0x{:X} grfid {} string 0x{:X} lang 0x{:X} string '{}' ({:X})", id, FormatArrayAsHex(grfid), stringid, langid_to_add, newtext, MakeStringID(TEXT_TAB_NEWGRF_START, id));
540
542}
543
555StringID AddGRFString(GrfID grfid, GRFStringID stringid, uint8_t langid_to_add, bool new_scheme, bool allow_newlines, std::string_view text_to_add, StringID def_string)
556{
557 if (new_scheme) return AddGRFString(grfid, stringid, static_cast<GRFLanguage>(langid_to_add), allow_newlines, text_to_add, def_string);
558
559 OldGRFLanguages old_lang{langid_to_add};
560
561 /* When working with the old language scheme (grf_version is less than 7) and
562 * English or American is among the set bits, simply add it as English in
563 * the new scheme, i.e. as langid = 1.
564 * If English is set, it is pretty safe to assume the translations are not
565 * actually translated.
566 */
567 if (old_lang.Any({GRFLanguage::American, GRFLanguage::English})) return AddGRFString(grfid, stringid, GRFLanguage::English, allow_newlines, text_to_add, def_string);
568
569 StringID ret = STR_EMPTY;
571 if (old_lang.Test(lang)) ret = AddGRFString(grfid, stringid, lang, allow_newlines, text_to_add, def_string);
572 }
573 return ret;
574}
575
583{
584 auto it = std::ranges::find_if(_grf_text, [&grfid, &stringid](const GRFTextEntry &grf_text) { return grf_text.grfid == grfid && grf_text.stringid == stringid; });
585 if (it != std::end(_grf_text)) {
586 StringIndexInTab id(it - std::begin(_grf_text));
588 }
589
590 return STR_UNDEFINED;
591}
592
593
602std::optional<std::string_view> GetGRFStringFromGRFText(const GRFTextList &text_list)
603{
604 std::optional<std::string_view> default_text;
605
606 /* Search the list of lang-strings of this stringid for current lang */
607 for (const auto &text : text_list) {
608 if (text.langid == _current_language->newgrflangid) return text.text;
609
610 /* If the current string is English or American, set it as the
611 * fallback language if the specific language isn't available. */
612 if (text.langid == GRFLanguage::Unspecified || (!default_text.has_value() && (text.langid == GRFLanguage::English || text.langid == GRFLanguage::American))) {
613 default_text = text.text;
614 }
615 }
616
617 return default_text;
618}
619
628std::optional<std::string_view> GetGRFStringFromGRFText(const GRFTextWrapper &text)
629{
630 return text ? GetGRFStringFromGRFText(*text) : std::nullopt;
631}
632
638std::string_view GetGRFStringPtr(StringIndexInTab stringid)
639{
640 assert(stringid.base() < _grf_text.size());
641 assert(!_grf_text[stringid].grfid.Empty());
642
643 auto str = GetGRFStringFromGRFText(_grf_text[stringid].textholder);
644 if (str.has_value()) return *str;
645
646 /* Use the default string ID if the fallback string isn't available */
647 return GetStringPtr(_grf_text[stringid].def_string);
648}
649
650bool CheckGrfLangID(uint8_t lang_id, uint8_t grf_version)
651{
652 if (grf_version < 7) {
653 OldGRFLanguages old_lang{lang_id};
654 switch (_current_language->newgrflangid) {
655 case GRFLanguage::German: return old_lang.Test(GRFLanguage::German);
656 case GRFLanguage::French: return old_lang.Test(GRFLanguage::French);
657 case GRFLanguage::Spanish: return old_lang.Test(GRFLanguage::Spanish);
658 default: return old_lang.Any({GRFLanguage::American, GRFLanguage::English});
659 }
660 }
661
662 GRFLanguage new_lang = static_cast<GRFLanguage>(lang_id);
663 return new_lang == _current_language->newgrflangid || new_lang == GRFLanguage::Unspecified;
664}
665
671{
672 _grf_text.clear();
673}
674
675struct TextRefStack {
676private:
677 std::vector<uint8_t> stack;
678public:
679 const GRFFile *grffile = nullptr;
680
681 TextRefStack(const GRFFile *grffile, std::span<const int32_t> textstack) : grffile(grffile)
682 {
683 this->stack.reserve(textstack.size() * 4 + 6); // for translations it is reasonable to push 3 word and rotate them
684 for (int32_t value : std::ranges::reverse_view(textstack)) {
685 this->PushDWord(value);
686 }
687 }
688
689 uint8_t PopUnsignedByte()
690 {
691 if (this->stack.empty()) return 0;
692 auto res = this->stack.back();
693 this->stack.pop_back();
694 return res;
695 }
696 int8_t PopSignedByte() { return (int8_t)this->PopUnsignedByte(); }
697
698 uint16_t PopUnsignedWord()
699 {
700 uint16_t val = this->PopUnsignedByte();
701 return val | (this->PopUnsignedByte() << 8);
702 }
703 int16_t PopSignedWord() { return (int32_t)this->PopUnsignedWord(); }
704
705 uint32_t PopUnsignedDWord()
706 {
707 uint32_t val = this->PopUnsignedWord();
708 return val | (this->PopUnsignedWord() << 16);
709 }
710 int32_t PopSignedDWord() { return (int32_t)this->PopUnsignedDWord(); }
711
712 uint64_t PopUnsignedQWord()
713 {
714 uint64_t val = this->PopUnsignedDWord();
715 return val | (((uint64_t)this->PopUnsignedDWord()) << 32);
716 }
717 int64_t PopSignedQWord() { return (int64_t)this->PopUnsignedQWord(); }
718
721 {
722 auto w1 = this->PopUnsignedWord();
723 auto w2 = this->PopUnsignedWord();
724 auto w3 = this->PopUnsignedWord();
725 auto w4 = this->PopUnsignedWord();
726 this->PushWord(w3);
727 this->PushWord(w2);
728 this->PushWord(w1);
729 this->PushWord(w4);
730 }
731
732 void PushByte(uint8_t b)
733 {
734 this->stack.push_back(b);
735 }
736
737 void PushWord(uint16_t w)
738 {
739 this->PushByte(GB(w, 8, 8));
740 this->PushByte(GB(w, 0, 8));
741 }
742
743 void PushDWord(uint32_t dw)
744 {
745 this->PushWord(GB(dw, 16, 16));
746 this->PushWord(GB(dw, 0, 16));
747 }
748};
749
750static void HandleNewGRFStringControlCodes(std::string_view str, TextRefStack &stack, std::vector<StringParameter> &params);
751
759static void ProcessNewGRFStringControlCode(char32_t scc, StringConsumer &consumer, TextRefStack &stack, std::vector<StringParameter> &params)
760{
761 /* There is data on the NewGRF text stack, and we want to move them to OpenTTD's string stack.
762 * After this call, a new call is made with `modify_parameters` set to false when the string is finally formatted. */
763 switch (scc) {
764 default: return;
765
766 case SCC_PLURAL_LIST:
767 consumer.SkipUint8(); // plural form
768 [[fallthrough]];
769 case SCC_GENDER_LIST: {
770 consumer.SkipUint8(); // offset
771 /* plural and gender choices cannot contain any string commands, so just skip the whole thing */
772 uint num = consumer.ReadUint8();
773 uint total_len = 0;
774 for (uint i = 0; i != num; i++) {
775 total_len += consumer.ReadUint8();
776 }
777 consumer.Skip(total_len);
778 break;
779 }
780
781 case SCC_SWITCH_CASE: {
782 /* skip all cases and continue with default case */
783 uint num = consumer.ReadUint8();
784 for (uint i = 0; i != num; i++) {
785 consumer.SkipUint8();
786 auto len = consumer.ReadUint16LE();
787 consumer.Skip(len);
788 }
789 consumer.SkipUint16LE(); // length of default
790 break;
791 }
792
793 case SCC_GENDER_INDEX:
794 case SCC_SET_CASE:
795 consumer.SkipUint8();
796 break;
797
798 case SCC_ARG_INDEX:
799 NOT_REACHED();
800 break;
801
802 case SCC_NEWGRF_PRINT_BYTE_SIGNED: params.emplace_back(stack.PopSignedByte()); break;
803 case SCC_NEWGRF_PRINT_QWORD_CURRENCY: params.emplace_back(stack.PopSignedQWord()); break;
804
806 case SCC_NEWGRF_PRINT_DWORD_SIGNED: params.emplace_back(stack.PopSignedDWord()); break;
807
808 case SCC_NEWGRF_PRINT_BYTE_HEX: params.emplace_back(stack.PopUnsignedByte()); break;
809 case SCC_NEWGRF_PRINT_QWORD_HEX: params.emplace_back(stack.PopUnsignedQWord()); break;
810
814 case SCC_NEWGRF_PRINT_WORD_SIGNED: params.emplace_back(stack.PopSignedWord()); break;
815
821 case SCC_NEWGRF_PRINT_WORD_UNSIGNED: params.emplace_back(stack.PopUnsignedWord()); break;
822
826 case SCC_NEWGRF_PRINT_DWORD_HEX: params.emplace_back(stack.PopUnsignedDWord()); break;
827
828 /* Dates from NewGRFs have 1920-01-01 as their zero point, convert it to OpenTTD's epoch. */
830 case SCC_NEWGRF_PRINT_WORD_DATE_SHORT: params.emplace_back(CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR + stack.PopUnsignedWord()); break;
831
832 case SCC_NEWGRF_DISCARD_WORD: stack.PopUnsignedWord(); break;
833
835 case SCC_NEWGRF_PUSH_WORD: stack.PushWord(consumer.ReadUtf8(0)); break;
836
840 params.emplace_back(GetCargoTranslation(stack.PopUnsignedWord(), stack.grffile));
841 params.emplace_back(stack.PopUnsignedWord());
842 break;
843
844 case SCC_NEWGRF_STRINL: {
845 StringID stringid{consumer.ReadUtf8(STR_NULL.base())};
846 /* We also need to handle the substring's stack usage. */
847 HandleNewGRFStringControlCodes(GetStringPtr(stringid), stack, params);
848 break;
849 }
850
852 StringID stringid = MapGRFStringID(stack.grffile->grfid, GRFStringID{stack.PopUnsignedWord()});
853 params.emplace_back(stringid);
854 /* We also need to handle the substring's stack usage. */
855 HandleNewGRFStringControlCodes(GetStringPtr(stringid), stack, params);
856 break;
857 }
858
860 CargoType cargo = GetCargoTranslation(stack.PopUnsignedWord(), stack.grffile);
861 params.emplace_back(cargo < NUM_CARGO ? 1ULL << cargo : 0);
862 break;
863 }
864 }
865}
866
873char32_t RemapNewGRFStringControlCode(char32_t scc, StringConsumer &consumer)
874{
875 switch (scc) {
876 default:
877 return scc;
878
883 return SCC_COMMA;
884
889 return SCC_HEX;
890
893 return SCC_CURRENCY_LONG;
894
897 return SCC_DATE_LONG;
898
901 return SCC_DATE_SHORT;
902
904 return SCC_VELOCITY;
905
907 return SCC_VOLUME_LONG;
908
910 return SCC_VOLUME_SHORT;
911
913 return SCC_WEIGHT_LONG;
914
916 return SCC_WEIGHT_SHORT;
917
919 return SCC_POWER;
920
922 return SCC_FORCE;
923
925 return SCC_CARGO_LONG;
926
928 return SCC_CARGO_SHORT;
929
931 return SCC_CARGO_TINY;
932
934 return SCC_CARGO_LIST;
935
937 return SCC_STATION_NAME;
938
939 /* These NewGRF string codes modify the NewGRF stack or otherwise do not map to OpenTTD string codes. */
941 consumer.SkipUtf8();
942 return 0;
943
946 return 0;
947 }
948}
949
956static void HandleNewGRFStringControlCodes(std::string_view str, TextRefStack &stack, std::vector<StringParameter> &params)
957{
958 StringConsumer consumer(str);
959 while (consumer.AnyBytesLeft()) {
960 char32_t scc = consumer.ReadUtf8();
961 ProcessNewGRFStringControlCode(scc, consumer, stack, params);
962 }
963}
964
972std::vector<StringParameter> GetGRFStringTextStackParameters(const GRFFile *grffile, StringID stringid, std::span<const int32_t> textstack)
973{
974 if (stringid == INVALID_STRING_ID) return {};
975
976 auto str = GetStringPtr(stringid);
977
978 std::vector<StringParameter> params;
979 params.reserve(20);
980
981 TextRefStack stack{grffile, textstack};
982 HandleNewGRFStringControlCodes(str, stack, params);
983
984 return params;
985}
986
994std::string GetGRFStringWithTextStack(const struct GRFFile *grffile, GRFStringID grfstringid, std::span<const int32_t> textstack)
995{
996 StringID stringid = GetGRFStringID(grffile->grfid, grfstringid);
997 auto params = GetGRFStringTextStackParameters(grffile, stringid, textstack);
998 return GetStringWithArgs(stringid, params);
999}
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
static constexpr CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:77
CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:22
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr bool Any(const Timpl &other) const
Test if any of the given values are set.
void PutUtf8(char32_t c)
Append UTF-8 char.
void PutUint16LE(uint16_t value)
Append binary uint16 using little endian.
void PutChar(char c)
Append 8-bit char.
void PutUint8(uint8_t value)
Append binary uint8.
Enum-as-bit-set wrapper.
Compose data into a growing std::string.
Parse data from a string / buffer.
char32_t ReadUtf8(char32_t def='?')
Read UTF-8 character, and advance reader.
void SkipUtf8()
Skip UTF-8 character, and advance reader.
bool AnyBytesLeft() const noexcept
Check whether any bytes left to read.
uint8_t ReadUint8(uint8_t def=0)
Read binary uint8, and advance reader.
void SkipUint16LE()
Skip binary uint16, and advance reader.
uint16_t ReadUint16LE(uint16_t def=0)
Read binary uint16 using little endian, and advance reader.
void SkipUint8()
Skip binary uint8.
std::optional< char32_t > TryReadUtf8()
Try to read a UTF-8 character, and then advance reader.
bool ReadUtf8If(char32_t c)
Check whether the next UTF-8 char matches 'c', and skip it.
void Skip(size_type len)
Discard some bytes.
static constexpr TimerGame< struct Calendar >::Date DAYS_TILL_ORIGINAL_BASE_YEAR
A sort-of mixin that implements 'at(pos)' and 'operator[](pos)' only for a specific type.
Control codes that are embedded in the translation strings.
StringControlCode
List of string control codes used for string formatting, displaying, and by strgen to generate the la...
@ SCC_NEWGRF_PRINT_WORD_VOLUME_SHORT
9A 19: Read 2 bytes from the stack as short signed volume
@ SCC_NEWGRF_PRINT_DWORD_CURRENCY
8F: Read 4 bytes from the stack as currency
@ SCC_NEWGRF_PRINT_WORD_HEX
9A 07: Read 2 bytes from the stack and print it as hex
@ SCC_NEWGRF_PRINT_WORD_WEIGHT_SHORT
9A 1A: Read 2 bytes from the stack as short unsigned weight
@ SCC_NEWGRF_ROTATE_TOP_4_WORDS
86: Rotate the top 4 words of the stack (W4 W1 W2 W3)
@ SCC_TINYFONT
Switch to small font.
@ SCC_NEWGRF_PRINT_DWORD_DATE_LONG
9A 16: Read 4 bytes from the stack as base 0 date
@ SCC_NEWGRF_PRINT_DWORD_HEX
9A 08: Read 4 bytes from the stack and print it as hex
@ SCC_NEWGRF_PRINT_WORD_DATE_SHORT
83: Read 2 bytes from the stack as base 1920 date
@ SCC_BIGFONT
Switch to large font.
@ SCC_NEWGRF_PRINT_BYTE_HEX
9A 06: Read 1 byte from the stack and print it as hex
@ SCC_NEWGRF_PRINT_WORD_UNSIGNED
7E: Read 2 bytes from the stack as unsigned value
@ SCC_NEWGRF_PRINT_BYTE_SIGNED
7D: Read 1 byte from the stack as signed value
@ SCC_NEWGRF_PRINT_WORD_CARGO_NAME
9A 1E: Read 2 bytes from the stack as cargo name
@ SCC_NEWGRF_PRINT_DWORD_SIGNED
7B: Read 4 bytes from the stack
@ SCC_NEWGRF_STRINL
Inline another string at the current position, StringID is encoded in the string.
@ SCC_NEWGRF_PRINT_WORD_CARGO_SHORT
9A 1C: Read 2 + 2 bytes from the stack as cargo type (translated) and unsigned cargo amount
@ SCC_NEWGRF_PRINT_WORD_STATION_NAME
9A 0C: Read 2 bytes from the stack as station name
@ SCC_NEWGRF_PRINT_WORD_DATE_LONG
82: Read 2 bytes from the stack as base 1920 date
@ SCC_NEWGRF_PRINT_WORD_SIGNED
7C: Read 2 bytes from the stack as signed value
@ SCC_NEWGRF_PRINT_WORD_POWER
9A 18: Read 2 bytes from the stack as unsigned power
@ SCC_NEWGRF_PUSH_WORD
9A 03: Pushes 2 bytes onto the stack
@ SCC_NEWGRF_PRINT_WORD_STRING_ID
81: Read 2 bytes from the stack as String ID
@ SCC_NEWGRF_PRINT_WORD_VOLUME_LONG
87: Read 2 bytes from the stack as long signed volume
@ SCC_NEWGRF_DISCARD_WORD
85: Discard the next two bytes
@ SCC_NEWGRF_PRINT_DWORD_FORCE
9A 21: Read 4 bytes from the stack as unsigned force
@ SCC_NEWGRF_PRINT_WORD_CARGO_TINY
9A 1D: Read 2 + 2 bytes from the stack as cargo type (translated) and unsigned cargo amount
@ SCC_NEWGRF_PRINT_QWORD_HEX
9A 0B: Read 8 bytes from the stack and print it as hex
@ SCC_NEWGRF_PRINT_WORD_CARGO_LONG
9A 1B: Read 2 + 2 bytes from the stack as cargo type (translated) and unsigned cargo amount
@ SCC_NEWGRF_PRINT_WORD_SPEED
84: Read 2 bytes from the stack as signed speed
@ SCC_NEWGRF_PRINT_DWORD_DATE_SHORT
9A 17: Read 4 bytes from the stack as base 0 date
@ SCC_NEWGRF_PRINT_WORD_WEIGHT_LONG
9A 0D: Read 2 bytes from the stack as long unsigned weight
@ SCC_NEWGRF_PRINT_QWORD_CURRENCY
9A 01: Read 8 bytes from the stack as currency
Functions related to debugging.
@ Notice
Notice.
Definition debug_type.h:18
@ Error
Error, but we are recovering.
Definition debug_type.h:16
Information about languages and their files.
const LanguageMetadata * _current_language
The currently loaded language.
Definition strings.cpp:53
constexpr To ClampTo(From value)
Clamp the given value down to lie within the requested type.
Base for the NewGRF implementation.
StringID MapGRFStringID(GrfID grfid, GRFStringID str)
Used when setting an object's property to map to the GRF's strings while taking in consideration the ...
CargoType GetCargoTranslation(uint8_t cargo, const GRFFile *grffile, bool usebit)
Translate a GRF-local cargo slot/bitnum into a CargoType.
Cargo support for NewGRFs.
void CleanUpStrings()
House cleaning.
std::string_view GetGRFStringPtr(StringIndexInTab stringid)
Get a C-string from a stringid set by a newgrf.
static void HandleNewGRFStringControlCodes(std::string_view str, TextRefStack &stack, std::vector< StringParameter > &params)
Handle control codes in a NewGRF string, processing the stack and filling parameters.
EnumBitSet< GRFLanguage, uint8_t > OldGRFLanguages
The bitmask of the old GRF languages, which are conveniently the same as the first few GRFLanguages.
std::vector< StringParameter > GetGRFStringTextStackParameters(const GRFFile *grffile, StringID stringid, std::span< const int32_t > textstack)
Process the text ref stack for a GRF String and return its parameters.
std::string GetGRFStringWithTextStack(const struct GRFFile *grffile, GRFStringID grfstringid, std::span< const int32_t > textstack)
Format a GRF string using the text ref stack for parameters.
std::string TranslateTTDPatchCodes(GrfID grfid, GRFLanguage language_id, bool allow_newlines, std::string_view str, StringControlCode byte80)
Translate TTDPatch string codes into something OpenTTD can handle (better).
std::optional< std::string_view > GetGRFStringFromGRFText(const GRFTextList &text_list)
Get a C-string from a GRFText-list.
StringID GetGRFStringID(GrfID grfid, GRFStringID stringid)
Returns the index for this stringid associated with its grfID.
static void AddGRFTextToList(GRFTextList &list, GRFLanguage langid, std::string_view text_to_add)
Add a new text to a GRFText list.
static void ProcessNewGRFStringControlCode(char32_t scc, StringConsumer &consumer, TextRefStack &stack, std::vector< StringParameter > &params)
Process NewGRF string control code instructions.
char32_t RemapNewGRFStringControlCode(char32_t scc, StringConsumer &consumer)
Emit OpenTTD's internal string code for the different NewGRF string codes.
static StringID AddGRFString(GrfID grfid, GRFStringID stringid, GRFLanguage langid_to_add, bool allow_newlines, std::string_view text_to_add, StringID def_string)
Add the new read string into our structure.
Header of Action 04 "universal holder" structure and functions.
GRFLanguage
Incomplete enumeration of the (New)GRF languages.
@ Unspecified
No specific language.
@ English
English (English UK).
@ American
American (English US).
@ Spanish
Spanish.
StrongType::Typedef< uint32_t, struct GRFStringIDTag, StrongType::Compare, StrongType::Integer > GRFStringID
Type for GRF-internal string IDs.
std::shared_ptr< GRFTextList > GRFTextWrapper
Reference counted wrapper around a GRFText pointer.
std::vector< GRFText > GRFTextList
A GRF text with a list of translations.
static const char32_t NFO_UTF8_IDENTIFIER
This character (thorn) indicates a unicode string to NFO.
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.
bool IsValidChar(char32_t key, CharSetFilter afilter)
Only allow certain keys.
Definition string.cpp:374
std::string FormatArrayAsHex(std::span< const uint8_t > data)
Format a byte array into a continuous hex string.
Definition string.cpp:76
Compose strings from textual and binary data.
Parse strings.
Functions related to low-level strings.
@ CS_ALPHANUMERAL
Both numeric and alphabetic and spaces and stuff.
Definition string_type.h:25
void GetStringWithArgs(StringBuilder &builder, StringID string, StringParameters &args, uint case_index, bool game_script)
Get a parsed string with most special stringcodes replaced by the string parameters.
Definition strings.cpp:335
Functions related to OTTD's strings.
StringID MakeStringID(StringTab tab, StringIndexInTab index)
Create a StringID.
Types and functions related to the internal workings of formatting OpenTTD's strings.
StrongType::Typedef< uint32_t, struct StringIndexInTabTag, StrongType::Compare, StrongType::Integer > StringIndexInTab
The index/offset of a string within a StringTab.
static const uint TAB_SIZE_NEWGRF
Number of strings for NewGRFs.
StrongType::Typedef< uint32_t, struct StringIDTag, StrongType::Compare, StrongType::Integer > StringID
Numeric value that represents a string, independent of the selected language.
static constexpr StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames).
@ TEXT_TAB_NEWGRF_START
Start of NewGRF supplied strings.
Dynamic data of a loaded NewGRF.
Definition newgrf.h:126
GrfID grfid
The GRF ID.
Definition newgrf.h:128
Holder of the above structure.
Mapping between NewGRF and OpenTTD IDs.
Mapping of language data between a NewGRF and OpenTTD.
std::vector< Mapping > gender_map
Mapping of NewGRF and OpenTTD IDs for genders.
int GetReverseMapping(int openttd_id, bool gender) const
Get the mapping from OpenTTD's internal ID to the NewGRF supplied ID.
int plural_form
The plural form used for this language.
static const LanguageMap * GetLanguageMap(GrfID grfid, GRFLanguage language_id)
Get the language map associated with a given NewGRF and language.
Definition newgrf.cpp:363
std::vector< Mapping > case_map
Mapping of NewGRF and OpenTTD IDs for cases.
int GetMapping(int newgrf_id, bool gender) const
Get the mapping from the NewGRF supplied ID to OpenTTD's internal ID.
void RotateTop4Words()
Rotate the top four words down: W1, W2, W3, W4 -> W4, W1, W2, W3.
StringControlCode type
The type of choice list.
int offset
The offset for the plural/gender form.
std::map< int, std::string > strings
Mapping of NewGRF supplied ID to the different strings in the choice list.
UnmappedChoiceList(StringControlCode type, int offset)
Initialise the mapping.
void Flush(const LanguageMap *lm, std::string &dest)
Flush this choice list into the destination string.
Definition of the game-calendar-timer.