OpenTTD Source  12.0-beta2
newgrf_text.cpp File Reference
#include "stdafx.h"
#include <array>
#include "newgrf.h"
#include "strings_func.h"
#include "newgrf_storage.h"
#include "newgrf_text.h"
#include "newgrf_cargo.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 <sstream>
#include <map>
#include "table/strings.h"
#include "table/control_codes.h"
#include "safeguards.h"

Go to the source code of this file.

Data Structures

struct  GRFTextEntry
 Holder of the above structure. More...
 
struct  UnmappedChoiceList
 Helper structure for mapping choice lists. More...
 
struct  TextRefStack
 

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

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). More...
 
static void AddGRFTextToList (GRFTextList &list, byte langid, const std::string &text_to_add)
 Add a new text to a GRFText list. More...
 
void AddGRFTextToList (GRFTextList &list, byte langid, uint32 grfid, bool allow_newlines, const char *text_to_add)
 Add a string to a GRFText list. More...
 
void AddGRFTextToList (GRFTextWrapper &list, byte langid, uint32 grfid, bool allow_newlines, const char *text_to_add)
 Add a string to a GRFText list. More...
 
void AddGRFTextToList (GRFTextWrapper &list, const std::string &text_to_add)
 Add a GRFText to a GRFText list. More...
 
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, StringID stringid)
 Returns the index for this stringid associated with its grfID.
 
const char * GetGRFStringFromGRFText (const GRFTextList &text_list)
 Get a C-string from a GRFText-list. More...
 
const char * GetGRFStringFromGRFText (const GRFTextWrapper &text)
 Get a C-string from a GRFText-list. More...
 
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. More...
 
bool CheckGrfLangID (byte lang_id, byte grf_version)
 
void CleanUpStrings ()
 House cleaning. More...
 
bool UsingNewGRFTextStack ()
 Check whether the NewGRF text stack is in use. More...
 
struct TextRefStackCreateTextRefStackBackup ()
 Create a backup of the current NewGRF text stack. More...
 
void RestoreTextRefStackBackup (struct TextRefStack *backup)
 Restore a copy of the text stack to the used stack. More...
 
void StartTextRefStackUsage (const GRFFile *grffile, byte numEntries, const uint32 *values)
 Start using the TTDP compatible string code parsing. More...
 
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, uint argv_size, bool modify_argv)
 FormatString for NewGRF specific "magic" string control codes. More...
 

Variables

static uint _num_grf_texts = 0
 
static GRFTextEntry _grf_text [TAB_SIZE_NEWGRF]
 
static byte _currentLangID = GRFLX_ENGLISH
 by default, english is used.
 
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 functionality had to be set.

Definition in file newgrf_text.cpp.

Enumeration Type Documentation

◆ 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 46 of file newgrf_text.cpp.

Function Documentation

◆ AddGRFTextToList() [1/4]

static void AddGRFTextToList ( GRFTextList list,
byte  langid,
const std::string &  text_to_add 
)
static

Add a new text to a GRFText list.

Parameters
listThe list where the text should be added to.
langidThe The language of the new text.
text_to_addThe text to add to the list.

Definition at line 493 of file newgrf_text.cpp.

Referenced by AddGRFTextToList(), ChangeGRFDescription(), ChangeGRFName(), ChangeGRFParamDescription(), ChangeGRFParamName(), ChangeGRFParamValueNames(), ChangeGRFURL(), and HandleIncomingNetworkGameInfoGRFConfig().

◆ AddGRFTextToList() [2/4]

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

Add a string to a GRFText list.

Parameters
listThe list where the text should be added to.
langidThe language of the new text.
grfidThe grfid where this string is defined.
allow_newlinesWhether newlines are allowed in this string.
text_to_addThe text to add to the list.
Note
All text-codes will be translated.

Definition at line 516 of file newgrf_text.cpp.

References AddGRFTextToList(), and TranslateTTDPatchCodes().

