Data Structures | Defines | Enumerations | Functions | Variables

# newgrf_text.cpp File Reference

Implementation of Action 04 "universal holder" structure and functions. More...

`#include "stdafx.h"`
`#include "newgrf.h"`
`#include "strings_func.h"`
`#include "newgrf_storage.h"`
`#include "newgrf_text.h"`
`#include "string_func.h"`
`#include "date_type.h"`
`#include "debug.h"`
`#include "core/alloc_type.hpp"`
`#include "core/smallmap_type.hpp"`
`#include "language.h"`
`#include "table/strings.h"`
`#include "table/control_codes.h"`

Go to the source code of this file.

## Data Structures
struct | GRFText
| Element of the linked list. More...
struct | GRFTextEntry
| Holder of the above structure. More...
struct | UnmappedChoiceList
| Helper structure for mapping choice lists. More...
struct | TextRefStack
## Defines
#define | GRFTAB 28
#define | TABSIZE 11
#define | TEXTID_TO_STRINGID(begin, end, stringid, stringend)
## Enumerations
enum | GRFBaseLanguages { GRFLB_AMERICAN = 0x01, GRFLB_ENGLISH = 0x02, GRFLB_GERMAN = 0x04, GRFLB_FRENCH = 0x08, GRFLB_SPANISH = 0x10, GRFLB_GENERIC = 0x80 }
| Explains the newgrf shift bit positioning. More...
enum | GRFExtendedLanguages { GRFLX_AMERICAN = 0x00, GRFLX_ENGLISH = 0x01, GRFLX_GERMAN = 0x02, GRFLX_FRENCH = 0x03, GRFLX_SPANISH = 0x04, GRFLX_UNSPECIFIED = 0x7F }
## Functions
StringID | TTDPStringIDToOTTDStringIDMapping (StringID str)
| Perform a mapping from TTDPatch's string IDs to OpenTTD's string IDs, but only for the ones we are aware off; the rest like likely unused and will show a warning.
char * | TranslateTTDPatchCodes (uint32 grfid, uint8 language_id, bool allow_newlines, const char *str, int *olen, StringControlCode byte80)
| Translate TTDPatch string codes into something OpenTTD can handle (better).
void | AddGRFTextToList (GRFText **list, GRFText *text_to_add)
| Add a GRFText to a GRFText list.
void | AddGRFTextToList (struct GRFText **list, byte langid, uint32 grfid, bool allow_newlines, const char *text_to_add)
| Add a string to a GRFText list.
void | AddGRFTextToList (struct GRFText **list, const char *text_to_add)
| Add a GRFText to a GRFText list.
GRFText * | DuplicateGRFText (GRFText *orig)
| Create a copy of this GRFText list.
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.
StringID | GetGRFStringID (uint32 grfid, uint16 stringid)
| Returns the index for this stringid associated with its grfID.
const char * | GetGRFStringFromGRFText (const GRFText *text)
| Get a C-string from a GRFText-list.
const char * | GetGRFStringPtr (uint16 stringid)
| Get a C-string from a stringid set by a newgrf.
void | SetCurrentGrfLangID (byte language_id)
| Equivalence Setter function between game and newgrf langID.
bool | CheckGrfLangID (byte lang_id, byte grf_version)
void | CleanUpGRFText (GRFText *grftext)
| Delete all items of a linked GRFText list.
void | CleanUpStrings ()
| House cleaning.
bool | UsingNewGRFTextStack ()
| Check whether the NewGRF text stack is in use.
struct TextRefStack * | CreateTextRefStackBackup ()
| Create a backup of the current NewGRF text stack.
void | RestoreTextRefStackBackup (struct TextRefStack *backup)
| Restore a copy of the text stack to the used stack.
void | StartTextRefStackUsage (byte numEntries, const uint32 *values)
| Start using the TTDP compatible string code parsing.
void | StopTextRefStackUsage ()
| Stop using the TTDP compatible string code parsing.
void | RewindTextRefStack ()
uint | RemapNewGRFStringControlCode (uint scc, char *buf_start, char **buff, const char **str, int64 *argv, bool modify_argv)
| FormatString for NewGRF specific "magic" string control codes.
## Variables
static uint | _num_grf_texts = 0
static GRFTextEntry | _grf_text [(1<< TABSIZE)*3]
static byte | _currentLangID = GRFLX_ENGLISH
| by default, english is used.
static uint32 | _last_grfid = 0
static TextRefStack | _newgrf_textrefstack
| The stack that is used for TTDP compatible string code parsing.

