tron@2186: /* $Id$ */ tron@2186: rubidium@9111: /** @file strings.cpp Handling of translated strings. */ belugas@6420: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@2291: #include "currency.h" rubidium@8764: #include "namegen_func.h" rubidium@8785: #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@6929: #include "fileio.h" peter1138@6091: #include "cargotype.h" rubidium@6643: #include "group.h" rubidium@6643: #include "debug.h" glx@6956: #include "newgrf_townname.h" rubidium@8790: #include "signs_base.h" peter1138@7059: #include "newgrf_engine.h" rubidium@8086: #include "spritecache.h" rubidium@8085: #include "fontcache.h" rubidium@8085: #include "gui.h" rubidium@8114: #include "strings_func.h" rubidium@8131: #include "functions.h" smatz@9085: #include "rev.h" rubidium@8120: #include "core/endian_func.hpp" rubidium@8140: #include "date_func.h" rubidium@8144: #include "vehicle_base.h" rubidium@8214: #include "string_func.h" rubidium@8254: #include "player_func.h" rubidium@8254: #include "player_base.h" rubidium@8264: #include "fios.h" rubidium@8270: #include "settings_type.h" rubidium@8275: #include "video/video_driver.hpp" rubidium@8786: #include "engine_func.h" peter1138@9070: #include "engine_base.h" rubidium@8987: #include "saveload.h" truelight@0: rubidium@8264: #include "table/strings.h" rubidium@8264: #include "table/control_codes.h" Darkvater@4219: rubidium@6320: DynamicLanguages _dynlang; tron@2201: char _userstring[128]; rubidium@7266: 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@7002: static char *GetSpecialPlayerNameString(char *buff, int ind, const int64 *argv, const char* last); truelight@0: rubidium@7002: static char *FormatString(char *buff, const char *str, const int64 *argv, uint casei, const char* last); truelight@0: rubidium@6248: struct LanguagePack { belugas@6420: 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@6420: char data[VARARRAY_SIZE]; // list of strings rubidium@6248: }; tron@1319: tron@1319: static char **_langpack_offs; tron@1319: static LanguagePack *_langpack; belugas@6420: 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@6420: /** Read an int64 from the argv array. */ rubidium@7002: static inline int64 GetInt64(const int64 **argv) ludde@2063: { ludde@2063: assert(argv); ludde@2063: return *(*argv)++; ludde@2063: } ludde@2063: rubidium@7002: /** Read an int32 from the argv array. */ rubidium@7002: static inline int32 GetInt32(const int64 **argv) rubidium@7002: { rubidium@7002: return (int32)GetInt64(argv); rubidium@7002: } rubidium@7002: belugas@6420: /** Read an array from the argv array. */ rubidium@7002: static inline const int64 *GetArgvPtr(const int64 **argv, int n) ludde@2063: { rubidium@7002: 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@6420: /* Array to hold the bound strings. */ ludde@2055: static const char *_bound_strings[NUM_BOUND_STRINGS]; ludde@2055: belugas@6420: /* This index is used to implement a "round-robin" allocating of belugas@6420: * slots for BindCString. NUM_BOUND_STRINGS slots are reserved. belugas@6420: * Which means that after NUM_BOUND_STRINGS calls to BindCString, belugas@6420: * the indices will be reused. */ ludde@2055: static int _bind_index; ludde@2055: peter1138@8445: const char *GetStringPtr(StringID string) truelight@0: { peter1138@8445: switch (GB(string, 11, 5)) { peter1138@8445: case 28: return GetGRFStringPtr(GB(string, 0, 11)); peter1138@8445: case 29: return GetGRFStringPtr(GB(string, 0, 11) + 0x0800); peter1138@8445: case 30: return GetGRFStringPtr(GB(string, 0, 11) + 0x1000); peter1138@8445: default: return _langpack_offs[_langtab_start[string >> 11] + (string & 0x7FF)]; peter1138@8445: } truelight@0: } truelight@0: belugas@6420: /** The highest 8 bits of string contain the "case index". belugas@6420: * These 8 bits will only be set when FormatString wants to print belugas@6420: * the string in a different case. No one else except FormatString belugas@6420: * should set those bits, therefore string CANNOT be StringID, but uint32. belugas@6420: * @param buffr belugas@6420: * @param string belugas@6420: * @param argv belugas@6420: * @param last belugas@6420: * @return a formatted string of char belugas@6420: */ rubidium@7002: static char *GetStringWithArgs(char *buffr, uint string, const int64 *argv, const char* last) truelight@0: { rubidium@7810: if (GB(string, 0, 16) == 0) return GetStringWithArgs(buffr, STR_UNDEFINED, argv, last); rubidium@7810: 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@8445: /* Old table for custom names. This is no longer used */ peter1138@8445: error("Incorrect conversion of custom name string."); tron@1321: peter1138@4710: case 26: peter1138@4710: /* Include string within newgrf text (format code 81) */ skidd13@7928: 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@8445: return FormatString(buffr, GetGRFStringPtr(index), argv, 0, last); belugas@3601: belugas@3601: case 29: peter1138@8445: return FormatString(buffr, GetGRFStringPtr(index + 0x0800), argv, 0, last); belugas@3601: belugas@3601: case 30: peter1138@8445: return FormatString(buffr, GetGRFStringPtr(index + 0x1000), argv, 0, last); belugas@3601: ludde@2063: case 31: belugas@6420: /* dynamic strings. These are NOT to be passed through the formatter, belugas@6420: * 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@7002: 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@6158: /** rubidium@6158: * This function takes a C-string and allocates a temporary string ID. rubidium@6158: * The StringID of the bound string is valid until BindCString is called rubidium@6158: * another NUM_BOUND_STRINGS times. So be careful when using it. belugas@6420: * @param str temp string to add belugas@6420: * @return the id of that temp string rubidium@6158: * @note formatting a DATE_TINY calls BindCString twice, thus reduces the rubidium@6158: * amount of 'user' bound strings by 2. rubidium@6158: * @todo rewrite the BindCString system to make the limit flexible and rubidium@6158: * non-round-robin. For example by using smart pointers that free rubidium@6158: * the allocated StringID when they go out-of-scope/are freed. rubidium@6158: */ 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@6420: /** This function is used to "bind" a C string to a OpenTTD dparam slot. belugas@6420: * @param n slot of the string belugas@6420: * @param str string to bind belugas@6420: */ 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@7006: memmove(_decode_parameters + amount, _decode_parameters, sizeof(_decode_parameters) - amount * sizeof(uint64)); truelight@0: } truelight@0: Darkvater@4912: // TODO rubidium@7356: static char *FormatCommaNumber(char *buff, int64 number, const char *last) truelight@0: { rubidium@7356: uint64 divisor = 10000000000000000000ULL; rubidium@7356: uint64 quot; truelight@0: int i; rubidium@7356: uint64 tot; rubidium@7356: 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@7356: for (i = 0; i < 20; i++) { truelight@0: quot = 0; rubidium@7356: if (num >= divisor) { rubidium@7356: quot = num / divisor; rubidium@7356: num = num % divisor; truelight@0: } rubidium@7356: if (tot |= quot || i == 19) { tron@1312: *buff++ = '0' + quot; rubidium@7356: if ((i % 3) == 1 && i != 19) *buff++ = ','; truelight@0: } rubidium@7356: rubidium@7356: divisor /= 10; truelight@0: } truelight@0: tron@1316: *buff = '\0'; truelight@0: truelight@0: return buff; truelight@0: } truelight@0: Darkvater@4912: // TODO rubidium@7356: static char *FormatNoCommaNumber(char *buff, int64 number, const char *last) truelight@0: { rubidium@7356: uint64 divisor = 10000000000000000000ULL; rubidium@7356: uint64 quot; truelight@0: int i; rubidium@7356: uint64 tot; rubidium@7356: 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@7356: for (i = 0; i < 20; i++) { truelight@0: quot = 0; rubidium@7356: if (num >= divisor) { rubidium@7356: quot = num / divisor; rubidium@7356: num = num % divisor; truelight@0: } rubidium@7356: if (tot |= quot || i == 19) { tron@1312: *buff++ = '0' + quot; truelight@0: } rubidium@7356: rubidium@7356: 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@7002: int64 args[3] = { ymd.day + STR_01AC_1ST - 1, STR_0162_JAN + ymd.month, ymd.year }; rubidium@6158: 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@7002: int64 args[2] = { STR_MONTH_JAN + ymd.month, ymd.year }; rubidium@6158: 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@6158: char day[3]; rubidium@6158: char month[3]; rubidium@6158: /* We want to zero-pad the days and months */ rubidium@6158: snprintf(day, lengthof(day), "%02i", ymd.day); rubidium@6158: snprintf(month, lengthof(month), "%02i", ymd.month + 1); rubidium@6158: rubidium@7002: int64 args[3] = { BindCString(day), BindCString(month), ymd.year }; rubidium@6158: return FormatString(buff, GetStringPtr(STR_DATE_TINY), args, 0, last); dominik@1097: } dominik@1097: rubidium@6996: static char *FormatGenericCurrency(char *buff, const CurrencySpec *spec, Money number, bool compact, const char* last) truelight@0: { rubidium@7763: /* We are going to make number absolute for printing, so rubidium@7763: * keep this piece of data as we need it later on */ rubidium@7763: bool negative = number < 0; rubidium@7763: const char *multiplier = ""; Darkvater@4912: char buf[40]; rubidium@7763: char *p; truelight@0: int j; truelight@0: rubidium@7763: number *= spec->rate; truelight@0: belugas@6420: /* convert from negative */ tron@1316: if (number < 0) { rubidium@7422: if (buff + Utf8CharLen(SCC_RED) > last) return buff; rubidium@7422: 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@6420: /* 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@6420: /* 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@7763: *--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@7763: if (negative) { rubidium@7422: if (buff + Utf8CharLen(SCC_PREVIOUS_COLOUR) > last) return buff; rubidium@7422: buff += Utf8Encode(buff, SCC_PREVIOUS_COLOUR); rubidium@7422: *buff = '\0'; rubidium@7422: } rubidium@7422: truelight@0: return buff; truelight@0: } truelight@0: maedhros@8735: static int DeterminePluralForm(int64 count) ludde@2082: { belugas@6420: /* The absolute value determines plurality */ maedhros@8735: uint64 n = abs(count); ludde@2082: tron@2952: switch (_langpack->plural_form) { maedhros@8735: default: maedhros@8735: NOT_REACHED(); ludde@2082: maedhros@8735: /* Two forms, singular used for one only maedhros@8735: * Used in: maedhros@8735: * Danish, Dutch, English, German, Norwegian, Swedish, Estonian, Finnish, maedhros@8735: * Greek, Hebrew, Italian, Portuguese, Spanish, Esperanto */ maedhros@8735: case 0: maedhros@8735: return n != 1; ludde@2082: maedhros@8735: /* Only one form maedhros@8735: * Used in: maedhros@8735: * Hungarian, Japanese, Korean, Turkish */ maedhros@8735: case 1: maedhros@8735: return 0; ludde@2082: maedhros@8735: /* Two forms, singular used for zero and one maedhros@8735: * Used in: maedhros@8735: * French, Brazilian Portuguese */ maedhros@8735: case 2: maedhros@8735: return n > 1; ludde@2082: maedhros@8735: /* Three forms, special case for zero maedhros@8735: * Used in: maedhros@8735: * Latvian */ maedhros@8735: case 3: maedhros@8735: return n % 10 == 1 && n % 100 != 11 ? 0 : n != 0 ? 1 : 2; maedhros@8733: maedhros@8735: /* Three forms, special case for one and two maedhros@8735: * Used in: maedhros@8735: * Gaelige (Irish) */ maedhros@8735: case 4: maedhros@8735: return n == 1 ? 0 : n == 2 ? 1 : 2; maedhros@8735: maedhros@8735: /* Three forms, special case for numbers ending in 1[2-9] maedhros@8735: * Used in: maedhros@8735: * Lithuanian */ maedhros@8735: case 5: maedhros@8735: return n % 10 == 1 && n % 100 != 11 ? 0 : n % 10 >= 2 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; maedhros@8735: maedhros@8735: /* Three forms, special cases for numbers ending in 1 and 2, 3, 4, except those ending in 1[1-4] maedhros@8735: * Used in: maedhros@8735: * Croatian, Czech, Russian, Slovak, Ukrainian */ maedhros@8735: case 6: maedhros@8735: return n % 10 == 1 && n % 100 != 11 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; maedhros@8735: maedhros@8735: /* Three forms, special case for one and some numbers ending in 2, 3, or 4 maedhros@8735: * Used in: maedhros@8735: * Polish */ maedhros@8735: case 7: maedhros@8735: return n == 1 ? 0 : n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20) ? 1 : 2; maedhros@8735: maedhros@8735: /* Four forms, special case for one and all numbers ending in 02, 03, or 04 maedhros@8735: * Used in: maedhros@8735: * Slovenian */ maedhros@8735: case 8: maedhros@8735: return n % 100 == 1 ? 0 : n % 100 == 2 ? 1 : n % 100 == 3 || n % 100 == 4 ? 2 : 3; maedhros@8735: maedhros@8735: /* Two forms; singular used for everything ending in 1 but not in 11. maedhros@8735: * Used in: maedhros@8735: * Icelandic */ maedhros@8735: case 9: maedhros@8735: 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@6491: 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@6248: 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@6248: }; peter1138@3342: peter1138@3489: /* Unit conversions */ peter1138@3342: static const Units units[] = { peter1138@5402: { // Imperial (Original, mph, hp, metric ton, litre, kN) peter1138@5874: 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@5402: 1, 0, STR_UNITS_FORCE_SI, peter1138@3342: }, peter1138@5402: { // Metric (km/h, hp, metric ton, litre, kN) peter1138@5874: 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@5402: 1, 0, STR_UNITS_FORCE_SI, peter1138@3342: }, peter1138@3489: { // SI (m/s, kilowatt, kilogram, cubic metres, kilonewton) peter1138@5874: 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@8898: /** rubidium@8898: * Convert the given (internal) speed to the display speed. rubidium@8898: * @param speed the speed to convert rubidium@8898: * @return the converted speed. rubidium@8898: */ rubidium@8898: uint ConvertSpeedToDisplaySpeed(uint speed) rubidium@8898: { rubidium@9346: return (speed * units[_opt.units].s_m) >> units[_opt.units].s_s; rubidium@8898: } rubidium@8898: rubidium@8898: /** rubidium@8898: * Convert the given display speed to the (internal) speed. rubidium@8898: * @param speed the speed to convert rubidium@8898: * @return the converted speed. rubidium@8898: */ rubidium@8898: uint ConvertDisplaySpeedToSpeed(uint speed) rubidium@8898: { rubidium@9346: return ((speed << units[_opt.units].s_s) + units[_opt.units].s_m / 2) / units[_opt.units].s_m; rubidium@8898: } rubidium@8898: rubidium@7002: static char* FormatString(char* buff, const char* str, const int64* argv, uint casei, const char* last) truelight@0: { peter1138@5108: WChar b; rubidium@7002: const int64 *argv_orig = argv; ludde@2087: uint modifier = 0; truelight@0: peter1138@5108: while ((b = Utf8Consume(&str)) != '\0') { rubidium@7616: if (SCC_NEWGRF_FIRST <= b && b <= SCC_NEWGRF_LAST) { rubidium@7616: /* We need to pass some stuff as it might be modified; oh boy. */ rubidium@7616: b = RemapNewGRFStringControlCode(b, &buff, &str, (int64*)argv); rubidium@7616: if (b == 0) continue; rubidium@7616: } rubidium@7616: 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@7002: int64 args[1]; rubidium@9346: assert(_opt.units < lengthof(units)); rubidium@8898: args[0] = ConvertSpeedToDisplaySpeed(GetInt32(&argv)); rubidium@9346: buff = FormatString(buff, GetStringPtr(units[_opt.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@6994: 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@6420: /* Short description of cargotypes. Layout: belugas@6420: * 8-bit = cargo type belugas@6420: * 16-bit = cargo count */ peter1138@6091: StringID cargo_str = GetCargo(GetInt32(&argv))->units_volume; peter1138@3342: switch (cargo_str) { peter1138@3342: case STR_TONS: { rubidium@7002: int64 args[1]; rubidium@9346: assert(_opt.units < lengthof(units)); rubidium@9346: args[0] = GetInt32(&argv) * units[_opt.units].w_m >> units[_opt.units].w_s; rubidium@9346: buff = FormatString(buff, GetStringPtr(units[_opt.units].l_weight), args, modifier >> 24, last); peter1138@3342: modifier = 0; peter1138@3342: break; peter1138@3342: } peter1138@3342: peter1138@3342: case STR_LITERS: { rubidium@7002: int64 args[1]; rubidium@9346: assert(_opt.units < lengthof(units)); rubidium@9346: args[0] = GetInt32(&argv) * units[_opt.units].v_m >> units[_opt.units].v_s; rubidium@9346: buff = FormatString(buff, GetStringPtr(units[_opt.units].l_volume), args, modifier >> 24, last); peter1138@3342: modifier = 0; peter1138@3342: break; peter1138@3342: } peter1138@3342: peter1138@3342: default: peter1138@6359: if (cargo_str >= 0xE000 && cargo_str < 0xF800) { peter1138@6359: /* NewGRF strings from Action 4 use a different format here, peter1138@6359: * of e.g. "x tonnes of coal", so process accordingly. */ peter1138@6359: buff = GetStringWithArgs(buff, cargo_str, argv++, last); peter1138@6359: } else { peter1138@6359: buff = FormatCommaNumber(buff, GetInt32(&argv), last); peter1138@6359: buff = strecpy(buff, " ", last); peter1138@6359: buff = strecpy(buff, GetStringPtr(cargo_str), last); peter1138@6359: } peter1138@3342: break; peter1138@3342: } tron@1316: } break; peter1138@5108: peter1138@5108: case SCC_STRING1: { /* {STRING1} */ belugas@6420: /* 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@6420: /* 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@6420: /* 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@6420: /* 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@6420: /* 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@7002: int64 args[2]; ludde@2070: belugas@6420: /* industry not valid anymore? */ rubidium@7390: if (!i->IsValid()) break; ludde@2070: belugas@6420: /* First print the town name and the industry type name belugas@6420: * 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@7002: int64 args[1]; rubidium@9346: assert(_opt.units < lengthof(units)); rubidium@9346: args[0] = GetInt32(&argv) * units[_opt.units].v_m >> units[_opt.units].v_s; rubidium@9346: buff = FormatString(buff, GetStringPtr(units[_opt.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@8445: const char *s = GetStringPtr(argv_orig[(byte)*str++]); // contains the string that determines gender. ludde@2084: int len; ludde@2084: int gender = 0; rubidium@7411: if (s != NULL) { rubidium@7411: wchar_t c = Utf8Consume(&s); rubidium@7411: /* Switch case is always put before genders, so remove those bits */ rubidium@7411: if (c == SCC_SWITCH_CASE) { rubidium@7411: /* Skip to the last (i.e. default) case */ rubidium@7411: for (uint num = (byte)*s++; num != 0; num--) s += 3 + (s[1] << 8) + s[2]; rubidium@7411: rubidium@7411: c = Utf8Consume(&s); rubidium@7411: } rubidium@7411: /* Does this string have a gender, if so, set it */ rubidium@7411: if (c == SCC_GENDER_INDEX) gender = (byte)s[0]; rubidium@7411: } 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@6420: /* Layout now is: belugas@6420: * 8bit - cargo type belugas@6420: * 16-bit - cargo count */ peter1138@4898: CargoID cargo = GetInt32(&argv); peter1138@7393: 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@7002: int64 args[1]; rubidium@9346: assert(_opt.units < lengthof(units)); rubidium@9346: args[0] = GetInt32(&argv) * units[_opt.units].p_m >> units[_opt.units].p_s; rubidium@9346: buff = FormatString(buff, GetStringPtr(units[_opt.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@7002: int64 args[1]; rubidium@9346: assert(_opt.units < lengthof(units)); rubidium@9346: args[0] = GetInt32(&argv) * units[_opt.units].v_m >> units[_opt.units].v_s; rubidium@9346: buff = FormatString(buff, GetStringPtr(units[_opt.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@7002: int64 args[1]; rubidium@9346: assert(_opt.units < lengthof(units)); rubidium@9346: args[0] = GetInt32(&argv) * units[_opt.units].w_m >> units[_opt.units].w_s; rubidium@9346: buff = FormatString(buff, GetStringPtr(units[_opt.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@7002: int64 args[1]; rubidium@9346: assert(_opt.units < lengthof(units)); rubidium@9346: args[0] = GetInt32(&argv) * units[_opt.units].w_m >> units[_opt.units].w_s; rubidium@9346: buff = FormatString(buff, GetStringPtr(units[_opt.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@7002: int64 args[1]; rubidium@9346: assert(_opt.units < lengthof(units)); rubidium@9346: args[0] = GetInt32(&argv) * units[_opt.units].f_m >> units[_opt.units].f_s; rubidium@9346: buff = FormatString(buff, GetStringPtr(units[_opt.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@6420: /* This sets up the gender for the string. belugas@6420: * 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@6420: /* WARNING. It's prohibited for the included string to consume any arguments. belugas@6420: * For included strings that consume argument, you should use STRING1, STRING2 etc. belugas@6420: * 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@7356: 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@7356: 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@7356: buff = FormatNoCommaNumber(buff, GetInt64(&argv), last); peter1138@5108: break; peter1138@5108: peter1138@5108: case SCC_CURRENCY: // {CURRENCY} rubidium@6994: 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@8258: peter1138@8258: if (!wp->IsValid()) { // waypoint doesn't exist anymore peter1138@8258: buff = GetStringWithArgs(buff, STR_UNKNOWN_DESTINATION, NULL, last); peter1138@8258: } else if (wp->name != NULL) { peter1138@8258: buff = strecpy(buff, wp->name, last); peter1138@5108: } else { peter1138@8258: int64 temp[2]; peter1138@5108: temp[0] = wp->town_index; peter1138@5108: temp[1] = wp->town_cn + 1; peter1138@8258: StringID str = wp->town_cn == 0 ? STR_WAYPOINTNAME_CITY : STR_WAYPOINTNAME_CITY_SERIAL; peter1138@8258: peter1138@8258: 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@5996: if (!st->IsValid()) { // station doesn't exist anymore peter1138@5108: buff = GetStringWithArgs(buff, STR_UNKNOWN_DESTINATION, NULL, last); peter1138@8258: } else if (st->name != NULL) { peter1138@8258: buff = strecpy(buff, st->name, last); peter1138@5108: } else { rubidium@7055: int64 temp[3]; glx@6956: temp[0] = STR_TOWN; glx@6956: temp[1] = st->town->index; rubidium@7055: 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@7002: int64 temp[1]; peter1138@5108: rubidium@7386: assert(t->IsValid()); peter1138@5108: peter1138@5108: temp[0] = t->townnameparts; glx@6956: uint32 grfid = t->townnamegrfid; glx@6956: peter1138@8258: if (t->name != NULL) { peter1138@8258: buff = strecpy(buff, t->name, last); peter1138@8258: } else if (grfid == 0) { glx@6956: /* Original town name */ glx@6956: buff = GetStringWithArgs(buff, t->townnametype, temp, last); glx@6956: } else { glx@6956: /* Newgrf town name */ glx@6956: if (GetGRFTownName(grfid) != NULL) { glx@6956: /* The grf is loaded */ glx@6956: buff = GRFTownNameGenerate(buff, t->townnamegrfid, t->townnametype, t->townnameparts, last); glx@6956: } else { glx@6956: /* Fallback to english original */ glx@6956: buff = GetStringWithArgs(buff, SPECSTR_TOWNNAME_ENGLISH, temp, last); glx@6956: } glx@6956: } peter1138@5108: break; peter1138@5108: } peter1138@5108: rubidium@6643: case SCC_GROUP_NAME: { // {GROUP} rubidium@6643: const Group *g = GetGroup(GetInt32(&argv)); rubidium@6643: rubidium@7382: assert(g->IsValid()); rubidium@6643: peter1138@8258: if (g->name != NULL) { peter1138@8258: buff = strecpy(buff, g->name, last); peter1138@8258: } else { peter1138@8258: int64 args[1]; rubidium@6643: peter1138@8258: args[0] = g->index; peter1138@8258: buff = GetStringWithArgs(buff, STR_GROUP_NAME_FORMAT, args, last); peter1138@8258: } rubidium@6643: break; rubidium@6643: } rubidium@6643: peter1138@7059: case SCC_ENGINE_NAME: { // {ENGINE} peter1138@7059: EngineID engine = (EngineID)GetInt32(&argv); peter1138@8258: const Engine *e = GetEngine(engine); peter1138@7059: peter1138@8258: if (e->name != NULL) { peter1138@8258: buff = strecpy(buff, e->name, last); peter1138@8258: } else { peter1138@9070: buff = GetStringWithArgs(buff, e->info.string_id, NULL, last); peter1138@8258: } peter1138@7059: break; peter1138@7059: } peter1138@7059: peter1138@7049: case SCC_VEHICLE_NAME: { // {VEHICLE} peter1138@7049: const Vehicle *v = GetVehicle(GetInt32(&argv)); peter1138@7049: peter1138@8258: if (v->name != NULL) { peter1138@8258: buff = strecpy(buff, v->name, last); peter1138@8258: } else { peter1138@8258: int64 args[1]; peter1138@8258: args[0] = v->unitnumber; peter1138@7049: peter1138@8258: StringID str; peter1138@8258: switch (v->type) { peter1138@8258: default: NOT_REACHED(); peter1138@8258: case VEH_TRAIN: str = STR_SV_TRAIN_NAME; break; peter1138@8258: case VEH_ROAD: str = STR_SV_ROADVEH_NAME; break; peter1138@8258: case VEH_SHIP: str = STR_SV_SHIP_NAME; break; peter1138@8258: case VEH_AIRCRAFT: str = STR_SV_AIRCRAFT_NAME; break; peter1138@8258: } peter1138@8258: peter1138@8258: buff = GetStringWithArgs(buff, str, args, last); peter1138@8258: } peter1138@7049: break; peter1138@7049: } peter1138@7049: peter1138@7056: case SCC_SIGN_NAME: { // {SIGN} peter1138@7056: const Sign *si = GetSign(GetInt32(&argv)); peter1138@8258: if (si->name != NULL) { peter1138@8258: buff = strecpy(buff, si->name, last); peter1138@8258: } else { peter1138@8258: buff = GetStringWithArgs(buff, STR_280A_SIGN, NULL, last); peter1138@8258: } peter1138@7056: break; peter1138@7056: } peter1138@7056: peter1138@7058: case SCC_COMPANY_NAME: { // {COMPANY} peter1138@7058: const Player *p = GetPlayer((PlayerID)GetInt32(&argv)); peter1138@8258: peter1138@8258: if (p->name != NULL) { peter1138@8258: buff = strecpy(buff, p->name, last); peter1138@8258: } else { peter1138@8258: int64 args[1]; peter1138@8258: args[0] = p->name_2; peter1138@8258: buff = GetStringWithArgs(buff, p->name_1, args, last); peter1138@8258: } peter1138@7058: break; peter1138@7058: } peter1138@7058: peter1138@7058: case SCC_COMPANY_NUM: { // {COMPANYNUM} peter1138@7058: PlayerID player = (PlayerID)GetInt32(&argv); peter1138@7058: peter1138@7058: /* Nothing is added for AI or inactive players */ peter1138@7058: if (IsHumanPlayer(player) && IsValidPlayer(player)) { peter1138@7058: int64 args[1]; peter1138@7058: args[0] = player + 1; peter1138@7058: buff = GetStringWithArgs(buff, STR_7002_PLAYER, args, last); peter1138@7058: } peter1138@7058: break; peter1138@7058: } peter1138@7058: peter1138@7058: case SCC_PLAYER_NAME: { // {PLAYERNAME} peter1138@7058: const Player *p = GetPlayer((PlayerID)GetInt32(&argv)); peter1138@8258: peter1138@8258: if (p->president_name != NULL) { peter1138@8258: buff = strecpy(buff, p->president_name, last); peter1138@8258: } else { peter1138@8258: int64 args[1]; peter1138@8258: args[0] = p->president_name_2; peter1138@8258: buff = GetStringWithArgs(buff, p->president_name_1, args, last); peter1138@8258: } peter1138@7058: break; peter1138@7058: } peter1138@7058: peter1138@5108: case SCC_SETCASE: { // {SETCASE} belugas@6420: /* This is a pseudo command, it's outputted when someone does {STRING.ack} belugas@6420: * 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@6420: /* <0x9E> belugas@6420: * 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@6420: /* Found the case, adjust str pointer and continue */ peter1138@5108: str += 3; peter1138@5108: break; peter1138@5108: } belugas@6420: /* 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@8998: if ((x & FACIL_TRAIN) && (buff + Utf8CharLen(SCC_TRAIN) < last)) buff += Utf8Encode(buff, SCC_TRAIN); peter1138@8998: if ((x & FACIL_TRUCK_STOP) && (buff + Utf8CharLen(SCC_LORRY) < last)) buff += Utf8Encode(buff, SCC_LORRY); peter1138@8998: if ((x & FACIL_BUS_STOP) && (buff + Utf8CharLen(SCC_BUS) < last)) buff += Utf8Encode(buff, SCC_BUS); peter1138@8998: if ((x & FACIL_AIRPORT) && (buff + Utf8CharLen(SCC_PLANE) < last)) buff += Utf8Encode(buff, SCC_PLANE); peter1138@8998: if ((x & FACIL_DOCK) && (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: rubidium@9346: if (_opt.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: rubidium@9346: if (_opt.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@7002: 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@6420: /* town name? */ rubidium@8969: 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@6420: /* language name? */ skidd13@7954: 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@6420: /* resolution size? */ skidd13@7954: 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@6420: /* screenshot format name? */ skidd13@7954: 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: rubidium@6898: #ifdef ENABLE_NETWORK glx@6793: extern void SortNetworkLanguages(); rubidium@6898: #else /* ENABLE_NETWORK */ rubidium@6898: static inline void SortNetworkLanguages() {} rubidium@6898: #endif /* ENABLE_NETWORK */ glx@6793: 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@6320: LanguagePack *lang_pack = (LanguagePack*)ReadFileToMem(_dynlang.ent[lang_index].file, &len, 200000); rubidium@6320: 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@6420: /* Allocate offsets */ KUDr@5609: langpack_offs = MallocT(tot_count); truelight@0: belugas@6420: /* 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@6344: const char *c_file = strrchr(_dynlang.ent[lang_index].file, PATHSEPCHAR) + 1; rubidium@6344: 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@6793: SortNetworkLanguages(); truelight@0: return true; truelight@0: } truelight@0: KUDr@7348: /* Win32 implementation in win32.cpp. */ bjarni@7435: /* OS X implementation in os/macosx/macos.mm. */ bjarni@7435: #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@6420: * @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: } rubidium@8991: #else rubidium@8991: const char *GetCurrentLocale(const char *param); bjarni@7435: #endif /* !(defined(WIN32) || defined(__APPLE__)) */ Darkvater@3329: glx@6793: int CDECL StringIDSorter(const void *a, const void *b) glx@6793: { glx@6793: const StringID va = *(const StringID*)a; glx@6793: const StringID vb = *(const StringID*)b; glx@6793: char stra[512]; glx@6793: char strb[512]; glx@6793: GetString(stra, va, lastof(stra)); glx@6793: GetString(strb, vb, lastof(strb)); glx@6793: glx@6793: return strcmp(stra, strb); glx@6793: } glx@6793: rubidium@6320: /** rubidium@6320: * Checks whether the given language is already found. rubidium@6320: * @param langs languages we've found so fa rubidium@6320: * @param max the length of the language list rubidium@6320: * @param language name of the language to check rubidium@6320: * @return true if and only if a language file with the same name has not been found rubidium@6320: */ rubidium@6320: static bool UniqueLanguageFile(const Language *langs, uint max, const char *language) Darkvater@4219: { rubidium@6320: for (uint i = 0; i < max; i++) { rubidium@6344: const char *f_name = strrchr(langs[i].file, PATHSEPCHAR) + 1; rubidium@6320: if (strcmp(f_name, language) == 0) return false; // duplicates rubidium@6320: } Darkvater@4219: rubidium@6320: return true; rubidium@6320: } rubidium@6320: rubidium@6320: /** rubidium@6320: * Reads the language file header and checks compatability. rubidium@6320: * @param file the file to read rubidium@6320: * @param hdr the place to write the header information to rubidium@6320: * @return true if and only if the language file is of a compatible version rubidium@6320: */ rubidium@6320: static bool GetLanguageFileHeader(const char *file, LanguagePack *hdr) rubidium@6320: { rubidium@6320: FILE *f = fopen(file, "rb"); rubidium@6320: if (f == NULL) return false; rubidium@6320: rubidium@6320: size_t read = fread(hdr, sizeof(*hdr), 1, f); rubidium@6320: fclose(f); rubidium@6320: rubidium@6320: return read == 1 && rubidium@6320: hdr->ident == TO_LE32(LANGUAGE_PACK_IDENT) && rubidium@6320: hdr->version == TO_LE32(LANGUAGE_PACK_VERSION); rubidium@6320: } rubidium@6320: rubidium@6320: /** rubidium@6320: * Gets a list of languages from the given directory. rubidium@6320: * @param langs the list to write to rubidium@6320: * @param start the initial offset in the list rubidium@6320: * @param max the length of the language list rubidium@6320: * @param path the base directory to search in rubidium@6320: * @return the number of added languages rubidium@6320: */ rubidium@6320: static int GetLanguageList(Language *langs, int start, int max, const char *path) rubidium@6320: { rubidium@6320: int i = start; rubidium@6320: rubidium@6320: DIR *dir = ttd_opendir(path); Darkvater@4219: if (dir != NULL) { rubidium@6320: struct dirent *dirent; rubidium@6320: while ((dirent = readdir(dir)) != NULL && i < max) { rubidium@6320: const char *d_name = FS2OTTD(dirent->d_name); rubidium@6320: const char *extension = strrchr(d_name, '.'); Darkvater@4219: rubidium@6320: /* Not a language file */ rubidium@6320: if (extension == NULL || strcmp(extension, ".lng") != 0) continue; rubidium@6320: rubidium@6320: /* Filter any duplicate language-files, first-come first-serve */ rubidium@6320: if (!UniqueLanguageFile(langs, i, d_name)) continue; rubidium@6320: rubidium@6320: langs[i].file = str_fmt("%s%s", path, d_name); rubidium@6320: rubidium@6320: /* Check whether the file is of the correct version */ rubidium@6320: LanguagePack hdr; rubidium@6320: if (!GetLanguageFileHeader(langs[i].file, &hdr)) { rubidium@6320: free(langs[i].file); rubidium@6320: continue; Darkvater@4219: } rubidium@6320: rubidium@6320: i++; Darkvater@4219: } Darkvater@4219: closedir(dir); Darkvater@4219: } rubidium@6320: return i - start; Darkvater@4219: } Darkvater@4219: rubidium@6320: /** rubidium@6320: * Make a list of the available language packs. put the data in rubidium@6320: * _dynlang struct. rubidium@6320: */ rubidium@6247: void InitializeLanguagePacks() truelight@0: { rubidium@6929: Searchpath sp; rubidium@6320: Language files[MAX_LANG]; rubidium@6929: uint language_count = 0; rubidium@6929: rubidium@6929: FOR_ALL_SEARCHPATHS(sp) { rubidium@6929: char path[MAX_PATH]; rubidium@6929: FioAppendDirectory(path, lengthof(path), sp, LANG_DIR); rubidium@6929: language_count += GetLanguageList(files, language_count, lengthof(files), path); rubidium@6929: } rubidium@6320: if (language_count == 0) error("No available language packs (invalid versions?)"); tron@2257: rubidium@6320: /* Acquire the locale of the current system */ rubidium@6320: const char *lang = GetCurrentLocale("LC_MESSAGES"); tron@4505: if (lang == NULL) lang = "en_GB"; tron@2257: rubidium@6320: int chosen_language = -1; ///< Matching the language in the configuartion file or the current locale rubidium@6320: int language_fallback = -1; ///< Using pt_PT for pt_BR locale when pt_BR is not available rubidium@6320: int en_GB_fallback = 0; ///< Fallback when no locale-matching language has been found truelight@0: rubidium@6320: DynamicLanguages *dl = &_dynlang; rubidium@6320: dl->num = 0; rubidium@6320: /* Fill the dynamic languages structures */ rubidium@6320: for (uint i = 0; i < language_count; i++) { rubidium@6320: /* File read the language header */ rubidium@6320: LanguagePack hdr; rubidium@6320: if (!GetLanguageFileHeader(files[i].file, &hdr)) continue; tron@1321: rubidium@6320: dl->ent[dl->num].file = files[i].file; rubidium@6320: dl->ent[dl->num].name = strdup(hdr.name); rubidium@6320: rubidium@6320: /* We are trying to find a default language. The priority is by rubidium@6320: * configuration file, local environment and last, if nothing found, rubidium@6320: * english. If def equals -1, we have not picked a default language */ rubidium@6344: const char *lang_file = strrchr(dl->ent[dl->num].file, PATHSEPCHAR) + 1; rubidium@6344: if (strcmp(lang_file, dl->curr_file) == 0) chosen_language = dl->num; rubidium@6320: rubidium@6320: if (chosen_language == -1) { rubidium@6320: if (strcmp (hdr.isocode, "en_GB") == 0) en_GB_fallback = dl->num; rubidium@6320: if (strncmp(hdr.isocode, lang, 5) == 0) chosen_language = dl->num; rubidium@6320: if (strncmp(hdr.isocode, lang, 2) == 0) language_fallback = dl->num; truelight@0: } truelight@0: rubidium@6320: dl->num++; truelight@0: } truelight@0: rubidium@6320: if (dl->num == 0) error("Invalid version of language packs"); truelight@0: rubidium@6320: /* We haven't found the language in the config nor the one in the locale. rubidium@6320: * Now we set it to one of the fallback languages */ rubidium@6320: if (chosen_language == -1) { rubidium@6320: chosen_language = (language_fallback != -1) ? language_fallback : en_GB_fallback; rubidium@6320: } truelight@0: rubidium@6320: if (!ReadLanguagePack(chosen_language)) error("Can't read language pack '%s'", dl->ent[chosen_language].file); Darkvater@2075: } rubidium@8085: rubidium@8085: /** rubidium@8085: * Check whether the currently loaded language pack rubidium@8085: * uses characters that the currently loaded font rubidium@8085: * does not support. If this is the case an error rubidium@8085: * message will be shown in English. The error rubidium@8085: * message will not be localized because that would rubidium@8085: * mean it might use characters that are not in the rubidium@8085: * font, which is the whole reason this check has rubidium@8085: * been added. rubidium@8085: */ rubidium@8085: void CheckForMissingGlyphsInLoadedLanguagePack() rubidium@8085: { rubidium@8201: const Sprite *question_mark = GetGlyph(FS_NORMAL, '?'); rubidium@8201: rubidium@8085: for (uint i = 0; i != 32; i++) { rubidium@8085: for (uint j = 0; j < _langtab_num[i]; j++) { rubidium@8085: const char *string = _langpack_offs[_langtab_start[i] + j]; rubidium@8085: WChar c; rubidium@8085: while ((c = Utf8Consume(&string)) != '\0') { rubidium@8085: if (c == SCC_SETX) { rubidium@8085: /* rubidium@8085: * SetX is, together with SetXY as special character that rubidium@8085: * uses the next (two) characters as data points. We have rubidium@8085: * to skip those, otherwise the UTF8 reading will go rubidium@8085: * haywire. rubidium@8085: */ rubidium@8085: string++; rubidium@8085: } else if (c == SCC_SETXY) { rubidium@8085: string += 2; rubidium@8201: } else if (IsPrintable(c) && c != '?' && GetGlyph(FS_NORMAL, c) == question_mark) { rubidium@8085: /* rubidium@8085: * The character is printable, but not in the normal font. rubidium@8085: * This is the case we were testing for. In this case we rubidium@8085: * have to show the error. As we do not want the string to rubidium@8085: * be translated by the translators, we 'force' it into the rubidium@8085: * binary and 'load' it via a BindCString. To do this rubidium@8085: * properly we have to set the color of the string, rubidium@8085: * otherwise we end up with a lot of artefacts. The color rubidium@8085: * 'character' might change in the future, so for safety rubidium@8085: * we just Utf8 Encode it into the string, which takes rubidium@8085: * exactly three characters, so it replaces the "XXX" with rubidium@8085: * the color marker. rubidium@8085: */ rubidium@8202: 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@8085: Utf8Encode(err_str, SCC_YELLOW); rubidium@8085: StringID err_msg = BindCString(err_str); rubidium@8085: ShowErrorMessage(INVALID_STRING_ID, err_msg, 0, 0); rubidium@8085: return; rubidium@8085: } rubidium@8085: } rubidium@8085: } rubidium@8085: } rubidium@8085: } rubidium@8987: rubidium@8987: rubidium@8987: /* --- Handling of saving/loading string IDs from old savegames --- */ rubidium@8987: rubidium@8987: /** rubidium@8987: * Remap a string ID from the old format to the new format rubidium@8987: * @param s StringID that requires remapping rubidium@8987: * @return translated ID rubidium@8987: */ rubidium@8987: StringID RemapOldStringID(StringID s) rubidium@8987: { rubidium@8987: switch (s) { rubidium@8987: case 0x0006: return STR_SV_EMPTY; rubidium@8987: case 0x7000: return STR_SV_UNNAMED; rubidium@8987: case 0x70E4: return SPECSTR_PLAYERNAME_ENGLISH; rubidium@8987: case 0x70E9: return SPECSTR_PLAYERNAME_ENGLISH; rubidium@8987: case 0x8864: return STR_SV_TRAIN_NAME; rubidium@8987: case 0x902B: return STR_SV_ROADVEH_NAME; rubidium@8987: case 0x9830: return STR_SV_SHIP_NAME; rubidium@8987: case 0xA02F: return STR_SV_AIRCRAFT_NAME; rubidium@8987: rubidium@8987: default: rubidium@8987: if (IsInsideMM(s, 0x300F, 0x3030)) { rubidium@8987: return s - 0x300F + STR_SV_STNAME; rubidium@8987: } else { rubidium@8987: return s; rubidium@8987: } rubidium@8987: } rubidium@8987: } rubidium@8987: rubidium@8987: /** Location to load the old names to. */ rubidium@8987: char *_old_name_array = NULL; rubidium@8987: rubidium@8987: /** rubidium@8987: * Copy and convert old custom names to UTF-8. rubidium@8987: * They were all stored in a 512 by 32 long string array and are rubidium@8987: * now stored with stations, waypoints and other places with names. rubidium@8987: * @param id the StringID of the custom name to clone. rubidium@8987: * @return the clones custom name. rubidium@8987: */ rubidium@8987: char *CopyFromOldName(StringID id) rubidium@8987: { rubidium@8987: /* Is this name an (old) custom name? */ rubidium@8987: if (GB(id, 11, 5) != 15) return NULL; rubidium@8987: rubidium@8987: if (CheckSavegameVersion(37)) { rubidium@8987: /* Old names were 32 characters long, so 128 characters should be rubidium@8987: * plenty to allow for expansion when converted to UTF-8. */ rubidium@8987: char tmp[128]; rubidium@8987: const char *strfrom = &_old_name_array[32 * GB(id, 0, 9)]; rubidium@8987: char *strto = tmp; rubidium@8987: rubidium@8987: for (; *strfrom != '\0'; strfrom++) { rubidium@8987: WChar c = (byte)*strfrom; rubidium@8987: rubidium@8987: /* Map from non-ISO8859-15 characters to UTF-8. */ rubidium@8987: switch (c) { rubidium@8987: case 0xA4: c = 0x20AC; break; // Euro rubidium@8987: case 0xA6: c = 0x0160; break; // S with caron rubidium@8987: case 0xA8: c = 0x0161; break; // s with caron rubidium@8987: case 0xB4: c = 0x017D; break; // Z with caron rubidium@8987: case 0xB8: c = 0x017E; break; // z with caron rubidium@8987: case 0xBC: c = 0x0152; break; // OE ligature rubidium@8987: case 0xBD: c = 0x0153; break; // oe ligature rubidium@8987: case 0xBE: c = 0x0178; break; // Y with diaresis rubidium@8987: default: break; rubidium@8987: } rubidium@8987: rubidium@8987: /* Check character will fit into our buffer. */ rubidium@8987: if (strto + Utf8CharLen(c) > lastof(tmp)) break; rubidium@8987: rubidium@8987: strto += Utf8Encode(strto, c); rubidium@8987: } rubidium@8987: rubidium@8987: /* Terminate the new string and copy it back to the name array */ rubidium@8987: *strto = '\0'; rubidium@8987: rubidium@8987: return strdup(tmp); rubidium@8987: } else { rubidium@8987: /* Name will already be in UTF-8. */ rubidium@8987: return strdup(&_old_name_array[32 * GB(id, 0, 9)]); rubidium@8987: } rubidium@8987: } rubidium@8987: rubidium@8987: /** rubidium@8987: * Free the memory of the old names array. rubidium@8987: * Should be called once the old names have all been converted. rubidium@8987: */ rubidium@8987: void ResetOldNames() rubidium@8987: { rubidium@8987: free(_old_name_array); rubidium@8987: _old_name_array = NULL; rubidium@8987: } rubidium@8987: rubidium@8987: /** rubidium@8987: * Initialize the old names table memory. rubidium@8987: */ rubidium@8987: void InitializeOldNames() rubidium@8987: { rubidium@8987: free(_old_name_array); rubidium@8987: _old_name_array = CallocT(512 * 32); rubidium@8987: } rubidium@8987: rubidium@8987: static void Load_NAME() rubidium@8987: { rubidium@8987: int index; rubidium@8987: rubidium@8987: while ((index = SlIterateArray()) != -1) { rubidium@8987: SlArray(&_old_name_array[32 * index], SlGetFieldLength(), SLE_UINT8); rubidium@8987: } rubidium@8987: } rubidium@8987: rubidium@8987: extern const ChunkHandler _name_chunk_handlers[] = { rubidium@8987: { 'NAME', NULL, Load_NAME, CH_ARRAY | CH_LAST}, rubidium@8987: };