◆ AddGRFTextToList() [3/4]

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

Add a string to a GRFText list.

Parameters
listThe list where the text should be added to.
langidThe language of the new text.
grfidThe grfid where this string is defined.
allow_newlinesWhether newlines are allowed in this string.
text_to_addThe text to add to the list.
Note
All text-codes will be translated.

Definition at line 530 of file newgrf_text.cpp.

References AddGRFTextToList().

◆ AddGRFTextToList() [4/4]

void AddGRFTextToList ( GRFTextWrapper list,
const std::string &  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
listThe list where the text should be added to.
text_to_addThe text to add to the list.

Definition at line 542 of file newgrf_text.cpp.

References AddGRFTextToList().

◆ CleanUpStrings()

void CleanUpStrings ( )

House cleaning.

Remove all strings and reset the text counter.

Definition at line 695 of file newgrf_text.cpp.

Referenced by ResetNewGRFData().

◆ CreateTextRefStackBackup()

struct TextRefStack* CreateTextRefStackBackup ( )

Create a backup of the current NewGRF text stack.

Returns
A copy of the current text stack.

Definition at line 788 of file newgrf_text.cpp.

References _newgrf_textrefstack.

Referenced by FormatString().

◆ GetGRFStringFromGRFText() [1/2]

const char* GetGRFStringFromGRFText ( const GRFTextList text_list)

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 nullptr is returned.

Parameters
text_listThe GRFTextList to get the string from.

Definition at line 620 of file newgrf_text.cpp.

References _currentLangID.

Referenced by NewGRFParametersWindow::DrawWidget(), GRFConfig::GetDescription(), GetGRFStringFromGRFText(), GRFConfig::GetName(), and GRFConfig::GetURL().

◆ GetGRFStringFromGRFText() [2/2]

const char* GetGRFStringFromGRFText ( const GRFTextWrapper 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 nullptr is returned.

Parameters
textThe GRFTextList to get the string from.

Definition at line 645 of file newgrf_text.cpp.

References GetGRFStringFromGRFText().

◆ RemapNewGRFStringControlCode()

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

◆ RestoreTextRefStackBackup()

void RestoreTextRefStackBackup ( struct TextRefStack backup)

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

Parameters
backupThe copy to restore.

Definition at line 797 of file newgrf_text.cpp.

References _newgrf_textrefstack.

Referenced by FormatString().

◆ SetCurrentGrfLangID()

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_idiso code of current selection

Definition at line 672 of file newgrf_text.cpp.

References _currentLangID.

◆ StartTextRefStackUsage()

void StartTextRefStackUsage ( const GRFFile grffile,
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
grffilethe NewGRF providing the stack data
numEntriesnumber of entries to copy from the registers
valuesvalues to copy onto the stack; if nullptr the temporary NewGRF registers will be used instead

Definition at line 821 of file newgrf_text.cpp.

Referenced by ErrorMessageData::CopyOutDParams(), ErrmsgWindow::DrawWidget(), GetCargoSuffix(), and ErrmsgWindow::UpdateWidgetSize().

◆ 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).

Parameters
grfidThe (NewGRF) ID associated with this string
language_idThe (NewGRF) language ID associated with this string.
allow_newlinesWhether newlines are allowed in the string or not.
strThe string to translate.
byte80The control code to use as replacement for the 0x80-value.
Returns
The translated string.

Definition at line 241 of file newgrf_text.cpp.

References NFO_UTF8_IDENTIFIER, Utf8Decode(), and Utf8EncodedCharLen().

Referenced by AddGRFTextToList().

◆ UsingNewGRFTextStack()

bool UsingNewGRFTextStack ( )

Check whether the NewGRF text stack is in use.

Returns
True iff the NewGRF text stack is used.

Definition at line 779 of file newgrf_text.cpp.

References _newgrf_textrefstack.

Referenced by FormatString().