## Detailed Description

Implementation of Action 04 "universal holder" structure and functions.

This file implements a linked-lists of strings, holding everything that the newgrf action 04 will send over to OpenTTD. One of the biggest problems is that Dynamic lang Array uses ISO codes as way to identifying current user lang, while newgrf uses bit shift codes not related to ISO. So equivalence functionnality had to be set.

Definition in file newgrf_text.cpp.

## Define Documentation

#define TEXTID_TO_STRINGID | ( |  | begin,
|  |  | end,
|  |  | stringid,
|  |  | stringend |
| ) |  |  |

Value:

    assert_compile(stringend - stringid == end - begin); \
      if (str >= begin && str <= end) return str + (stringid - begin)

## Enumeration Type Documentation

enum GRFBaseLanguages

Explains the newgrf shift bit positioning.

the grf base will not be used in order to find the string, but rather for jumping from standard langID scheme to the new one.

Definition at line 107 of file newgrf_text.cpp.

## Function Documentation

void AddGRFTextToList | ( | struct GRFText ** | list,
|  | byte | langid,
|  | uint32 | grfid,
|  | bool | allow_newlines,
|  | const char * | text_to_add |
| ) |  |  |

Add a string to a GRFText list.

Parameters:

  | list | The list where the text should be added to.
  | langid | The language of the new text.
  | grfid | The grfid where this string is defined.
  | allow_newlines | Whether newlines are allowed in this string.
  | text_to_add | The text to add to the list.

Note:
  All text-codes will be translated.

Definition at line 664 of file newgrf_text.cpp.

References AddGRFTextToList(), free(), GRFText::New(), and TranslateTTDPatchCodes().

void AddGRFTextToList | ( | struct GRFText ** | list,
|  | const char * | text_to_add |
| ) |  |  |

Add a GRFText to a GRFText list.

The text should not contain any text-codes. The text will be added as a 'default language'-text.

Parameters:

  | list | The list where the text should be added to.
  | text_to_add | The text to add to the list.

Definition at line 680 of file newgrf_text.cpp.

References AddGRFTextToList(), and GRFText::New().

void AddGRFTextToList | ( | GRFText ** | list,
|  | GRFText * | text_to_add |
| ) |  |  |

Add a GRFText to a GRFText list.

Parameters:

  | list | The list where the text should be added to.
  | text_to_add | The GRFText to add to the list.

Definition at line 637 of file newgrf_text.cpp.

References GRFText::langid, and GRFText::next.

Referenced by AddGRFString(), AddGRFTextToList(), ChangeGRFDescription(), ChangeGRFName(), ChangeGRFParamDescription(), ChangeGRFParamName(), ChangeGRFParamValueNames(), ChangeGRFURL(), FindUnknownGRFName(), and ClientNetworkUDPSocketHandler::Receive_SERVER_NEWGRFS().

void CleanUpGRFText | ( | GRFText * | grftext | ) |

Delete all items of a linked GRFText list.

Parameters:

  | grftext | the head of the list to delete

Definition at line 854 of file newgrf_text.cpp.

References GRFText::next.

Referenced by CleanUpStrings(), GRFParameterInfo::~GRFParameterInfo(), and GRFTextWrapper::~GRFTextWrapper().

void CleanUpStrings | ( |  | ) |

House cleaning.

Remove all strings and reset the text counter.

Definition at line 867 of file newgrf_text.cpp.

References CleanUpGRFText().

Referenced by ResetNewGRFData().

struct TextRefStack* CreateTextRefStackBackup | ( |  | ) | `[read]`

Create a backup of the current NewGRF text stack.

Returns:
  A copy of the current text stack.

Definition at line 961 of file newgrf_text.cpp.

Referenced by FormatString().

GRFText* DuplicateGRFText | ( | GRFText * | orig | ) | `[read]`

Create a copy of this GRFText list.

Parameters:

  | orig | The GRFText list to copy.

Returns:
  A duplicate of the given GRFText.

