4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
11 * @file newgrf_text.cpp
12 * Implementation of Action 04 "universal holder" structure and functions.
13 * This file implements a linked-lists of strings,
14 * holding everything that the newgrf action 04 will send over to OpenTTD.
15 * One of the biggest problems is that Dynamic lang Array uses ISO codes
16 * as way to identifying current user lang, while newgrf uses bit shift codes
17 * not related to ISO. So equivalence functionnality had to be set.
22 #include "strings_func.h"
23 #include "newgrf_storage.h"
24 #include "newgrf_text.h"
25 #include "newgrf_cargo.h"
27 #include "date_type.h"
29 #include "core/alloc_type.hpp"
30 #include "core/smallmap_type.hpp"
33 #include "table/strings.h"
34 #include "table/control_codes.h"
40 * Explains the newgrf shift bit positioning.
41 * the grf base will not be used in order to find the string, but rather for
42 * jumping from standard langID scheme to the new one.
44 enum GRFBaseLanguages
{
45 GRFLB_AMERICAN
= 0x01,
53 enum GRFExtendedLanguages
{
54 GRFLX_AMERICAN
= 0x00,
59 GRFLX_UNSPECIFIED
= 0x7F,
64 * Holder of a GRFTextMap.
65 * Putting both grfid and stringid together allows us to avoid duplicates,
66 * since it is NOT SUPPOSED to happen.
76 static uint _num_grf_texts
= 0;
77 static GRFTextEntry _grf_text
[(1 << TABSIZE
) * 3];
78 static byte _currentLangID
= GRFLX_ENGLISH
; ///< by default, english is used.
81 * Get the mapping from the NewGRF supplied ID to OpenTTD's internal ID.
82 * @param newgrf_id The NewGRF ID to map.
83 * @param gender Whether to map genders or cases.
84 * @return The, to OpenTTD's internal ID, mapped index, or -1 if there is no mapping.
86 int LanguageMap::GetMapping(int newgrf_id
, bool gender
) const
88 const SmallVector
<Mapping
, 1> &map
= gender
? this->gender_map
: this->case_map
;
89 for (const Mapping
*m
= map
.Begin(); m
!= map
.End(); m
++) {
90 if (m
->newgrf_id
== newgrf_id
) return m
->openttd_id
;
96 * Get the mapping from OpenTTD's internal ID to the NewGRF supplied ID.
97 * @param openttd_id The OpenTTD ID to map.
98 * @param gender Whether to map genders or cases.
99 * @return The, to the NewGRF supplied ID, mapped index, or -1 if there is no mapping.
101 int LanguageMap::GetReverseMapping(int openttd_id
, bool gender
) const
103 const SmallVector
<Mapping
, 1> &map
= gender
? this->gender_map
: this->case_map
;
104 for (const Mapping
*m
= map
.Begin(); m
!= map
.End(); m
++) {
105 if (m
->openttd_id
== openttd_id
) return m
->newgrf_id
;
111 /** Construct a copy of this text map. */
112 GRFTextMap::GRFTextMap (const GRFTextMap
&other
)
113 : std::map
<byte
, GRFText
*> (other
)
115 for (iterator iter
= this->begin(); iter
!= this->end(); iter
++) {
116 iter
->second
= iter
->second
->clone();
120 /** Destroy the text map. */
121 GRFTextMap::~GRFTextMap()
123 for (iterator iter
= this->begin(); iter
!= this->end(); iter
++) {
128 /** Get the GRFText for the current language, or a default. */
129 const GRFText
*GRFTextMap::get_current (void) const
131 byte langs
[4] = { _currentLangID
, GRFLX_UNSPECIFIED
, GRFLX_ENGLISH
, GRFLX_AMERICAN
};
133 for (uint i
= 0; i
< lengthof(langs
); i
++) {
134 const_iterator iter
= this->find (langs
[i
]);
135 if (iter
!= this->end()) return iter
->second
;
141 /** Add a GRFText to this map. */
142 void GRFTextMap::add (byte langid
, GRFText
*text
)
144 std::pair
<iterator
, bool> pair
=
145 this->insert (std::make_pair (langid
, text
));
148 /* The langid already existed in the map. */
149 GRFText
*old
= pair
.first
->second
;
150 pair
.first
->second
= text
;
156 * Add a string to this map.
157 * @param langid The language of the new text.
158 * @param grfid The grfid where this string is defined.
159 * @param allow_newlines Whether newlines are allowed in this string.
160 * @param text The text to add to the list.
161 * @note All text-codes will be translated.
163 void GRFTextMap::add (byte langid
, uint32 grfid
, bool allow_newlines
, const char *text
)
166 char *translatedtext
= TranslateTTDPatchCodes (grfid
, langid
, allow_newlines
, text
, &len
);
167 GRFText
*newtext
= GRFText::create (translatedtext
, len
);
168 free (translatedtext
);
170 this->add (langid
, newtext
);
174 /** Helper structure for mapping choice lists. */
175 struct UnmappedChoiceList
: ZeroedMemoryAllocator
{
176 /** Clean everything up. */
177 ~UnmappedChoiceList()
179 for (SmallPair
<byte
, char *> *p
= this->strings
.Begin(); p
< this->strings
.End(); p
++) {
185 * Initialise the mapping.
186 * @param type The type of mapping.
187 * @param old_d The old begin of the string, i.e. from where to start writing again.
188 * @param offset The offset to get the plural/gender from.
190 UnmappedChoiceList(StringControlCode type
, char *old_d
, int offset
) :
191 type(type
), old_d(old_d
), offset(offset
)
195 StringControlCode type
; ///< The type of choice list.
196 char *old_d
; ///< The old/original location of the "d" local variable.
197 int offset
; ///< The offset for the plural/gender form.
199 /** Mapping of NewGRF supplied ID to the different strings in the choice list. */
200 SmallMap
<byte
, char *> strings
;
203 * Flush this choice list into the old d variable.
204 * @param lm The current language mapping.
205 * @return The new location of the output string.
207 char *Flush(const LanguageMap
*lm
)
209 if (!this->strings
.Contains(0)) {
210 /* In case of a (broken) NewGRF without a default,
211 * assume an empty string. */
212 grfmsg(1, "choice list misses default value");
213 this->strings
[0] = xstrdup("");
218 /* In case there is no mapping, just ignore everything but the default.
219 * A probable cause for this happening is when the language file has
220 * been removed by the user and as such no mapping could be made. */
221 size_t len
= strlen(this->strings
[0]);
222 memcpy(d
, this->strings
[0], len
);
226 d
+= Utf8Encode(d
, this->type
);
228 if (this->type
== SCC_SWITCH_CASE
) {
230 * Format for case switch:
231 * <NUM CASES> <CASE1> <LEN1> <STRING1> <CASE2> <LEN2> <STRING2> <CASE3> <LEN3> <STRING3> <STRINGDEFAULT>
232 * Each LEN is printed using 2 bytes in big endian order.
237 for (uint8 i
= 0; i
< _current_language
->num_cases
; i
++) {
238 /* Count the ones we have a mapped string for. */
239 if (this->strings
.Contains(lm
->GetReverseMapping(i
, false))) count
++;
243 for (uint8 i
= 0; i
< _current_language
->num_cases
; i
++) {
244 /* Resolve the string we're looking for. */
245 int idx
= lm
->GetReverseMapping(i
, false);
246 if (!this->strings
.Contains(idx
)) continue;
247 char *str
= this->strings
[idx
];
253 size_t len
= strlen(str
) + 1;
254 *d
++ = GB(len
, 8, 8);
255 *d
++ = GB(len
, 0, 8);
262 /* "<STRINGDEFAULT>" */
263 size_t len
= strlen(this->strings
[0]) + 1;
264 memcpy(d
, this->strings
[0], len
);
267 if (this->type
== SCC_PLURAL_LIST
) {
268 *d
++ = lm
->plural_form
;
272 * Format for choice list:
273 * <OFFSET> <NUM CHOICES> <LENs> <STRINGs>
277 *d
++ = this->offset
- 0x80;
279 /* "<NUM CHOICES>" */
280 int count
= (this->type
== SCC_GENDER_LIST
? _current_language
->num_genders
: LANGUAGE_MAX_PLURAL_FORMS
);
284 for (int i
= 0; i
< count
; i
++) {
285 int idx
= (this->type
== SCC_GENDER_LIST
? lm
->GetReverseMapping(i
, true) : i
+ 1);
286 const char *str
= this->strings
[this->strings
.Contains(idx
) ? idx
: 0];
287 size_t len
= strlen(str
) + 1;
288 if (len
> 0xFF) grfmsg(1, "choice list string is too long");
289 *d
++ = GB(len
, 0, 8);
293 for (int i
= 0; i
< count
; i
++) {
294 int idx
= (this->type
== SCC_GENDER_LIST
? lm
->GetReverseMapping(i
, true) : i
+ 1);
295 const char *str
= this->strings
[this->strings
.Contains(idx
) ? idx
: 0];
296 /* Limit the length of the string we copy to 0xFE. The length is written above
297 * as a byte and we need room for the final '\0'. */
298 size_t len
= min
<size_t>(0xFE, strlen(str
));
309 * Translate TTDPatch string codes into something OpenTTD can handle (better).
310 * @param grfid The (NewGRF) ID associated with this string
311 * @param language_id The (NewGRF) language ID associated with this string.
312 * @param allow_newlines Whether newlines are allowed in the string or not.
313 * @param str The string to translate.
314 * @param [out] olen The length of the final string.
315 * @param byte80 The control code to use as replacement for the 0x80-value.
316 * @return The translated string.
318 char *TranslateTTDPatchCodes(uint32 grfid
, uint8 language_id
, bool allow_newlines
, const char *str
, int *olen
, StringControlCode byte80
)
320 char *tmp
= xmalloc (strlen(str
) * 10 + 1); // Allocate space to allow for expansion
322 bool unicode
= false;
324 size_t len
= Utf8Decode(&c
, str
);
326 /* Helper variable for a possible (string) mapping. */
327 UnmappedChoiceList
*mapping
= NULL
;
329 if (c
== NFO_UTF8_IDENTIFIER
) {
335 if (unicode
&& Utf8EncodedCharLen(*str
) != 0) {
336 c
= Utf8Consume(&str
);
337 /* 'Magic' range of control codes. */
338 if (GB(c
, 8, 8) == 0xE0) {
340 } else if (c
>= 0x20) {
341 if (!IsValidChar(c
, CS_ALPHANUMERAL
)) c
= '?';
342 d
+= Utf8Encode(d
, c
);
348 if (c
== '\0') break;
352 if (str
[0] == '\0') goto string_end
;
353 d
+= Utf8Encode(d
, ' ');
358 if (allow_newlines
) {
361 grfmsg(1, "Detected newline in string that does not allow one");
364 case 0x0E: d
+= Utf8Encode(d
, SCC_TINYFONT
); break;
365 case 0x0F: d
+= Utf8Encode(d
, SCC_BIGFONT
); break;
367 if (str
[0] == '\0' || str
[1] == '\0') goto string_end
;
368 d
+= Utf8Encode(d
, ' ');
375 case 0x7F: d
+= Utf8Encode(d
, SCC_NEWGRF_PRINT_DWORD_SIGNED
+ c
- 0x7B); break;
376 case 0x80: d
+= Utf8Encode(d
, byte80
); break;
378 if (str
[0] == '\0' || str
[1] == '\0') goto string_end
;
380 string
= ((uint8
)*str
++);
381 string
|= ((uint8
)*str
++) << 8;
382 d
+= Utf8Encode(d
, SCC_NEWGRF_STRINL
);
383 d
+= Utf8Encode(d
, MapGRFStringID(grfid
, string
));
388 case 0x84: d
+= Utf8Encode(d
, SCC_NEWGRF_PRINT_WORD_DATE_LONG
+ c
- 0x82); break;
389 case 0x85: d
+= Utf8Encode(d
, SCC_NEWGRF_DISCARD_WORD
); break;
390 case 0x86: d
+= Utf8Encode(d
, SCC_NEWGRF_ROTATE_TOP_4_WORDS
); break;
391 case 0x87: d
+= Utf8Encode(d
, SCC_NEWGRF_PRINT_WORD_VOLUME_LONG
); break;
392 case 0x88: d
+= Utf8Encode(d
, SCC_BLUE
); break;
393 case 0x89: d
+= Utf8Encode(d
, SCC_SILVER
); break;
394 case 0x8A: d
+= Utf8Encode(d
, SCC_GOLD
); break;
395 case 0x8B: d
+= Utf8Encode(d
, SCC_RED
); break;
396 case 0x8C: d
+= Utf8Encode(d
, SCC_PURPLE
); break;
397 case 0x8D: d
+= Utf8Encode(d
, SCC_LTBROWN
); break;
398 case 0x8E: d
+= Utf8Encode(d
, SCC_ORANGE
); break;
399 case 0x8F: d
+= Utf8Encode(d
, SCC_GREEN
); break;
400 case 0x90: d
+= Utf8Encode(d
, SCC_YELLOW
); break;
401 case 0x91: d
+= Utf8Encode(d
, SCC_DKGREEN
); break;
402 case 0x92: d
+= Utf8Encode(d
, SCC_CREAM
); break;
403 case 0x93: d
+= Utf8Encode(d
, SCC_BROWN
); break;
404 case 0x94: d
+= Utf8Encode(d
, SCC_WHITE
); break;
405 case 0x95: d
+= Utf8Encode(d
, SCC_LTBLUE
); break;
406 case 0x96: d
+= Utf8Encode(d
, SCC_GRAY
); break;
407 case 0x97: d
+= Utf8Encode(d
, SCC_DKBLUE
); break;
408 case 0x98: d
+= Utf8Encode(d
, SCC_BLACK
); break;
412 case 0x00: goto string_end
;
413 case 0x01: d
+= Utf8Encode(d
, SCC_NEWGRF_PRINT_QWORD_CURRENCY
); break;
414 /* 0x02: ignore next colour byte is not supported. It works on the final
415 * string and as such hooks into the string drawing routine. At that
416 * point many things already happened, such as splitting up of strings
417 * when drawn over multiple lines or right-to-left translations, which
418 * make the behaviour peculiar, e.g. only happening at specific width
419 * of windows. Or we need to add another pass over the string to just
420 * support this. As such it is not implemented in OpenTTD. */
422 if (str
[0] == '\0' || str
[1] == '\0') goto string_end
;
423 uint16 tmp
= ((uint8
)*str
++);
424 tmp
|= ((uint8
)*str
++) << 8;
425 d
+= Utf8Encode(d
, SCC_NEWGRF_PUSH_WORD
);
426 d
+= Utf8Encode(d
, tmp
);
430 if (str
[0] == '\0') goto string_end
;
431 d
+= Utf8Encode(d
, SCC_NEWGRF_UNPRINT
);
432 d
+= Utf8Encode(d
, *str
++);
434 case 0x06: d
+= Utf8Encode(d
, SCC_NEWGRF_PRINT_BYTE_HEX
); break;
435 case 0x07: d
+= Utf8Encode(d
, SCC_NEWGRF_PRINT_WORD_HEX
); break;
436 case 0x08: d
+= Utf8Encode(d
, SCC_NEWGRF_PRINT_DWORD_HEX
); break;
437 /* 0x09, 0x0A are TTDPatch internal use only string codes. */
438 case 0x0B: d
+= Utf8Encode(d
, SCC_NEWGRF_PRINT_QWORD_HEX
); break;
439 case 0x0C: d
+= Utf8Encode(d
, SCC_NEWGRF_PRINT_WORD_STATION_NAME
); break;
440 case 0x0D: d
+= Utf8Encode(d
, SCC_NEWGRF_PRINT_WORD_WEIGHT_LONG
); break;
443 if (str
[0] == '\0') goto string_end
;
444 const LanguageMap
*lm
= LanguageMap::GetLanguageMap(grfid
, language_id
);
446 int mapped
= lm
!= NULL
? lm
->GetMapping(index
, code
== 0x0E) : -1;
448 d
+= Utf8Encode(d
, code
== 0x0E ? SCC_GENDER_INDEX
: SCC_SET_CASE
);
449 d
+= Utf8Encode(d
, code
== 0x0E ? mapped
: mapped
+ 1);
456 if (str
[0] == '\0') goto string_end
;
457 if (mapping
== NULL
) {
458 if (code
== 0x10) str
++; // Skip the index
459 grfmsg(1, "choice list %s marker found when not expected", code
== 0x10 ? "next" : "default");
462 /* Terminate the previous string. */
464 int index
= (code
== 0x10 ? *str
++ : 0);
465 if (mapping
->strings
.Contains(index
)) {
466 grfmsg(1, "duplicate choice list string, ignoring");
469 d
= mapping
->strings
[index
] = xmalloc (strlen(str
) * 10 + 1);
475 if (mapping
== NULL
) {
476 grfmsg(1, "choice list end marker found when not expected");
478 /* Terminate the previous string. */
481 /* Now we can start flushing everything and clean everything up. */
482 d
= mapping
->Flush(LanguageMap::GetLanguageMap(grfid
, language_id
));
491 if (str
[0] == '\0') goto string_end
;
492 if (mapping
!= NULL
) {
493 grfmsg(1, "choice lists can't be stacked, it's going to get messy now...");
494 if (code
!= 0x14) str
++;
496 static const StringControlCode mp
[] = { SCC_GENDER_LIST
, SCC_SWITCH_CASE
, SCC_PLURAL_LIST
};
497 mapping
= new UnmappedChoiceList(mp
[code
- 0x13], d
, code
== 0x14 ? 0 : *str
++);
510 d
+= Utf8Encode(d
, SCC_NEWGRF_PRINT_DWORD_DATE_LONG
+ code
- 0x16);
514 grfmsg(1, "missing handler for extended format code");
520 case 0x9E: d
+= Utf8Encode(d
, 0x20AC); break; // Euro
521 case 0x9F: d
+= Utf8Encode(d
, 0x0178); break; // Y with diaeresis
522 case 0xA0: d
+= Utf8Encode(d
, SCC_UP_ARROW
); break;
523 case 0xAA: d
+= Utf8Encode(d
, SCC_DOWN_ARROW
); break;
524 case 0xAC: d
+= Utf8Encode(d
, SCC_CHECKMARK
); break;
525 case 0xAD: d
+= Utf8Encode(d
, SCC_CROSS
); break;
526 case 0xAF: d
+= Utf8Encode(d
, SCC_RIGHT_ARROW
); break;
527 case 0xB4: d
+= Utf8Encode(d
, SCC_TRAIN
); break;
528 case 0xB5: d
+= Utf8Encode(d
, SCC_LORRY
); break;
529 case 0xB6: d
+= Utf8Encode(d
, SCC_BUS
); break;
530 case 0xB7: d
+= Utf8Encode(d
, SCC_PLANE
); break;
531 case 0xB8: d
+= Utf8Encode(d
, SCC_SHIP
); break;
532 case 0xB9: d
+= Utf8Encode(d
, SCC_SUPERSCRIPT_M1
); break;
533 case 0xBC: d
+= Utf8Encode(d
, SCC_SMALL_UP_ARROW
); break;
534 case 0xBD: d
+= Utf8Encode(d
, SCC_SMALL_DOWN_ARROW
); break;
536 /* Validate any unhandled character */
537 if (!IsValidChar(c
, CS_ALPHANUMERAL
)) c
= '?';
538 d
+= Utf8Encode(d
, c
);
544 if (mapping
!= NULL
) {
545 grfmsg(1, "choice list was incomplete, the whole list is ignored");
550 if (olen
!= NULL
) *olen
= d
- tmp
+ 1;
551 tmp
= xrealloc (tmp
, d
- tmp
+ 1);
556 * Add the new read string into our structure.
558 StringID
AddGRFString(uint32 grfid
, uint16 stringid
, byte langid_to_add
, bool new_scheme
, bool allow_newlines
, const char *text_to_add
, StringID def_string
)
560 char *translatedtext
;
563 /* When working with the old language scheme (grf_version is less than 7) and
564 * English or American is among the set bits, simply add it as English in
565 * the new scheme, i.e. as langid = 1.
566 * If English is set, it is pretty safe to assume the translations are not
567 * actually translated.
570 if (langid_to_add
& (GRFLB_AMERICAN
| GRFLB_ENGLISH
)) {
571 langid_to_add
= GRFLX_ENGLISH
;
573 StringID ret
= STR_EMPTY
;
574 if (langid_to_add
& GRFLB_GERMAN
) ret
= AddGRFString(grfid
, stringid
, GRFLX_GERMAN
, true, allow_newlines
, text_to_add
, def_string
);
575 if (langid_to_add
& GRFLB_FRENCH
) ret
= AddGRFString(grfid
, stringid
, GRFLX_FRENCH
, true, allow_newlines
, text_to_add
, def_string
);
576 if (langid_to_add
& GRFLB_SPANISH
) ret
= AddGRFString(grfid
, stringid
, GRFLX_SPANISH
, true, allow_newlines
, text_to_add
, def_string
);
581 for (id
= 0; id
< _num_grf_texts
; id
++) {
582 if (_grf_text
[id
].grfid
== grfid
&& _grf_text
[id
].stringid
== stringid
) {
587 /* Too many strings allocated, return empty */
588 if (id
== lengthof(_grf_text
)) return STR_EMPTY
;
591 translatedtext
= TranslateTTDPatchCodes(grfid
, langid_to_add
, allow_newlines
, text_to_add
, &len
);
593 GRFText
*newtext
= GRFText::create (translatedtext
, len
);
595 free(translatedtext
);
597 /* If we didn't find our stringid and grfid in the list, allocate a new id */
598 if (id
== _num_grf_texts
) _num_grf_texts
++;
600 if (_grf_text
[id
].map
== NULL
) {
601 _grf_text
[id
].grfid
= grfid
;
602 _grf_text
[id
].stringid
= stringid
;
603 _grf_text
[id
].def_string
= def_string
;
604 _grf_text
[id
].map
= new GRFTextMap
;
606 _grf_text
[id
].map
->add (langid_to_add
, newtext
);
608 grfmsg (3, "Added 0x%X: grfid %08X string 0x%X lang 0x%X string '%s'", id
, grfid
, stringid
, langid_to_add
, newtext
->text
);
610 return (GRFTAB
<< TABSIZE
) + id
;
614 * Returns the index for this stringid associated with its grfID
616 StringID
GetGRFStringID(uint32 grfid
, uint16 stringid
)
618 for (uint id
= 0; id
< _num_grf_texts
; id
++) {
619 if (_grf_text
[id
].grfid
== grfid
&& _grf_text
[id
].stringid
== stringid
) {
620 return (GRFTAB
<< TABSIZE
) + id
;
624 return STR_UNDEFINED
;
629 * Get a C-string from a stringid set by a newgrf.
631 const char *GetGRFStringPtr(uint16 stringid
)
633 assert(_grf_text
[stringid
].grfid
!= 0);
635 const char *str
= _grf_text
[stringid
].map
->get_string();
636 if (str
!= NULL
) return str
;
638 /* Use the default string ID if the fallback string isn't available */
639 return GetStringPtr(_grf_text
[stringid
].def_string
);
643 * Equivalence Setter function between game and newgrf langID.
644 * This function will adjust _currentLangID as to what is the LangID
645 * of the current language set by the user.
646 * This function is called after the user changed language,
647 * from strings.cpp:ReadLanguagePack
648 * @param language_id iso code of current selection
650 void SetCurrentGrfLangID(byte language_id
)
652 _currentLangID
= language_id
;
655 bool CheckGrfLangID(byte lang_id
, byte grf_version
)
657 if (grf_version
< 7) {
658 switch (_currentLangID
) {
659 case GRFLX_GERMAN
: return (lang_id
& GRFLB_GERMAN
) != 0;
660 case GRFLX_FRENCH
: return (lang_id
& GRFLB_FRENCH
) != 0;
661 case GRFLX_SPANISH
: return (lang_id
& GRFLB_SPANISH
) != 0;
662 default: return (lang_id
& (GRFLB_ENGLISH
| GRFLB_AMERICAN
)) != 0;
666 return (lang_id
== _currentLangID
|| lang_id
== GRFLX_UNSPECIFIED
);
671 * Remove all strings and reset the text counter.
673 void CleanUpStrings()
677 for (id
= 0; id
< _num_grf_texts
; id
++) {
678 delete _grf_text
[id
].map
;
679 _grf_text
[id
].grfid
= 0;
680 _grf_text
[id
].stringid
= 0;
681 _grf_text
[id
].map
= NULL
;
687 struct TextRefStack
{
690 const GRFFile
*grffile
;
693 TextRefStack() : position(0), grffile(NULL
), used(false) {}
695 TextRefStack(const TextRefStack
&stack
) :
696 position(stack
.position
),
697 grffile(stack
.grffile
),
700 memcpy(this->stack
, stack
.stack
, sizeof(this->stack
));
703 uint8
PopUnsignedByte() { assert(this->position
< lengthof(this->stack
)); return this->stack
[this->position
++]; }
704 int8
PopSignedByte() { return (int8
)this->PopUnsignedByte(); }
706 uint16
PopUnsignedWord()
708 uint16 val
= this->PopUnsignedByte();
709 return val
| (this->PopUnsignedByte() << 8);
711 int16
PopSignedWord() { return (int32
)this->PopUnsignedWord(); }
713 uint32
PopUnsignedDWord()
715 uint32 val
= this->PopUnsignedWord();
716 return val
| (this->PopUnsignedWord() << 16);
718 int32
PopSignedDWord() { return (int32
)this->PopUnsignedDWord(); }
720 uint64
PopUnsignedQWord()
722 uint64 val
= this->PopUnsignedDWord();
723 return val
| (((uint64
)this->PopUnsignedDWord()) << 32);
725 int64
PopSignedQWord() { return (int64
)this->PopUnsignedQWord(); }
727 /** Rotate the top four words down: W1, W2, W3, W4 -> W4, W1, W2, W3 */
728 void RotateTop4Words()
731 for (int i
= 0; i
< 2; i
++) tmp
[i
] = this->stack
[this->position
+ i
+ 6];
732 for (int i
= 5; i
>= 0; i
--) this->stack
[this->position
+ i
+ 2] = this->stack
[this->position
+ i
];
733 for (int i
= 0; i
< 2; i
++) this->stack
[this->position
+ i
] = tmp
[i
];
736 void PushWord(uint16 word
)
738 if (this->position
>= 2) {
741 for (int i
= lengthof(stack
) - 1; i
>= this->position
+ 2; i
--) {
742 this->stack
[i
] = this->stack
[i
- 2];
745 this->stack
[this->position
] = GB(word
, 0, 8);
746 this->stack
[this->position
+ 1] = GB(word
, 8, 8);
749 void ResetStack(const GRFFile
*grffile
)
751 assert(grffile
!= NULL
);
753 this->grffile
= grffile
;
757 void RewindStack() { this->position
= 0; }
760 /** The stack that is used for TTDP compatible string code parsing */
761 static TextRefStack _newgrf_textrefstack
;
764 * Check whether the NewGRF text stack is in use.
765 * @return True iff the NewGRF text stack is used.
767 bool UsingNewGRFTextStack()
769 return _newgrf_textrefstack
.used
;
773 * Create a backup of the current NewGRF text stack.
774 * @return A copy of the current text stack.
776 struct TextRefStack
*CreateTextRefStackBackup()
778 return new TextRefStack(_newgrf_textrefstack
);
782 * Restore a copy of the text stack to the used stack.
783 * @param backup The copy to restore.
785 void RestoreTextRefStackBackup(struct TextRefStack
*backup
)
787 _newgrf_textrefstack
= *backup
;
792 * Start using the TTDP compatible string code parsing.
794 * On start a number of values is copied on the #TextRefStack.
795 * You can then use #GetString() and the normal string drawing functions,
796 * and they will use the #TextRefStack for NewGRF string codes.
798 * However, when you want to draw a string multiple times using the same stack,
799 * you have to call #RewindTextRefStack() between draws.
801 * After you are done with drawing, you must disable usage of the #TextRefStack
802 * by calling #StopTextRefStackUsage(), so NewGRF string codes operate on the
803 * normal string parameters again.
805 * @param grffile the NewGRF providing the stack data
806 * @param numEntries number of entries to copy from the registers
807 * @param values values to copy onto the stack; if NULL the temporary NewGRF registers will be used instead
809 void StartTextRefStackUsage(const GRFFile
*grffile
, byte numEntries
, const uint32
*values
)
811 extern TemporaryStorageArray
<int32
, 0x110> _temp_store
;
813 _newgrf_textrefstack
.ResetStack(grffile
);
815 byte
*p
= _newgrf_textrefstack
.stack
;
816 for (uint i
= 0; i
< numEntries
; i
++) {
817 uint32 value
= values
!= NULL
? values
[i
] : _temp_store
.GetValue(0x100 + i
);
818 for (uint j
= 0; j
< 32; j
+= 8) {
819 *p
= GB(value
, j
, 8);
825 /** Stop using the TTDP compatible string code parsing */
826 void StopTextRefStackUsage()
828 _newgrf_textrefstack
.used
= false;
831 void RewindTextRefStack()
833 _newgrf_textrefstack
.RewindStack();
837 * FormatString for NewGRF specific "magic" string control codes
838 * @param scc the string control code that has been read
839 * @param buf the buffer we're writing to
840 * @param str the string that we need to write
841 * @param argv the OpenTTD stack of values
842 * @param argv_size space on the stack \a argv
843 * @param modify_argv When true, modify the OpenTTD stack.
844 * @return the string control code to "execute" now
846 uint
RemapNewGRFStringControlCode (uint scc
, stringb
*buf
, const char **str
, int64
*argv
, uint argv_size
, bool modify_argv
)
851 case SCC_NEWGRF_PRINT_DWORD_SIGNED
:
852 case SCC_NEWGRF_PRINT_WORD_SIGNED
:
853 case SCC_NEWGRF_PRINT_BYTE_SIGNED
:
854 case SCC_NEWGRF_PRINT_WORD_UNSIGNED
:
855 case SCC_NEWGRF_PRINT_BYTE_HEX
:
856 case SCC_NEWGRF_PRINT_WORD_HEX
:
857 case SCC_NEWGRF_PRINT_DWORD_HEX
:
858 case SCC_NEWGRF_PRINT_QWORD_HEX
:
859 case SCC_NEWGRF_PRINT_DWORD_CURRENCY
:
860 case SCC_NEWGRF_PRINT_QWORD_CURRENCY
:
861 case SCC_NEWGRF_PRINT_WORD_STRING_ID
:
862 case SCC_NEWGRF_PRINT_WORD_DATE_LONG
:
863 case SCC_NEWGRF_PRINT_DWORD_DATE_LONG
:
864 case SCC_NEWGRF_PRINT_WORD_DATE_SHORT
:
865 case SCC_NEWGRF_PRINT_DWORD_DATE_SHORT
:
866 case SCC_NEWGRF_PRINT_WORD_SPEED
:
867 case SCC_NEWGRF_PRINT_WORD_VOLUME_LONG
:
868 case SCC_NEWGRF_PRINT_WORD_VOLUME_SHORT
:
869 case SCC_NEWGRF_PRINT_WORD_WEIGHT_LONG
:
870 case SCC_NEWGRF_PRINT_WORD_WEIGHT_SHORT
:
871 case SCC_NEWGRF_PRINT_WORD_POWER
:
872 case SCC_NEWGRF_PRINT_WORD_STATION_NAME
:
873 case SCC_NEWGRF_PRINT_WORD_CARGO_NAME
:
875 DEBUG(misc
, 0, "Too many NewGRF string parameters.");
880 case SCC_NEWGRF_PRINT_WORD_CARGO_LONG
:
881 case SCC_NEWGRF_PRINT_WORD_CARGO_SHORT
:
882 case SCC_NEWGRF_PRINT_WORD_CARGO_TINY
:
884 DEBUG(misc
, 0, "Too many NewGRF string parameters.");
890 if (_newgrf_textrefstack
.used
&& modify_argv
) {
892 default: NOT_REACHED();
893 case SCC_NEWGRF_PRINT_BYTE_SIGNED
: *argv
= _newgrf_textrefstack
.PopSignedByte(); break;
894 case SCC_NEWGRF_PRINT_QWORD_CURRENCY
: *argv
= _newgrf_textrefstack
.PopSignedQWord(); break;
896 case SCC_NEWGRF_PRINT_DWORD_CURRENCY
:
897 case SCC_NEWGRF_PRINT_DWORD_SIGNED
: *argv
= _newgrf_textrefstack
.PopSignedDWord(); break;
899 case SCC_NEWGRF_PRINT_BYTE_HEX
: *argv
= _newgrf_textrefstack
.PopUnsignedByte(); break;
900 case SCC_NEWGRF_PRINT_QWORD_HEX
: *argv
= _newgrf_textrefstack
.PopUnsignedQWord(); break;
902 case SCC_NEWGRF_PRINT_WORD_SPEED
:
903 case SCC_NEWGRF_PRINT_WORD_VOLUME_LONG
:
904 case SCC_NEWGRF_PRINT_WORD_VOLUME_SHORT
:
905 case SCC_NEWGRF_PRINT_WORD_SIGNED
: *argv
= _newgrf_textrefstack
.PopSignedWord(); break;
907 case SCC_NEWGRF_PRINT_WORD_HEX
:
908 case SCC_NEWGRF_PRINT_WORD_WEIGHT_LONG
:
909 case SCC_NEWGRF_PRINT_WORD_WEIGHT_SHORT
:
910 case SCC_NEWGRF_PRINT_WORD_POWER
:
911 case SCC_NEWGRF_PRINT_WORD_STATION_NAME
:
912 case SCC_NEWGRF_PRINT_WORD_UNSIGNED
: *argv
= _newgrf_textrefstack
.PopUnsignedWord(); break;
914 case SCC_NEWGRF_PRINT_DWORD_DATE_LONG
:
915 case SCC_NEWGRF_PRINT_DWORD_DATE_SHORT
:
916 case SCC_NEWGRF_PRINT_DWORD_HEX
: *argv
= _newgrf_textrefstack
.PopUnsignedDWord(); break;
918 case SCC_NEWGRF_PRINT_WORD_DATE_LONG
:
919 case SCC_NEWGRF_PRINT_WORD_DATE_SHORT
: *argv
= _newgrf_textrefstack
.PopUnsignedWord() + DAYS_TILL_ORIGINAL_BASE_YEAR
; break;
921 case SCC_NEWGRF_DISCARD_WORD
: _newgrf_textrefstack
.PopUnsignedWord(); break;
923 case SCC_NEWGRF_ROTATE_TOP_4_WORDS
: _newgrf_textrefstack
.RotateTop4Words(); break;
924 case SCC_NEWGRF_PUSH_WORD
: _newgrf_textrefstack
.PushWord(Utf8Consume(str
)); break;
926 case SCC_NEWGRF_UNPRINT
: {
927 size_t unprint
= Utf8Consume(str
);
928 if (unprint
< buf
->length()) {
929 buf
->truncate (buf
->length() - unprint
);
936 case SCC_NEWGRF_PRINT_WORD_CARGO_LONG
:
937 case SCC_NEWGRF_PRINT_WORD_CARGO_SHORT
:
938 case SCC_NEWGRF_PRINT_WORD_CARGO_TINY
:
939 argv
[0] = GetCargoTranslation(_newgrf_textrefstack
.PopUnsignedWord(), _newgrf_textrefstack
.grffile
);
940 argv
[1] = _newgrf_textrefstack
.PopUnsignedWord();
943 case SCC_NEWGRF_PRINT_WORD_STRING_ID
:
944 *argv
= MapGRFStringID(_newgrf_textrefstack
.grffile
->grfid
, _newgrf_textrefstack
.PopUnsignedWord());
947 case SCC_NEWGRF_PRINT_WORD_CARGO_NAME
: {
948 CargoID cargo
= GetCargoTranslation(_newgrf_textrefstack
.PopUnsignedWord(), _newgrf_textrefstack
.grffile
);
949 *argv
= cargo
< NUM_CARGO
? 1 << cargo
: 0;
954 /* Consume additional parameter characters */
958 case SCC_NEWGRF_PUSH_WORD
:
959 case SCC_NEWGRF_UNPRINT
:
966 default: NOT_REACHED();
967 case SCC_NEWGRF_PRINT_DWORD_SIGNED
:
968 case SCC_NEWGRF_PRINT_WORD_SIGNED
:
969 case SCC_NEWGRF_PRINT_BYTE_SIGNED
:
970 case SCC_NEWGRF_PRINT_WORD_UNSIGNED
:
973 case SCC_NEWGRF_PRINT_BYTE_HEX
:
974 case SCC_NEWGRF_PRINT_WORD_HEX
:
975 case SCC_NEWGRF_PRINT_DWORD_HEX
:
976 case SCC_NEWGRF_PRINT_QWORD_HEX
:
979 case SCC_NEWGRF_PRINT_DWORD_CURRENCY
:
980 case SCC_NEWGRF_PRINT_QWORD_CURRENCY
:
981 return SCC_CURRENCY_LONG
;
983 case SCC_NEWGRF_PRINT_WORD_STRING_ID
:
984 return SCC_NEWGRF_PRINT_WORD_STRING_ID
;
986 case SCC_NEWGRF_PRINT_WORD_DATE_LONG
:
987 case SCC_NEWGRF_PRINT_DWORD_DATE_LONG
:
988 return SCC_DATE_LONG
;
990 case SCC_NEWGRF_PRINT_WORD_DATE_SHORT
:
991 case SCC_NEWGRF_PRINT_DWORD_DATE_SHORT
:
992 return SCC_DATE_SHORT
;
994 case SCC_NEWGRF_PRINT_WORD_SPEED
:
997 case SCC_NEWGRF_PRINT_WORD_VOLUME_LONG
:
998 return SCC_VOLUME_LONG
;
1000 case SCC_NEWGRF_PRINT_WORD_VOLUME_SHORT
:
1001 return SCC_VOLUME_SHORT
;
1003 case SCC_NEWGRF_PRINT_WORD_WEIGHT_LONG
:
1004 return SCC_WEIGHT_LONG
;
1006 case SCC_NEWGRF_PRINT_WORD_WEIGHT_SHORT
:
1007 return SCC_WEIGHT_SHORT
;
1009 case SCC_NEWGRF_PRINT_WORD_POWER
:
1012 case SCC_NEWGRF_PRINT_WORD_CARGO_LONG
:
1013 return SCC_CARGO_LONG
;
1015 case SCC_NEWGRF_PRINT_WORD_CARGO_SHORT
:
1016 return SCC_CARGO_SHORT
;
1018 case SCC_NEWGRF_PRINT_WORD_CARGO_TINY
:
1019 return SCC_CARGO_TINY
;
1021 case SCC_NEWGRF_PRINT_WORD_CARGO_NAME
:
1022 return SCC_CARGO_LIST
;
1024 case SCC_NEWGRF_PRINT_WORD_STATION_NAME
:
1025 return SCC_STATION_NAME
;
1027 case SCC_NEWGRF_DISCARD_WORD
:
1028 case SCC_NEWGRF_ROTATE_TOP_4_WORDS
:
1029 case SCC_NEWGRF_PUSH_WORD
:
1030 case SCC_NEWGRF_UNPRINT
: