OpenTTD Source  12.0-beta2
newgrf_text.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
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 
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 
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;
81 
88 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 
103 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 
121  {
122  }
123 
125  int offset;
126 
128  std::map<byte, std::stringstream> strings;
129 
135  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 
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;
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;
361  Utf8Encode(d, tmp);
362  break;
363  }
364  case 0x04:
365  if (src[0] == '\0') goto string_end;
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:
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 
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 
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 
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 
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 
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 
596 }
597 
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) {
606  }
607  }
608 
609  return STR_UNDEFINED;
610 }
611 
612 
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 
645 const char *GetGRFStringFromGRFText(const GRFTextWrapper &text)
646 {
647  return text ? GetGRFStringFromGRFText(*text) : nullptr;
648 }
649 
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 
672 void SetCurrentGrfLangID(byte language_id)
673 {
674  _currentLangID = language_id;
675 }
676 
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 
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 
714  TextRefStack() : position(0), grffile(nullptr), used(false) {}
715 
716  uint8 PopUnsignedByte() { assert(this->position < this->stack.size()); return this->stack[this->position++]; }
717  int8 PopSignedByte() { return (int8)this->PopUnsignedByte(); }
718 
719  uint16 PopUnsignedWord()
720  {
721  uint16 val = this->PopUnsignedByte();
722  return val | (this->PopUnsignedByte() << 8);
723  }
724  int16 PopSignedWord() { return (int32)this->PopUnsignedWord(); }
725 
726  uint32 PopUnsignedDWord()
727  {
728  uint32 val = this->PopUnsignedWord();
729  return val | (this->PopUnsignedWord() << 16);
730  }
731  int32 PopSignedDWord() { return (int32)this->PopUnsignedDWord(); }
732 
733  uint64 PopUnsignedQWord()
734  {
735  uint64 val = this->PopUnsignedDWord();
736  return val | (((uint64)this->PopUnsignedDWord()) << 32);
737  }
738  int64 PopSignedQWord() { return (int64)this->PopUnsignedQWord(); }
739 
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 
749  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 
761  void ResetStack(const GRFFile *grffile)
762  {
763  assert(grffile != nullptr);
764  this->position = 0;
765  this->grffile = grffile;
766  this->used = true;
767  }
768 
769  void RewindStack() { this->position = 0; }
770 };
771 
774 
780 {
781  return _newgrf_textrefstack.used;
782 }
783 
789 {
791 }
792 
798 {
799  _newgrf_textrefstack = *backup;
800  delete backup;
801 }
802 
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 
839 {
840  _newgrf_textrefstack.used = false;
841 }
842 
843 void RewindTextRefStack()
844 {
845  _newgrf_textrefstack.RewindStack();
846 }
847 
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 
886  if (argv_size < 1) {
887  Debug(misc, 0, "Too many NewGRF string parameters.");
888  return 0;
889  }
890  break;
891 
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 
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 
917  case SCC_NEWGRF_PRINT_WORD_SIGNED: *argv = _newgrf_textrefstack.PopSignedWord(); break;
918 
924  case SCC_NEWGRF_PRINT_WORD_UNSIGNED: *argv = _newgrf_textrefstack.PopUnsignedWord(); break;
925 
928  case SCC_NEWGRF_PRINT_DWORD_HEX: *argv = _newgrf_textrefstack.PopUnsignedDWord(); break;
929 
932 
933  case SCC_NEWGRF_DISCARD_WORD: _newgrf_textrefstack.PopUnsignedWord(); break;
934 
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 
942  argv[0] = GetCargoTranslation(_newgrf_textrefstack.PopUnsignedWord(), _newgrf_textrefstack.grffile);
943  argv[1] = _newgrf_textrefstack.PopUnsignedWord();
944  break;
945 
947  *argv = MapGRFStringID(_newgrf_textrefstack.grffile->grfid, _newgrf_textrefstack.PopUnsignedWord());
948  break;
949 
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 
962  case SCC_NEWGRF_UNPRINT:
963  Utf8Consume(str);
964  break;
965  }
966  }
967 
968  switch (scc) {
969  default: NOT_REACHED();
974  return SCC_COMMA;
975 
980  return SCC_HEX;
981 
984  return SCC_CURRENCY_LONG;
985 
988 
991  return SCC_DATE_LONG;
992 
995  return SCC_DATE_SHORT;
996 
998  return SCC_VELOCITY;
999 
1001  return SCC_VOLUME_LONG;
1002 
1004  return SCC_VOLUME_SHORT;
1005 
1007  return SCC_WEIGHT_LONG;
1008 
1010  return SCC_WEIGHT_SHORT;
1011 
1013  return SCC_POWER;
1014 
1016  return SCC_CARGO_LONG;
1017 
1019  return SCC_CARGO_SHORT;
1020 
1022  return SCC_CARGO_TINY;
1023 
1025  return SCC_CARGO_LIST;
1026 
1028  return SCC_STATION_NAME;
1029 
1032  case SCC_NEWGRF_PUSH_WORD:
1033  case SCC_NEWGRF_UNPRINT:
1034  return 0;
1035  }
1036 }
SCC_NEWGRF_ROTATE_TOP_4_WORDS
@ SCC_NEWGRF_ROTATE_TOP_4_WORDS
86: Rotate the top 4 words of the stack (W4 W1 W2 W3)
Definition: control_codes.h:153
GRFText
A GRF text with associated language ID.
Definition: newgrf_text.h:25
LanguageMap::case_map
std::vector< Mapping > case_map
Mapping of NewGRF and OpenTTD IDs for cases.
Definition: newgrf_text.h:72
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:35
GRFTextList
std::vector< GRFText > GRFTextList
A GRF text with a list of translations.
Definition: newgrf_text.h:31
SCC_NEWGRF_STRINL
@ SCC_NEWGRF_STRINL
Inline another string at the current position, StringID is encoded in the string.
Definition: control_codes.h:156
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
SCC_NEWGRF_PRINT_WORD_SPEED
@ SCC_NEWGRF_PRINT_WORD_SPEED
84: Read 2 bytes from the stack as signed speed
Definition: control_codes.h:132
SCC_NEWGRF_PRINT_WORD_CARGO_SHORT
@ SCC_NEWGRF_PRINT_WORD_CARGO_SHORT
9A 1C: Read 2 + 2 bytes from the stack as cargo type (translated) and unsigned cargo amount
Definition: control_codes.h:147
TEXT_TAB_NEWGRF_START
@ TEXT_TAB_NEWGRF_START
Start of NewGRF supplied strings.
Definition: strings_type.h:40
MakeStringID
static StringID MakeStringID(StringTab tab, uint index)
Create a StringID.
Definition: strings_func.h:47
grfmsg
void CDECL grfmsg(int severity, const char *str,...)
Debug() function dedicated to newGRF debugging messages Function is essentially the same as Debug(grf...
Definition: newgrf.cpp:391
LanguageMap::plural_form
int plural_form
The plural form used for this language.
Definition: newgrf_text.h:73
SCC_BIGFONT
@ SCC_BIGFONT
Switch to large font.
Definition: control_codes.h:31
SCC_NEWGRF_PRINT_WORD_HEX
@ SCC_NEWGRF_PRINT_WORD_HEX
9A 07: Read 2 bytes from the stack and print it as hex
Definition: control_codes.h:138
GRFTextEntry
Holder of the above structure.
Definition: newgrf_text.cpp:70
Utf8Encode
size_t Utf8Encode(T buf, WChar c)
Encode a unicode character and place it in the buffer.
Definition: string.cpp:616
StartTextRefStackUsage
void StartTextRefStackUsage(const GRFFile *grffile, byte numEntries, const uint32 *values)
Start using the TTDP compatible string code parsing.
Definition: newgrf_text.cpp:821
LanguagePackHeader::num_cases
uint8 num_cases
the number of cases of this language
Definition: language.h:54
smallmap_type.hpp
StopTextRefStackUsage
void StopTextRefStackUsage()
Stop using the TTDP compatible string code parsing.
Definition: newgrf_text.cpp:838
TAB_SIZE_NEWGRF
static const uint TAB_SIZE_NEWGRF
Number of strings for NewGRFs.
Definition: strings_type.h:52
TranslateTTDPatchCodes
std::string TranslateTTDPatchCodes(uint32 grfid, uint8 language_id, bool allow_newlines, const std::string &str, StringControlCode byte80)
Translate TTDPatch string codes into something OpenTTD can handle (better).
Definition: newgrf_text.cpp:241
SCC_NEWGRF_PRINT_DWORD_DATE_LONG
@ SCC_NEWGRF_PRINT_DWORD_DATE_LONG
9A 16: Read 4 bytes from the stack as base 0 date
Definition: control_codes.h:141
GRFBaseLanguages
GRFBaseLanguages
Explains the newgrf shift bit positioning.
Definition: newgrf_text.cpp:46
RestoreTextRefStackBackup
void RestoreTextRefStackBackup(struct TextRefStack *backup)
Restore a copy of the text stack to the used stack.
Definition: newgrf_text.cpp:797
alloc_type.hpp
control_codes.h
SCC_NEWGRF_DISCARD_WORD
@ SCC_NEWGRF_DISCARD_WORD
85: Discard the next two bytes
Definition: control_codes.h:152
AddGRFString
StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid_to_add, bool new_scheme, bool allow_newlines, const char *text_to_add, StringID def_string)
Add the new read string into our structure.
Definition: newgrf_text.cpp:551
SCC_NEWGRF_PUSH_WORD
@ SCC_NEWGRF_PUSH_WORD
9A 03: Pushes 2 bytes onto the stack
Definition: control_codes.h:150
NFO_UTF8_IDENTIFIER
static const WChar NFO_UTF8_IDENTIFIER
This character, the thorn ('รพ'), indicates a unicode string to NFO.
Definition: newgrf_text.h:22
SCC_NEWGRF_PRINT_WORD_STRING_ID
@ SCC_NEWGRF_PRINT_WORD_STRING_ID
81: Read 2 bytes from the stack as String ID
Definition: control_codes.h:129
UnmappedChoiceList::Flush
void Flush(const LanguageMap *lm, std::ostringstream &dest)
Flush this choice list into the destination string.
Definition: newgrf_text.cpp:135
LanguageMap::GetLanguageMap
static const LanguageMap * GetLanguageMap(uint32 grfid, uint8 language_id)
Get the language map associated with a given NewGRF and language.
Definition: newgrf.cpp:2559
RemapNewGRFStringControlCode
uint RemapNewGRFStringControlCode(uint scc, char *buf_start, char **buff, const char **str, int64 *argv, uint argv_size, bool modify_argv)
FormatString for NewGRF specific "magic" string control codes.
Definition: newgrf_text.cpp:858
SCC_TINYFONT
@ SCC_TINYFONT
Switch to small font.
Definition: control_codes.h:30
TextRefStack::RotateTop4Words
void RotateTop4Words()
Rotate the top four words down: W1, W2, W3, W4 -> W4, W1, W2, W3.
Definition: newgrf_text.cpp:741
GetGRFStringFromGRFText
const char * GetGRFStringFromGRFText(const GRFTextList &text_list)
Get a C-string from a GRFText-list.
Definition: newgrf_text.cpp:620
SCC_NEWGRF_PRINT_WORD_CARGO_NAME
@ SCC_NEWGRF_PRINT_WORD_CARGO_NAME
9A 1E: Read 2 bytes from the stack as cargo name
Definition: control_codes.h:149
CleanUpStrings
void CleanUpStrings()
House cleaning.
Definition: newgrf_text.cpp:695
_currentLangID
static byte _currentLangID
by default, english is used.
Definition: newgrf_text.cpp:80
SCC_NEWGRF_PRINT_WORD_CARGO_TINY
@ SCC_NEWGRF_PRINT_WORD_CARGO_TINY
9A 1D: Read 2 + 2 bytes from the stack as cargo type (translated) and unsigned cargo amount
Definition: control_codes.h:148
LanguageMap::gender_map
std::vector< Mapping > gender_map
Mapping of NewGRF and OpenTTD IDs for genders.
Definition: newgrf_text.h:71
SCC_NEWGRF_PRINT_DWORD_SIGNED
@ SCC_NEWGRF_PRINT_DWORD_SIGNED
7B: Read 4 bytes from the stack
Definition: control_codes.h:124
_current_language
const LanguageMetadata * _current_language
The currently loaded language.
Definition: strings.cpp:46
safeguards.h
GetGRFStringPtr
const char * GetGRFStringPtr(uint16 stringid)
Get a C-string from a stringid set by a newgrf.
Definition: newgrf_text.cpp:653
IsValidChar
bool IsValidChar(WChar key, CharSetFilter afilter)
Only allow certain keys.
Definition: string.cpp:476
GetGRFStringID
StringID GetGRFStringID(uint32 grfid, StringID stringid)
Returns the index for this stringid associated with its grfID.
Definition: newgrf_text.cpp:601
newgrf_text.h
UnmappedChoiceList
Helper structure for mapping choice lists.
Definition: newgrf_text.cpp:113
language.h
stdafx.h
SCC_NEWGRF_PRINT_WORD_STATION_NAME
@ SCC_NEWGRF_PRINT_WORD_STATION_NAME
9A 0C: Read 2 bytes from the stack as station name
Definition: control_codes.h:135
CS_ALPHANUMERAL
@ CS_ALPHANUMERAL
Both numeric and alphabetic and spaces and stuff.
Definition: string_type.h:27
UnmappedChoiceList::UnmappedChoiceList
UnmappedChoiceList(StringControlCode type, int offset)
Initialise the mapping.
Definition: newgrf_text.cpp:119
LanguageMap::Mapping
Mapping between NewGRF and OpenTTD IDs.
Definition: newgrf_text.h:60
Utf8Decode
size_t Utf8Decode(WChar *c, const char *s)
Decode and consume the next UTF-8 encoded character.
Definition: string.cpp:574
date_type.h
SCC_NEWGRF_PRINT_WORD_DATE_LONG
@ SCC_NEWGRF_PRINT_WORD_DATE_LONG
82: Read 2 bytes from the stack as base 1920 date
Definition: control_codes.h:130
string_func.h
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
SCC_NEWGRF_PRINT_QWORD_CURRENCY
@ SCC_NEWGRF_PRINT_QWORD_CURRENCY
9A 01: Read 8 bytes from the stack as currency
Definition: control_codes.h:136
UnmappedChoiceList::offset
int offset
The offset for the plural/gender form.
Definition: newgrf_text.cpp:125
strings_func.h
LanguageMap
Mapping of language data between a NewGRF and OpenTTD.
Definition: newgrf_text.h:58
UnmappedChoiceList::strings
std::map< byte, std::stringstream > strings
Mapping of NewGRF supplied ID to the different strings in the choice list.
Definition: newgrf_text.cpp:128
LanguageMap::GetMapping
int GetMapping(int newgrf_id, bool gender) const
Get the mapping from the NewGRF supplied ID to OpenTTD's internal ID.
Definition: newgrf_text.cpp:88
StringControlCode
StringControlCode
List of string control codes used for string formatting, displaying, and by strgen to generate the la...
Definition: control_codes.h:17
SCC_NEWGRF_PRINT_WORD_SIGNED
@ SCC_NEWGRF_PRINT_WORD_SIGNED
7C: Read 2 bytes from the stack as signed value
Definition: control_codes.h:125
SCC_NEWGRF_PRINT_WORD_VOLUME_SHORT
@ SCC_NEWGRF_PRINT_WORD_VOLUME_SHORT
9A 19: Read 2 bytes from the stack as short signed volume
Definition: control_codes.h:144
SCC_NEWGRF_PRINT_QWORD_HEX
@ SCC_NEWGRF_PRINT_QWORD_HEX
9A 0B: Read 8 bytes from the stack and print it as hex
Definition: control_codes.h:140
newgrf.h
SCC_NEWGRF_PRINT_DWORD_DATE_SHORT
@ SCC_NEWGRF_PRINT_DWORD_DATE_SHORT
9A 17: Read 4 bytes from the stack as base 0 date
Definition: control_codes.h:142
NUM_CARGO
@ NUM_CARGO
Maximal number of cargo types in a game.
Definition: cargo_type.h:65
GetCargoTranslation
CargoID GetCargoTranslation(uint8 cargo, const GRFFile *grffile, bool usebit)
Translate a GRF-local cargo slot/bitnum into a CargoID.
Definition: newgrf_cargo.cpp:79
SCC_NEWGRF_PRINT_DWORD_CURRENCY
@ SCC_NEWGRF_PRINT_DWORD_CURRENCY
8F: Read 4 bytes from the stack as currency
Definition: control_codes.h:128
SCC_NEWGRF_PRINT_BYTE_HEX
@ SCC_NEWGRF_PRINT_BYTE_HEX
9A 06: Read 1 byte from the stack and print it as hex
Definition: control_codes.h:137
UnmappedChoiceList::type
StringControlCode type
The type of choice list.
Definition: newgrf_text.cpp:124
SCC_NEWGRF_PRINT_WORD_CARGO_LONG
@ SCC_NEWGRF_PRINT_WORD_CARGO_LONG
9A 1B: Read 2 + 2 bytes from the stack as cargo type (translated) and unsigned cargo amount
Definition: control_codes.h:146
CreateTextRefStackBackup
struct TextRefStack * CreateTextRefStackBackup()
Create a backup of the current NewGRF text stack.
Definition: newgrf_text.cpp:788
SCC_NEWGRF_PRINT_WORD_DATE_SHORT
@ SCC_NEWGRF_PRINT_WORD_DATE_SHORT
83: Read 2 bytes from the stack as base 1920 date
Definition: control_codes.h:131
SetCurrentGrfLangID
void SetCurrentGrfLangID(byte language_id)
Equivalence Setter function between game and newgrf langID.
Definition: newgrf_text.cpp:672
LanguagePackHeader::num_genders
uint8 num_genders
the number of genders of this language
Definition: language.h:53
SCC_NEWGRF_PRINT_WORD_VOLUME_LONG
@ SCC_NEWGRF_PRINT_WORD_VOLUME_LONG
87: Read 2 bytes from the stack as long signed volume
Definition: control_codes.h:133
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:378
LanguageMap::GetReverseMapping
int GetReverseMapping(int openttd_id, bool gender) const
Get the mapping from OpenTTD's internal ID to the NewGRF supplied ID.
Definition: newgrf_text.cpp:103
SCC_NEWGRF_PRINT_WORD_UNSIGNED
@ SCC_NEWGRF_PRINT_WORD_UNSIGNED
7E: Read 2 bytes from the stack as unsigned value
Definition: control_codes.h:127
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
TemporaryStorageArray
Class for temporary storage of data.
Definition: newgrf_storage.h:150
SCC_NEWGRF_PRINT_WORD_WEIGHT_LONG
@ SCC_NEWGRF_PRINT_WORD_WEIGHT_LONG
9A 0D: Read 2 bytes from the stack as long unsigned weight
Definition: control_codes.h:134
GRFTextWrapper
std::shared_ptr< GRFTextList > GRFTextWrapper
Reference counted wrapper around a GRFText pointer.
Definition: newgrf_text.h:33
SCC_NEWGRF_PRINT_DWORD_HEX
@ SCC_NEWGRF_PRINT_DWORD_HEX
9A 08: Read 4 bytes from the stack and print it as hex
Definition: control_codes.h:139
MapGRFStringID
StringID MapGRFStringID(uint32 grfid, StringID str)
Used when setting an object's property to map to the GRF's strings while taking in consideration the ...
Definition: newgrf.cpp:563
SCC_NEWGRF_PRINT_WORD_POWER
@ SCC_NEWGRF_PRINT_WORD_POWER
9A 18: Read 2 bytes from the stack as unsigned power
Definition: control_codes.h:143
SCC_NEWGRF_PRINT_BYTE_SIGNED
@ SCC_NEWGRF_PRINT_BYTE_SIGNED
7D: Read 1 byte from the stack as signed value
Definition: control_codes.h:126
newgrf_storage.h
SCC_NEWGRF_UNPRINT
@ SCC_NEWGRF_UNPRINT
9A 04: "Unprints" the given number of bytes from the string
Definition: control_codes.h:151
newgrf_cargo.h
_newgrf_textrefstack
static TextRefStack _newgrf_textrefstack
The stack that is used for TTDP compatible string code parsing.
Definition: newgrf_text.cpp:773
AddGRFTextToList
static void AddGRFTextToList(GRFTextList &list, byte langid, const std::string &text_to_add)
Add a new text to a GRFText list.
Definition: newgrf_text.cpp:493
GRFFile
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:105
UsingNewGRFTextStack
bool UsingNewGRFTextStack()
Check whether the NewGRF text stack is in use.
Definition: newgrf_text.cpp:779
debug.h
DAYS_TILL_ORIGINAL_BASE_YEAR
#define DAYS_TILL_ORIGINAL_BASE_YEAR
The offset in days from the '_date == 0' till 'ConvertYMDToDate(ORIGINAL_BASE_YEAR,...
Definition: date_type.h:81
SCC_NEWGRF_PRINT_WORD_WEIGHT_SHORT
@ SCC_NEWGRF_PRINT_WORD_WEIGHT_SHORT
9A 1A: Read 2 bytes from the stack as short unsigned weight
Definition: control_codes.h:145
Utf8EncodedCharLen
static int8 Utf8EncodedCharLen(char c)
Return the length of an UTF-8 encoded value based on a single char.
Definition: string_func.h:132
TextRefStack
Definition: newgrf_text.cpp:708