Definition at line 690 of file newgrf_text.cpp.

References GRFText::Copy(), and GRFText::next.

Referenced by GRFParameterInfo::GRFParameterInfo().

const char* GetGRFStringFromGRFText | ( | const GRFText * | text | ) |

Get a C-string from a GRFText-list.

If there is a translation for the current language it is returned, otherwise the default translation is returned. If there is neither a default nor a translation for the current language NULL is returned.

Parameters:

  | text | The GRFText to get the string from.

Definition at line 788 of file newgrf_text.cpp.

References _currentLangID, GRFText::langid, GRFText::next, and GRFText::text.

Referenced by GRFFileScanner::AddFile(), NewGRFParametersWindow::DrawWidget(), GRFConfig::GetDescription(), GetGRFStringPtr(), GRFConfig::GetName(), GRFConfig::GetURL(), ClientNetworkUDPSocketHandler::Receive_SERVER_NEWGRFS(), and NewGRFParametersWindow::UpdateWidgetSize().

uint RemapNewGRFStringControlCode | ( | uint | scc,
|  | char * | buf_start,
|  | char ** | buff,
|  | const char ** | str,
|  | int64 * | argv,
|  | bool | modify_argv |
| ) |  |  |

FormatString for NewGRF specific "magic" string control codes.

Parameters:

  | scc | the string control code that has been read
  | buff | the buffer we're writing to
  | str | the string that we need to write
  | argv | the OpenTTD stack of values
  | modify_argv | When true, modify the OpenTTD stack.

Returns:
  the string control code to "execute" now

Definition at line 1029 of file newgrf_text.cpp.

References max(), TextRefStack::RotateTop4Words(), SCC_NEWGRF_DISCARD_WORD, SCC_NEWGRF_PRINT_BYTE_HEX, SCC_NEWGRF_PRINT_BYTE_SIGNED, SCC_NEWGRF_PRINT_DWORD_CURRENCY, SCC_NEWGRF_PRINT_DWORD_DATE_LONG, SCC_NEWGRF_PRINT_DWORD_DATE_SHORT, SCC_NEWGRF_PRINT_DWORD_HEX, SCC_NEWGRF_PRINT_DWORD_SIGNED, SCC_NEWGRF_PRINT_QWORD_CURRENCY, SCC_NEWGRF_PRINT_QWORD_HEX, SCC_NEWGRF_PRINT_WORD_DATE_LONG, SCC_NEWGRF_PRINT_WORD_DATE_SHORT, SCC_NEWGRF_PRINT_WORD_HEX, SCC_NEWGRF_PRINT_WORD_POWER, SCC_NEWGRF_PRINT_WORD_SIGNED, SCC_NEWGRF_PRINT_WORD_SPEED, SCC_NEWGRF_PRINT_WORD_STATION_NAME, SCC_NEWGRF_PRINT_WORD_STRING_ID, SCC_NEWGRF_PRINT_WORD_UNSIGNED, SCC_NEWGRF_PRINT_WORD_VOLUME_LONG, SCC_NEWGRF_PRINT_WORD_VOLUME_SHORT, SCC_NEWGRF_PRINT_WORD_WEIGHT_LONG, SCC_NEWGRF_PRINT_WORD_WEIGHT_SHORT, SCC_NEWGRF_PUSH_WORD, SCC_NEWGRF_ROTATE_TOP_4_WORDS, SCC_NEWGRF_UNPRINT, and TTDPStringIDToOTTDStringIDMapping().

Referenced by FormatString().

void RestoreTextRefStackBackup | ( | struct TextRefStack * | backup | ) |

Restore a copy of the text stack to the used stack.

Parameters:

  | backup | The copy to restore.

Definition at line 970 of file newgrf_text.cpp.

Referenced by FormatString().

void SetCurrentGrfLangID | ( | byte | language_id | ) |

Equivalence Setter function between game and newgrf langID.

This function will adjust _currentLangID as to what is the LangID of the current language set by the user. This function is called after the user changed language, from strings.cpp:ReadLanguagePack

Parameters:

  | language_id | iso code of current selection

Definition at line 831 of file newgrf_text.cpp.

References _currentLangID.

Referenced by ReadLanguagePack().

