tron@2186: /* $Id$ */ tron@2186: belugas@6916: /** @file strings.cpp */ belugas@6916: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@2291: #include "currency.h" rubidium@9260: #include "namegen_func.h" rubidium@9281: #include "station_base.h" truelight@0: #include "town.h" tron@430: #include "screenshot.h" truelight@1542: #include "waypoint.h" ludde@2070: #include "industry.h" tron@2159: #include "variables.h" belugas@3601: #include "newgrf_text.h" belugas@4120: #include "music.h" belugas@4942: #include "industry.h" rubidium@7425: #include "fileio.h" peter1138@6417: #include "cargotype.h" rubidium@7139: #include "group.h" rubidium@7139: #include "debug.h" glx@7452: #include "newgrf_townname.h" rubidium@9286: #include "signs_base.h" peter1138@7555: #include "newgrf_engine.h" rubidium@8582: #include "spritecache.h" rubidium@8581: #include "fontcache.h" rubidium@8581: #include "gui.h" rubidium@8610: #include "strings_func.h" rubidium@8627: #include "functions.h" rubidium@8616: #include "core/endian_func.hpp" rubidium@8636: #include "date_func.h" rubidium@8640: #include "vehicle_base.h" rubidium@8710: #include "string_func.h" rubidium@8750: #include "player_func.h" rubidium@8750: #include "player_base.h" rubidium@8760: #include "fios.h" rubidium@8766: #include "settings_type.h" rubidium@8771: #include "video/video_driver.hpp" rubidium@9282: #include "engine_func.h" truelight@0: rubidium@8760: #include "table/strings.h" rubidium@8760: #include "table/control_codes.h" Darkvater@4219: rubidium@6646: DynamicLanguages _dynlang; tron@2201: char _userstring[128]; rubidium@7762: uint64 _decode_parameters[20]; tron@2201: Darkvater@4912: static char *StationGetSpecialString(char *buff, int x, const char* last); Darkvater@4912: static char *GetSpecialTownNameString(char *buff, int ind, uint32 seed, const char* last); rubidium@7498: static char *GetSpecialPlayerNameString(char *buff, int ind, const int64 *argv, const char* last); truelight@0: rubidium@7498: static char *FormatString(char *buff, const char *str, const int64 *argv, uint casei, const char* last); truelight@0: rubidium@6574: struct LanguagePack { belugas@6916: uint32 ident; // 32-bits identifier rubidium@4434: uint32 version; // 32-bits of auto generated version info which is basically a hash of strings.h rubidium@4434: char name[32]; // the international name of this language rubidium@4434: char own_name[32]; // the localized name of this language rubidium@4434: char isocode[16]; // the ISO code for the language (not country code) rubidium@4434: uint16 offsets[32]; // the offsets rubidium@4434: byte plural_form; // how to compute plural forms rubidium@4434: byte pad[3]; // pad header to be a multiple of 4 belugas@6916: char data[VARARRAY_SIZE]; // list of strings rubidium@6574: }; tron@1319: tron@1319: static char **_langpack_offs; tron@1319: static LanguagePack *_langpack; belugas@6916: static uint _langtab_num[32]; // Offset into langpack offs tron@1319: static uint _langtab_start[32]; // Offset into langpack offs truelight@0: truelight@0: belugas@6916: /** Read an int64 from the argv array. */ rubidium@7498: static inline int64 GetInt64(const int64 **argv) ludde@2063: { ludde@2063: assert(argv); ludde@2063: return *(*argv)++; ludde@2063: } ludde@2063: rubidium@7498: /** Read an int32 from the argv array. */ rubidium@7498: static inline int32 GetInt32(const int64 **argv) rubidium@7498: { rubidium@7498: return (int32)GetInt64(argv); rubidium@7498: } rubidium@7498: belugas@6916: /** Read an array from the argv array. */ rubidium@7498: static inline const int64 *GetArgvPtr(const int64 **argv, int n) ludde@2063: { rubidium@7498: const int64 *result; ludde@2063: assert(*argv); ludde@2063: result = *argv; ludde@2063: (*argv) += n; ludde@2063: return result; ludde@2063: } ludde@2063: ludde@2063: ludde@2055: #define NUM_BOUND_STRINGS 8 ludde@2055: belugas@6916: /* Array to hold the bound strings. */ ludde@2055: static const char *_bound_strings[NUM_BOUND_STRINGS]; ludde@2055: belugas@6916: /* This index is used to implement a "round-robin" allocating of belugas@6916: * slots for BindCString. NUM_BOUND_STRINGS slots are reserved. belugas@6916: * Which means that after NUM_BOUND_STRINGS calls to BindCString, belugas@6916: * the indices will be reused. */ ludde@2055: static int _bind_index; ludde@2055: peter1138@8941: const char *GetStringPtr(StringID string) truelight@0: { peter1138@8941: switch (GB(string, 11, 5)) { peter1138@8941: case 28: return GetGRFStringPtr(GB(string, 0, 11)); peter1138@8941: case 29: return GetGRFStringPtr(GB(string, 0, 11) + 0x0800); peter1138@8941: case 30: return GetGRFStringPtr(GB(string, 0, 11) + 0x1000); peter1138@8941: default: return _langpack_offs[_langtab_start[string >> 11] + (string & 0x7FF)]; peter1138@8941: } truelight@0: } truelight@0: belugas@6916: /** The highest 8 bits of string contain the "case index". belugas@6916: * These 8 bits will only be set when FormatString wants to print belugas@6916: * the string in a different case. No one else except FormatString belugas@6916: * should set those bits, therefore string CANNOT be StringID, but uint32. belugas@6916: * @param buffr belugas@6916: * @param string belugas@6916: * @param argv belugas@6916: * @param last belugas@6916: * @return a formatted string of char belugas@6916: */ rubidium@7498: static char *GetStringWithArgs(char *buffr, uint string, const int64 *argv, const char* last) truelight@0: { rubidium@8306: if (GB(string, 0, 16) == 0) return GetStringWithArgs(buffr, STR_UNDEFINED, argv, last); rubidium@8306: tron@2140: uint index = GB(string, 0, 11); tron@2140: uint tab = GB(string, 11, 5); truelight@0: tron@1321: switch (tab) { tron@1321: case 4: ludde@2063: if (index >= 0xC0) Darkvater@4912: return GetSpecialTownNameString(buffr, index - 0xC0, GetInt32(&argv), last); tron@1321: break; tron@1321: tron@1321: case 14: ludde@2063: if (index >= 0xE4) Darkvater@4912: return GetSpecialPlayerNameString(buffr, index - 0xE4, argv, last); tron@1321: break; tron@1321: tron@1321: case 15: peter1138@8941: /* Old table for custom names. This is no longer used */ peter1138@8941: error("Incorrect conversion of custom name string."); tron@1321: peter1138@4710: case 26: peter1138@4710: /* Include string within newgrf text (format code 81) */ skidd13@8424: if (HasBit(index, 10)) { peter1138@4710: StringID string = GetGRFStringID(0, 0xD000 + GB(index, 0, 10)); Darkvater@4912: return GetStringWithArgs(buffr, string, argv, last); peter1138@4710: } peter1138@4710: break; peter1138@4710: belugas@3601: case 28: peter1138@8941: return FormatString(buffr, GetGRFStringPtr(index), argv, 0, last); belugas@3601: belugas@3601: case 29: peter1138@8941: return FormatString(buffr, GetGRFStringPtr(index + 0x0800), argv, 0, last); belugas@3601: belugas@3601: case 30: peter1138@8941: return FormatString(buffr, GetGRFStringPtr(index + 0x1000), argv, 0, last); belugas@3601: ludde@2063: case 31: belugas@6916: /* dynamic strings. These are NOT to be passed through the formatter, belugas@6916: * but passed through verbatim. */ ludde@2055: if (index < (STR_SPEC_USERSTRING & 0x7FF)) { Darkvater@4912: return strecpy(buffr, _bound_strings[index], last); ludde@2055: } tron@1321: Darkvater@4912: return FormatString(buffr, _userstring, NULL, 0, last); truelight@0: } truelight@0: tron@2639: if (index >= _langtab_num[tab]) { tron@1321: error( tron@1321: "!String 0x%X is invalid. " tron@1321: "Probably because an old version of the .lng file.\n", string tron@1321: ); tron@2639: } truelight@0: Darkvater@4912: return FormatString(buffr, GetStringPtr(GB(string, 0, 16)), argv, GB(string, 24, 8), last); truelight@0: } truelight@0: Darkvater@4912: char *GetString(char *buffr, StringID string, const char* last) ludde@2063: { rubidium@7498: return GetStringWithArgs(buffr, string, (int64*)_decode_parameters, last); ludde@2063: } ludde@2063: ludde@2063: peter1138@5108: char *InlineString(char *buf, StringID string) peter1138@5108: { peter1138@5108: buf += Utf8Encode(buf, SCC_STRING_ID); peter1138@5108: buf += Utf8Encode(buf, string); peter1138@5108: return buf; peter1138@5108: } peter1138@5108: peter1138@5108: rubidium@6484: /** rubidium@6484: * This function takes a C-string and allocates a temporary string ID. rubidium@6484: * The StringID of the bound string is valid until BindCString is called rubidium@6484: * another NUM_BOUND_STRINGS times. So be careful when using it. belugas@6916: * @param str temp string to add belugas@6916: * @return the id of that temp string rubidium@6484: * @note formatting a DATE_TINY calls BindCString twice, thus reduces the rubidium@6484: * amount of 'user' bound strings by 2. rubidium@6484: * @todo rewrite the BindCString system to make the limit flexible and rubidium@6484: * non-round-robin. For example by using smart pointers that free rubidium@6484: * the allocated StringID when they go out-of-scope/are freed. rubidium@6484: */ ludde@2055: StringID BindCString(const char *str) ludde@2055: { ludde@2055: int idx = (++_bind_index) & (NUM_BOUND_STRINGS - 1); ludde@2055: _bound_strings[idx] = str; ludde@2055: return idx + STR_SPEC_DYNSTRING; ludde@2055: } ludde@2055: belugas@6916: /** This function is used to "bind" a C string to a OpenTTD dparam slot. belugas@6916: * @param n slot of the string belugas@6916: * @param str string to bind belugas@6916: */ ludde@2055: void SetDParamStr(uint n, const char *str) ludde@2055: { ludde@2055: SetDParam(n, BindCString(str)); ludde@2055: } ludde@2055: tron@1309: void InjectDParam(int amount) truelight@0: { rubidium@7502: memmove(_decode_parameters + amount, _decode_parameters, sizeof(_decode_parameters) - amount * sizeof(uint64)); truelight@0: } truelight@0: Darkvater@4912: // TODO rubidium@7852: static char *FormatCommaNumber(char *buff, int64 number, const char *last) truelight@0: { rubidium@7852: uint64 divisor = 10000000000000000000ULL; rubidium@7852: uint64 quot; truelight@0: int i; rubidium@7852: uint64 tot; rubidium@7852: uint64 num; truelight@0: truelight@0: if (number < 0) { truelight@0: *buff++ = '-'; truelight@0: number = -number; truelight@0: } truelight@0: truelight@0: num = number; truelight@0: truelight@0: tot = 0; rubidium@7852: for (i = 0; i < 20; i++) { truelight@0: quot = 0; rubidium@7852: if (num >= divisor) { rubidium@7852: quot = num / divisor; rubidium@7852: num = num % divisor; truelight@0: } rubidium@7852: if (tot |= quot || i == 19) { tron@1312: *buff++ = '0' + quot; rubidium@7852: if ((i % 3) == 1 && i != 19) *buff++ = ','; truelight@0: } rubidium@7852: rubidium@7852: divisor /= 10; truelight@0: } truelight@0: tron@1316: *buff = '\0'; truelight@0: truelight@0: return buff; truelight@0: } truelight@0: Darkvater@4912: // TODO rubidium@7852: static char *FormatNoCommaNumber(char *buff, int64 number, const char *last) truelight@0: { rubidium@7852: uint64 divisor = 10000000000000000000ULL; rubidium@7852: uint64 quot; truelight@0: int i; rubidium@7852: uint64 tot; rubidium@7852: uint64 num; truelight@0: truelight@0: if (number < 0) { Darkvater@4912: buff = strecpy(buff, "-", last); truelight@0: number = -number; truelight@0: } truelight@0: truelight@0: num = number; truelight@0: truelight@0: tot = 0; rubidium@7852: for (i = 0; i < 20; i++) { truelight@0: quot = 0; rubidium@7852: if (num >= divisor) { rubidium@7852: quot = num / divisor; rubidium@7852: num = num % divisor; truelight@0: } rubidium@7852: if (tot |= quot || i == 19) { tron@1312: *buff++ = '0' + quot; truelight@0: } rubidium@7852: rubidium@7852: divisor /= 10; truelight@0: } truelight@0: tron@1316: *buff = '\0'; truelight@0: truelight@0: return buff; truelight@0: } truelight@0: truelight@0: Darkvater@4912: static char *FormatYmdString(char *buff, Date date, const char* last) truelight@0: { truelight@0: YearMonthDay ymd; rubidium@4289: ConvertDateToYMD(date, &ymd); truelight@0: rubidium@7498: int64 args[3] = { ymd.day + STR_01AC_1ST - 1, STR_0162_JAN + ymd.month, ymd.year }; rubidium@6484: return FormatString(buff, GetStringPtr(STR_DATE_LONG), args, 0, last); truelight@0: } truelight@0: Darkvater@4912: static char *FormatMonthAndYear(char *buff, Date date, const char* last) truelight@0: { truelight@0: YearMonthDay ymd; rubidium@4289: ConvertDateToYMD(date, &ymd); truelight@0: rubidium@7498: int64 args[2] = { STR_MONTH_JAN + ymd.month, ymd.year }; rubidium@6484: return FormatString(buff, GetStringPtr(STR_DATE_SHORT), args, 0, last); truelight@0: } truelight@0: Darkvater@4912: static char *FormatTinyDate(char *buff, Date date, const char* last) dominik@1097: { dominik@1097: YearMonthDay ymd; rubidium@4289: ConvertDateToYMD(date, &ymd); dominik@1097: rubidium@6484: char day[3]; rubidium@6484: char month[3]; rubidium@6484: /* We want to zero-pad the days and months */ rubidium@6484: snprintf(day, lengthof(day), "%02i", ymd.day); rubidium@6484: snprintf(month, lengthof(month), "%02i", ymd.month + 1); rubidium@6484: rubidium@7498: int64 args[3] = { BindCString(day), BindCString(month), ymd.year }; rubidium@6484: return FormatString(buff, GetStringPtr(STR_DATE_TINY), args, 0, last); dominik@1097: } dominik@1097: rubidium@7492: static char *FormatGenericCurrency(char *buff, const CurrencySpec *spec, Money number, bool compact, const char* last) truelight@0: { rubidium@8259: /* We are going to make number absolute for printing, so rubidium@8259: * keep this piece of data as we need it later on */ rubidium@8259: bool negative = number < 0; rubidium@8259: const char *multiplier = ""; Darkvater@4912: char buf[40]; rubidium@8259: char *p; truelight@0: int j; truelight@0: rubidium@8259: number *= spec->rate; truelight@0: belugas@6916: /* convert from negative */ tron@1316: if (number < 0) { rubidium@7918: if (buff + Utf8CharLen(SCC_RED) > last) return buff; rubidium@7918: buff += Utf8Encode(buff, SCC_RED); Darkvater@4912: buff = strecpy(buff, "-", last); tron@1316: number = -number; tron@1316: } truelight@193: belugas@4602: /* Add prefix part, folowing symbol_pos specification. belugas@4602: * Here, it can can be either 0 (prefix) or 2 (both prefix anf suffix). belugas@4602: * The only remaining value is 1 (suffix), so everything that is not 1 */ Darkvater@4912: if (spec->symbol_pos != 1) buff = strecpy(buff, spec->prefix, last); truelight@0: belugas@6916: /* for huge numbers, compact the number into k or M */ truelight@0: if (compact) { truelight@0: if (number >= 1000000000) { truelight@0: number = (number + 500000) / 1000000; Darkvater@4912: multiplier = "M"; truelight@0: } else if (number >= 1000000) { truelight@0: number = (number + 500) / 1000; Darkvater@4912: multiplier = "k"; truelight@193: } truelight@0: } truelight@193: belugas@6916: /* convert to ascii number and add commas */ Darkvater@4912: p = endof(buf); Darkvater@4912: *--p = '\0'; truelight@0: j = 4; truelight@193: do { tron@1316: if (--j == 0) { Darkvater@4912: *--p = spec->separator; tron@1316: j = 3; tron@1316: } rubidium@8259: *--p = '0' + (char)(number % 10); Darkvater@4912: } while ((number /= 10) != 0); Darkvater@4912: buff = strecpy(buff, p, last); truelight@0: Darkvater@4912: buff = strecpy(buff, multiplier, last); truelight@0: belugas@4602: /* Add suffix part, folowing symbol_pos specification. belugas@4602: * Here, it can can be either 1 (suffix) or 2 (both prefix anf suffix). belugas@4602: * The only remaining value is 1 (prefix), so everything that is not 0 */ Darkvater@4912: if (spec->symbol_pos != 0) buff = strecpy(buff, spec->suffix, last); truelight@0: rubidium@8259: if (negative) { rubidium@7918: if (buff + Utf8CharLen(SCC_PREVIOUS_COLOUR) > last) return buff; rubidium@7918: buff += Utf8Encode(buff, SCC_PREVIOUS_COLOUR); rubidium@7918: *buff = '\0'; rubidium@7918: } rubidium@7918: truelight@0: return buff; truelight@0: } truelight@0: maedhros@9231: static int DeterminePluralForm(int64 count) ludde@2082: { belugas@6916: /* The absolute value determines plurality */ maedhros@9231: uint64 n = abs(count); ludde@2082: tron@2952: switch (_langpack->plural_form) { maedhros@9231: default: maedhros@9231: NOT_REACHED(); ludde@2082: maedhros@9231: /* Two forms, singular used for one only maedhros@9231: * Used in: maedhros@9231: * Danish, Dutch, English, German, Norwegian, Swedish, Estonian, Finnish, maedhros@9231: * Greek, Hebrew, Italian, Portuguese, Spanish, Esperanto */ maedhros@9231: case 0: maedhros@9231: return n != 1; ludde@2082: maedhros@9231: /* Only one form maedhros@9231: * Used in: maedhros@9231: * Hungarian, Japanese, Korean, Turkish */ maedhros@9231: case 1: maedhros@9231: return 0; ludde@2082: maedhros@9231: /* Two forms, singular used for zero and one maedhros@9231: * Used in: maedhros@9231: * French, Brazilian Portuguese */ maedhros@9231: case 2: maedhros@9231: return n > 1; ludde@2082: maedhros@9231: /* Three forms, special case for zero maedhros@9231: * Used in: maedhros@9231: * Latvian */ maedhros@9231: case 3: maedhros@9231: return n % 10 == 1 && n % 100 != 11 ? 0 : n != 0 ? 1 : 2; maedhros@9229: maedhros@9231: /* Three forms, special case for one and two maedhros@9231: * Used in: maedhros@9231: * Gaelige (Irish) */ maedhros@9231: case 4: maedhros@9231: return n == 1 ? 0 : n == 2 ? 1 : 2; maedhros@9231: maedhros@9231: /* Three forms, special case for numbers ending in 1[2-9] maedhros@9231: * Used in: maedhros@9231: * Lithuanian */ maedhros@9231: case 5: maedhros@9231: return n % 10 == 1 && n % 100 != 11 ? 0 : n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; maedhros@9231: maedhros@9231: /* Three forms, special cases for numbers ending in 1 and 2, 3, 4, except those ending in 1[1-4] maedhros@9231: * Used in: maedhros@9231: * Croatian, Czech, Russian, Slovak, Ukrainian */ maedhros@9231: case 6: maedhros@9231: return n % 10 == 1 && n % 100 != 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; maedhros@9231: maedhros@9231: /* Three forms, special case for one and some numbers ending in 2, 3, or 4 maedhros@9231: * Used in: maedhros@9231: * Polish */ maedhros@9231: case 7: maedhros@9231: return n == 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; maedhros@9231: maedhros@9231: /* Four forms, special case for one and all numbers ending in 02, 03, or 04 maedhros@9231: * Used in: maedhros@9231: * Slovenian */ maedhros@9231: case 8: maedhros@9231: return n % 100 == 1 ? 0 : n % 100 == 2 ? 1 : n % 100 == 3 || n % 100 == 4 ? 2 : 3; maedhros@9231: maedhros@9231: /* Two forms; singular used for everything ending in 1 but not in 11. maedhros@9231: * Used in: maedhros@9231: * Icelandic */ maedhros@9231: case 9: maedhros@9231: return n % 10 == 1 && n % 100 != 11 ? 0 : 1; ludde@2082: } ludde@2082: } ludde@2082: ludde@2082: static const char *ParseStringChoice(const char *b, uint form, char *dst, int *dstlen) ludde@2082: { ludde@2082: // {Length of each string} {each string} ludde@2082: uint n = (byte)*b++; rubidium@6987: uint pos, i, mylen = 0, mypos = 0; tron@2952: tron@2952: for (i = pos = 0; i != n; i++) { ludde@2082: uint len = (byte)*b++; ludde@2082: if (i == form) { ludde@2082: mypos = pos; ludde@2082: mylen = len; ludde@2082: } ludde@2082: pos += len; ludde@2082: } ludde@2082: *dstlen = mylen; ludde@2082: memcpy(dst, b + mypos, mylen); ludde@2082: return b + pos; ludde@2082: } ludde@2082: rubidium@6574: struct Units { peter1138@3342: int s_m; ///< Multiplier for velocity peter1138@3342: int s_s; ///< Shift for velocity peter1138@3342: StringID velocity; ///< String for velocity peter1138@3342: int p_m; ///< Multiplier for power peter1138@3342: int p_s; ///< Shift for power peter1138@3342: StringID power; ///< String for velocity peter1138@3342: int w_m; ///< Multiplier for weight peter1138@3342: int w_s; ///< Shift for weight peter1138@3342: StringID s_weight; ///< Short string for weight peter1138@3342: StringID l_weight; ///< Long string for weight peter1138@3342: int v_m; ///< Multiplier for volume peter1138@3342: int v_s; ///< Shift for volume peter1138@3342: StringID s_volume; ///< Short string for volume peter1138@3342: StringID l_volume; ///< Long string for volume peter1138@3489: int f_m; ///< Multiplier for force peter1138@3489: int f_s; ///< Shift for force peter1138@3489: StringID force; ///< String for force rubidium@6574: }; peter1138@3342: peter1138@3489: /* Unit conversions */ peter1138@3342: static const Units units[] = { peter1138@5653: { // Imperial (Original, mph, hp, metric ton, litre, kN) peter1138@6125: 1, 0, STR_UNITS_VELOCITY_IMPERIAL, peter1138@3342: 1, 0, STR_UNITS_POWER_IMPERIAL, peter1138@3342: 1, 0, STR_UNITS_WEIGHT_SHORT_METRIC, STR_UNITS_WEIGHT_LONG_METRIC, peter1138@3342: 1000, 0, STR_UNITS_VOLUME_SHORT_METRIC, STR_UNITS_VOLUME_LONG_METRIC, peter1138@5653: 1, 0, STR_UNITS_FORCE_SI, peter1138@3342: }, peter1138@5653: { // Metric (km/h, hp, metric ton, litre, kN) peter1138@6125: 103, 6, STR_UNITS_VELOCITY_METRIC, peter1138@3342: 1, 0, STR_UNITS_POWER_METRIC, peter1138@3342: 1, 0, STR_UNITS_WEIGHT_SHORT_METRIC, STR_UNITS_WEIGHT_LONG_METRIC, peter1138@3342: 1000, 0, STR_UNITS_VOLUME_SHORT_METRIC, STR_UNITS_VOLUME_LONG_METRIC, peter1138@5653: 1, 0, STR_UNITS_FORCE_SI, peter1138@3342: }, peter1138@3489: { // SI (m/s, kilowatt, kilogram, cubic metres, kilonewton) peter1138@6125: 1831, 12, STR_UNITS_VELOCITY_SI, peter1138@3342: 764, 10, STR_UNITS_POWER_SI, peter1138@3342: 1000, 0, STR_UNITS_WEIGHT_SHORT_SI, STR_UNITS_WEIGHT_LONG_SI, peter1138@3485: 1, 0, STR_UNITS_VOLUME_SHORT_SI, STR_UNITS_VOLUME_LONG_SI, peter1138@3489: 1, 0, STR_UNITS_FORCE_SI, peter1138@3342: }, peter1138@3342: }; ludde@2082: rubidium@7498: static char* FormatString(char* buff, const char* str, const int64* argv, uint casei, const char* last) truelight@0: { Darkvater@4840: extern const char _openttd_revision[]; peter1138@5108: WChar b; rubidium@7498: const int64 *argv_orig = argv; ludde@2087: uint modifier = 0; truelight@0: peter1138@5108: while ((b = Utf8Consume(&str)) != '\0') { rubidium@8112: if (SCC_NEWGRF_FIRST <= b && b <= SCC_NEWGRF_LAST) { rubidium@8112: /* We need to pass some stuff as it might be modified; oh boy. */ rubidium@8112: b = RemapNewGRFStringControlCode(b, &buff, &str, (int64*)argv); rubidium@8112: if (b == 0) continue; rubidium@8112: } rubidium@8112: tron@1316: switch (b) { peter1138@5108: case SCC_SETX: // {SETX} peter1138@5108: if (buff + Utf8CharLen(SCC_SETX) + 1 < last) { peter1138@5108: buff += Utf8Encode(buff, SCC_SETX); peter1138@5108: *buff++ = *str++; peter1138@5108: } peter1138@5108: break; tron@2410: peter1138@5108: case SCC_SETXY: // {SETXY} peter1138@5108: if (buff + Utf8CharLen(SCC_SETXY) + 2 < last) { peter1138@5108: buff += Utf8Encode(buff, SCC_SETXY); peter1138@5108: *buff++ = *str++; peter1138@5108: *buff++ = *str++; peter1138@5108: } peter1138@5108: break; peter1138@5108: peter1138@5108: case SCC_STRING_ID: // {STRINL} peter1138@5108: buff = GetStringWithArgs(buff, Utf8Consume(&str), argv, last); peter1138@5108: break; peter1138@5108: peter1138@5108: case SCC_DATE_LONG: // {DATE_LONG} peter1138@5108: buff = FormatYmdString(buff, GetInt32(&argv), last); peter1138@5108: break; peter1138@5108: peter1138@5108: case SCC_DATE_SHORT: // {DATE_SHORT} peter1138@5108: buff = FormatMonthAndYear(buff, GetInt32(&argv), last); peter1138@5108: break; peter1138@5108: peter1138@5108: case SCC_VELOCITY: {// {VELOCITY} rubidium@7498: int64 args[1]; peter1138@5108: assert(_opt_ptr->units < lengthof(units)); peter1138@5108: args[0] = GetInt32(&argv) * units[_opt_ptr->units].s_m >> units[_opt_ptr->units].s_s; peter1138@5108: buff = FormatString(buff, GetStringPtr(units[_opt_ptr->units].velocity), args, modifier >> 24, last); peter1138@5108: modifier = 0; peter1138@5108: break; peter1138@5108: } peter1138@5108: peter1138@5108: case SCC_CURRENCY_COMPACT: /* {CURRCOMPACT} */ rubidium@7490: buff = FormatGenericCurrency(buff, _currency, GetInt64(&argv), true, last); truelight@0: break; peter1138@5108: peter1138@5108: case SCC_REVISION: /* {REV} */ Darkvater@4912: buff = strecpy(buff, _openttd_revision, last); truelight@0: break; peter1138@5108: peter1138@5108: case SCC_CARGO_SHORT: { /* {SHORTCARGO} */ belugas@6916: /* Short description of cargotypes. Layout: belugas@6916: * 8-bit = cargo type belugas@6916: * 16-bit = cargo count */ peter1138@6417: StringID cargo_str = GetCargo(GetInt32(&argv))->units_volume; peter1138@3342: switch (cargo_str) { peter1138@3342: case STR_TONS: { rubidium@7498: int64 args[1]; peter1138@3342: assert(_opt_ptr->units < lengthof(units)); peter1138@3342: args[0] = GetInt32(&argv) * units[_opt_ptr->units].w_m >> units[_opt_ptr->units].w_s; Darkvater@4912: buff = FormatString(buff, GetStringPtr(units[_opt_ptr->units].l_weight), args, modifier >> 24, last); peter1138@3342: modifier = 0; peter1138@3342: break; peter1138@3342: } peter1138@3342: peter1138@3342: case STR_LITERS: { rubidium@7498: int64 args[1]; peter1138@3342: assert(_opt_ptr->units < lengthof(units)); peter1138@3342: args[0] = GetInt32(&argv) * units[_opt_ptr->units].v_m >> units[_opt_ptr->units].v_s; Darkvater@4912: buff = FormatString(buff, GetStringPtr(units[_opt_ptr->units].l_volume), args, modifier >> 24, last); peter1138@3342: modifier = 0; peter1138@3342: break; peter1138@3342: } peter1138@3342: peter1138@3342: default: peter1138@6685: if (cargo_str >= 0xE000 && cargo_str < 0xF800) { peter1138@6685: /* NewGRF strings from Action 4 use a different format here, peter1138@6685: * of e.g. "x tonnes of coal", so process accordingly. */ peter1138@6685: buff = GetStringWithArgs(buff, cargo_str, argv++, last); peter1138@6685: } else { peter1138@6685: buff = FormatCommaNumber(buff, GetInt32(&argv), last); peter1138@6685: buff = strecpy(buff, " ", last); peter1138@6685: buff = strecpy(buff, GetStringPtr(cargo_str), last); peter1138@6685: } peter1138@3342: break; peter1138@3342: } tron@1316: } break; peter1138@5108: peter1138@5108: case SCC_STRING1: { /* {STRING1} */ belugas@6916: /* String that consumes ONE argument */ ludde@2087: uint str = modifier + GetInt32(&argv); Darkvater@4912: buff = GetStringWithArgs(buff, str, GetArgvPtr(&argv, 1), last); ludde@2087: modifier = 0; ludde@2063: break; ludde@2063: } peter1138@5108: peter1138@5108: case SCC_STRING2: { /* {STRING2} */ belugas@6916: /* String that consumes TWO arguments */ ludde@2087: uint str = modifier + GetInt32(&argv); Darkvater@4912: buff = GetStringWithArgs(buff, str, GetArgvPtr(&argv, 2), last); ludde@2087: modifier = 0; ludde@2063: break; ludde@2063: } peter1138@5108: peter1138@5108: case SCC_STRING3: { /* {STRING3} */ belugas@6916: /* String that consumes THREE arguments */ ludde@2087: uint str = modifier + GetInt32(&argv); Darkvater@4912: buff = GetStringWithArgs(buff, str, GetArgvPtr(&argv, 3), last); ludde@2087: modifier = 0; ludde@2063: break; ludde@2063: } peter1138@5108: peter1138@5108: case SCC_STRING4: { /* {STRING4} */ belugas@6916: /* String that consumes FOUR arguments */ ludde@2087: uint str = modifier + GetInt32(&argv); Darkvater@4912: buff = GetStringWithArgs(buff, str, GetArgvPtr(&argv, 4), last); ludde@2087: modifier = 0; ludde@2063: break; ludde@2063: } peter1138@5108: peter1138@5108: case SCC_STRING5: { /* {STRING5} */ belugas@6916: /* String that consumes FIVE arguments */ ludde@2087: uint str = modifier + GetInt32(&argv); Darkvater@4912: buff = GetStringWithArgs(buff, str, GetArgvPtr(&argv, 5), last); ludde@2087: modifier = 0; ludde@2063: break; ludde@2063: } ludde@2063: peter1138@5108: case SCC_STATION_FEATURES: { /* {STATIONFEATURES} */ Darkvater@4912: buff = StationGetSpecialString(buff, GetInt32(&argv), last); ludde@2063: break; ludde@2063: } truelight@0: peter1138@5108: case SCC_INDUSTRY_NAME: { /* {INDUSTRY} */ tron@3033: const Industry* i = GetIndustry(GetInt32(&argv)); rubidium@7498: int64 args[2]; ludde@2070: belugas@6916: /* industry not valid anymore? */ rubidium@7886: if (!i->IsValid()) break; ludde@2070: belugas@6916: /* First print the town name and the industry type name belugas@6916: * The string STR_INDUSTRY_PATTERN controls the formatting */ ludde@2070: args[0] = i->town->index; belugas@4942: args[1] = GetIndustrySpec(i->type)->name; Darkvater@4912: buff = FormatString(buff, GetStringPtr(STR_INDUSTRY_FORMAT), args, modifier >> 24, last); ludde@2087: modifier = 0; ludde@2070: break; ludde@2070: } ludde@2070: peter1138@5108: case SCC_VOLUME: { // {VOLUME} rubidium@7498: int64 args[1]; peter1138@3342: assert(_opt_ptr->units < lengthof(units)); peter1138@3342: args[0] = GetInt32(&argv) * units[_opt_ptr->units].v_m >> units[_opt_ptr->units].v_s; Darkvater@4912: buff = FormatString(buff, GetStringPtr(units[_opt_ptr->units].l_volume), args, modifier >> 24, last); ludde@2087: modifier = 0; ludde@2084: break; ludde@2084: } ludde@2084: peter1138@5108: case SCC_GENDER_LIST: { // {G 0 Der Die Das} peter1138@8941: const char *s = GetStringPtr(argv_orig[(byte)*str++]); // contains the string that determines gender. ludde@2084: int len; ludde@2084: int gender = 0; rubidium@7907: if (s != NULL) { rubidium@7907: wchar_t c = Utf8Consume(&s); rubidium@7907: /* Switch case is always put before genders, so remove those bits */ rubidium@7907: if (c == SCC_SWITCH_CASE) { rubidium@7907: /* Skip to the last (i.e. default) case */ rubidium@7907: for (uint num = (byte)*s++; num != 0; num--) s += 3 + (s[1] << 8) + s[2]; rubidium@7907: rubidium@7907: c = Utf8Consume(&s); rubidium@7907: } rubidium@7907: /* Does this string have a gender, if so, set it */ rubidium@7907: if (c == SCC_GENDER_INDEX) gender = (byte)s[0]; rubidium@7907: } ludde@2084: str = ParseStringChoice(str, gender, buff, &len); ludde@2084: buff += len; ludde@2084: break; ludde@2084: } ludde@2084: peter1138@5108: case SCC_DATE_TINY: { // {DATE_TINY} Darkvater@4912: buff = FormatTinyDate(buff, GetInt32(&argv), last); ludde@2087: break; ludde@2087: } ludde@2087: peter1138@5108: case SCC_CARGO: { // {CARGO} belugas@6916: /* Layout now is: belugas@6916: * 8bit - cargo type belugas@6916: * 16-bit - cargo count */ peter1138@4898: CargoID cargo = GetInt32(&argv); peter1138@7889: StringID cargo_str = (cargo == CT_INVALID) ? STR_8838_N_A : GetCargo(cargo)->quantifier; Darkvater@4912: buff = GetStringWithArgs(buff, cargo_str, argv++, last); ludde@2087: break; ludde@2087: } ludde@2087: peter1138@5108: case SCC_POWER: { // {POWER} rubidium@7498: int64 args[1]; peter1138@3342: assert(_opt_ptr->units < lengthof(units)); peter1138@3342: args[0] = GetInt32(&argv) * units[_opt_ptr->units].p_m >> units[_opt_ptr->units].p_s; Darkvater@4912: buff = FormatString(buff, GetStringPtr(units[_opt_ptr->units].power), args, modifier >> 24, last); peter1138@3342: modifier = 0; peter1138@3342: break; peter1138@3342: } peter1138@3342: peter1138@5108: case SCC_VOLUME_SHORT: { // {VOLUME_S} rubidium@7498: int64 args[1]; peter1138@3342: assert(_opt_ptr->units < lengthof(units)); peter1138@3342: args[0] = GetInt32(&argv) * units[_opt_ptr->units].v_m >> units[_opt_ptr->units].v_s; Darkvater@4912: buff = FormatString(buff, GetStringPtr(units[_opt_ptr->units].s_volume), args, modifier >> 24, last); peter1138@3342: modifier = 0; peter1138@3342: break; peter1138@3342: } peter1138@3342: peter1138@5108: case SCC_WEIGHT: { // {WEIGHT} rubidium@7498: int64 args[1]; peter1138@3342: assert(_opt_ptr->units < lengthof(units)); peter1138@3342: args[0] = GetInt32(&argv) * units[_opt_ptr->units].w_m >> units[_opt_ptr->units].w_s; Darkvater@4912: buff = FormatString(buff, GetStringPtr(units[_opt_ptr->units].l_weight), args, modifier >> 24, last); peter1138@3342: modifier = 0; peter1138@3342: break; peter1138@3342: } peter1138@3342: peter1138@5108: case SCC_WEIGHT_SHORT: { // {WEIGHT_S} rubidium@7498: int64 args[1]; peter1138@3342: assert(_opt_ptr->units < lengthof(units)); peter1138@3342: args[0] = GetInt32(&argv) * units[_opt_ptr->units].w_m >> units[_opt_ptr->units].w_s; Darkvater@4912: buff = FormatString(buff, GetStringPtr(units[_opt_ptr->units].s_weight), args, modifier >> 24, last); peter1138@3342: modifier = 0; peter1138@3342: break; peter1138@3342: } peter1138@3342: peter1138@5108: case SCC_FORCE: { // {FORCE} rubidium@7498: int64 args[1]; peter1138@3489: assert(_opt_ptr->units < lengthof(units)); peter1138@3489: args[0] = GetInt32(&argv) * units[_opt_ptr->units].f_m >> units[_opt_ptr->units].f_s; Darkvater@4912: buff = FormatString(buff, GetStringPtr(units[_opt_ptr->units].force), args, modifier >> 24, last); peter1138@3489: modifier = 0; peter1138@3489: break; peter1138@3489: } peter1138@3489: peter1138@5108: case SCC_SKIP: // {SKIP} peter1138@5108: argv++; peter1138@5108: break; tron@2410: belugas@6916: /* This sets up the gender for the string. belugas@6916: * We just ignore this one. It's used in {G 0 Der Die Das} to determine the case. */ peter1138@5108: case SCC_GENDER_INDEX: // {GENDER 0} peter1138@5108: str++; peter1138@5108: break; tron@2630: peter1138@5108: case SCC_STRING: {// {STRING} peter1138@5108: uint str = modifier + GetInt32(&argv); belugas@6916: /* WARNING. It's prohibited for the included string to consume any arguments. belugas@6916: * For included strings that consume argument, you should use STRING1, STRING2 etc. belugas@6916: * To debug stuff you can set argv to NULL and it will tell you */ peter1138@5108: buff = GetStringWithArgs(buff, str, argv, last); peter1138@5108: modifier = 0; peter1138@5108: break; peter1138@5108: } ludde@2087: peter1138@5108: case SCC_COMMA: // {COMMA} rubidium@7852: buff = FormatCommaNumber(buff, GetInt64(&argv), last); peter1138@5108: break; peter1138@5108: peter1138@5108: case SCC_ARG_INDEX: // Move argument pointer peter1138@5108: argv = argv_orig + (byte)*str++; peter1138@5108: break; peter1138@5108: peter1138@5108: case SCC_PLURAL_LIST: { // {P} rubidium@7852: int64 v = argv_orig[(byte)*str++]; // contains the number that determines plural peter1138@5108: int len; peter1138@5108: str = ParseStringChoice(str, DeterminePluralForm(v), buff, &len); peter1138@5108: buff += len; peter1138@5108: break; peter1138@5108: } peter1138@5108: peter1138@5108: case SCC_NUM: // {NUM} rubidium@7852: buff = FormatNoCommaNumber(buff, GetInt64(&argv), last); peter1138@5108: break; peter1138@5108: peter1138@5108: case SCC_CURRENCY: // {CURRENCY} rubidium@7490: buff = FormatGenericCurrency(buff, _currency, GetInt64(&argv), false, last); peter1138@5108: break; peter1138@5108: peter1138@5108: case SCC_WAYPOINT_NAME: { // {WAYPOINT} peter1138@5108: Waypoint *wp = GetWaypoint(GetInt32(&argv)); peter1138@8754: peter1138@8754: if (!wp->IsValid()) { // waypoint doesn't exist anymore peter1138@8754: buff = GetStringWithArgs(buff, STR_UNKNOWN_DESTINATION, NULL, last); peter1138@8754: } else if (wp->name != NULL) { peter1138@8754: buff = strecpy(buff, wp->name, last); peter1138@5108: } else { peter1138@8754: int64 temp[2]; peter1138@5108: temp[0] = wp->town_index; peter1138@5108: temp[1] = wp->town_cn + 1; peter1138@8754: StringID str = wp->town_cn == 0 ? STR_WAYPOINTNAME_CITY : STR_WAYPOINTNAME_CITY_SERIAL; peter1138@8754: peter1138@8754: buff = GetStringWithArgs(buff, str, temp, last); ludde@2087: } peter1138@5108: break; truelight@0: } dominik@1097: peter1138@5108: case SCC_STATION_NAME: { // {STATION} peter1138@5108: const Station* st = GetStation(GetInt32(&argv)); peter1138@5108: celestar@6322: if (!st->IsValid()) { // station doesn't exist anymore peter1138@5108: buff = GetStringWithArgs(buff, STR_UNKNOWN_DESTINATION, NULL, last); peter1138@8754: } else if (st->name != NULL) { peter1138@8754: buff = strecpy(buff, st->name, last); peter1138@5108: } else { rubidium@7551: int64 temp[3]; glx@7452: temp[0] = STR_TOWN; glx@7452: temp[1] = st->town->index; rubidium@7551: temp[2] = st->index; peter1138@5108: buff = GetStringWithArgs(buff, st->string_id, temp, last); peter1138@5108: } peter1138@5108: break; peter1138@5108: } peter1138@5108: peter1138@5108: case SCC_TOWN_NAME: { // {TOWN} peter1138@5108: const Town* t = GetTown(GetInt32(&argv)); rubidium@7498: int64 temp[1]; peter1138@5108: rubidium@7882: assert(t->IsValid()); peter1138@5108: peter1138@5108: temp[0] = t->townnameparts; glx@7452: uint32 grfid = t->townnamegrfid; glx@7452: peter1138@8754: if (t->name != NULL) { peter1138@8754: buff = strecpy(buff, t->name, last); peter1138@8754: } else if (grfid == 0) { glx@7452: /* Original town name */ glx@7452: buff = GetStringWithArgs(buff, t->townnametype, temp, last); glx@7452: } else { glx@7452: /* Newgrf town name */ glx@7452: if (GetGRFTownName(grfid) != NULL) { glx@7452: /* The grf is loaded */ glx@7452: buff = GRFTownNameGenerate(buff, t->townnamegrfid, t->townnametype, t->townnameparts, last); glx@7452: } else { glx@7452: /* Fallback to english original */ glx@7452: buff = GetStringWithArgs(buff, SPECSTR_TOWNNAME_ENGLISH, temp, last); glx@7452: } glx@7452: } peter1138@5108: break; peter1138@5108: } peter1138@5108: rubidium@7139: case SCC_GROUP_NAME: { // {GROUP} rubidium@7139: const Group *g = GetGroup(GetInt32(&argv)); rubidium@7139: rubidium@7878: assert(g->IsValid()); rubidium@7139: peter1138@8754: if (g->name != NULL) { peter1138@8754: buff = strecpy(buff, g->name, last); peter1138@8754: } else { peter1138@8754: int64 args[1]; rubidium@7139: peter1138@8754: args[0] = g->index; peter1138@8754: buff = GetStringWithArgs(buff, STR_GROUP_NAME_FORMAT, args, last); peter1138@8754: } rubidium@7139: break; rubidium@7139: } rubidium@7139: peter1138@7555: case SCC_ENGINE_NAME: { // {ENGINE} peter1138@7555: EngineID engine = (EngineID)GetInt32(&argv); peter1138@8754: const Engine *e = GetEngine(engine); peter1138@7555: peter1138@8754: if (e->name != NULL) { peter1138@8754: buff = strecpy(buff, e->name, last); peter1138@8754: } else { peter1138@8754: buff = GetStringWithArgs(buff, EngInfo(engine)->string_id, NULL, last); peter1138@8754: } peter1138@7555: break; peter1138@7555: } peter1138@7555: peter1138@7545: case SCC_VEHICLE_NAME: { // {VEHICLE} peter1138@7545: const Vehicle *v = GetVehicle(GetInt32(&argv)); peter1138@7545: peter1138@8754: if (v->name != NULL) { peter1138@8754: buff = strecpy(buff, v->name, last); peter1138@8754: } else { peter1138@8754: int64 args[1]; peter1138@8754: args[0] = v->unitnumber; peter1138@7545: peter1138@8754: StringID str; peter1138@8754: switch (v->type) { peter1138@8754: default: NOT_REACHED(); peter1138@8754: case VEH_TRAIN: str = STR_SV_TRAIN_NAME; break; peter1138@8754: case VEH_ROAD: str = STR_SV_ROADVEH_NAME; break; peter1138@8754: case VEH_SHIP: str = STR_SV_SHIP_NAME; break; peter1138@8754: case VEH_AIRCRAFT: str = STR_SV_AIRCRAFT_NAME; break; peter1138@8754: } peter1138@8754: peter1138@8754: buff = GetStringWithArgs(buff, str, args, last); peter1138@8754: } peter1138@7545: break; peter1138@7545: } peter1138@7545: peter1138@7552: case SCC_SIGN_NAME: { // {SIGN} peter1138@7552: const Sign *si = GetSign(GetInt32(&argv)); peter1138@8754: if (si->name != NULL) { peter1138@8754: buff = strecpy(buff, si->name, last); peter1138@8754: } else { peter1138@8754: buff = GetStringWithArgs(buff, STR_280A_SIGN, NULL, last); peter1138@8754: } peter1138@7552: break; peter1138@7552: } peter1138@7552: peter1138@7554: case SCC_COMPANY_NAME: { // {COMPANY} peter1138@7554: const Player *p = GetPlayer((PlayerID)GetInt32(&argv)); peter1138@8754: peter1138@8754: if (p->name != NULL) { peter1138@8754: buff = strecpy(buff, p->name, last); peter1138@8754: } else { peter1138@8754: int64 args[1]; peter1138@8754: args[0] = p->name_2; peter1138@8754: buff = GetStringWithArgs(buff, p->name_1, args, last); peter1138@8754: } peter1138@7554: break; peter1138@7554: } peter1138@7554: peter1138@7554: case SCC_COMPANY_NUM: { // {COMPANYNUM} peter1138@7554: PlayerID player = (PlayerID)GetInt32(&argv); peter1138@7554: peter1138@7554: /* Nothing is added for AI or inactive players */ peter1138@7554: if (IsHumanPlayer(player) && IsValidPlayer(player)) { peter1138@7554: int64 args[1]; peter1138@7554: args[0] = player + 1; peter1138@7554: buff = GetStringWithArgs(buff, STR_7002_PLAYER, args, last); peter1138@7554: } peter1138@7554: break; peter1138@7554: } peter1138@7554: peter1138@7554: case SCC_PLAYER_NAME: { // {PLAYERNAME} peter1138@7554: const Player *p = GetPlayer((PlayerID)GetInt32(&argv)); peter1138@8754: peter1138@8754: if (p->president_name != NULL) { peter1138@8754: buff = strecpy(buff, p->president_name, last); peter1138@8754: } else { peter1138@8754: int64 args[1]; peter1138@8754: args[0] = p->president_name_2; peter1138@8754: buff = GetStringWithArgs(buff, p->president_name_1, args, last); peter1138@8754: } peter1138@7554: break; peter1138@7554: } peter1138@7554: peter1138@5108: case SCC_SETCASE: { // {SETCASE} belugas@6916: /* This is a pseudo command, it's outputted when someone does {STRING.ack} belugas@6916: * The modifier is added to all subsequent GetStringWithArgs that accept the modifier. */ peter1138@5108: modifier = (byte)*str++ << 24; peter1138@5108: break; peter1138@5108: } peter1138@5108: peter1138@5108: case SCC_SWITCH_CASE: { // {Used to implement case switching} belugas@6916: /* <0x9E> belugas@6916: * Each LEN is printed using 2 bytes in big endian order. */ peter1138@5108: uint num = (byte)*str++; peter1138@5108: while (num) { peter1138@5108: if ((byte)str[0] == casei) { belugas@6916: /* Found the case, adjust str pointer and continue */ peter1138@5108: str += 3; peter1138@5108: break; peter1138@5108: } belugas@6916: /* Otherwise skip to the next case */ peter1138@5108: str += 3 + (str[1] << 8) + str[2]; peter1138@5108: num--; peter1138@5108: } peter1138@5108: break; peter1138@5108: } peter1138@5108: peter1138@5108: default: peter1138@5108: if (buff + Utf8CharLen(b) < last) buff += Utf8Encode(buff, b); peter1138@5108: break; truelight@0: } truelight@0: } tron@1316: *buff = '\0'; truelight@0: return buff; truelight@0: } truelight@0: truelight@0: Darkvater@4912: static char *StationGetSpecialString(char *buff, int x, const char* last) truelight@0: { peter1138@5108: if ((x & 0x01) && (buff + Utf8CharLen(SCC_TRAIN) < last)) buff += Utf8Encode(buff, SCC_TRAIN); peter1138@5108: if ((x & 0x02) && (buff + Utf8CharLen(SCC_LORRY) < last)) buff += Utf8Encode(buff, SCC_LORRY); peter1138@5108: if ((x & 0x04) && (buff + Utf8CharLen(SCC_BUS) < last)) buff += Utf8Encode(buff, SCC_BUS); peter1138@5108: if ((x & 0x08) && (buff + Utf8CharLen(SCC_PLANE) < last)) buff += Utf8Encode(buff, SCC_PLANE); peter1138@5108: if ((x & 0x10) && (buff + Utf8CharLen(SCC_SHIP) < last)) buff += Utf8Encode(buff, SCC_SHIP); peter1138@5108: *buff = '\0'; truelight@0: return buff; truelight@0: } truelight@0: Darkvater@4912: static char *GetSpecialTownNameString(char *buff, int ind, uint32 seed, const char* last) tron@1316: { Darkvater@4912: char name[512]; truelight@0: peter1138@4920: _town_name_generators[ind](name, seed, lastof(name)); Darkvater@4912: return strecpy(buff, name, last); truelight@0: } truelight@0: tron@2650: static const char* const _silly_company_names[] = { truelight@0: "Bloggs Brothers", truelight@0: "Tiny Transport Ltd.", truelight@0: "Express Travel", truelight@0: "Comfy-Coach & Co.", truelight@0: "Crush & Bump Ltd.", truelight@0: "Broken & Late Ltd.", truelight@0: "Sam Speedy & Son", truelight@0: "Supersonic Travel", truelight@0: "Mike's Motors", truelight@0: "Lightning International", truelight@0: "Pannik & Loozit Ltd.", truelight@0: "Inter-City Transport", tron@2650: "Getout & Pushit Ltd." truelight@0: }; truelight@0: tron@2650: static const char* const _surname_list[] = { truelight@0: "Adams", truelight@0: "Allan", truelight@0: "Baker", truelight@0: "Bigwig", truelight@0: "Black", truelight@0: "Bloggs", truelight@0: "Brown", truelight@0: "Campbell", truelight@0: "Gordon", truelight@0: "Hamilton", truelight@0: "Hawthorn", truelight@0: "Higgins", truelight@0: "Green", truelight@0: "Gribble", truelight@0: "Jones", truelight@0: "McAlpine", truelight@0: "MacDonald", truelight@0: "McIntosh", truelight@0: "Muir", truelight@0: "Murphy", truelight@0: "Nelson", truelight@0: "O'Donnell", truelight@0: "Parker", truelight@0: "Phillips", truelight@0: "Pilkington", truelight@0: "Quigley", truelight@0: "Sharkey", truelight@0: "Thomson", tron@2650: "Watkins" tron@2650: }; tron@2650: tron@2650: static const char* const _silly_surname_list[] = { truelight@0: "Grumpy", truelight@0: "Dozy", truelight@0: "Speedy", truelight@0: "Nosey", truelight@0: "Dribble", truelight@0: "Mushroom", truelight@0: "Cabbage", truelight@0: "Sniffle", truelight@0: "Fishy", truelight@0: "Swindle", truelight@0: "Sneaky", tron@2650: "Nutkins" truelight@0: }; truelight@0: tron@1312: static const char _initial_name_letters[] = { tron@1321: 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', tron@1321: 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', truelight@0: }; truelight@0: Darkvater@4912: static char *GenAndCoName(char *buff, uint32 arg, const char* last) truelight@0: { tron@2650: const char* const* base; tron@2650: uint num; truelight@0: belugas@6683: if (_opt_ptr->landscape == LT_TOYLAND) { tron@2650: base = _silly_surname_list; tron@2650: num = lengthof(_silly_surname_list); tron@2650: } else { tron@2650: base = _surname_list; tron@2650: num = lengthof(_surname_list); truelight@0: } truelight@0: Darkvater@4912: buff = strecpy(buff, base[num * GB(arg, 16, 8) >> 8], last); Darkvater@4912: buff = strecpy(buff, " & Co.", last); truelight@0: truelight@0: return buff; truelight@0: } truelight@0: Darkvater@4912: static char *GenPresidentName(char *buff, uint32 x, const char* last) truelight@0: { Darkvater@4912: char initial[] = "?. "; tron@2650: const char* const* base; tron@2650: uint num; tron@2650: uint i; truelight@0: Darkvater@4912: initial[0] = _initial_name_letters[sizeof(_initial_name_letters) * GB(x, 0, 8) >> 8]; Darkvater@4912: buff = strecpy(buff, initial, last); truelight@0: tron@2150: i = (sizeof(_initial_name_letters) + 35) * GB(x, 8, 8) >> 8; truelight@0: if (i < sizeof(_initial_name_letters)) { Darkvater@4912: initial[0] = _initial_name_letters[i]; Darkvater@4912: buff = strecpy(buff, initial, last); truelight@0: } truelight@0: belugas@6683: if (_opt_ptr->landscape == LT_TOYLAND) { tron@2650: base = _silly_surname_list; tron@2650: num = lengthof(_silly_surname_list); tron@2650: } else { tron@2650: base = _surname_list; tron@2650: num = lengthof(_surname_list); truelight@0: } truelight@0: Darkvater@4912: buff = strecpy(buff, base[num * GB(x, 16, 8) >> 8], last); truelight@0: truelight@0: return buff; truelight@0: } truelight@0: rubidium@7498: static char *GetSpecialPlayerNameString(char *buff, int ind, const int64 *argv, const char* last) truelight@0: { tron@1316: switch (ind) { tron@1321: case 1: // not used Darkvater@4912: return strecpy(buff, _silly_company_names[GetInt32(&argv) & 0xFFFF], last); truelight@0: tron@1321: case 2: // used for Foobar & Co company names Darkvater@4912: return GenAndCoName(buff, GetInt32(&argv), last); truelight@0: tron@1321: case 3: // President name Darkvater@4912: return GenPresidentName(buff, GetInt32(&argv), last); truelight@0: tron@1321: case 4: // song names Darkvater@4912: return strecpy(buff, origin_songs_specs[GetInt32(&argv) - 1].song_name, last); truelight@0: } truelight@0: belugas@6916: /* town name? */ skidd13@8450: if (IsInsideMM(ind - 6, 0, SPECSTR_TOWNNAME_LAST-SPECSTR_TOWNNAME_START + 1)) { Darkvater@4912: buff = GetSpecialTownNameString(buff, ind - 6, GetInt32(&argv), last); Darkvater@4912: return strecpy(buff, " Transport", last); truelight@0: } truelight@0: belugas@6916: /* language name? */ skidd13@8450: if (IsInsideMM(ind, (SPECSTR_LANGUAGE_START - 0x70E4), (SPECSTR_LANGUAGE_END - 0x70E4) + 1)) { truelight@0: int i = ind - (SPECSTR_LANGUAGE_START - 0x70E4); tron@1853: return strecpy(buff, Darkvater@4912: i == _dynlang.curr ? _langpack->own_name : _dynlang.ent[i].name, last); truelight@0: } truelight@0: belugas@6916: /* resolution size? */ skidd13@8450: if (IsInsideMM(ind, (SPECSTR_RESOLUTION_START - 0x70E4), (SPECSTR_RESOLUTION_END - 0x70E4) + 1)) { truelight@0: int i = ind - (SPECSTR_RESOLUTION_START - 0x70E4); Darkvater@4912: buff += snprintf( Darkvater@4912: buff, last - buff + 1, "%dx%d", _resolutions[i][0], _resolutions[i][1] Darkvater@4912: ); Darkvater@4912: return buff; truelight@0: } truelight@0: belugas@6916: /* screenshot format name? */ skidd13@8450: if (IsInsideMM(ind, (SPECSTR_SCREENSHOT_START - 0x70E4), (SPECSTR_SCREENSHOT_END - 0x70E4) + 1)) { truelight@0: int i = ind - (SPECSTR_SCREENSHOT_START - 0x70E4); Darkvater@4912: return strecpy(buff, GetScreenshotFormatDesc(i), last); truelight@0: } truelight@0: truelight@0: assert(0); truelight@0: return NULL; truelight@0: } truelight@0: belugas@6916: /** belugas@6916: * remap a string ID from the old format to the new format belugas@6916: * @param s StringID that requires remapping belugas@6916: * @return translated ID*/ truelight@0: StringID RemapOldStringID(StringID s) truelight@0: { tron@1321: switch (s) { tron@1321: case 0x0006: return STR_SV_EMPTY; tron@1321: case 0x7000: return STR_SV_UNNAMED; tron@1321: case 0x70E4: return SPECSTR_PLAYERNAME_ENGLISH; tron@1321: case 0x70E9: return SPECSTR_PLAYERNAME_ENGLISH; tron@1321: case 0x8864: return STR_SV_TRAIN_NAME; tron@1321: case 0x902B: return STR_SV_ROADVEH_NAME; tron@1321: case 0x9830: return STR_SV_SHIP_NAME; tron@1321: case 0xA02F: return STR_SV_AIRCRAFT_NAME; tron@1321: tron@1321: default: skidd13@8450: if (IsInsideMM(s, 0x300F, 0x3030)) { tron@1321: return s - 0x300F + STR_SV_STNAME; tron@2951: } else { tron@1321: return s; tron@2951: } tron@1321: } truelight@0: } truelight@0: rubidium@7394: #ifdef ENABLE_NETWORK glx@7289: extern void SortNetworkLanguages(); rubidium@7394: #else /* ENABLE_NETWORK */ rubidium@7394: static inline void SortNetworkLanguages() {} rubidium@7394: #endif /* ENABLE_NETWORK */ glx@7289: tron@1316: bool ReadLanguagePack(int lang_index) tron@1316: { truelight@0: int tot_count, i; truelight@0: size_t len; tron@1312: char **langpack_offs; tron@1312: char *s; truelight@0: rubidium@6646: LanguagePack *lang_pack = (LanguagePack*)ReadFileToMem(_dynlang.ent[lang_index].file, &len, 200000); rubidium@6646: truelight@0: if (lang_pack == NULL) return false; tron@1319: if (len < sizeof(LanguagePack) || tron@1319: lang_pack->ident != TO_LE32(LANGUAGE_PACK_IDENT) || tron@1319: lang_pack->version != TO_LE32(LANGUAGE_PACK_VERSION)) { truelight@0: free(lang_pack); truelight@0: return false; truelight@0: } truelight@193: truelight@0: #if defined(TTD_BIG_ENDIAN) tron@1316: for (i = 0; i != 32; i++) { Darkvater@2966: lang_pack->offsets[i] = ReadLE16Aligned(&lang_pack->offsets[i]); truelight@0: } truelight@0: #endif truelight@0: truelight@0: tot_count = 0; tron@1316: for (i = 0; i != 32; i++) { tron@1319: uint num = lang_pack->offsets[i]; truelight@0: _langtab_start[i] = tot_count; truelight@0: _langtab_num[i] = num; truelight@0: tot_count += num; truelight@0: } truelight@0: belugas@6916: /* Allocate offsets */ KUDr@5860: langpack_offs = MallocT(tot_count); truelight@0: belugas@6916: /* Fill offsets */ tron@1319: s = lang_pack->data; tron@1316: for (i = 0; i != tot_count; i++) { tron@1312: len = (byte)*s; tron@1316: *s++ = '\0'; // zero terminate the string before. tron@1316: if (len >= 0xC0) len = ((len & 0x3F) << 8) + (byte)*s++; truelight@0: langpack_offs[i] = s; truelight@0: s += len; truelight@0: } truelight@0: tron@1321: free(_langpack); truelight@0: _langpack = lang_pack; truelight@0: tron@1321: free(_langpack_offs); truelight@0: _langpack_offs = langpack_offs; truelight@0: rubidium@6670: const char *c_file = strrchr(_dynlang.ent[lang_index].file, PATHSEPCHAR) + 1; rubidium@6670: ttd_strlcpy(_dynlang.curr_file, c_file, lengthof(_dynlang.curr_file)); truelight@0: truelight@0: _dynlang.curr = lang_index; belugas@3601: SetCurrentGrfLangID(_langpack->isocode); glx@7289: SortNetworkLanguages(); truelight@0: return true; truelight@0: } truelight@0: KUDr@7844: /* Win32 implementation in win32.cpp. */ bjarni@7931: /* OS X implementation in os/macosx/macos.mm. */ bjarni@7931: #if !(defined(WIN32) || defined(__APPLE__)) Darkvater@3329: /** Determine the current charset based on the environment Darkvater@3329: * First check some default values, after this one we passed ourselves Darkvater@3329: * and if none exist return the value for $LANG belugas@6916: * @param param environment variable to check conditionally if default ones are not Darkvater@3329: * set. Pass NULL if you don't want additional checks. Darkvater@3329: * @return return string containing current charset, or NULL if not-determinable */ Darkvater@3329: const char *GetCurrentLocale(const char *param) Darkvater@3329: { Darkvater@3329: const char *env; Darkvater@3329: Darkvater@3329: env = getenv("LANGUAGE"); Darkvater@3329: if (env != NULL) return env; Darkvater@3329: Darkvater@3329: env = getenv("LC_ALL"); Darkvater@3329: if (env != NULL) return env; Darkvater@3329: Darkvater@3329: if (param != NULL) { Darkvater@3329: env = getenv(param); Darkvater@3329: if (env != NULL) return env; Darkvater@3329: } Darkvater@3329: Darkvater@3329: return getenv("LANG"); Darkvater@3329: } bjarni@7931: #endif /* !(defined(WIN32) || defined(__APPLE__)) */ Darkvater@3329: glx@7289: int CDECL StringIDSorter(const void *a, const void *b) glx@7289: { glx@7289: const StringID va = *(const StringID*)a; glx@7289: const StringID vb = *(const StringID*)b; glx@7289: char stra[512]; glx@7289: char strb[512]; glx@7289: GetString(stra, va, lastof(stra)); glx@7289: GetString(strb, vb, lastof(strb)); glx@7289: glx@7289: return strcmp(stra, strb); glx@7289: } glx@7289: rubidium@6646: /** rubidium@6646: * Checks whether the given language is already found. rubidium@6646: * @param langs languages we've found so fa rubidium@6646: * @param max the length of the language list rubidium@6646: * @param language name of the language to check rubidium@6646: * @return true if and only if a language file with the same name has not been found rubidium@6646: */ rubidium@6646: static bool UniqueLanguageFile(const Language *langs, uint max, const char *language) Darkvater@4219: { rubidium@6646: for (uint i = 0; i < max; i++) { rubidium@6670: const char *f_name = strrchr(langs[i].file, PATHSEPCHAR) + 1; rubidium@6646: if (strcmp(f_name, language) == 0) return false; // duplicates rubidium@6646: } Darkvater@4219: rubidium@6646: return true; rubidium@6646: } rubidium@6646: rubidium@6646: /** rubidium@6646: * Reads the language file header and checks compatability. rubidium@6646: * @param file the file to read rubidium@6646: * @param hdr the place to write the header information to rubidium@6646: * @return true if and only if the language file is of a compatible version rubidium@6646: */ rubidium@6646: static bool GetLanguageFileHeader(const char *file, LanguagePack *hdr) rubidium@6646: { rubidium@6646: FILE *f = fopen(file, "rb"); rubidium@6646: if (f == NULL) return false; rubidium@6646: rubidium@6646: size_t read = fread(hdr, sizeof(*hdr), 1, f); rubidium@6646: fclose(f); rubidium@6646: rubidium@6646: return read == 1 && rubidium@6646: hdr->ident == TO_LE32(LANGUAGE_PACK_IDENT) && rubidium@6646: hdr->version == TO_LE32(LANGUAGE_PACK_VERSION); rubidium@6646: } rubidium@6646: rubidium@6646: /** rubidium@6646: * Gets a list of languages from the given directory. rubidium@6646: * @param langs the list to write to rubidium@6646: * @param start the initial offset in the list rubidium@6646: * @param max the length of the language list rubidium@6646: * @param path the base directory to search in rubidium@6646: * @return the number of added languages rubidium@6646: */ rubidium@6646: static int GetLanguageList(Language *langs, int start, int max, const char *path) rubidium@6646: { rubidium@6646: int i = start; rubidium@6646: rubidium@6646: DIR *dir = ttd_opendir(path); Darkvater@4219: if (dir != NULL) { rubidium@6646: struct dirent *dirent; rubidium@6646: while ((dirent = readdir(dir)) != NULL && i < max) { rubidium@6646: const char *d_name = FS2OTTD(dirent->d_name); rubidium@6646: const char *extension = strrchr(d_name, '.'); Darkvater@4219: rubidium@6646: /* Not a language file */ rubidium@6646: if (extension == NULL || strcmp(extension, ".lng") != 0) continue; rubidium@6646: rubidium@6646: /* Filter any duplicate language-files, first-come first-serve */ rubidium@6646: if (!UniqueLanguageFile(langs, i, d_name)) continue; rubidium@6646: rubidium@6646: langs[i].file = str_fmt("%s%s", path, d_name); rubidium@6646: rubidium@6646: /* Check whether the file is of the correct version */ rubidium@6646: LanguagePack hdr; rubidium@6646: if (!GetLanguageFileHeader(langs[i].file, &hdr)) { rubidium@6646: free(langs[i].file); rubidium@6646: continue; Darkvater@4219: } rubidium@6646: rubidium@6646: i++; Darkvater@4219: } Darkvater@4219: closedir(dir); Darkvater@4219: } rubidium@6646: return i - start; Darkvater@4219: } Darkvater@4219: rubidium@6646: /** rubidium@6646: * Make a list of the available language packs. put the data in rubidium@6646: * _dynlang struct. rubidium@6646: */ rubidium@6573: void InitializeLanguagePacks() truelight@0: { rubidium@7425: Searchpath sp; rubidium@6646: Language files[MAX_LANG]; rubidium@7425: uint language_count = 0; rubidium@7425: rubidium@7425: FOR_ALL_SEARCHPATHS(sp) { rubidium@7425: char path[MAX_PATH]; rubidium@7425: FioAppendDirectory(path, lengthof(path), sp, LANG_DIR); rubidium@7425: language_count += GetLanguageList(files, language_count, lengthof(files), path); rubidium@7425: } rubidium@6646: if (language_count == 0) error("No available language packs (invalid versions?)"); tron@2257: rubidium@6646: /* Acquire the locale of the current system */ rubidium@6646: const char *lang = GetCurrentLocale("LC_MESSAGES"); tron@4505: if (lang == NULL) lang = "en_GB"; tron@2257: rubidium@6646: int chosen_language = -1; ///< Matching the language in the configuartion file or the current locale rubidium@6646: int language_fallback = -1; ///< Using pt_PT for pt_BR locale when pt_BR is not available rubidium@6646: int en_GB_fallback = 0; ///< Fallback when no locale-matching language has been found truelight@0: rubidium@6646: DynamicLanguages *dl = &_dynlang; rubidium@6646: dl->num = 0; rubidium@6646: /* Fill the dynamic languages structures */ rubidium@6646: for (uint i = 0; i < language_count; i++) { rubidium@6646: /* File read the language header */ rubidium@6646: LanguagePack hdr; rubidium@6646: if (!GetLanguageFileHeader(files[i].file, &hdr)) continue; tron@1321: rubidium@6646: dl->ent[dl->num].file = files[i].file; rubidium@6646: dl->ent[dl->num].name = strdup(hdr.name); rubidium@6646: rubidium@6646: /* We are trying to find a default language. The priority is by rubidium@6646: * configuration file, local environment and last, if nothing found, rubidium@6646: * english. If def equals -1, we have not picked a default language */ rubidium@6670: const char *lang_file = strrchr(dl->ent[dl->num].file, PATHSEPCHAR) + 1; rubidium@6670: if (strcmp(lang_file, dl->curr_file) == 0) chosen_language = dl->num; rubidium@6646: rubidium@6646: if (chosen_language == -1) { rubidium@6646: if (strcmp (hdr.isocode, "en_GB") == 0) en_GB_fallback = dl->num; rubidium@6646: if (strncmp(hdr.isocode, lang, 5) == 0) chosen_language = dl->num; rubidium@6646: if (strncmp(hdr.isocode, lang, 2) == 0) language_fallback = dl->num; truelight@0: } truelight@0: rubidium@6646: dl->num++; truelight@0: } truelight@0: rubidium@6646: if (dl->num == 0) error("Invalid version of language packs"); truelight@0: rubidium@6646: /* We haven't found the language in the config nor the one in the locale. rubidium@6646: * Now we set it to one of the fallback languages */ rubidium@6646: if (chosen_language == -1) { rubidium@6646: chosen_language = (language_fallback != -1) ? language_fallback : en_GB_fallback; rubidium@6646: } truelight@0: rubidium@6646: if (!ReadLanguagePack(chosen_language)) error("Can't read language pack '%s'", dl->ent[chosen_language].file); Darkvater@2075: } rubidium@8581: rubidium@8581: /** rubidium@8581: * Check whether the currently loaded language pack rubidium@8581: * uses characters that the currently loaded font rubidium@8581: * does not support. If this is the case an error rubidium@8581: * message will be shown in English. The error rubidium@8581: * message will not be localized because that would rubidium@8581: * mean it might use characters that are not in the rubidium@8581: * font, which is the whole reason this check has rubidium@8581: * been added. rubidium@8581: */ rubidium@8581: void CheckForMissingGlyphsInLoadedLanguagePack() rubidium@8581: { rubidium@8697: const Sprite *question_mark = GetGlyph(FS_NORMAL, '?'); rubidium@8697: rubidium@8581: for (uint i = 0; i != 32; i++) { rubidium@8581: for (uint j = 0; j < _langtab_num[i]; j++) { rubidium@8581: const char *string = _langpack_offs[_langtab_start[i] + j]; rubidium@8581: WChar c; rubidium@8581: while ((c = Utf8Consume(&string)) != '\0') { rubidium@8581: if (c == SCC_SETX) { rubidium@8581: /* rubidium@8581: * SetX is, together with SetXY as special character that rubidium@8581: * uses the next (two) characters as data points. We have rubidium@8581: * to skip those, otherwise the UTF8 reading will go rubidium@8581: * haywire. rubidium@8581: */ rubidium@8581: string++; rubidium@8581: } else if (c == SCC_SETXY) { rubidium@8581: string += 2; rubidium@8697: } else if (IsPrintable(c) && c != '?' && GetGlyph(FS_NORMAL, c) == question_mark) { rubidium@8581: /* rubidium@8581: * The character is printable, but not in the normal font. rubidium@8581: * This is the case we were testing for. In this case we rubidium@8581: * have to show the error. As we do not want the string to rubidium@8581: * be translated by the translators, we 'force' it into the rubidium@8581: * binary and 'load' it via a BindCString. To do this rubidium@8581: * properly we have to set the color of the string, rubidium@8581: * otherwise we end up with a lot of artefacts. The color rubidium@8581: * 'character' might change in the future, so for safety rubidium@8581: * we just Utf8 Encode it into the string, which takes rubidium@8581: * exactly three characters, so it replaces the "XXX" with rubidium@8581: * the color marker. rubidium@8581: */ rubidium@8698: static char *err_str = strdup("XXXThe current font is missing some of the characters used in the texts for this language. Read the readme to see how to solve this."); rubidium@8581: Utf8Encode(err_str, SCC_YELLOW); rubidium@8581: StringID err_msg = BindCString(err_str); rubidium@8581: ShowErrorMessage(INVALID_STRING_ID, err_msg, 0, 0); rubidium@8581: return; rubidium@8581: } rubidium@8581: } rubidium@8581: } rubidium@8581: } rubidium@8581: }