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 */
7
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.
16 */
17
18 #include "stdafx.h"
19
20 #include <array>
21
22 #include "newgrf.h"
23 #include "strings_func.h"
24 #include "newgrf_storage.h"
25 #include "newgrf_text.h"
26 #include "newgrf_cargo.h"
27 #include "string_func.h"
28 #include "date_type.h"
29 #include "debug.h"
30 #include "core/alloc_type.hpp"
31 #include "core/smallmap_type.hpp"
32 #include "language.h"
33 #include <sstream>
34 #include <map>
35
36 #include "table/strings.h"
37 #include "table/control_codes.h"
38
39 #include "safeguards.h"
40
41 /**
42 * Explains the newgrf shift bit positioning.
43 * the grf base will not be used in order to find the string, but rather for
44 * jumping from standard langID scheme to the new one.
45 */
46 enum GRFBaseLanguages {
47 GRFLB_AMERICAN = 0x01,
48 GRFLB_ENGLISH = 0x02,
49 GRFLB_GERMAN = 0x04,
50 GRFLB_FRENCH = 0x08,
51 GRFLB_SPANISH = 0x10,
52 GRFLB_GENERIC = 0x80,
53 };
54
55 enum GRFExtendedLanguages {
56 GRFLX_AMERICAN = 0x00,
57 GRFLX_ENGLISH = 0x01,
58 GRFLX_GERMAN = 0x02,
59 GRFLX_FRENCH = 0x03,
60 GRFLX_SPANISH = 0x04,
61 GRFLX_UNSPECIFIED = 0x7F,
62 };
63
64
65 /**
66 * Holder of the above structure.
67 * Putting both grfid and stringid together allows us to avoid duplicates,
68 * since it is NOT SUPPOSED to happen.
69 */
70 struct GRFTextEntry {
71 uint32 grfid;
72 uint16 stringid;
73 StringID def_string;
74 GRFTextList textholder;
75 };
76
77
78 static uint _num_grf_texts = 0;
79 static GRFTextEntry _grf_text[TAB_SIZE_NEWGRF];
80 static byte _currentLangID = GRFLX_ENGLISH; ///< by default, english is used.
81
82 /**
83 * Get the mapping from the NewGRF supplied ID to OpenTTD's internal ID.
84 * @param newgrf_id The NewGRF ID to map.
85 * @param gender Whether to map genders or cases.
86 * @return The, to OpenTTD's internal ID, mapped index, or -1 if there is no mapping.
87 */
GetMapping(int newgrf_id,bool gender) const88 int LanguageMap::GetMapping(int newgrf_id, bool gender) const
89 {
90 const std::vector<Mapping> &map = gender ? this->gender_map : this->case_map;
91 for (const Mapping &m : map) {
92 if (m.newgrf_id == newgrf_id) return m.openttd_id;
93 }
94 return -1;
95 }
96
97 /**
98 * Get the mapping from OpenTTD's internal ID to the NewGRF supplied ID.
99 * @param openttd_id The OpenTTD ID to map.
100 * @param gender Whether to map genders or cases.
101 * @return The, to the NewGRF supplied ID, mapped index, or -1 if there is no mapping.
102 */
GetReverseMapping(int openttd_id,bool gender) const103 int LanguageMap::GetReverseMapping(int openttd_id, bool gender) const
104 {
105 const std::vector<Mapping> &map = gender ? this->gender_map : this->case_map;
106 for (const Mapping &m : map) {
107 if (m.openttd_id == openttd_id) return m.newgrf_id;
108 }
109 return -1;
110 }
111
112 /** Helper structure for mapping choice lists. */
113 struct UnmappedChoiceList {
114 /**
115 * Initialise the mapping.
116 * @param type The type of mapping.
117 * @param offset The offset to get the plural/gender from.
118 */
UnmappedChoiceListUnmappedChoiceList119 UnmappedChoiceList(StringControlCode type, int offset) :
120 type(type), offset(offset)
121 {
122 }
123
124 StringControlCode type; ///< The type of choice list.
125 int offset; ///< The offset for the plural/gender form.
126
127 /** Mapping of NewGRF supplied ID to the different strings in the choice list. */
128 std::map<byte, std::stringstream> strings;
129
130 /**
131 * Flush this choice list into the destination string.
132 * @param lm The current language mapping.
133 * @param dest Target to write to.
134 */
FlushUnmappedChoiceList135 void Flush(const LanguageMap *lm, std::ostringstream &dest)
136 {
137 if (this->strings.find(0) == this->strings.end()) {
138 /* In case of a (broken) NewGRF without a default,
139 * assume an empty string. */
140 grfmsg(1, "choice list misses default value");
141 this->strings[0] = std::stringstream();
142 }
143
144 std::ostreambuf_iterator<char> d(dest);
145
146 if (lm == nullptr) {
147 /* In case there is no mapping, just ignore everything but the default.
148 * A probable cause for this happening is when the language file has
149 * been removed by the user and as such no mapping could be made. */
150 dest << this->strings[0].rdbuf();
151 return;
152 }
153
154 Utf8Encode(d, this->type);
155
156 if (this->type == SCC_SWITCH_CASE) {
157 /*
158 * Format for case switch:
159 * <NUM CASES> <CASE1> <LEN1> <STRING1> <CASE2> <LEN2> <STRING2> <CASE3> <LEN3> <STRING3> <STRINGDEFAULT>
160 * Each LEN is printed using 2 bytes in big endian order.
161 */
162
163 /* "<NUM CASES>" */
164 int count = 0;
165 for (uint8 i = 0; i < _current_language->num_cases; i++) {
166 /* Count the ones we have a mapped string for. */
167 if (this->strings.find(lm->GetReverseMapping(i, false)) != this->strings.end()) count++;
168 }
169 *d++ = count;
170
171 for (uint8 i = 0; i < _current_language->num_cases; i++) {
172 /* Resolve the string we're looking for. */
173 int idx = lm->GetReverseMapping(i, false);
174 if (this->strings.find(idx) == this->strings.end()) continue;
175 auto str = this->strings[idx].str();
176
177 /* "<CASEn>" */
178 *d++ = i + 1;
179
180 /* "<LENn>": Limit the length of the string to 0xFFFE to leave space for the '\0'. */
181 size_t len = std::min<size_t>(0xFFFE, str.size());
182 *d++ = GB(len + 1, 8, 8);
183 *d++ = GB(len + 1, 0, 8);
184
185 /* "<STRINGn>" */
186 dest.write(str.c_str(), len);
187 *d++ = '\0';
188 }
189
190 /* "<STRINGDEFAULT>" */
191 dest << this->strings[0].rdbuf() << '\0';
192 } else {
193 if (this->type == SCC_PLURAL_LIST) {
194 *d++ = lm->plural_form;
195 }
196
197 /*
198 * Format for choice list:
199 * <OFFSET> <NUM CHOICES> <LENs> <STRINGs>
200 */
201
202 /* "<OFFSET>" */
203 *d++ = this->offset - 0x80;
204
205 /* "<NUM CHOICES>" */
206 int count = (this->type == SCC_GENDER_LIST ? _current_language->num_genders : LANGUAGE_MAX_PLURAL_FORMS);
207 *d++ = count;
208
209 /* "<LENs>" */
210 for (int i = 0; i < count; i++) {
211 int idx = (this->type == SCC_GENDER_LIST ? lm->GetReverseMapping(i, true) : i + 1);
212 const auto &str = this->strings[this->strings.find(idx) != this->strings.end() ? idx : 0].str();
213 size_t len = str.size() + 1;
214 if (len > 0xFF) grfmsg(1, "choice list string is too long");
215 *d++ = GB(len, 0, 8);
216 }
217
218 /* "<STRINGs>" */
219 for (int i = 0; i < count; i++) {
220 int idx = (this->type == SCC_GENDER_LIST ? lm->GetReverseMapping(i, true) : i + 1);
221 const auto &str = this->strings[this->strings.find(idx) != this->strings.end() ? idx : 0].str();
222 /* Limit the length of the string we copy to 0xFE. The length is written above
223 * as a byte and we need room for the final '\0'. */
224 size_t len = std::min<size_t>(0xFE, str.size());
225 dest.write(str.c_str(), len);
226 *d++ = '\0';
227 }
228 }
229 }
230 };
231
232 /**
233 * Translate TTDPatch string codes into something OpenTTD can handle (better).
234 * @param grfid The (NewGRF) ID associated with this string
235 * @param language_id The (NewGRF) language ID associated with this string.
236 * @param allow_newlines Whether newlines are allowed in the string or not.
237 * @param str The string to translate.
238 * @param byte80 The control code to use as replacement for the 0x80-value.
239 * @return The translated string.
240 */
TranslateTTDPatchCodes(uint32 grfid,uint8 language_id,bool allow_newlines,const std::string & str,StringControlCode byte80)241 std::string TranslateTTDPatchCodes(uint32 grfid, uint8 language_id, bool allow_newlines, const std::string &str, StringControlCode byte80)
242 {
243 /* Empty input string? Nothing to do here. */
244 if (str.empty()) return str;
245
246 std::string::const_iterator src = str.cbegin();
247
248 /* Is this an unicode string? */
249 bool unicode = false;
250 WChar marker;
251 size_t len = Utf8Decode(&marker, &*src);
252
253 if (marker == NFO_UTF8_IDENTIFIER) {
254 unicode = true;
255 src += len;
256 }
257
258 /* Helper variable for a possible (string) mapping. */
259 UnmappedChoiceList *mapping = nullptr;
260
261 std::ostringstream dest;
262 std::ostreambuf_iterator<char> d(dest);
263 while (src != str.cend()) {
264 WChar c;
265
266 if (unicode && Utf8EncodedCharLen(*src) != 0) {
267 c = Utf8Consume(src);
268 /* 'Magic' range of control codes. */
269 if (GB(c, 8, 8) == 0xE0) {
270 c = GB(c, 0, 8);
271 } else if (c >= 0x20) {
272 if (!IsValidChar(c, CS_ALPHANUMERAL)) c = '?';
273 Utf8Encode(d, c);
274 continue;
275 }
276 } else {
277 c = (byte)*src++;
278 }
279
280 if (c == '\0') break;
281
282 switch (c) {
283 case 0x01:
284 if (*src == '\0') goto string_end;
285 Utf8Encode(d, ' ');
286 src++;
287 break;
288 case 0x0A: break;
289 case 0x0D:
290 if (allow_newlines) {
291 *d++ = 0x0A;
292 } else {
293 grfmsg(1, "Detected newline in string that does not allow one");
294 }
295 break;
296 case 0x0E: Utf8Encode(d, SCC_TINYFONT); break;
297 case 0x0F: Utf8Encode(d, SCC_BIGFONT); break;
298 case 0x1F:
299 if (src[0] == '\0' || src[1] == '\0') goto string_end;
300 Utf8Encode(d, ' ');
301 src += 2;
302 break;
303 case 0x7B:
304 case 0x7C:
305 case 0x7D:
306 case 0x7E:
307 case 0x7F: Utf8Encode(d, SCC_NEWGRF_PRINT_DWORD_SIGNED + c - 0x7B); break;
308 case 0x80: Utf8Encode(d, byte80); break;
309 case 0x81:
310 {
311 if (src[0] == '\0' || src[1] == '\0') goto string_end;
312 StringID string;
313 string = ((uint8)* src++);
314 string |= ((uint8)* src++) << 8;
315 Utf8Encode(d, SCC_NEWGRF_STRINL);
316 Utf8Encode(d, MapGRFStringID(grfid, string));
317 break;
318 }
319 case 0x82:
320 case 0x83:
321 case 0x84: Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_DATE_LONG + c - 0x82); break;
322 case 0x85: Utf8Encode(d, SCC_NEWGRF_DISCARD_WORD); break;
323 case 0x86: Utf8Encode(d, SCC_NEWGRF_ROTATE_TOP_4_WORDS); break;
324 case 0x87: Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_VOLUME_LONG); break;
325 case 0x88: Utf8Encode(d, SCC_BLUE); break;
326 case 0x89: Utf8Encode(d, SCC_SILVER); break;
327 case 0x8A: Utf8Encode(d, SCC_GOLD); break;
328 case 0x8B: Utf8Encode(d, SCC_RED); break;
329 case 0x8C: Utf8Encode(d, SCC_PURPLE); break;
330 case 0x8D: Utf8Encode(d, SCC_LTBROWN); break;
331 case 0x8E: Utf8Encode(d, SCC_ORANGE); break;
332 case 0x8F: Utf8Encode(d, SCC_GREEN); break;
333 case 0x90: Utf8Encode(d, SCC_YELLOW); break;
334 case 0x91: Utf8Encode(d, SCC_DKGREEN); break;
335 case 0x92: Utf8Encode(d, SCC_CREAM); break;
336 case 0x93: Utf8Encode(d, SCC_BROWN); break;
337 case 0x94: Utf8Encode(d, SCC_WHITE); break;
338 case 0x95: Utf8Encode(d, SCC_LTBLUE); break;
339 case 0x96: Utf8Encode(d, SCC_GRAY); break;
340 case 0x97: Utf8Encode(d, SCC_DKBLUE); break;
341 case 0x98: Utf8Encode(d, SCC_BLACK); break;
342 case 0x9A:
343 {
344 int code = *src++;
345 switch (code) {
346 case 0x00: goto string_end;
347 case 0x01: Utf8Encode(d, SCC_NEWGRF_PRINT_QWORD_CURRENCY); break;
348 /* 0x02: ignore next colour byte is not supported. It works on the final
349 * string and as such hooks into the string drawing routine. At that
350 * point many things already happened, such as splitting up of strings
351 * when drawn over multiple lines or right-to-left translations, which
352 * make the behaviour peculiar, e.g. only happening at specific width
353 * of windows. Or we need to add another pass over the string to just
354 * support this. As such it is not implemented in OpenTTD. */
355 case 0x03:
356 {
357 if (src[0] == '\0' || src[1] == '\0') goto string_end;
358 uint16 tmp = ((uint8)* src++);
359 tmp |= ((uint8)* src++) << 8;
360 Utf8Encode(d, SCC_NEWGRF_PUSH_WORD);
361 Utf8Encode(d, tmp);
362 break;
363 }
364 case 0x04:
365 if (src[0] == '\0') goto string_end;
366 Utf8Encode(d, SCC_NEWGRF_UNPRINT);
367 Utf8Encode(d, *src++);
368 break;
369 case 0x06: Utf8Encode(d, SCC_NEWGRF_PRINT_BYTE_HEX); break;
370 case 0x07: Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_HEX); break;
371 case 0x08: Utf8Encode(d, SCC_NEWGRF_PRINT_DWORD_HEX); break;
372 /* 0x09, 0x0A are TTDPatch internal use only string codes. */
373 case 0x0B: Utf8Encode(d, SCC_NEWGRF_PRINT_QWORD_HEX); break;
374 case 0x0C: Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_STATION_NAME); break;
375 case 0x0D: Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_WEIGHT_LONG); break;
376 case 0x0E:
377 case 0x0F:
378 {
379 if (str[0] == '\0') goto string_end;
380 const LanguageMap *lm = LanguageMap::GetLanguageMap(grfid, language_id);
381 int index = *src++;
382 int mapped = lm != nullptr ? lm->GetMapping(index, code == 0x0E) : -1;
383 if (mapped >= 0) {
384 Utf8Encode(d, code == 0x0E ? SCC_GENDER_INDEX : SCC_SET_CASE);
385 Utf8Encode(d, code == 0x0E ? mapped : mapped + 1);
386 }
387 break;
388 }
389
390 case 0x10:
391 case 0x11:
392 if (str[0] == '\0') goto string_end;
393 if (mapping == nullptr) {
394 if (code == 0x10) src++; // Skip the index
395 grfmsg(1, "choice list %s marker found when not expected", code == 0x10 ? "next" : "default");
396 break;
397 } else {
398 int index = (code == 0x10 ? *src++ : 0);
399 if (mapping->strings.find(index) != mapping->strings.end()) {
400 grfmsg(1, "duplicate choice list string, ignoring");
401 } else {
402 d = std::ostreambuf_iterator<char>(mapping->strings[index]);
403 }
404 }
405 break;
406
407 case 0x12:
408 if (mapping == nullptr) {
409 grfmsg(1, "choice list end marker found when not expected");
410 } else {
411 /* Now we can start flushing everything and clean everything up. */
412 mapping->Flush(LanguageMap::GetLanguageMap(grfid, language_id), dest);
413 delete mapping;
414 mapping = nullptr;
415
416 d = std::ostreambuf_iterator<char>(dest);
417 }
418 break;
419
420 case 0x13:
421 case 0x14:
422 case 0x15:
423 if (src[0] == '\0') goto string_end;
424 if (mapping != nullptr) {
425 grfmsg(1, "choice lists can't be stacked, it's going to get messy now...");
426 if (code != 0x14) src++;
427 } else {
428 static const StringControlCode mp[] = { SCC_GENDER_LIST, SCC_SWITCH_CASE, SCC_PLURAL_LIST };
429 mapping = new UnmappedChoiceList(mp[code - 0x13], code == 0x14 ? 0 : *src++);
430 }
431 break;
432
433 case 0x16:
434 case 0x17:
435 case 0x18:
436 case 0x19:
437 case 0x1A:
438 case 0x1B:
439 case 0x1C:
440 case 0x1D:
441 case 0x1E:
442 Utf8Encode(d, SCC_NEWGRF_PRINT_DWORD_DATE_LONG + code - 0x16);
443 break;
444
445 case 0x1F: Utf8Encode(d, SCC_PUSH_COLOUR); break;
446 case 0x20: Utf8Encode(d, SCC_POP_COLOUR); break;
447
448 default:
449 grfmsg(1, "missing handler for extended format code");
450 break;
451 }
452 break;
453 }
454
455 case 0x9E: Utf8Encode(d, 0x20AC); break; // Euro
456 case 0x9F: Utf8Encode(d, 0x0178); break; // Y with diaeresis
457 case 0xA0: Utf8Encode(d, SCC_UP_ARROW); break;
458 case 0xAA: Utf8Encode(d, SCC_DOWN_ARROW); break;
459 case 0xAC: Utf8Encode(d, SCC_CHECKMARK); break;
460 case 0xAD: Utf8Encode(d, SCC_CROSS); break;
461 case 0xAF: Utf8Encode(d, SCC_RIGHT_ARROW); break;
462 case 0xB4: Utf8Encode(d, SCC_TRAIN); break;
463 case 0xB5: Utf8Encode(d, SCC_LORRY); break;
464 case 0xB6: Utf8Encode(d, SCC_BUS); break;
465 case 0xB7: Utf8Encode(d, SCC_PLANE); break;
466 case 0xB8: Utf8Encode(d, SCC_SHIP); break;
467 case 0xB9: Utf8Encode(d, SCC_SUPERSCRIPT_M1); break;
468 case 0xBC: Utf8Encode(d, SCC_SMALL_UP_ARROW); break;
469 case 0xBD: Utf8Encode(d, SCC_SMALL_DOWN_ARROW); break;
470 default:
471 /* Validate any unhandled character */
472 if (!IsValidChar(c, CS_ALPHANUMERAL)) c = '?';
473 Utf8Encode(d, c);
474 break;
475 }
476 }
477
478 string_end:
479 if (mapping != nullptr) {
480 grfmsg(1, "choice list was incomplete, the whole list is ignored");
481 delete mapping;
482 }
483
484 return dest.str();
485 }
486
487 /**
488 * Add a new text to a GRFText list.
489 * @param list The list where the text should be added to.
490 * @param langid The The language of the new text.
491 * @param text_to_add The text to add to the list.
492 */
AddGRFTextToList(GRFTextList & list,byte langid,const std::string & text_to_add)493 static void AddGRFTextToList(GRFTextList &list, byte langid, const std::string &text_to_add)
494 {
495 /* Loop through all languages and see if we can replace a string */
496 for (auto &text : list) {
497 if (text.langid == langid) {
498 text.text = text_to_add;
499 return;
500 }
501 }
502
503 /* If a string wasn't replaced, then we must append the new string */
504 list.push_back(GRFText{ langid, text_to_add });
505 }
506
507 /**
508 * Add a string to a GRFText list.
509 * @param list The list where the text should be added to.
510 * @param langid The language of the new text.
511 * @param grfid The grfid where this string is defined.
512 * @param allow_newlines Whether newlines are allowed in this string.
513 * @param text_to_add The text to add to the list.
514 * @note All text-codes will be translated.
515 */
AddGRFTextToList(GRFTextList & list,byte langid,uint32 grfid,bool allow_newlines,const char * text_to_add)516 void AddGRFTextToList(GRFTextList &list, byte langid, uint32 grfid, bool allow_newlines, const char *text_to_add)
517 {
518 AddGRFTextToList(list, langid, TranslateTTDPatchCodes(grfid, langid, allow_newlines, text_to_add));
519 }
520
521 /**
522 * Add a string to a GRFText list.
523 * @param list The list where the text should be added to.
524 * @param langid The language of the new text.
525 * @param grfid The grfid where this string is defined.
526 * @param allow_newlines Whether newlines are allowed in this string.
527 * @param text_to_add The text to add to the list.
528 * @note All text-codes will be translated.
529 */
AddGRFTextToList(GRFTextWrapper & list,byte langid,uint32 grfid,bool allow_newlines,const char * text_to_add)530 void AddGRFTextToList(GRFTextWrapper &list, byte langid, uint32 grfid, bool allow_newlines, const char *text_to_add)
531 {
532 if (!list) list.reset(new GRFTextList());
533 AddGRFTextToList(*list, langid, grfid, allow_newlines, text_to_add);
534 }
535
536 /**
537 * Add a GRFText to a GRFText list. The text should not contain any text-codes.
538 * The text will be added as a 'default language'-text.
539 * @param list The list where the text should be added to.
540 * @param text_to_add The text to add to the list.
541 */
AddGRFTextToList(GRFTextWrapper & list,const std::string & text_to_add)542 void AddGRFTextToList(GRFTextWrapper &list, const std::string &text_to_add)
543 {
544 if (!list) list.reset(new GRFTextList());
545 AddGRFTextToList(*list, GRFLX_UNSPECIFIED, text_to_add);
546 }
547
548 /**
549 * Add the new read string into our structure.
550 */
AddGRFString(uint32 grfid,uint16 stringid,byte langid_to_add,bool new_scheme,bool allow_newlines,const char * text_to_add,StringID def_string)551 StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid_to_add, bool new_scheme, bool allow_newlines, const char *text_to_add, StringID def_string)
552 {
553 /* When working with the old language scheme (grf_version is less than 7) and
554 * English or American is among the set bits, simply add it as English in
555 * the new scheme, i.e. as langid = 1.
556 * If English is set, it is pretty safe to assume the translations are not
557 * actually translated.
558 */
559 if (!new_scheme) {
560 if (langid_to_add & (GRFLB_AMERICAN | GRFLB_ENGLISH)) {
561 langid_to_add = GRFLX_ENGLISH;
562 } else {
563 StringID ret = STR_EMPTY;
564 if (langid_to_add & GRFLB_GERMAN) ret = AddGRFString(grfid, stringid, GRFLX_GERMAN, true, allow_newlines, text_to_add, def_string);
565 if (langid_to_add & GRFLB_FRENCH) ret = AddGRFString(grfid, stringid, GRFLX_FRENCH, true, allow_newlines, text_to_add, def_string);
566 if (langid_to_add & GRFLB_SPANISH) ret = AddGRFString(grfid, stringid, GRFLX_SPANISH, true, allow_newlines, text_to_add, def_string);
567 return ret;
568 }
569 }
570
571 uint id;
572 for (id = 0; id < _num_grf_texts; id++) {
573 if (_grf_text[id].grfid == grfid && _grf_text[id].stringid == stringid) {
574 break;
575 }
576 }
577
578 /* Too many strings allocated, return empty */
579 if (id == lengthof(_grf_text)) return STR_EMPTY;
580
581 std::string newtext = TranslateTTDPatchCodes(grfid, langid_to_add, allow_newlines, text_to_add);
582
583 /* If we didn't find our stringid and grfid in the list, allocate a new id */
584 if (id == _num_grf_texts) _num_grf_texts++;
585
586 if (_grf_text[id].textholder.empty()) {
587 _grf_text[id].grfid = grfid;
588 _grf_text[id].stringid = stringid;
589 _grf_text[id].def_string = def_string;
590 }
591 AddGRFTextToList(_grf_text[id].textholder, langid_to_add, newtext);
592
593 grfmsg(3, "Added 0x%X: grfid %08X string 0x%X lang 0x%X string '%s' (%X)", id, grfid, stringid, langid_to_add, newtext.c_str(), MakeStringID(TEXT_TAB_NEWGRF_START, id));
594
595 return MakeStringID(TEXT_TAB_NEWGRF_START, id);
596 }
597
598 /**
599 * Returns the index for this stringid associated with its grfID
600 */
GetGRFStringID(uint32 grfid,StringID stringid)601 StringID GetGRFStringID(uint32 grfid, StringID stringid)
602 {
603 for (uint id = 0; id < _num_grf_texts; id++) {
604 if (_grf_text[id].grfid == grfid && _grf_text[id].stringid == stringid) {
605 return MakeStringID(TEXT_TAB_NEWGRF_START, id);
606 }
607 }
608
609 return STR_UNDEFINED;
610 }
611
612
613 /**
614 * Get a C-string from a GRFText-list. If there is a translation for the
615 * current language it is returned, otherwise the default translation
616 * is returned. If there is neither a default nor a translation for the
617 * current language nullptr is returned.
618 * @param text_list The GRFTextList to get the string from.
619 */
GetGRFStringFromGRFText(const GRFTextList & text_list)620 const char *GetGRFStringFromGRFText(const GRFTextList &text_list)
621 {
622 const char *default_text = nullptr;
623
624 /* Search the list of lang-strings of this stringid for current lang */
625 for (const auto &text : text_list) {
626 if (text.langid == _currentLangID) return text.text.c_str();
627
628 /* If the current string is English or American, set it as the
629 * fallback language if the specific language isn't available. */
630 if (text.langid == GRFLX_UNSPECIFIED || (default_text == nullptr && (text.langid == GRFLX_ENGLISH || text.langid == GRFLX_AMERICAN))) {
631 default_text = text.text.c_str();
632 }
633 }
634
635 return default_text;
636 }
637
638 /**
639 * Get a C-string from a GRFText-list. If there is a translation for the
640 * current language it is returned, otherwise the default translation
641 * is returned. If there is neither a default nor a translation for the
642 * current language nullptr is returned.
643 * @param text The GRFTextList to get the string from.
644 */
GetGRFStringFromGRFText(const GRFTextWrapper & text)645 const char *GetGRFStringFromGRFText(const GRFTextWrapper &text)
646 {
647 return text ? GetGRFStringFromGRFText(*text) : nullptr;
648 }
649
650 /**
651 * Get a C-string from a stringid set by a newgrf.
652 */
GetGRFStringPtr(uint16 stringid)653 const char *GetGRFStringPtr(uint16 stringid)
654 {
655 assert(_grf_text[stringid].grfid != 0);
656
657 const char *str = GetGRFStringFromGRFText(_grf_text[stringid].textholder);
658 if (str != nullptr) return str;
659
660 /* Use the default string ID if the fallback string isn't available */
661 return GetStringPtr(_grf_text[stringid].def_string);
662 }
663
664 /**
665 * Equivalence Setter function between game and newgrf langID.
666 * This function will adjust _currentLangID as to what is the LangID
667 * of the current language set by the user.
668 * This function is called after the user changed language,
669 * from strings.cpp:ReadLanguagePack
670 * @param language_id iso code of current selection
671 */
SetCurrentGrfLangID(byte language_id)672 void SetCurrentGrfLangID(byte language_id)
673 {
674 _currentLangID = language_id;
675 }
676
CheckGrfLangID(byte lang_id,byte grf_version)677 bool CheckGrfLangID(byte lang_id, byte grf_version)
678 {
679 if (grf_version < 7) {
680 switch (_currentLangID) {
681 case GRFLX_GERMAN: return (lang_id & GRFLB_GERMAN) != 0;
682 case GRFLX_FRENCH: return (lang_id & GRFLB_FRENCH) != 0;
683 case GRFLX_SPANISH: return (lang_id & GRFLB_SPANISH) != 0;
684 default: return (lang_id & (GRFLB_ENGLISH | GRFLB_AMERICAN)) != 0;
685 }
686 }
687
688 return (lang_id == _currentLangID || lang_id == GRFLX_UNSPECIFIED);
689 }
690
691 /**
692 * House cleaning.
693 * Remove all strings and reset the text counter.
694 */
CleanUpStrings()695 void CleanUpStrings()
696 {
697 uint id;
698
699 for (id = 0; id < _num_grf_texts; id++) {
700 _grf_text[id].grfid = 0;
701 _grf_text[id].stringid = 0;
702 _grf_text[id].textholder.clear();
703 }
704
705 _num_grf_texts = 0;
706 }
707
708 struct TextRefStack {
709 std::array<byte, 0x30> stack;
710 byte position;
711 const GRFFile *grffile;
712 bool used;
713
TextRefStackTextRefStack714 TextRefStack() : position(0), grffile(nullptr), used(false) {}
715
PopUnsignedByteTextRefStack716 uint8 PopUnsignedByte() { assert(this->position < this->stack.size()); return this->stack[this->position++]; }
PopSignedByteTextRefStack717 int8 PopSignedByte() { return (int8)this->PopUnsignedByte(); }
718
PopUnsignedWordTextRefStack719 uint16 PopUnsignedWord()
720 {
721 uint16 val = this->PopUnsignedByte();
722 return val | (this->PopUnsignedByte() << 8);
723 }
PopSignedWordTextRefStack724 int16 PopSignedWord() { return (int32)this->PopUnsignedWord(); }
725
PopUnsignedDWordTextRefStack726 uint32 PopUnsignedDWord()
727 {
728 uint32 val = this->PopUnsignedWord();
729 return val | (this->PopUnsignedWord() << 16);
730 }
PopSignedDWordTextRefStack731 int32 PopSignedDWord() { return (int32)this->PopUnsignedDWord(); }
732
PopUnsignedQWordTextRefStack733 uint64 PopUnsignedQWord()
734 {
735 uint64 val = this->PopUnsignedDWord();
736 return val | (((uint64)this->PopUnsignedDWord()) << 32);
737 }
PopSignedQWordTextRefStack738 int64 PopSignedQWord() { return (int64)this->PopUnsignedQWord(); }
739
740 /** Rotate the top four words down: W1, W2, W3, W4 -> W4, W1, W2, W3 */
RotateTop4WordsTextRefStack741 void RotateTop4Words()
742 {
743 byte tmp[2];
744 for (int i = 0; i < 2; i++) tmp[i] = this->stack[this->position + i + 6];
745 for (int i = 5; i >= 0; i--) this->stack[this->position + i + 2] = this->stack[this->position + i];
746 for (int i = 0; i < 2; i++) this->stack[this->position + i] = tmp[i];
747 }
748
PushWordTextRefStack749 void PushWord(uint16 word)
750 {
751 if (this->position >= 2) {
752 this->position -= 2;
753 } else {
754 // Rotate right 2 positions
755 std::rotate(this->stack.rbegin(), this->stack.rbegin() + 2, this->stack.rend());
756 }
757 this->stack[this->position] = GB(word, 0, 8);
758 this->stack[this->position + 1] = GB(word, 8, 8);
759 }
760
ResetStackTextRefStack761 void ResetStack(const GRFFile *grffile)
762 {
763 assert(grffile != nullptr);
764 this->position = 0;
765 this->grffile = grffile;
766 this->used = true;
767 }
768
RewindStackTextRefStack769 void RewindStack() { this->position = 0; }
770 };
771
772 /** The stack that is used for TTDP compatible string code parsing */
773 static TextRefStack _newgrf_textrefstack;
774
775 /**
776 * Check whether the NewGRF text stack is in use.
777 * @return True iff the NewGRF text stack is used.
778 */
UsingNewGRFTextStack()779 bool UsingNewGRFTextStack()
780 {
781 return _newgrf_textrefstack.used;
782 }
783
784 /**
785 * Create a backup of the current NewGRF text stack.
786 * @return A copy of the current text stack.
787 */
CreateTextRefStackBackup()788 struct TextRefStack *CreateTextRefStackBackup()
789 {
790 return new TextRefStack(_newgrf_textrefstack);
791 }
792
793 /**
794 * Restore a copy of the text stack to the used stack.
795 * @param backup The copy to restore.
796 */
RestoreTextRefStackBackup(struct TextRefStack * backup)797 void RestoreTextRefStackBackup(struct TextRefStack *backup)
798 {
799 _newgrf_textrefstack = *backup;
800 delete backup;
801 }
802
803 /**
804 * Start using the TTDP compatible string code parsing.
805 *
806 * On start a number of values is copied on the #TextRefStack.
807 * You can then use #GetString() and the normal string drawing functions,
808 * and they will use the #TextRefStack for NewGRF string codes.
809 *
810 * However, when you want to draw a string multiple times using the same stack,
811 * you have to call #RewindTextRefStack() between draws.
812 *
813 * After you are done with drawing, you must disable usage of the #TextRefStack
814 * by calling #StopTextRefStackUsage(), so NewGRF string codes operate on the
815 * normal string parameters again.
816 *
817 * @param grffile the NewGRF providing the stack data
818 * @param numEntries number of entries to copy from the registers
819 * @param values values to copy onto the stack; if nullptr the temporary NewGRF registers will be used instead
820 */
StartTextRefStackUsage(const GRFFile * grffile,byte numEntries,const uint32 * values)821 void StartTextRefStackUsage(const GRFFile *grffile, byte numEntries, const uint32 *values)
822 {
823 extern TemporaryStorageArray<int32, 0x110> _temp_store;
824
825 _newgrf_textrefstack.ResetStack(grffile);
826
827 auto stack_it = _newgrf_textrefstack.stack.begin();
828 for (uint i = 0; i < numEntries; i++) {
829 uint32 value = values != nullptr ? values[i] : _temp_store.GetValue(0x100 + i);
830 for (uint j = 0; j < 32; j += 8) {
831 *stack_it = GB(value, j, 8);
832 stack_it++;
833 }
834 }
835 }
836
837 /** Stop using the TTDP compatible string code parsing */
StopTextRefStackUsage()838 void StopTextRefStackUsage()
839 {
840 _newgrf_textrefstack.used = false;
841 }
842
RewindTextRefStack()843 void RewindTextRefStack()
844 {
845 _newgrf_textrefstack.RewindStack();
846 }
847
848 /**
849 * FormatString for NewGRF specific "magic" string control codes
850 * @param scc the string control code that has been read
851 * @param buff the buffer we're writing to
852 * @param str the string that we need to write
853 * @param argv the OpenTTD stack of values
854 * @param argv_size space on the stack \a argv
855 * @param modify_argv When true, modify the OpenTTD stack.
856 * @return the string control code to "execute" now
857 */
RemapNewGRFStringControlCode(uint scc,char * buf_start,char ** buff,const char ** str,int64 * argv,uint argv_size,bool modify_argv)858 uint RemapNewGRFStringControlCode(uint scc, char *buf_start, char **buff, const char **str, int64 *argv, uint argv_size, bool modify_argv)
859 {
860 switch (scc) {
861 default: break;
862
863 case SCC_NEWGRF_PRINT_DWORD_SIGNED:
864 case SCC_NEWGRF_PRINT_WORD_SIGNED:
865 case SCC_NEWGRF_PRINT_BYTE_SIGNED:
866 case SCC_NEWGRF_PRINT_WORD_UNSIGNED:
867 case SCC_NEWGRF_PRINT_BYTE_HEX:
868 case SCC_NEWGRF_PRINT_WORD_HEX:
869 case SCC_NEWGRF_PRINT_DWORD_HEX:
870 case SCC_NEWGRF_PRINT_QWORD_HEX:
871 case SCC_NEWGRF_PRINT_DWORD_CURRENCY:
872 case SCC_NEWGRF_PRINT_QWORD_CURRENCY:
873 case SCC_NEWGRF_PRINT_WORD_STRING_ID:
874 case SCC_NEWGRF_PRINT_WORD_DATE_LONG:
875 case SCC_NEWGRF_PRINT_DWORD_DATE_LONG:
876 case SCC_NEWGRF_PRINT_WORD_DATE_SHORT:
877 case SCC_NEWGRF_PRINT_DWORD_DATE_SHORT:
878 case SCC_NEWGRF_PRINT_WORD_SPEED:
879 case SCC_NEWGRF_PRINT_WORD_VOLUME_LONG:
880 case SCC_NEWGRF_PRINT_WORD_VOLUME_SHORT:
881 case SCC_NEWGRF_PRINT_WORD_WEIGHT_LONG:
882 case SCC_NEWGRF_PRINT_WORD_WEIGHT_SHORT:
883 case SCC_NEWGRF_PRINT_WORD_POWER:
884 case SCC_NEWGRF_PRINT_WORD_STATION_NAME:
885 case SCC_NEWGRF_PRINT_WORD_CARGO_NAME:
886 if (argv_size < 1) {
887 Debug(misc, 0, "Too many NewGRF string parameters.");
888 return 0;
889 }
890 break;
891
892 case SCC_NEWGRF_PRINT_WORD_CARGO_LONG:
893 case SCC_NEWGRF_PRINT_WORD_CARGO_SHORT:
894 case SCC_NEWGRF_PRINT_WORD_CARGO_TINY:
895 if (argv_size < 2) {
896 Debug(misc, 0, "Too many NewGRF string parameters.");
897 return 0;
898 }
899 break;
900 }
901
902 if (_newgrf_textrefstack.used && modify_argv) {
903 switch (scc) {
904 default: NOT_REACHED();
905 case SCC_NEWGRF_PRINT_BYTE_SIGNED: *argv = _newgrf_textrefstack.PopSignedByte(); break;
906 case SCC_NEWGRF_PRINT_QWORD_CURRENCY: *argv = _newgrf_textrefstack.PopSignedQWord(); break;
907
908 case SCC_NEWGRF_PRINT_DWORD_CURRENCY:
909 case SCC_NEWGRF_PRINT_DWORD_SIGNED: *argv = _newgrf_textrefstack.PopSignedDWord(); break;
910
911 case SCC_NEWGRF_PRINT_BYTE_HEX: *argv = _newgrf_textrefstack.PopUnsignedByte(); break;
912 case SCC_NEWGRF_PRINT_QWORD_HEX: *argv = _newgrf_textrefstack.PopUnsignedQWord(); break;
913
914 case SCC_NEWGRF_PRINT_WORD_SPEED:
915 case SCC_NEWGRF_PRINT_WORD_VOLUME_LONG:
916 case SCC_NEWGRF_PRINT_WORD_VOLUME_SHORT:
917 case SCC_NEWGRF_PRINT_WORD_SIGNED: *argv = _newgrf_textrefstack.PopSignedWord(); break;
918
919 case SCC_NEWGRF_PRINT_WORD_HEX:
920 case SCC_NEWGRF_PRINT_WORD_WEIGHT_LONG:
921 case SCC_NEWGRF_PRINT_WORD_WEIGHT_SHORT:
922 case SCC_NEWGRF_PRINT_WORD_POWER:
923 case SCC_NEWGRF_PRINT_WORD_STATION_NAME:
924 case SCC_NEWGRF_PRINT_WORD_UNSIGNED: *argv = _newgrf_textrefstack.PopUnsignedWord(); break;
925
926 case SCC_NEWGRF_PRINT_DWORD_DATE_LONG:
927 case SCC_NEWGRF_PRINT_DWORD_DATE_SHORT:
928 case SCC_NEWGRF_PRINT_DWORD_HEX: *argv = _newgrf_textrefstack.PopUnsignedDWord(); break;
929
930 case SCC_NEWGRF_PRINT_WORD_DATE_LONG:
931 case SCC_NEWGRF_PRINT_WORD_DATE_SHORT: *argv = _newgrf_textrefstack.PopUnsignedWord() + DAYS_TILL_ORIGINAL_BASE_YEAR; break;
932
933 case SCC_NEWGRF_DISCARD_WORD: _newgrf_textrefstack.PopUnsignedWord(); break;
934
935 case SCC_NEWGRF_ROTATE_TOP_4_WORDS: _newgrf_textrefstack.RotateTop4Words(); break;
936 case SCC_NEWGRF_PUSH_WORD: _newgrf_textrefstack.PushWord(Utf8Consume(str)); break;
937 case SCC_NEWGRF_UNPRINT: *buff = std::max(*buff - Utf8Consume(str), buf_start); break;
938
939 case SCC_NEWGRF_PRINT_WORD_CARGO_LONG:
940 case SCC_NEWGRF_PRINT_WORD_CARGO_SHORT:
941 case SCC_NEWGRF_PRINT_WORD_CARGO_TINY:
942 argv[0] = GetCargoTranslation(_newgrf_textrefstack.PopUnsignedWord(), _newgrf_textrefstack.grffile);
943 argv[1] = _newgrf_textrefstack.PopUnsignedWord();
944 break;
945
946 case SCC_NEWGRF_PRINT_WORD_STRING_ID:
947 *argv = MapGRFStringID(_newgrf_textrefstack.grffile->grfid, _newgrf_textrefstack.PopUnsignedWord());
948 break;
949
950 case SCC_NEWGRF_PRINT_WORD_CARGO_NAME: {
951 CargoID cargo = GetCargoTranslation(_newgrf_textrefstack.PopUnsignedWord(), _newgrf_textrefstack.grffile);
952 *argv = cargo < NUM_CARGO ? 1ULL << cargo : 0;
953 break;
954 }
955 }
956 } else {
957 /* Consume additional parameter characters */
958 switch (scc) {
959 default: break;
960
961 case SCC_NEWGRF_PUSH_WORD:
962 case SCC_NEWGRF_UNPRINT:
963 Utf8Consume(str);
964 break;
965 }
966 }
967
968 switch (scc) {
969 default: NOT_REACHED();
970 case SCC_NEWGRF_PRINT_DWORD_SIGNED:
971 case SCC_NEWGRF_PRINT_WORD_SIGNED:
972 case SCC_NEWGRF_PRINT_BYTE_SIGNED:
973 case SCC_NEWGRF_PRINT_WORD_UNSIGNED:
974 return SCC_COMMA;
975
976 case SCC_NEWGRF_PRINT_BYTE_HEX:
977 case SCC_NEWGRF_PRINT_WORD_HEX:
978 case SCC_NEWGRF_PRINT_DWORD_HEX:
979 case SCC_NEWGRF_PRINT_QWORD_HEX:
980 return SCC_HEX;
981
982 case SCC_NEWGRF_PRINT_DWORD_CURRENCY:
983 case SCC_NEWGRF_PRINT_QWORD_CURRENCY:
984 return SCC_CURRENCY_LONG;
985
986 case SCC_NEWGRF_PRINT_WORD_STRING_ID:
987 return SCC_NEWGRF_PRINT_WORD_STRING_ID;
988
989 case SCC_NEWGRF_PRINT_WORD_DATE_LONG:
990 case SCC_NEWGRF_PRINT_DWORD_DATE_LONG:
991 return SCC_DATE_LONG;
992
993 case SCC_NEWGRF_PRINT_WORD_DATE_SHORT:
994 case SCC_NEWGRF_PRINT_DWORD_DATE_SHORT:
995 return SCC_DATE_SHORT;
996
997 case SCC_NEWGRF_PRINT_WORD_SPEED:
998 return SCC_VELOCITY;
999
1000 case SCC_NEWGRF_PRINT_WORD_VOLUME_LONG:
1001 return SCC_VOLUME_LONG;
1002
1003 case SCC_NEWGRF_PRINT_WORD_VOLUME_SHORT:
1004 return SCC_VOLUME_SHORT;
1005
1006 case SCC_NEWGRF_PRINT_WORD_WEIGHT_LONG:
1007 return SCC_WEIGHT_LONG;
1008
1009 case SCC_NEWGRF_PRINT_WORD_WEIGHT_SHORT:
1010 return SCC_WEIGHT_SHORT;
1011
1012 case SCC_NEWGRF_PRINT_WORD_POWER:
1013 return SCC_POWER;
1014
1015 case SCC_NEWGRF_PRINT_WORD_CARGO_LONG:
1016 return SCC_CARGO_LONG;
1017
1018 case SCC_NEWGRF_PRINT_WORD_CARGO_SHORT:
1019 return SCC_CARGO_SHORT;
1020
1021 case SCC_NEWGRF_PRINT_WORD_CARGO_TINY:
1022 return SCC_CARGO_TINY;
1023
1024 case SCC_NEWGRF_PRINT_WORD_CARGO_NAME:
1025 return SCC_CARGO_LIST;
1026
1027 case SCC_NEWGRF_PRINT_WORD_STATION_NAME:
1028 return SCC_STATION_NAME;
1029
1030 case SCC_NEWGRF_DISCARD_WORD:
1031 case SCC_NEWGRF_ROTATE_TOP_4_WORDS:
1032 case SCC_NEWGRF_PUSH_WORD:
1033 case SCC_NEWGRF_UNPRINT:
1034 return 0;
1035 }
1036 }
1037