void StartTextRefStackUsage | ( | byte | numEntries,
|  | const uint32 * | values |
| ) |  |  |

Start using the TTDP compatible string code parsing.

On start a number of values is copied on the TextRefStack. You can then use GetString() and the normal string drawing functions, and they will use the TextRefStack for NewGRF string codes.

However, when you want to draw a string multiple times using the same stack, you have to call RewindTextRefStack() between draws.

After you are done with drawing, you must disable usage of the TextRefStack by calling StopTextRefStackUsage(), so NewGRF string codes operate on the normal string parameters again.

Parameters:

  | numEntries | number of entries to copy from the registers
  | values | values to copy onto the stack; if NULL the temporary NewGRF registers will be used instead

Definition at line 993 of file newgrf_text.cpp.

References GB(), and TemporaryStorageArray< TYPE, SIZE >::GetValue().

Referenced by IndustryViewWindow::DrawInfo(), BuildObjectWindow::DrawWidget(), BuildIndustryWindow::DrawWidget(), ErrorMessageData::ErrorMessageData(), GetCargoSuffix(), ShowAdditionalText(), and ShowErrorMessage().

char* TranslateTTDPatchCodes | ( | uint32 | grfid,
|  | uint8 | language_id,
|  | bool | allow_newlines,
|  | const char * | str,
|  | int * | olen,
|  | StringControlCode | byte80 |
| ) |  |  |

Translate TTDPatch string codes into something OpenTTD can handle (better).

Parameters:

  | grfid | The (NewGRF) ID associated with this string
  | language_id | The (NewGRF) language ID associated with this string.
  | allow_newlines | Whether newlines are allowed in the string or not.
  | str | The string to translate.
  `[out]` | olen | The length of the final string.
  | byte80 | The control code to use as replacement for the 0x80-value.

Returns:
  The translated string.

Definition at line 400 of file newgrf_text.cpp.

References SmallMap< T, U, S >::Contains(), CS_ALPHANUMERAL, UnmappedChoiceList::Flush(), GB(), LanguageMap::GetLanguageMap(), LanguageMap::GetMapping(), grfmsg(), IsValidChar(), MapGRFStringID(), NFO_UTF8_IDENTIFIER, ReallocT(), SCC_BIGFONT, SCC_NEWGRF_DISCARD_WORD, SCC_NEWGRF_PRINT_BYTE_HEX, SCC_NEWGRF_PRINT_DWORD_DATE_LONG, SCC_NEWGRF_PRINT_DWORD_HEX, SCC_NEWGRF_PRINT_DWORD_SIGNED, SCC_NEWGRF_PRINT_QWORD_CURRENCY, SCC_NEWGRF_PRINT_QWORD_HEX, SCC_NEWGRF_PRINT_WORD_DATE_LONG, SCC_NEWGRF_PRINT_WORD_HEX, SCC_NEWGRF_PRINT_WORD_STATION_NAME, SCC_NEWGRF_PRINT_WORD_VOLUME_LONG, SCC_NEWGRF_PRINT_WORD_WEIGHT_LONG, SCC_NEWGRF_PUSH_WORD, SCC_NEWGRF_ROTATE_TOP_4_WORDS, SCC_NEWGRF_STRINL, SCC_NEWGRF_UNPRINT, SCC_TINYFONT, UnmappedChoiceList::strings, Utf8Decode(), Utf8Encode(), and Utf8EncodedCharLen().

Referenced by AddGRFString(), AddGRFTextToList(), and FeatureTownName().

StringID TTDPStringIDToOTTDStringIDMapping | ( | StringID | str | ) |

Perform a mapping from TTDPatch's string IDs to OpenTTD's string IDs, but only for the ones we are aware off; the rest like likely unused and will show a warning.

Parameters:

  | str | the string ID to convert

Returns:
  the converted string ID

Definition at line 45 of file newgrf_text.cpp.

References DEBUG, and IsInsideMM().

Referenced by MapGRFStringID(), and RemapNewGRFStringControlCode().

bool UsingNewGRFTextStack | ( |  | ) |

Check whether the NewGRF text stack is in use.

Returns:
  True iff the NewGRF text stack is used.

Definition at line 952 of file newgrf_text.cpp.

Referenced by FormatString().