OpenTTD Source 20250501-master-ga30f251d05
newgrf_text.cpp File Reference

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

#include "stdafx.h"
#include "debug.h"
#include "newgrf.h"
#include "strings_internal.h"
#include "newgrf_storage.h"
#include "newgrf_text.h"
#include "newgrf_cargo.h"
#include "string_func.h"
#include "timer/timer_game_calendar.h"
#include "core/alloc_type.hpp"
#include "core/string_builder.hpp"
#include "core/string_consumer.hpp"
#include "language.h"
#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 : uint8_t {
  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 : uint8_t {
  GRFLX_AMERICAN = 0x00 , GRFLX_ENGLISH = 0x01 , GRFLX_GERMAN = 0x02 , GRFLX_FRENCH = 0x03 ,
  GRFLX_SPANISH = 0x04 , GRFLX_UNSPECIFIED = 0x7F
}
 

Functions

std::string TranslateTTDPatchCodes (uint32_t grfid, uint8_t language_id, bool allow_newlines, std::string_view str, StringControlCode byte80)
 Translate TTDPatch string codes into something OpenTTD can handle (better).
 
static void AddGRFTextToList (GRFTextList &list, uint8_t langid, std::string_view text_to_add)
 Add a new text to a GRFText list.
 
void AddGRFTextToList (GRFTextList &list, uint8_t langid, uint32_t grfid, bool allow_newlines, std::string_view text_to_add)
 Add a string to a GRFText list.
 
void AddGRFTextToList (GRFTextWrapper &list, uint8_t langid, uint32_t grfid, bool allow_newlines, std::string_view text_to_add)
 Add a string to a GRFText list.
 
void AddGRFTextToList (GRFTextWrapper &list, std::string_view text_to_add)
 Add a GRFText to a GRFText list.
 
StringID AddGRFString (uint32_t grfid, GRFStringID stringid, uint8_t langid_to_add, bool new_scheme, bool allow_newlines, std::string_view text_to_add, StringID def_string)
 Add the new read string into our structure.
 
StringID GetGRFStringID (uint32_t grfid, GRFStringID stringid)
 Returns the index for this stringid associated with its grfID.
 
std::optional< std::string_view > GetGRFStringFromGRFText (const GRFTextList &text_list)
 Get a C-string from a GRFText-list.
 
std::optional< std::string_view > GetGRFStringFromGRFText (const GRFTextWrapper &text)
 Get a C-string from a GRFText-list.
 
std::string_view GetGRFStringPtr (StringIndexInTab stringid)
 Get a C-string from a stringid set by a newgrf.
 
void SetCurrentGrfLangID (uint8_t language_id)
 Equivalence Setter function between game and newgrf langID.
 
bool CheckGrfLangID (uint8_t lang_id, uint8_t grf_version)
 
void CleanUpStrings ()
 House cleaning.
 
static void HandleNewGRFStringControlCodes (std::string_view str, TextRefStack &stack, std::vector< StringParameter > &params)
 Handle control codes in a NewGRF string, processing the stack and filling parameters.
 
static void ProcessNewGRFStringControlCode (char32_t scc, StringConsumer &consumer, TextRefStack &stack, std::vector< StringParameter > &params)
 Process NewGRF string control code instructions.
 
char32_t RemapNewGRFStringControlCode (char32_t scc, StringConsumer &consumer)
 Emit OpenTTD's internal string code for the different NewGRF string codes.
 
std::vector< StringParameterGetGRFSringTextStackParameters (const GRFFile *grffile, StringID stringid, uint8_t num_entries)
 Process the text ref stack for a GRF String and return its parameters.
 
std::string GetGRFStringWithTextStack (const struct GRFFile *grffile, GRFStringID grfstringid, uint8_t num_entries)
 Format a GRF string using the text ref stack for parameters.
 

Variables

static ReferenceThroughBaseContainer< std::vector< GRFTextEntry > > _grf_text
 
static uint8_t _current_lang_id = GRFLX_ENGLISH
 by default, english is used.
 

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

enum GRFBaseLanguages : uint8_t

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

◆ GRFExtendedLanguages

enum GRFExtendedLanguages : uint8_t

Definition at line 53 of file newgrf_text.cpp.

Function Documentation

◆ AddGRFString()

StringID AddGRFString ( uint32_t  grfid,
GRFStringID  stringid,
uint8_t  langid_to_add,
bool  new_scheme,
bool  allow_newlines,
std::string_view  text_to_add,
StringID  def_string 
)

Add the new read string into our structure.

Definition at line 534 of file newgrf_text.cpp.

References AddGRFString(), AddGRFTextToList(), MakeStringID(), TAB_SIZE_NEWGRF, TEXT_TAB_NEWGRF_START, and TranslateTTDPatchCodes().

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

◆ AddGRFTextToList() [1/4]

