Add: INR currency (#8136)
[openttd-github.git] / src / newgrf_text.cpp
blobc08877198d47baec86ae12a8719ae1c23d0066be
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 <http://www.gnu.org/licenses/>.
6 */
8 /**
9 * @file newgrf_text.cpp
10 * Implementation of Action 04 "universal holder" structure and functions.
11 * This file implements a linked-lists of strings,
12 * holding everything that the newgrf action 04 will send over to OpenTTD.
13 * One of the biggest problems is that Dynamic lang Array uses ISO codes
14 * as way to identifying current user lang, while newgrf uses bit shift codes
15 * not related to ISO. So equivalence functionality had to be set.
18 #include "stdafx.h"
20 #include <algorithm>
21 #include <array>
23 #include "newgrf.h"
24 #include "strings_func.h"
25 #include "newgrf_storage.h"
26 #include "newgrf_text.h"
27 #include "newgrf_cargo.h"
28 #include "string_func.h"
29 #include "date_type.h"
30 #include "debug.h"
31 #include "core/alloc_type.hpp"
32 #include "core/smallmap_type.hpp"
33 #include "language.h"
35 #include "table/strings.h"
36 #include "table/control_codes.h"
38 #include "safeguards.h"
40 /**
41 * Explains the newgrf shift bit positioning.
42 * the grf base will not be used in order to find the string, but rather for
43 * jumping from standard langID scheme to the new one.
45 enum GRFBaseLanguages {
46 GRFLB_AMERICAN = 0x01,
47 GRFLB_ENGLISH = 0x02,
48 GRFLB_GERMAN = 0x04,
49 GRFLB_FRENCH = 0x08,
50 GRFLB_SPANISH = 0x10,
51 GRFLB_GENERIC = 0x80,
54 enum GRFExtendedLanguages {
55 GRFLX_AMERICAN = 0x00,
56 GRFLX_ENGLISH = 0x01,
57 GRFLX_GERMAN = 0x02,
58 GRFLX_FRENCH = 0x03,
59 GRFLX_SPANISH = 0x04,
60 GRFLX_UNSPECIFIED = 0x7F,
63 /**
64 * Element of the linked list.
65 * Each of those elements represent the string,
66 * but according to a different lang.
68 struct GRFText {
69 public:
70 /**
71 * Allocate, and assign a new GRFText with the given text.
72 * As these strings can have string terminations in them, e.g.
73 * due to "choice lists" we (sometimes) cannot rely on detecting
74 * the length by means of strlen. Also, if the length of already
75 * known not scanning the whole string is more efficient.
76 * @param langid The language of the text.
77 * @param text The text to store in the new GRFText.
78 * @param len The length of the text.
80 static GRFText *New(byte langid, const char *text, size_t len)
82 return new (len) GRFText(langid, text, len);
85 /**
86 * Create a copy of this GRFText.
87 * @param orig the grftext to copy.
88 * @return an exact copy of the given text.
90 static GRFText *Copy(GRFText *orig)
92 return GRFText::New(orig->langid, orig->text, orig->len);
95 /**
96 * Helper allocation function to disallow something.
97 * Don't allow simple 'news'; they wouldn't have enough memory.
98 * @param size the amount of space not to allocate.
100 void *operator new(size_t size)
102 NOT_REACHED();
106 * Free the memory we allocated.
107 * @param p memory to free.
109 void operator delete(void *p)
111 free(p);
113 private:
115 * Actually construct the GRFText.
116 * @param langid_ The language of the text.
117 * @param text_ The text to store in this GRFText.
118 * @param len_ The length of the text to store.
120 GRFText(byte langid_, const char *text_, size_t len_) : next(nullptr), len(len_), langid(langid_)
122 /* We need to use memcpy instead of strcpy due to
123 * the possibility of "choice lists" and therefore
124 * intermediate string terminators. */
125 memcpy(this->text, text_, len);
129 * Allocate memory for this class.
130 * @param size the size of the instance
131 * @param extra the extra memory for the text
132 * @return the requested amount of memory for both the instance and the text
134 void *operator new(size_t size, size_t extra)
136 return MallocT<byte>(size + extra);
139 public:
140 GRFText *next; ///< The next GRFText in this chain.
141 size_t len; ///< The length of the stored string, used for copying.
142 byte langid; ///< The language associated with this GRFText.
143 char text[]; ///< The actual (translated) text.
148 * Holder of the above structure.
149 * Putting both grfid and stringid together allows us to avoid duplicates,
150 * since it is NOT SUPPOSED to happen.
152 struct GRFTextEntry {
153 uint32 grfid;
154 uint16 stringid;
155 StringID def_string;
156 GRFText *textholder;
160 static uint _num_grf_texts = 0;
161 static GRFTextEntry _grf_text[TAB_SIZE_NEWGRF];
162 static byte _currentLangID = GRFLX_ENGLISH; ///< by default, english is used.
165 * Get the mapping from the NewGRF supplied ID to OpenTTD's internal ID.
166 * @param newgrf_id The NewGRF ID to map.
167 * @param gender Whether to map genders or cases.
168 * @return The, to OpenTTD's internal ID, mapped index, or -1 if there is no mapping.
170 int LanguageMap::GetMapping(int newgrf_id, bool gender) const
172 const std::vector<Mapping> &map = gender ? this->gender_map : this->case_map;
173 for (const Mapping &m : map) {
174 if (m.newgrf_id == newgrf_id) return m.openttd_id;
176 return -1;
180 * Get the mapping from OpenTTD's internal ID to the NewGRF supplied ID.
181 * @param openttd_id The OpenTTD ID to map.
182 * @param gender Whether to map genders or cases.
183 * @return The, to the NewGRF supplied ID, mapped index, or -1 if there is no mapping.
185 int LanguageMap::GetReverseMapping(int openttd_id, bool gender) const
187 const std::vector<Mapping> &map = gender ? this->gender_map : this->case_map;
188 for (const Mapping &m : map) {
189 if (m.openttd_id == openttd_id) return m.newgrf_id;
191 return -1;
194 /** Helper structure for mapping choice lists. */
195 struct UnmappedChoiceList : ZeroedMemoryAllocator {
196 /** Clean everything up. */
197 ~UnmappedChoiceList()
199 for (SmallPair<byte, char *> p : this->strings) {
200 free(p.second);
205 * Initialise the mapping.
206 * @param type The type of mapping.
207 * @param old_d The old begin of the string, i.e. from where to start writing again.
208 * @param offset The offset to get the plural/gender from.
210 UnmappedChoiceList(StringControlCode type, char *old_d, int offset) :
211 type(type), old_d(old_d), offset(offset)
215 StringControlCode type; ///< The type of choice list.
216 char *old_d; ///< The old/original location of the "d" local variable.
217 int offset; ///< The offset for the plural/gender form.
219 /** Mapping of NewGRF supplied ID to the different strings in the choice list. */
220 SmallMap<byte, char *> strings;
223 * Flush this choice list into the old d variable.
224 * @param lm The current language mapping.
225 * @return The new location of the output string.
227 char *Flush(const LanguageMap *lm)
229 if (!this->strings.Contains(0)) {
230 /* In case of a (broken) NewGRF without a default,
231 * assume an empty string. */
232 grfmsg(1, "choice list misses default value");
233 this->strings[0] = stredup("");
236 char *d = old_d;
237 if (lm == nullptr) {
238 /* In case there is no mapping, just ignore everything but the default.
239 * A probable cause for this happening is when the language file has
240 * been removed by the user and as such no mapping could be made. */
241 size_t len = strlen(this->strings[0]);
242 memcpy(d, this->strings[0], len);
243 return d + len;
246 d += Utf8Encode(d, this->type);
248 if (this->type == SCC_SWITCH_CASE) {
250 * Format for case switch:
251 * <NUM CASES> <CASE1> <LEN1> <STRING1> <CASE2> <LEN2> <STRING2> <CASE3> <LEN3> <STRING3> <STRINGDEFAULT>
252 * Each LEN is printed using 2 bytes in big endian order.
255 /* "<NUM CASES>" */
256 int count = 0;
257 for (uint8 i = 0; i < _current_language->num_cases; i++) {
258 /* Count the ones we have a mapped string for. */
259 if (this->strings.Contains(lm->GetReverseMapping(i, false))) count++;
261 *d++ = count;
263 for (uint8 i = 0; i < _current_language->num_cases; i++) {
264 /* Resolve the string we're looking for. */
265 int idx = lm->GetReverseMapping(i, false);
266 if (!this->strings.Contains(idx)) continue;
267 char *str = this->strings[idx];
269 /* "<CASEn>" */
270 *d++ = i + 1;
272 /* "<LENn>" */
273 size_t len = strlen(str) + 1;
274 *d++ = GB(len, 8, 8);
275 *d++ = GB(len, 0, 8);
277 /* "<STRINGn>" */
278 memcpy(d, str, len);
279 d += len;
282 /* "<STRINGDEFAULT>" */
283 size_t len = strlen(this->strings[0]) + 1;
284 memcpy(d, this->strings[0], len);
285 d += len;
286 } else {
287 if (this->type == SCC_PLURAL_LIST) {
288 *d++ = lm->plural_form;
292 * Format for choice list:
293 * <OFFSET> <NUM CHOICES> <LENs> <STRINGs>
296 /* "<OFFSET>" */
297 *d++ = this->offset - 0x80;
299 /* "<NUM CHOICES>" */
300 int count = (this->type == SCC_GENDER_LIST ? _current_language->num_genders : LANGUAGE_MAX_PLURAL_FORMS);
301 *d++ = count;
303 /* "<LENs>" */
304 for (int i = 0; i < count; i++) {
305 int idx = (this->type == SCC_GENDER_LIST ? lm->GetReverseMapping(i, true) : i + 1);
306 const char *str = this->strings[this->strings.Contains(idx) ? idx : 0];
307 size_t len = strlen(str) + 1;
308 if (len > 0xFF) grfmsg(1, "choice list string is too long");
309 *d++ = GB(len, 0, 8);
312 /* "<STRINGs>" */
313 for (int i = 0; i < count; i++) {
314 int idx = (this->type == SCC_GENDER_LIST ? lm->GetReverseMapping(i, true) : i + 1);
315 const char *str = this->strings[this->strings.Contains(idx) ? idx : 0];
316 /* Limit the length of the string we copy to 0xFE. The length is written above
317 * as a byte and we need room for the final '\0'. */
318 size_t len = min<size_t>(0xFE, strlen(str));
319 memcpy(d, str, len);
320 d += len;
321 *d++ = '\0';
324 return d;
329 * Translate TTDPatch string codes into something OpenTTD can handle (better).
330 * @param grfid The (NewGRF) ID associated with this string
331 * @param language_id The (NewGRF) language ID associated with this string.
332 * @param allow_newlines Whether newlines are allowed in the string or not.
333 * @param str The string to translate.
334 * @param[out] olen The length of the final string.
335 * @param byte80 The control code to use as replacement for the 0x80-value.
336 * @return The translated string.
338 char *TranslateTTDPatchCodes(uint32 grfid, uint8 language_id, bool allow_newlines, const char *str, int *olen, StringControlCode byte80)
340 char *tmp = MallocT<char>(strlen(str) * 10 + 1); // Allocate space to allow for expansion
341 char *d = tmp;
342 bool unicode = false;
343 WChar c;
344 size_t len = Utf8Decode(&c, str);
346 /* Helper variable for a possible (string) mapping. */
347 UnmappedChoiceList *mapping = nullptr;
349 if (c == NFO_UTF8_IDENTIFIER) {
350 unicode = true;
351 str += len;
354 for (;;) {
355 if (unicode && Utf8EncodedCharLen(*str) != 0) {
356 c = Utf8Consume(&str);
357 /* 'Magic' range of control codes. */
358 if (GB(c, 8, 8) == 0xE0) {
359 c = GB(c, 0, 8);
360 } else if (c >= 0x20) {
361 if (!IsValidChar(c, CS_ALPHANUMERAL)) c = '?';
362 d += Utf8Encode(d, c);
363 continue;
365 } else {
366 c = (byte)*str++;
368 if (c == '\0') break;
370 switch (c) {
371 case 0x01:
372 if (str[0] == '\0') goto string_end;
373 d += Utf8Encode(d, ' ');
374 str++;
375 break;
376 case 0x0A: break;
377 case 0x0D:
378 if (allow_newlines) {
379 *d++ = 0x0A;
380 } else {
381 grfmsg(1, "Detected newline in string that does not allow one");
383 break;
384 case 0x0E: d += Utf8Encode(d, SCC_TINYFONT); break;
385 case 0x0F: d += Utf8Encode(d, SCC_BIGFONT); break;
386 case 0x1F:
387 if (str[0] == '\0' || str[1] == '\0') goto string_end;
388 d += Utf8Encode(d, ' ');
389 str += 2;
390 break;
391 case 0x7B:
392 case 0x7C:
393 case 0x7D:
394 case 0x7E:
395 case 0x7F: d += Utf8Encode(d, SCC_NEWGRF_PRINT_DWORD_SIGNED + c - 0x7B); break;
396 case 0x80: d += Utf8Encode(d, byte80); break;
397 case 0x81: {
398 if (str[0] == '\0' || str[1] == '\0') goto string_end;
399 StringID string;
400 string = ((uint8)*str++);
401 string |= ((uint8)*str++) << 8;
402 d += Utf8Encode(d, SCC_NEWGRF_STRINL);
403 d += Utf8Encode(d, MapGRFStringID(grfid, string));
404 break;
406 case 0x82:
407 case 0x83:
408 case 0x84: d += Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_DATE_LONG + c - 0x82); break;
409 case 0x85: d += Utf8Encode(d, SCC_NEWGRF_DISCARD_WORD); break;
410 case 0x86: d += Utf8Encode(d, SCC_NEWGRF_ROTATE_TOP_4_WORDS); break;
411 case 0x87: d += Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_VOLUME_LONG); break;
412 case 0x88: d += Utf8Encode(d, SCC_BLUE); break;
413 case 0x89: d += Utf8Encode(d, SCC_SILVER); break;
414 case 0x8A: d += Utf8Encode(d, SCC_GOLD); break;
415 case 0x8B: d += Utf8Encode(d, SCC_RED); break;
416 case 0x8C: d += Utf8Encode(d, SCC_PURPLE); break;
417 case 0x8D: d += Utf8Encode(d, SCC_LTBROWN); break;
418 case 0x8E: d += Utf8Encode(d, SCC_ORANGE); break;
419 case 0x8F: d += Utf8Encode(d, SCC_GREEN); break;
420 case 0x90: d += Utf8Encode(d, SCC_YELLOW); break;
421 case 0x91: d += Utf8Encode(d, SCC_DKGREEN); break;
422 case 0x92: d += Utf8Encode(d, SCC_CREAM); break;
423 case 0x93: d += Utf8Encode(d, SCC_BROWN); break;
424 case 0x94: d += Utf8Encode(d, SCC_WHITE); break;
425 case 0x95: d += Utf8Encode(d, SCC_LTBLUE); break;
426 case 0x96: d += Utf8Encode(d, SCC_GRAY); break;
427 case 0x97: d += Utf8Encode(d, SCC_DKBLUE); break;
428 case 0x98: d += Utf8Encode(d, SCC_BLACK); break;
429 case 0x9A: {
430 int code = *str++;
431 switch (code) {
432 case 0x00: goto string_end;
433 case 0x01: d += Utf8Encode(d, SCC_NEWGRF_PRINT_QWORD_CURRENCY); break;
434 /* 0x02: ignore next colour byte is not supported. It works on the final
435 * string and as such hooks into the string drawing routine. At that
436 * point many things already happened, such as splitting up of strings
437 * when drawn over multiple lines or right-to-left translations, which
438 * make the behaviour peculiar, e.g. only happening at specific width
439 * of windows. Or we need to add another pass over the string to just
440 * support this. As such it is not implemented in OpenTTD. */
441 case 0x03: {
442 if (str[0] == '\0' || str[1] == '\0') goto string_end;
443 uint16 tmp = ((uint8)*str++);
444 tmp |= ((uint8)*str++) << 8;
445 d += Utf8Encode(d, SCC_NEWGRF_PUSH_WORD);
446 d += Utf8Encode(d, tmp);
447 break;
449 case 0x04:
450 if (str[0] == '\0') goto string_end;
451 d += Utf8Encode(d, SCC_NEWGRF_UNPRINT);
452 d += Utf8Encode(d, *str++);
453 break;
454 case 0x06: d += Utf8Encode(d, SCC_NEWGRF_PRINT_BYTE_HEX); break;
455 case 0x07: d += Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_HEX); break;
456 case 0x08: d += Utf8Encode(d, SCC_NEWGRF_PRINT_DWORD_HEX); break;
457 /* 0x09, 0x0A are TTDPatch internal use only string codes. */
458 case 0x0B: d += Utf8Encode(d, SCC_NEWGRF_PRINT_QWORD_HEX); break;
459 case 0x0C: d += Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_STATION_NAME); break;
460 case 0x0D: d += Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_WEIGHT_LONG); break;
461 case 0x0E:
462 case 0x0F: {
463 if (str[0] == '\0') goto string_end;
464 const LanguageMap *lm = LanguageMap::GetLanguageMap(grfid, language_id);
465 int index = *str++;
466 int mapped = lm != nullptr ? lm->GetMapping(index, code == 0x0E) : -1;
467 if (mapped >= 0) {
468 d += Utf8Encode(d, code == 0x0E ? SCC_GENDER_INDEX : SCC_SET_CASE);
469 d += Utf8Encode(d, code == 0x0E ? mapped : mapped + 1);
471 break;
474 case 0x10:
475 case 0x11:
476 if (str[0] == '\0') goto string_end;
477 if (mapping == nullptr) {
478 if (code == 0x10) str++; // Skip the index
479 grfmsg(1, "choice list %s marker found when not expected", code == 0x10 ? "next" : "default");
480 break;
481 } else {
482 /* Terminate the previous string. */
483 *d = '\0';
484 int index = (code == 0x10 ? *str++ : 0);
485 if (mapping->strings.Contains(index)) {
486 grfmsg(1, "duplicate choice list string, ignoring");
487 d++;
488 } else {
489 d = mapping->strings[index] = MallocT<char>(strlen(str) * 10 + 1);
492 break;
494 case 0x12:
495 if (mapping == nullptr) {
496 grfmsg(1, "choice list end marker found when not expected");
497 } else {
498 /* Terminate the previous string. */
499 *d = '\0';
501 /* Now we can start flushing everything and clean everything up. */
502 d = mapping->Flush(LanguageMap::GetLanguageMap(grfid, language_id));
503 delete mapping;
504 mapping = nullptr;
506 break;
508 case 0x13:
509 case 0x14:
510 case 0x15:
511 if (str[0] == '\0') goto string_end;
512 if (mapping != nullptr) {
513 grfmsg(1, "choice lists can't be stacked, it's going to get messy now...");
514 if (code != 0x14) str++;
515 } else {
516 static const StringControlCode mp[] = { SCC_GENDER_LIST, SCC_SWITCH_CASE, SCC_PLURAL_LIST };
517 mapping = new UnmappedChoiceList(mp[code - 0x13], d, code == 0x14 ? 0 : *str++);
519 break;
521 case 0x16:
522 case 0x17:
523 case 0x18:
524 case 0x19:
525 case 0x1A:
526 case 0x1B:
527 case 0x1C:
528 case 0x1D:
529 case 0x1E:
530 d += Utf8Encode(d, SCC_NEWGRF_PRINT_DWORD_DATE_LONG + code - 0x16);
531 break;
533 case 0x1F: d += Utf8Encode(d, SCC_PUSH_COLOUR); break;
534 case 0x20: d += Utf8Encode(d, SCC_POP_COLOUR); break;
536 default:
537 grfmsg(1, "missing handler for extended format code");
538 break;
540 break;
543 case 0x9E: d += Utf8Encode(d, 0x20AC); break; // Euro
544 case 0x9F: d += Utf8Encode(d, 0x0178); break; // Y with diaeresis
545 case 0xA0: d += Utf8Encode(d, SCC_UP_ARROW); break;
546 case 0xAA: d += Utf8Encode(d, SCC_DOWN_ARROW); break;
547 case 0xAC: d += Utf8Encode(d, SCC_CHECKMARK); break;
548 case 0xAD: d += Utf8Encode(d, SCC_CROSS); break;
549 case 0xAF: d += Utf8Encode(d, SCC_RIGHT_ARROW); break;
550 case 0xB4: d += Utf8Encode(d, SCC_TRAIN); break;
551 case 0xB5: d += Utf8Encode(d, SCC_LORRY); break;
552 case 0xB6: d += Utf8Encode(d, SCC_BUS); break;
553 case 0xB7: d += Utf8Encode(d, SCC_PLANE); break;
554 case 0xB8: d += Utf8Encode(d, SCC_SHIP); break;
555 case 0xB9: d += Utf8Encode(d, SCC_SUPERSCRIPT_M1); break;
556 case 0xBC: d += Utf8Encode(d, SCC_SMALL_UP_ARROW); break;
557 case 0xBD: d += Utf8Encode(d, SCC_SMALL_DOWN_ARROW); break;
558 default:
559 /* Validate any unhandled character */
560 if (!IsValidChar(c, CS_ALPHANUMERAL)) c = '?';
561 d += Utf8Encode(d, c);
562 break;
566 string_end:
567 if (mapping != nullptr) {
568 grfmsg(1, "choice list was incomplete, the whole list is ignored");
569 delete mapping;
572 *d = '\0';
573 if (olen != nullptr) *olen = d - tmp + 1;
574 tmp = ReallocT(tmp, d - tmp + 1);
575 return tmp;
579 * Add a GRFText to a GRFText list.
580 * @param list The list where the text should be added to.
581 * @param text_to_add The GRFText to add to the list.
583 void AddGRFTextToList(GRFText **list, GRFText *text_to_add)
585 GRFText **ptext, *text;
587 /* Loop through all languages and see if we can replace a string */
588 for (ptext = list; (text = *ptext) != nullptr; ptext = &text->next) {
589 if (text->langid == text_to_add->langid) {
590 text_to_add->next = text->next;
591 *ptext = text_to_add;
592 delete text;
593 return;
597 /* If a string wasn't replaced, then we must append the new string */
598 *ptext = text_to_add;
602 * Add a string to a GRFText list.
603 * @param list The list where the text should be added to.
604 * @param langid The language of the new text.
605 * @param grfid The grfid where this string is defined.
606 * @param allow_newlines Whether newlines are allowed in this string.
607 * @param text_to_add The text to add to the list.
608 * @note All text-codes will be translated.
610 void AddGRFTextToList(struct GRFText **list, byte langid, uint32 grfid, bool allow_newlines, const char *text_to_add)
612 int len;
613 char *translatedtext = TranslateTTDPatchCodes(grfid, langid, allow_newlines, text_to_add, &len);
614 GRFText *newtext = GRFText::New(langid, translatedtext, len);
615 free(translatedtext);
617 AddGRFTextToList(list, newtext);
621 * Add a GRFText to a GRFText list. The text should not contain any text-codes.
622 * The text will be added as a 'default language'-text.
623 * @param list The list where the text should be added to.
624 * @param text_to_add The text to add to the list.
626 void AddGRFTextToList(struct GRFText **list, const char *text_to_add)
628 AddGRFTextToList(list, GRFText::New(0x7F, text_to_add, strlen(text_to_add) + 1));
632 * Create a copy of this GRFText list.
633 * @param orig The GRFText list to copy.
634 * @return A duplicate of the given GRFText.
636 GRFText *DuplicateGRFText(GRFText *orig)
638 GRFText *newtext = nullptr;
639 GRFText **ptext = &newtext;
640 for (; orig != nullptr; orig = orig->next) {
641 *ptext = GRFText::Copy(orig);
642 ptext = &(*ptext)->next;
644 return newtext;
648 * Add the new read string into our structure.
650 StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid_to_add, bool new_scheme, bool allow_newlines, const char *text_to_add, StringID def_string)
652 char *translatedtext;
653 uint id;
655 /* When working with the old language scheme (grf_version is less than 7) and
656 * English or American is among the set bits, simply add it as English in
657 * the new scheme, i.e. as langid = 1.
658 * If English is set, it is pretty safe to assume the translations are not
659 * actually translated.
661 if (!new_scheme) {
662 if (langid_to_add & (GRFLB_AMERICAN | GRFLB_ENGLISH)) {
663 langid_to_add = GRFLX_ENGLISH;
664 } else {
665 StringID ret = STR_EMPTY;
666 if (langid_to_add & GRFLB_GERMAN) ret = AddGRFString(grfid, stringid, GRFLX_GERMAN, true, allow_newlines, text_to_add, def_string);
667 if (langid_to_add & GRFLB_FRENCH) ret = AddGRFString(grfid, stringid, GRFLX_FRENCH, true, allow_newlines, text_to_add, def_string);
668 if (langid_to_add & GRFLB_SPANISH) ret = AddGRFString(grfid, stringid, GRFLX_SPANISH, true, allow_newlines, text_to_add, def_string);
669 return ret;
673 for (id = 0; id < _num_grf_texts; id++) {
674 if (_grf_text[id].grfid == grfid && _grf_text[id].stringid == stringid) {
675 break;
679 /* Too many strings allocated, return empty */
680 if (id == lengthof(_grf_text)) return STR_EMPTY;
682 int len;
683 translatedtext = TranslateTTDPatchCodes(grfid, langid_to_add, allow_newlines, text_to_add, &len);
685 GRFText *newtext = GRFText::New(langid_to_add, translatedtext, len);
687 free(translatedtext);
689 /* If we didn't find our stringid and grfid in the list, allocate a new id */
690 if (id == _num_grf_texts) _num_grf_texts++;
692 if (_grf_text[id].textholder == nullptr) {
693 _grf_text[id].grfid = grfid;
694 _grf_text[id].stringid = stringid;
695 _grf_text[id].def_string = def_string;
697 AddGRFTextToList(&_grf_text[id].textholder, newtext);
699 grfmsg(3, "Added 0x%X: grfid %08X string 0x%X lang 0x%X string '%s' (%X)", id, grfid, stringid, newtext->langid, newtext->text, MakeStringID(TEXT_TAB_NEWGRF_START, id));
701 return MakeStringID(TEXT_TAB_NEWGRF_START, id);
705 * Returns the index for this stringid associated with its grfID
707 StringID GetGRFStringID(uint32 grfid, StringID stringid)
709 for (uint id = 0; id < _num_grf_texts; id++) {
710 if (_grf_text[id].grfid == grfid && _grf_text[id].stringid == stringid) {
711 return MakeStringID(TEXT_TAB_NEWGRF_START, id);
715 return STR_UNDEFINED;
720 * Get a C-string from a GRFText-list. If there is a translation for the
721 * current language it is returned, otherwise the default translation
722 * is returned. If there is neither a default nor a translation for the
723 * current language nullptr is returned.
724 * @param text The GRFText to get the string from.
726 const char *GetGRFStringFromGRFText(const GRFText *text)
728 const char *default_text = nullptr;
730 /* Search the list of lang-strings of this stringid for current lang */
731 for (; text != nullptr; text = text->next) {
732 if (text->langid == _currentLangID) return text->text;
734 /* If the current string is English or American, set it as the
735 * fallback language if the specific language isn't available. */
736 if (text->langid == GRFLX_UNSPECIFIED || (default_text == nullptr && (text->langid == GRFLX_ENGLISH || text->langid == GRFLX_AMERICAN))) {
737 default_text = text->text;
741 return default_text;
745 * Get a C-string from a stringid set by a newgrf.
747 const char *GetGRFStringPtr(uint16 stringid)
749 assert(_grf_text[stringid].grfid != 0);
751 const char *str = GetGRFStringFromGRFText(_grf_text[stringid].textholder);
752 if (str != nullptr) return str;
754 /* Use the default string ID if the fallback string isn't available */
755 return GetStringPtr(_grf_text[stringid].def_string);
759 * Equivalence Setter function between game and newgrf langID.
760 * This function will adjust _currentLangID as to what is the LangID
761 * of the current language set by the user.
762 * This function is called after the user changed language,
763 * from strings.cpp:ReadLanguagePack
764 * @param language_id iso code of current selection
766 void SetCurrentGrfLangID(byte language_id)
768 _currentLangID = language_id;
771 bool CheckGrfLangID(byte lang_id, byte grf_version)
773 if (grf_version < 7) {
774 switch (_currentLangID) {
775 case GRFLX_GERMAN: return (lang_id & GRFLB_GERMAN) != 0;
776 case GRFLX_FRENCH: return (lang_id & GRFLB_FRENCH) != 0;
777 case GRFLX_SPANISH: return (lang_id & GRFLB_SPANISH) != 0;
778 default: return (lang_id & (GRFLB_ENGLISH | GRFLB_AMERICAN)) != 0;
782 return (lang_id == _currentLangID || lang_id == GRFLX_UNSPECIFIED);
786 * Delete all items of a linked GRFText list.
787 * @param grftext the head of the list to delete
789 void CleanUpGRFText(GRFText *grftext)
791 while (grftext != nullptr) {
792 GRFText *grftext2 = grftext->next;
793 delete grftext;
794 grftext = grftext2;
799 * House cleaning.
800 * Remove all strings and reset the text counter.
802 void CleanUpStrings()
804 uint id;
806 for (id = 0; id < _num_grf_texts; id++) {
807 CleanUpGRFText(_grf_text[id].textholder);
808 _grf_text[id].grfid = 0;
809 _grf_text[id].stringid = 0;
810 _grf_text[id].textholder = nullptr;
813 _num_grf_texts = 0;
816 struct TextRefStack {
817 std::array<byte, 0x30> stack;
818 byte position;
819 const GRFFile *grffile;
820 bool used;
822 TextRefStack() : position(0), grffile(nullptr), used(false) {}
824 uint8 PopUnsignedByte() { assert(this->position < this->stack.size()); return this->stack[this->position++]; }
825 int8 PopSignedByte() { return (int8)this->PopUnsignedByte(); }
827 uint16 PopUnsignedWord()
829 uint16 val = this->PopUnsignedByte();
830 return val | (this->PopUnsignedByte() << 8);
832 int16 PopSignedWord() { return (int32)this->PopUnsignedWord(); }
834 uint32 PopUnsignedDWord()
836 uint32 val = this->PopUnsignedWord();
837 return val | (this->PopUnsignedWord() << 16);
839 int32 PopSignedDWord() { return (int32)this->PopUnsignedDWord(); }
841 uint64 PopUnsignedQWord()
843 uint64 val = this->PopUnsignedDWord();
844 return val | (((uint64)this->PopUnsignedDWord()) << 32);
846 int64 PopSignedQWord() { return (int64)this->PopUnsignedQWord(); }
848 /** Rotate the top four words down: W1, W2, W3, W4 -> W4, W1, W2, W3 */
849 void RotateTop4Words()
851 byte tmp[2];
852 for (int i = 0; i < 2; i++) tmp[i] = this->stack[this->position + i + 6];
853 for (int i = 5; i >= 0; i--) this->stack[this->position + i + 2] = this->stack[this->position + i];
854 for (int i = 0; i < 2; i++) this->stack[this->position + i] = tmp[i];
857 void PushWord(uint16 word)
859 if (this->position >= 2) {
860 this->position -= 2;
861 } else {
862 // Rotate right 2 positions
863 std::rotate(this->stack.rbegin(), this->stack.rbegin() + 2, this->stack.rend());
865 this->stack[this->position] = GB(word, 0, 8);
866 this->stack[this->position + 1] = GB(word, 8, 8);
869 void ResetStack(const GRFFile *grffile)
871 assert(grffile != nullptr);
872 this->position = 0;
873 this->grffile = grffile;
874 this->used = true;
877 void RewindStack() { this->position = 0; }
880 /** The stack that is used for TTDP compatible string code parsing */
881 static TextRefStack _newgrf_textrefstack;
884 * Check whether the NewGRF text stack is in use.
885 * @return True iff the NewGRF text stack is used.
887 bool UsingNewGRFTextStack()
889 return _newgrf_textrefstack.used;
893 * Create a backup of the current NewGRF text stack.
894 * @return A copy of the current text stack.
896 struct TextRefStack *CreateTextRefStackBackup()
898 return new TextRefStack(_newgrf_textrefstack);
902 * Restore a copy of the text stack to the used stack.
903 * @param backup The copy to restore.
905 void RestoreTextRefStackBackup(struct TextRefStack *backup)
907 _newgrf_textrefstack = *backup;
908 delete backup;
912 * Start using the TTDP compatible string code parsing.
914 * On start a number of values is copied on the #TextRefStack.
915 * You can then use #GetString() and the normal string drawing functions,
916 * and they will use the #TextRefStack for NewGRF string codes.
918 * However, when you want to draw a string multiple times using the same stack,
919 * you have to call #RewindTextRefStack() between draws.
921 * After you are done with drawing, you must disable usage of the #TextRefStack
922 * by calling #StopTextRefStackUsage(), so NewGRF string codes operate on the
923 * normal string parameters again.
925 * @param grffile the NewGRF providing the stack data
926 * @param numEntries number of entries to copy from the registers
927 * @param values values to copy onto the stack; if nullptr the temporary NewGRF registers will be used instead
929 void StartTextRefStackUsage(const GRFFile *grffile, byte numEntries, const uint32 *values)
931 extern TemporaryStorageArray<int32, 0x110> _temp_store;
933 _newgrf_textrefstack.ResetStack(grffile);
935 auto stack_it = _newgrf_textrefstack.stack.begin();
936 for (uint i = 0; i < numEntries; i++) {
937 uint32 value = values != nullptr ? values[i] : _temp_store.GetValue(0x100 + i);
938 for (uint j = 0; j < 32; j += 8) {
939 *stack_it = GB(value, j, 8);
940 stack_it++;
945 /** Stop using the TTDP compatible string code parsing */
946 void StopTextRefStackUsage()
948 _newgrf_textrefstack.used = false;
951 void RewindTextRefStack()
953 _newgrf_textrefstack.RewindStack();
957 * FormatString for NewGRF specific "magic" string control codes
958 * @param scc the string control code that has been read
959 * @param buff the buffer we're writing to
960 * @param str the string that we need to write
961 * @param argv the OpenTTD stack of values
962 * @param argv_size space on the stack \a argv
963 * @param modify_argv When true, modify the OpenTTD stack.
964 * @return the string control code to "execute" now
966 uint RemapNewGRFStringControlCode(uint scc, char *buf_start, char **buff, const char **str, int64 *argv, uint argv_size, bool modify_argv)
968 switch (scc) {
969 default: break;
971 case SCC_NEWGRF_PRINT_DWORD_SIGNED:
972 case SCC_NEWGRF_PRINT_WORD_SIGNED:
973 case SCC_NEWGRF_PRINT_BYTE_SIGNED:
974 case SCC_NEWGRF_PRINT_WORD_UNSIGNED:
975 case SCC_NEWGRF_PRINT_BYTE_HEX:
976 case SCC_NEWGRF_PRINT_WORD_HEX:
977 case SCC_NEWGRF_PRINT_DWORD_HEX:
978 case SCC_NEWGRF_PRINT_QWORD_HEX:
979 case SCC_NEWGRF_PRINT_DWORD_CURRENCY:
980 case SCC_NEWGRF_PRINT_QWORD_CURRENCY:
981 case SCC_NEWGRF_PRINT_WORD_STRING_ID:
982 case SCC_NEWGRF_PRINT_WORD_DATE_LONG:
983 case SCC_NEWGRF_PRINT_DWORD_DATE_LONG:
984 case SCC_NEWGRF_PRINT_WORD_DATE_SHORT:
985 case SCC_NEWGRF_PRINT_DWORD_DATE_SHORT:
986 case SCC_NEWGRF_PRINT_WORD_SPEED:
987 case SCC_NEWGRF_PRINT_WORD_VOLUME_LONG:
988 case SCC_NEWGRF_PRINT_WORD_VOLUME_SHORT:
989 case SCC_NEWGRF_PRINT_WORD_WEIGHT_LONG:
990 case SCC_NEWGRF_PRINT_WORD_WEIGHT_SHORT:
991 case SCC_NEWGRF_PRINT_WORD_POWER:
992 case SCC_NEWGRF_PRINT_WORD_STATION_NAME:
993 case SCC_NEWGRF_PRINT_WORD_CARGO_NAME:
994 if (argv_size < 1) {
995 DEBUG(misc, 0, "Too many NewGRF string parameters.");
996 return 0;
998 break;
1000 case SCC_NEWGRF_PRINT_WORD_CARGO_LONG:
1001 case SCC_NEWGRF_PRINT_WORD_CARGO_SHORT:
1002 case SCC_NEWGRF_PRINT_WORD_CARGO_TINY:
1003 if (argv_size < 2) {
1004 DEBUG(misc, 0, "Too many NewGRF string parameters.");
1005 return 0;
1007 break;
1010 if (_newgrf_textrefstack.used && modify_argv) {
1011 switch (scc) {
1012 default: NOT_REACHED();
1013 case SCC_NEWGRF_PRINT_BYTE_SIGNED: *argv = _newgrf_textrefstack.PopSignedByte(); break;
1014 case SCC_NEWGRF_PRINT_QWORD_CURRENCY: *argv = _newgrf_textrefstack.PopSignedQWord(); break;
1016 case SCC_NEWGRF_PRINT_DWORD_CURRENCY:
1017 case SCC_NEWGRF_PRINT_DWORD_SIGNED: *argv = _newgrf_textrefstack.PopSignedDWord(); break;
1019 case SCC_NEWGRF_PRINT_BYTE_HEX: *argv = _newgrf_textrefstack.PopUnsignedByte(); break;
1020 case SCC_NEWGRF_PRINT_QWORD_HEX: *argv = _newgrf_textrefstack.PopUnsignedQWord(); break;
1022 case SCC_NEWGRF_PRINT_WORD_SPEED:
1023 case SCC_NEWGRF_PRINT_WORD_VOLUME_LONG:
1024 case SCC_NEWGRF_PRINT_WORD_VOLUME_SHORT:
1025 case SCC_NEWGRF_PRINT_WORD_SIGNED: *argv = _newgrf_textrefstack.PopSignedWord(); break;
1027 case SCC_NEWGRF_PRINT_WORD_HEX:
1028 case SCC_NEWGRF_PRINT_WORD_WEIGHT_LONG:
1029 case SCC_NEWGRF_PRINT_WORD_WEIGHT_SHORT:
1030 case SCC_NEWGRF_PRINT_WORD_POWER:
1031 case SCC_NEWGRF_PRINT_WORD_STATION_NAME:
1032 case SCC_NEWGRF_PRINT_WORD_UNSIGNED: *argv = _newgrf_textrefstack.PopUnsignedWord(); break;
1034 case SCC_NEWGRF_PRINT_DWORD_DATE_LONG:
1035 case SCC_NEWGRF_PRINT_DWORD_DATE_SHORT:
1036 case SCC_NEWGRF_PRINT_DWORD_HEX: *argv = _newgrf_textrefstack.PopUnsignedDWord(); break;
1038 case SCC_NEWGRF_PRINT_WORD_DATE_LONG:
1039 case SCC_NEWGRF_PRINT_WORD_DATE_SHORT: *argv = _newgrf_textrefstack.PopUnsignedWord() + DAYS_TILL_ORIGINAL_BASE_YEAR; break;
1041 case SCC_NEWGRF_DISCARD_WORD: _newgrf_textrefstack.PopUnsignedWord(); break;
1043 case SCC_NEWGRF_ROTATE_TOP_4_WORDS: _newgrf_textrefstack.RotateTop4Words(); break;
1044 case SCC_NEWGRF_PUSH_WORD: _newgrf_textrefstack.PushWord(Utf8Consume(str)); break;
1045 case SCC_NEWGRF_UNPRINT: *buff = max(*buff - Utf8Consume(str), buf_start); break;
1047 case SCC_NEWGRF_PRINT_WORD_CARGO_LONG:
1048 case SCC_NEWGRF_PRINT_WORD_CARGO_SHORT:
1049 case SCC_NEWGRF_PRINT_WORD_CARGO_TINY:
1050 argv[0] = GetCargoTranslation(_newgrf_textrefstack.PopUnsignedWord(), _newgrf_textrefstack.grffile);
1051 argv[1] = _newgrf_textrefstack.PopUnsignedWord();
1052 break;
1054 case SCC_NEWGRF_PRINT_WORD_STRING_ID:
1055 *argv = MapGRFStringID(_newgrf_textrefstack.grffile->grfid, _newgrf_textrefstack.PopUnsignedWord());
1056 break;
1058 case SCC_NEWGRF_PRINT_WORD_CARGO_NAME: {
1059 CargoID cargo = GetCargoTranslation(_newgrf_textrefstack.PopUnsignedWord(), _newgrf_textrefstack.grffile);
1060 *argv = cargo < NUM_CARGO ? 1 << cargo : 0;
1061 break;
1064 } else {
1065 /* Consume additional parameter characters */
1066 switch (scc) {
1067 default: break;
1069 case SCC_NEWGRF_PUSH_WORD:
1070 case SCC_NEWGRF_UNPRINT:
1071 Utf8Consume(str);
1072 break;
1076 switch (scc) {
1077 default: NOT_REACHED();
1078 case SCC_NEWGRF_PRINT_DWORD_SIGNED:
1079 case SCC_NEWGRF_PRINT_WORD_SIGNED:
1080 case SCC_NEWGRF_PRINT_BYTE_SIGNED:
1081 case SCC_NEWGRF_PRINT_WORD_UNSIGNED:
1082 return SCC_COMMA;
1084 case SCC_NEWGRF_PRINT_BYTE_HEX:
1085 case SCC_NEWGRF_PRINT_WORD_HEX:
1086 case SCC_NEWGRF_PRINT_DWORD_HEX:
1087 case SCC_NEWGRF_PRINT_QWORD_HEX:
1088 return SCC_HEX;
1090 case SCC_NEWGRF_PRINT_DWORD_CURRENCY:
1091 case SCC_NEWGRF_PRINT_QWORD_CURRENCY:
1092 return SCC_CURRENCY_LONG;
1094 case SCC_NEWGRF_PRINT_WORD_STRING_ID:
1095 return SCC_NEWGRF_PRINT_WORD_STRING_ID;
1097 case SCC_NEWGRF_PRINT_WORD_DATE_LONG:
1098 case SCC_NEWGRF_PRINT_DWORD_DATE_LONG:
1099 return SCC_DATE_LONG;
1101 case SCC_NEWGRF_PRINT_WORD_DATE_SHORT:
1102 case SCC_NEWGRF_PRINT_DWORD_DATE_SHORT:
1103 return SCC_DATE_SHORT;
1105 case SCC_NEWGRF_PRINT_WORD_SPEED:
1106 return SCC_VELOCITY;
1108 case SCC_NEWGRF_PRINT_WORD_VOLUME_LONG:
1109 return SCC_VOLUME_LONG;
1111 case SCC_NEWGRF_PRINT_WORD_VOLUME_SHORT:
1112 return SCC_VOLUME_SHORT;
1114 case SCC_NEWGRF_PRINT_WORD_WEIGHT_LONG:
1115 return SCC_WEIGHT_LONG;
1117 case SCC_NEWGRF_PRINT_WORD_WEIGHT_SHORT:
1118 return SCC_WEIGHT_SHORT;
1120 case SCC_NEWGRF_PRINT_WORD_POWER:
1121 return SCC_POWER;
1123 case SCC_NEWGRF_PRINT_WORD_CARGO_LONG:
1124 return SCC_CARGO_LONG;
1126 case SCC_NEWGRF_PRINT_WORD_CARGO_SHORT:
1127 return SCC_CARGO_SHORT;
1129 case SCC_NEWGRF_PRINT_WORD_CARGO_TINY:
1130 return SCC_CARGO_TINY;
1132 case SCC_NEWGRF_PRINT_WORD_CARGO_NAME:
1133 return SCC_CARGO_LIST;
1135 case SCC_NEWGRF_PRINT_WORD_STATION_NAME:
1136 return SCC_STATION_NAME;
1138 case SCC_NEWGRF_DISCARD_WORD:
1139 case SCC_NEWGRF_ROTATE_TOP_4_WORDS:
1140 case SCC_NEWGRF_PUSH_WORD:
1141 case SCC_NEWGRF_UNPRINT:
1142 return 0;