static void AddGRFTextToList ( GRFTextList list,
uint8_t  langid,
std::string_view  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 476 of file newgrf_text.cpp.

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

◆ AddGRFTextToList() [2/4]

void AddGRFTextToList ( GRFTextList list,
uint8_t  langid,
uint32_t  grfid,
bool  allow_newlines,
std::string_view  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 499 of file newgrf_text.cpp.

References AddGRFTextToList(), and TranslateTTDPatchCodes().

◆ AddGRFTextToList() [3/4]

void AddGRFTextToList ( GRFTextWrapper list,
std::string_view  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 525 of file newgrf_text.cpp.

References AddGRFTextToList().

◆ AddGRFTextToList() [4/4]

void AddGRFTextToList ( GRFTextWrapper list,
uint8_t  langid,
uint32_t  grfid,
bool  allow_newlines,
std::string_view  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 513 of file newgrf_text.cpp.

References AddGRFTextToList().

◆ CheckGrfLangID()

bool CheckGrfLangID ( uint8_t  lang_id,
uint8_t  grf_version 
)

Definition at line 655 of file newgrf_text.cpp.

◆ CleanUpStrings()

void CleanUpStrings ( )

House cleaning.

Remove all strings.

Definition at line 673 of file newgrf_text.cpp.

Referenced by ResetNewGRFData().

◆ GetGRFSringTextStackParameters()

std::vector< StringParameter > GetGRFSringTextStackParameters ( const GRFFile grffile,
StringID  stringid,
uint8_t  num_entries 
)

Process the text ref stack for a GRF String and return its parameters.

Parameters
grffileGRFFile of string.
stringidStringID of string.
num_entriesNumber of temporary storage registers to import.
Returns
Parameters for GRF string.

Definition at line 966 of file newgrf_text.cpp.

References HandleNewGRFStringControlCodes(), and INVALID_STRING_ID.

Referenced by GetErrorMessageFromLocationCallbackResult(), and GetGRFStringWithTextStack().

◆ GetGRFStringFromGRFText() [1/2]

std::optional< std::string_view > 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 597 of file newgrf_text.cpp.

References _current_lang_id.

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

◆ GetGRFStringFromGRFText() [2/2]

std::optional< std::string_view > 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 622 of file newgrf_text.cpp.

References GetGRFStringFromGRFText().

◆ GetGRFStringID()

StringID GetGRFStringID ( uint32_t  grfid,
GRFStringID  stringid 
)

◆ GetGRFStringPtr()

std::string_view GetGRFStringPtr ( StringIndexInTab  stringid)

Get a C-string from a stringid set by a newgrf.

Definition at line 630 of file newgrf_text.cpp.

References GetGRFStringFromGRFText().

Referenced by GetStringWithArgs().

◆ GetGRFStringWithTextStack()

std::string GetGRFStringWithTextStack ( const struct GRFFile grffile,
GRFStringID  grfstringid,
uint8_t  num_entries 
)

Format a GRF string using the text ref stack for parameters.

Parameters
grffileGRFFile of string.
grfstringidGRFStringID of string.
num_entriesNumber of temporary storage registers to import.
Returns
Formatted string.

Definition at line 988 of file newgrf_text.cpp.

References GetGRFSringTextStackParameters(), GetGRFStringID(), and GetStringWithArgs().

Referenced by IndustryViewWindow::DrawInfo(), BuildIndustryWindow::DrawWidget(), BuildObjectWindow::DrawWidget(), FormatString(), GetCargoSuffix(), and GetNewGRFAdditionalText().

◆ HandleNewGRFStringControlCodes()

static void HandleNewGRFStringControlCodes ( std::string_view  str,
TextRefStack stack,
std::vector< StringParameter > &  params 
)
static

Handle control codes in a NewGRF string, processing the stack and filling parameters.

Parameters
strString to process.
[in,out]stackStack to use.
[out]paramsParameters to fill.

Definition at line 950 of file newgrf_text.cpp.

References StringConsumer::AnyBytesLeft(), ProcessNewGRFStringControlCode(), and StringConsumer::ReadUtf8().

Referenced by GetGRFSringTextStackParameters(), and ProcessNewGRFStringControlCode().

◆ ProcessNewGRFStringControlCode()

static void ProcessNewGRFStringControlCode ( char32_t  scc,
StringConsumer consumer,
TextRefStack stack,
std::vector< StringParameter > &  params 
)
static

Process NewGRF string control code instructions.

Parameters
sccThe string control code that has been read.
consumerThe string that we are reading from.
stackThe TextRefStack.
[out]paramsOutput parameters

Definition at line 753 of file newgrf_text.cpp.

References TimerGameConst< struct Calendar >::DAYS_TILL_ORIGINAL_BASE_YEAR, GetCargoTranslation(), HandleNewGRFStringControlCodes(), MapGRFStringID(), NUM_CARGO, StringConsumer::ReadUint16LE(), StringConsumer::ReadUint8(), StringConsumer::ReadUtf8(), 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_FORCE, SCC_NEWGRF_PRINT_DWORD_HEX, SCC_NEWGRF_PRINT_DWORD_SIGNED, SCC_NEWGRF_PRINT_QWORD_CURRENCY, SCC_NEWGRF_PRINT_QWORD_HEX, SCC_NEWGRF_PRINT_WORD_CARGO_LONG, SCC_NEWGRF_PRINT_WORD_CARGO_NAME, SCC_NEWGRF_PRINT_WORD_CARGO_SHORT, SCC_NEWGRF_PRINT_WORD_CARGO_TINY, 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_STRINL, StringConsumer::Skip(), StringConsumer::SkipUint16LE(), and StringConsumer::SkipUint8().

Referenced by HandleNewGRFStringControlCodes().

◆ RemapNewGRFStringControlCode()

◆ SetCurrentGrfLangID()

void SetCurrentGrfLangID ( uint8_t  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 650 of file newgrf_text.cpp.

References _current_lang_id.

Referenced by ReadLanguagePack().

◆ TranslateTTDPatchCodes()

std::string TranslateTTDPatchCodes ( uint32_t  grfid,
uint8_t  language_id,
bool  allow_newlines,
std::string_view  str,
StringControlCode  byte80 
)

Variable Documentation

◆ _current_lang_id

uint8_t _current_lang_id = GRFLX_ENGLISH
static

by default, english is used.

Definition at line 77 of file newgrf_text.cpp.

Referenced by GetGRFStringFromGRFText(), and SetCurrentGrfLangID().

◆ _grf_text

ReferenceThroughBaseContainer<std::vector<GRFTextEntry> > _grf_text
static

Definition at line 76 of file newgrf_text.cpp.