newgrf_text.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_text.cpp 22654 2011-07-10 20:35:31Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00020 #include "stdafx.h"
00021 #include "newgrf.h"
00022 #include "strings_func.h"
00023 #include "newgrf_storage.h"
00024 #include "newgrf_text.h"
00025 #include "string_func.h"
00026 #include "date_type.h"
00027 #include "debug.h"
00028 #include "core/alloc_type.hpp"
00029 #include "core/smallmap_type.hpp"
00030 #include "language.h"
00031 
00032 #include "table/strings.h"
00033 #include "table/control_codes.h"
00034 
00035 #define GRFTAB  28
00036 #define TABSIZE 11
00037 
00045 StringID TTDPStringIDToOTTDStringIDMapping(StringID str)
00046 {
00047   /* StringID table for TextIDs 0x4E->0x6D */
00048   static const StringID units_volume[] = {
00049     STR_ITEMS,      STR_PASSENGERS, STR_TONS,       STR_BAGS,
00050     STR_LITERS,     STR_ITEMS,      STR_CRATES,     STR_TONS,
00051     STR_TONS,       STR_TONS,       STR_TONS,       STR_BAGS,
00052     STR_TONS,       STR_TONS,       STR_TONS,       STR_BAGS,
00053     STR_TONS,       STR_TONS,       STR_BAGS,       STR_LITERS,
00054     STR_TONS,       STR_LITERS,     STR_TONS,       STR_ITEMS,
00055     STR_BAGS,       STR_LITERS,     STR_TONS,       STR_ITEMS,
00056     STR_TONS,       STR_ITEMS,      STR_LITERS,     STR_ITEMS
00057   };
00058 
00059   /* A string straight from a NewGRF; no need to remap this as it's already mapped. */
00060   if (IsInsideMM(str, 0xD000, 0xD7FF) || IsInsideMM(str, 0xDC00, 0xDCFF)) return str;
00061 
00062 #define TEXTID_TO_STRINGID(begin, end, stringid) if (str >= begin && str <= end) return str + (stringid - begin)
00063   /* We have some changes in our cargo strings, resulting in some missing. */
00064   TEXTID_TO_STRINGID(0x000E, 0x002D, STR_CARGO_PLURAL_NOTHING);
00065   TEXTID_TO_STRINGID(0x002E, 0x004D, STR_CARGO_SINGULAR_NOTHING);
00066   if (str >= 0x004E && str <= 0x006D) return units_volume[str - 0x004E];
00067   TEXTID_TO_STRINGID(0x006E, 0x008D, STR_QUANTITY_NOTHING);
00068   TEXTID_TO_STRINGID(0x008E, 0x00AD, STR_ABBREV_NOTHING);
00069 
00070   /* Map building names according to our lang file changes. There are several
00071    * ranges of house ids, all of which need to be remapped to allow newgrfs
00072    * to use original house names. */
00073   TEXTID_TO_STRINGID(0x200F, 0x201F, STR_TOWN_BUILDING_NAME_TALL_OFFICE_BLOCK_1);
00074   TEXTID_TO_STRINGID(0x2036, 0x2041, STR_TOWN_BUILDING_NAME_COTTAGES_1);
00075   TEXTID_TO_STRINGID(0x2059, 0x205C, STR_TOWN_BUILDING_NAME_IGLOO_1);
00076 
00077   /* Same thing for industries */
00078   TEXTID_TO_STRINGID(0x4802, 0x4826, STR_INDUSTRY_NAME_COAL_MINE);
00079   TEXTID_TO_STRINGID(0x482D, 0x482E, STR_NEWS_INDUSTRY_CONSTRUCTION);
00080   TEXTID_TO_STRINGID(0x4832, 0x4834, STR_NEWS_INDUSTRY_CLOSURE_GENERAL);
00081   TEXTID_TO_STRINGID(0x4835, 0x4838, STR_NEWS_INDUSTRY_PRODUCTION_INCREASE_GENERAL);
00082   TEXTID_TO_STRINGID(0x4839, 0x483A, STR_NEWS_INDUSTRY_PRODUCTION_DECREASE_GENERAL);
00083 
00084   switch (str) {
00085     case 0x4830: return STR_ERROR_CAN_T_CONSTRUCT_THIS_INDUSTRY;
00086     case 0x4831: return STR_ERROR_FOREST_CAN_ONLY_BE_PLANTED;
00087     case 0x483B: return STR_ERROR_CAN_ONLY_BE_POSITIONED;
00088   }
00089 #undef TEXTID_TO_STRINGID
00090 
00091   if (str == STR_NULL) return STR_EMPTY;
00092 
00093   DEBUG(grf, 0, "Unknown StringID 0x%04X remapped to STR_EMPTY. Please open a Feature Request if you need it", str);
00094 
00095   return STR_EMPTY;
00096 }
00097 
00103 enum GRFBaseLanguages {
00104   GRFLB_AMERICAN    = 0x01,
00105   GRFLB_ENGLISH     = 0x02,
00106   GRFLB_GERMAN      = 0x04,
00107   GRFLB_FRENCH      = 0x08,
00108   GRFLB_SPANISH     = 0x10,
00109   GRFLB_GENERIC     = 0x80,
00110 };
00111 
00112 enum GRFExtendedLanguages {
00113   GRFLX_AMERICAN    = 0x00,
00114   GRFLX_ENGLISH     = 0x01,
00115   GRFLX_GERMAN      = 0x02,
00116   GRFLX_FRENCH      = 0x03,
00117   GRFLX_SPANISH     = 0x04,
00118   GRFLX_UNSPECIFIED = 0x7F,
00119 };
00120 
00126 struct GRFText {
00127 public:
00138   static GRFText *New(byte langid, const char *text, size_t len)
00139   {
00140     return new (len) GRFText(langid, text, len);
00141   }
00142 
00148   static GRFText *Copy(GRFText *orig)
00149   {
00150     return GRFText::New(orig->langid, orig->text, orig->len);
00151   }
00152 
00158   void *operator new(size_t size)
00159   {
00160     NOT_REACHED();
00161   }
00162 
00167   void operator delete(void *p)
00168   {
00169     free(p);
00170   }
00171 private:
00178   GRFText(byte langid_, const char *text_, size_t len_) : next(NULL), len(len_), langid(langid_)
00179   {
00180     /* We need to use memcpy instead of strcpy due to
00181      * the possibility of "choice lists" and therefor
00182      * intermediate string terminators. */
00183     memcpy(this->text, text_, len);
00184   }
00185 
00192   void *operator new(size_t size, size_t extra)
00193   {
00194     return MallocT<byte>(size + extra);
00195   }
00196 
00197 public:
00198   GRFText *next; 
00199   size_t len;    
00200   byte langid;   
00201   char text[];   
00202 };
00203 
00204 
00210 struct GRFTextEntry {
00211   uint32 grfid;
00212   uint16 stringid;
00213   StringID def_string;
00214   GRFText *textholder;
00215 };
00216 
00217 
00218 static uint _num_grf_texts = 0;
00219 static GRFTextEntry _grf_text[(1 << TABSIZE) * 3];
00220 static byte _currentLangID = GRFLX_ENGLISH;  
00221 
00228 int LanguageMap::GetMapping(int newgrf_id, bool gender) const
00229 {
00230   const SmallVector<Mapping, 1> &map = gender ? this->gender_map : this->case_map;
00231   for (const Mapping *m = map.Begin(); m != map.End(); m++) {
00232     if (m->newgrf_id == newgrf_id) return m->openttd_id;
00233   }
00234   return -1;
00235 }
00236 
00243 int LanguageMap::GetReverseMapping(int openttd_id, bool gender) const
00244 {
00245   const SmallVector<Mapping, 1> &map = gender ? this->gender_map : this->case_map;
00246   for (const Mapping *m = map.Begin(); m != map.End(); m++) {
00247     if (m->openttd_id == openttd_id) return m->newgrf_id;
00248   }
00249   return -1;
00250 }
00251 
00253 struct UnmappedChoiceList : ZeroedMemoryAllocator {
00255   ~UnmappedChoiceList()
00256   {
00257     for (SmallPair<byte, char *> *p = this->strings.Begin(); p < this->strings.End(); p++) {
00258       free(p->second);
00259     }
00260   }
00261 
00268   UnmappedChoiceList(StringControlCode type, char *old_d, int offset) :
00269     type(type), old_d(old_d), offset(offset)
00270   {
00271   }
00272 
00273   StringControlCode type; 
00274   char *old_d;            
00275   int offset;             
00276 
00278   SmallMap<byte, char *> strings;
00279 
00285   char *Flush(const LanguageMap *lm)
00286   {
00287     if (!this->strings.Contains(0)) {
00288       /* In case of a (broken) NewGRF without a default,
00289        * assume an empty string. */
00290       grfmsg(1, "choice list misses default value");
00291       this->strings[0] = strdup("");
00292     }
00293 
00294     char *d = old_d;
00295     if (lm == NULL && this->type != SCC_PLURAL_LIST) {
00296       NOT_REACHED();
00297       /* In case there is no mapping, just ignore everything but the default. */
00298       size_t len = strlen(this->strings[0]);
00299       memcpy(d, this->strings[0], len);
00300       return d + len;
00301     }
00302 
00303     d += Utf8Encode(d, this->type);
00304 
00305     if (this->type == SCC_SWITCH_CASE) {
00306       /*
00307        * Format for case switch:
00308        * <NUM CASES> <CASE1> <LEN1> <STRING1> <CASE2> <LEN2> <STRING2> <CASE3> <LEN3> <STRING3> <STRINGDEFAULT>
00309        * Each LEN is printed using 2 bytes in big endian order.
00310        */
00311 
00312       /* "<NUM CASES>" */
00313       int count = 0;
00314       for (uint8 i = 0; i < _current_language->num_cases; i++) {
00315         /* Count the ones we have a mapped string for. */
00316         if (this->strings.Contains(lm->GetReverseMapping(i, false))) count++;
00317       }
00318       *d++ = count;
00319 
00320       for (uint8 i = 0; i < _current_language->num_cases; i++) {
00321         /* Resolve the string we're looking for. */
00322         int idx = lm->GetReverseMapping(i, false);
00323         if (!this->strings.Contains(idx)) continue;
00324         char *str = this->strings[idx];
00325 
00326         /* "<CASEn>" */
00327         *d++ = i + 1;
00328 
00329         /* "<LENn>" */
00330         size_t len = strlen(str) + 1;
00331         *d++ = GB(len, 8, 8);
00332         *d++ = GB(len, 0, 8);
00333 
00334         /* "<STRINGn>" */
00335         memcpy(d, str, len);
00336         d += len;
00337       }
00338 
00339       /* "<STRINGDEFAULT>" */
00340       size_t len = strlen(this->strings[0]) + 1;
00341       memcpy(d, this->strings[0], len);
00342       d += len;
00343     } else {
00344       if (this->type == SCC_PLURAL_LIST) {
00345         *d++ = lm->plural_form;
00346       }
00347 
00348       /*
00349        * Format for choice list:
00350        * <OFFSET> <NUM CHOICES> <LENs> <STRINGs>
00351        */
00352 
00353       /* "<OFFSET>" */
00354       *d++ = this->offset - 0x80;
00355 
00356       /* "<NUM CHOICES>" */
00357       int count = (this->type == SCC_GENDER_LIST ? _current_language->num_genders : LANGUAGE_MAX_PLURAL_FORMS);
00358       *d++ = count;
00359 
00360       /* "<LENs>" */
00361       for (int i = 0; i < count; i++) {
00362         int idx = (this->type == SCC_GENDER_LIST ? lm->GetReverseMapping(i, true) : i + 1);
00363         const char *str = this->strings[this->strings.Contains(idx) ? idx : 0];
00364         size_t len = strlen(str) + 1;
00365         if (len > 0xFF) grfmsg(1, "choice list string is too long");
00366         *d++ = GB(len, 0, 8);
00367       }
00368 
00369       /* "<STRINGs>" */
00370       for (int i = 0; i < count; i++) {
00371         int idx = (this->type == SCC_GENDER_LIST ? lm->GetReverseMapping(i, true) : i + 1);
00372         const char *str = this->strings[this->strings.Contains(idx) ? idx : 0];
00373         size_t len = strlen(str);
00374         memcpy(d, str, len);
00375         d += len;
00376         *d++ = '\0';
00377       }
00378     }
00379     return d;
00380   }
00381 };
00382 
00391 char *TranslateTTDPatchCodes(uint32 grfid, uint8 language_id, const char *str, int *olen)
00392 {
00393   char *tmp = MallocT<char>(strlen(str) * 10 + 1); // Allocate space to allow for expansion
00394   char *d = tmp;
00395   bool unicode = false;
00396   WChar c;
00397   size_t len = Utf8Decode(&c, str);
00398 
00399   /* Helper variable for a possible (string) mapping. */
00400   UnmappedChoiceList *mapping = NULL;
00401 
00402   if (c == NFO_UTF8_IDENTIFIER) {
00403     unicode = true;
00404     str += len;
00405   }
00406 
00407   for (;;) {
00408     if (unicode && Utf8EncodedCharLen(*str) != 0) {
00409       c = Utf8Consume(&str);
00410       /* 'Magic' range of control codes. */
00411       if (GB(c, 8, 8) == 0xE0) {
00412         c = GB(c, 0, 8);
00413       } else if (c >= 0x20) {
00414         if (!IsValidChar(c, CS_ALPHANUMERAL)) c = '?';
00415         d += Utf8Encode(d, c);
00416         continue;
00417       }
00418     } else {
00419       c = (byte)*str++;
00420     }
00421     if (c == '\0') break;
00422 
00423     switch (c) {
00424       case 0x01:
00425         if (str[0] == '\0') goto string_end;
00426         d += Utf8Encode(d, SCC_SETX);
00427         *d++ = *str++;
00428         break;
00429       case 0x0A: break;
00430       case 0x0D: *d++ = 0x0A; break;
00431       case 0x0E: d += Utf8Encode(d, SCC_TINYFONT); break;
00432       case 0x0F: d += Utf8Encode(d, SCC_BIGFONT); break;
00433       case 0x1F:
00434         if (str[0] == '\0' || str[1] == '\0') goto string_end;
00435         d += Utf8Encode(d, SCC_SETXY);
00436         *d++ = *str++;
00437         *d++ = *str++;
00438         break;
00439       case 0x7B:
00440       case 0x7C:
00441       case 0x7D:
00442       case 0x7E:
00443       case 0x7F:
00444       case 0x80: d += Utf8Encode(d, SCC_NEWGRF_PRINT_DWORD + c - 0x7B); break;
00445       case 0x81: {
00446         if (str[0] == '\0' || str[1] == '\0') goto string_end;
00447         StringID string;
00448         string  = ((uint8)*str++);
00449         string |= ((uint8)*str++) << 8;
00450         d += Utf8Encode(d, SCC_NEWGRF_STRINL);
00451         d += Utf8Encode(d, MapGRFStringID(grfid, string));
00452         break;
00453       }
00454       case 0x82:
00455       case 0x83:
00456       case 0x84: d += Utf8Encode(d, SCC_NEWGRF_PRINT_DATE + c - 0x82); break;
00457       case 0x85: d += Utf8Encode(d, SCC_NEWGRF_DISCARD_WORD);       break;
00458       case 0x86: d += Utf8Encode(d, SCC_NEWGRF_ROTATE_TOP_4_WORDS); break;
00459       case 0x87: d += Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_VOLUME);  break;
00460       case 0x88: d += Utf8Encode(d, SCC_BLUE);    break;
00461       case 0x89: d += Utf8Encode(d, SCC_SILVER);  break;
00462       case 0x8A: d += Utf8Encode(d, SCC_GOLD);    break;
00463       case 0x8B: d += Utf8Encode(d, SCC_RED);     break;
00464       case 0x8C: d += Utf8Encode(d, SCC_PURPLE);  break;
00465       case 0x8D: d += Utf8Encode(d, SCC_LTBROWN); break;
00466       case 0x8E: d += Utf8Encode(d, SCC_ORANGE);  break;
00467       case 0x8F: d += Utf8Encode(d, SCC_GREEN);   break;
00468       case 0x90: d += Utf8Encode(d, SCC_YELLOW);  break;
00469       case 0x91: d += Utf8Encode(d, SCC_DKGREEN); break;
00470       case 0x92: d += Utf8Encode(d, SCC_CREAM);   break;
00471       case 0x93: d += Utf8Encode(d, SCC_BROWN);   break;
00472       case 0x94: d += Utf8Encode(d, SCC_WHITE);   break;
00473       case 0x95: d += Utf8Encode(d, SCC_LTBLUE);  break;
00474       case 0x96: d += Utf8Encode(d, SCC_GRAY);    break;
00475       case 0x97: d += Utf8Encode(d, SCC_DKBLUE);  break;
00476       case 0x98: d += Utf8Encode(d, SCC_BLACK);   break;
00477       case 0x9A: {
00478         int code = *str++;
00479         switch (code) {
00480           case 0x00: goto string_end;
00481           case 0x01: d += Utf8Encode(d, SCC_NEWGRF_PRINT_QWORD_CURRENCY); break;
00482           /* 0x02: ignore next colour byte is not supported. It works on the final
00483            * string and as such hooks into the string drawing routine. At that
00484            * point many things already happened, such as splitting up of strings
00485            * when drawn over multiple lines or right-to-left translations, which
00486            * make the behaviour peculiar, e.g. only happening at specific width
00487            * of windows. Or we need to add another pass over the string to just
00488            * support this. As such it is not implemented in OpenTTD. */
00489           case 0x03: {
00490             if (str[0] == '\0' || str[1] == '\0') goto string_end;
00491             uint16 tmp  = ((uint8)*str++);
00492             tmp        |= ((uint8)*str++) << 8;
00493             d += Utf8Encode(d, SCC_NEWGRF_PUSH_WORD);
00494             d += Utf8Encode(d, tmp);
00495             break;
00496           }
00497           case 0x04:
00498             if (str[0] == '\0') goto string_end;
00499             d += Utf8Encode(d, SCC_NEWGRF_UNPRINT);
00500             d += Utf8Encode(d, *str++);
00501             break;
00502           case 0x06: d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_BYTE);          break;
00503           case 0x07: d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_WORD);          break;
00504           case 0x08: d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_DWORD);         break;
00505           /* 0x09, 0x0A are TTDPatch internal use only string codes. */
00506           case 0x0B: d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_QWORD);         break;
00507           case 0x0C: d += Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_STATION_NAME); break;
00508           case 0x0D: d += Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_WEIGHT);       break;
00509           case 0x0E:
00510           case 0x0F: {
00511             if (str[0] == '\0') goto string_end;
00512             const LanguageMap *lm = LanguageMap::GetLanguageMap(grfid, language_id);
00513             int index = *str++;
00514             int mapped = lm != NULL ? lm->GetMapping(index, code == 0x0E) : -1;
00515             if (mapped >= 0) {
00516               d += Utf8Encode(d, code == 0x0E ? SCC_GENDER_INDEX : SCC_SETCASE);
00517               d += Utf8Encode(d, mapped);
00518             }
00519             break;
00520           }
00521 
00522           case 0x10:
00523           case 0x11:
00524             if (str[0] == '\0') goto string_end;
00525             if (mapping == NULL) {
00526               if (code == 0x10) str++; // Skip the index
00527               grfmsg(1, "choice list %s marker found when not expected", code == 0x10 ? "next" : "default");
00528               break;
00529             } else {
00530               /* Terminate the previous string. */
00531               *d = '\0';
00532               int index = (code == 0x10 ? *str++ : 0);
00533               if (mapping->strings.Contains(index)) {
00534                 grfmsg(1, "duplicate choice list string, ignoring");
00535                 d++;
00536               } else {
00537                 d = mapping->strings[index] = MallocT<char>(strlen(str) * 10 + 1);
00538               }
00539             }
00540             break;
00541 
00542           case 0x12:
00543             if (mapping == NULL) {
00544               grfmsg(1, "choice list end marker found when not expected");
00545             } else {
00546               /* Terminate the previous string. */
00547               *d = '\0';
00548 
00549               /* Now we can start flushing everything and clean everything up. */
00550               d = mapping->Flush(LanguageMap::GetLanguageMap(grfid, language_id));
00551               delete mapping;
00552               mapping = NULL;
00553             }
00554             break;
00555 
00556           case 0x13:
00557           case 0x14:
00558           case 0x15:
00559             if (str[0] == '\0') goto string_end;
00560             if (mapping != NULL) {
00561               grfmsg(1, "choice lists can't be stacked, it's going to get messy now...");
00562               if (code != 0x14) str++;
00563             } else {
00564               static const StringControlCode mp[] = { SCC_GENDER_LIST, SCC_SWITCH_CASE, SCC_PLURAL_LIST };
00565               mapping = new UnmappedChoiceList(mp[code - 0x13], d, code == 0x14 ? 0 : *str++);
00566             }
00567             break;
00568 
00569           default:
00570             grfmsg(1, "missing handler for extended format code");
00571             break;
00572         }
00573         break;
00574       }
00575 
00576       case 0x9E: d += Utf8Encode(d, 0x20AC);             break; // Euro
00577       case 0x9F: d += Utf8Encode(d, 0x0178);             break; // Y with diaeresis
00578       case 0xA0: d += Utf8Encode(d, SCC_UPARROW);        break;
00579       case 0xAA: d += Utf8Encode(d, SCC_DOWNARROW);      break;
00580       case 0xAC: d += Utf8Encode(d, SCC_CHECKMARK);      break;
00581       case 0xAD: d += Utf8Encode(d, SCC_CROSS);          break;
00582       case 0xAF: d += Utf8Encode(d, SCC_RIGHTARROW);     break;
00583       case 0xB4: d += Utf8Encode(d, SCC_TRAIN);          break;
00584       case 0xB5: d += Utf8Encode(d, SCC_LORRY);          break;
00585       case 0xB6: d += Utf8Encode(d, SCC_BUS);            break;
00586       case 0xB7: d += Utf8Encode(d, SCC_PLANE);          break;
00587       case 0xB8: d += Utf8Encode(d, SCC_SHIP);           break;
00588       case 0xB9: d += Utf8Encode(d, SCC_SUPERSCRIPT_M1); break;
00589       case 0xBC: d += Utf8Encode(d, SCC_SMALLUPARROW);   break;
00590       case 0xBD: d += Utf8Encode(d, SCC_SMALLDOWNARROW); break;
00591       default:
00592         /* Validate any unhandled character */
00593         if (!IsValidChar(c, CS_ALPHANUMERAL)) c = '?';
00594         d += Utf8Encode(d, c);
00595         break;
00596     }
00597   }
00598 
00599 string_end:
00600   if (mapping != NULL) {
00601     grfmsg(1, "choice list was incomplete, the whole list is ignored");
00602     delete mapping;
00603   }
00604 
00605   *d = '\0';
00606   if (olen != NULL) *olen = d - tmp + 1;
00607   tmp = ReallocT(tmp, d - tmp + 1);
00608   return tmp;
00609 }
00610 
00616 void AddGRFTextToList(GRFText **list, GRFText *text_to_add)
00617 {
00618   GRFText **ptext, *text;
00619 
00620   /* Loop through all languages and see if we can replace a string */
00621   for (ptext = list; (text = *ptext) != NULL; ptext = &text->next) {
00622     if (text->langid == text_to_add->langid) {
00623       text_to_add->next = text->next;
00624       *ptext = text_to_add;
00625       delete text;
00626       return;
00627     }
00628   }
00629 
00630   /* If a string wasn't replaced, then we must append the new string */
00631   *ptext = text_to_add;
00632 }
00633 
00642 void AddGRFTextToList(struct GRFText **list, byte langid, uint32 grfid, const char *text_to_add)
00643 {
00644   int len;
00645   char *translatedtext = TranslateTTDPatchCodes(grfid, langid, text_to_add, &len);
00646   GRFText *newtext = GRFText::New(langid, translatedtext, len);
00647   free(translatedtext);
00648 
00649   AddGRFTextToList(list, newtext);
00650 }
00651 
00658 void AddGRFTextToList(struct GRFText **list, const char *text_to_add)
00659 {
00660   AddGRFTextToList(list, GRFText::New(0x7F, text_to_add, strlen(text_to_add) + 1));
00661 }
00662 
00668 GRFText *DuplicateGRFText(GRFText *orig)
00669 {
00670   GRFText *newtext = NULL;
00671   GRFText **ptext = &newtext;
00672   for (; orig != NULL; orig = orig->next) {
00673     *ptext = GRFText::Copy(orig);
00674     ptext = &(*ptext)->next;
00675   }
00676   return newtext;
00677 }
00678 
00682 StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid_to_add, bool new_scheme, const char *text_to_add, StringID def_string)
00683 {
00684   char *translatedtext;
00685   uint id;
00686 
00687   /* When working with the old language scheme (grf_version is less than 7) and
00688    * English or American is among the set bits, simply add it as English in
00689    * the new scheme, i.e. as langid = 1.
00690    * If English is set, it is pretty safe to assume the translations are not
00691    * actually translated.
00692    */
00693   if (!new_scheme) {
00694     if (langid_to_add & (GRFLB_AMERICAN | GRFLB_ENGLISH)) {
00695       langid_to_add = GRFLX_ENGLISH;
00696     } else {
00697       StringID ret = STR_EMPTY;
00698       if (langid_to_add & GRFLB_GERMAN)  ret = AddGRFString(grfid, stringid, GRFLX_GERMAN,  true, text_to_add, def_string);
00699       if (langid_to_add & GRFLB_FRENCH)  ret = AddGRFString(grfid, stringid, GRFLX_FRENCH,  true, text_to_add, def_string);
00700       if (langid_to_add & GRFLB_SPANISH) ret = AddGRFString(grfid, stringid, GRFLX_SPANISH, true, text_to_add, def_string);
00701       return ret;
00702     }
00703   }
00704 
00705   for (id = 0; id < _num_grf_texts; id++) {
00706     if (_grf_text[id].grfid == grfid && _grf_text[id].stringid == stringid) {
00707       break;
00708     }
00709   }
00710 
00711   /* Too many strings allocated, return empty */
00712   if (id == lengthof(_grf_text)) return STR_EMPTY;
00713 
00714   int len;
00715   translatedtext = TranslateTTDPatchCodes(grfid, langid_to_add, text_to_add, &len);
00716 
00717   GRFText *newtext = GRFText::New(langid_to_add, translatedtext, len);
00718 
00719   free(translatedtext);
00720 
00721   /* If we didn't find our stringid and grfid in the list, allocate a new id */
00722   if (id == _num_grf_texts) _num_grf_texts++;
00723 
00724   if (_grf_text[id].textholder == NULL) {
00725     _grf_text[id].grfid      = grfid;
00726     _grf_text[id].stringid   = stringid;
00727     _grf_text[id].def_string = def_string;
00728   }
00729   AddGRFTextToList(&_grf_text[id].textholder, newtext);
00730 
00731   grfmsg(3, "Added 0x%X: grfid %08X string 0x%X lang 0x%X string '%s'", id, grfid, stringid, newtext->langid, newtext->text);
00732 
00733   return (GRFTAB << TABSIZE) + id;
00734 }
00735 
00736 /* Used to remember the grfid that the last retrieved string came from */
00737 static uint32 _last_grfid = 0;
00738 
00742 StringID GetGRFStringID(uint32 grfid, uint16 stringid)
00743 {
00744   uint id;
00745 
00746   /* grfid is zero when we're being called via an include */
00747   if (grfid == 0) grfid = _last_grfid;
00748 
00749   for (id = 0; id < _num_grf_texts; id++) {
00750     if (_grf_text[id].grfid == grfid && _grf_text[id].stringid == stringid) {
00751       return (GRFTAB << TABSIZE) + id;
00752     }
00753   }
00754 
00755   return STR_UNDEFINED;
00756 }
00757 
00758 
00766 const char *GetGRFStringFromGRFText(const GRFText *text)
00767 {
00768   const char *default_text = NULL;
00769 
00770   /* Search the list of lang-strings of this stringid for current lang */
00771   for (; text != NULL; text = text->next) {
00772     if (text->langid == _currentLangID) return text->text;
00773 
00774     /* If the current string is English or American, set it as the
00775      * fallback language if the specific language isn't available. */
00776     if (text->langid == GRFLX_UNSPECIFIED || (default_text == NULL && (text->langid == GRFLX_ENGLISH || text->langid == GRFLX_AMERICAN))) {
00777       default_text = text->text;
00778     }
00779   }
00780 
00781   return default_text;
00782 }
00783 
00787 const char *GetGRFStringPtr(uint16 stringid)
00788 {
00789   assert(_grf_text[stringid].grfid != 0);
00790 
00791   /* Remember this grfid in case the string has included text */
00792   _last_grfid = _grf_text[stringid].grfid;
00793 
00794   const char *str = GetGRFStringFromGRFText(_grf_text[stringid].textholder);
00795   if (str != NULL) return str;
00796 
00797   /* Use the default string ID if the fallback string isn't available */
00798   return GetStringPtr(_grf_text[stringid].def_string);
00799 }
00800 
00809 void SetCurrentGrfLangID(byte language_id)
00810 {
00811   _currentLangID = language_id;
00812 }
00813 
00814 bool CheckGrfLangID(byte lang_id, byte grf_version)
00815 {
00816   if (grf_version < 7) {
00817     switch (_currentLangID) {
00818       case GRFLX_GERMAN:  return (lang_id & GRFLB_GERMAN)  != 0;
00819       case GRFLX_FRENCH:  return (lang_id & GRFLB_FRENCH)  != 0;
00820       case GRFLX_SPANISH: return (lang_id & GRFLB_SPANISH) != 0;
00821       default:            return (lang_id & (GRFLB_ENGLISH | GRFLB_AMERICAN)) != 0;
00822     }
00823   }
00824 
00825   return (lang_id == _currentLangID || lang_id == GRFLX_UNSPECIFIED);
00826 }
00827 
00832 void CleanUpGRFText(GRFText *grftext)
00833 {
00834   while (grftext != NULL) {
00835     GRFText *grftext2 = grftext->next;
00836     delete grftext;
00837     grftext = grftext2;
00838   }
00839 }
00840 
00845 void CleanUpStrings()
00846 {
00847   uint id;
00848 
00849   for (id = 0; id < _num_grf_texts; id++) {
00850     CleanUpGRFText(_grf_text[id].textholder);
00851     _grf_text[id].grfid      = 0;
00852     _grf_text[id].stringid   = 0;
00853     _grf_text[id].textholder = NULL;
00854   }
00855 
00856   _num_grf_texts = 0;
00857 }
00858 
00859 struct TextRefStack {
00860   byte stack[0x30];
00861   byte position;
00862   bool used;
00863 
00864   TextRefStack() : used(false) {}
00865 
00866   TextRefStack(const TextRefStack &stack) :
00867     position(stack.position),
00868     used(stack.used)
00869   {
00870     memcpy(this->stack, stack.stack, sizeof(this->stack));
00871   }
00872 
00873   uint8  PopUnsignedByte()  { assert(this->position < lengthof(this->stack)); return this->stack[this->position++]; }
00874   int8   PopSignedByte()    { return (int8)this->PopUnsignedByte(); }
00875 
00876   uint16 PopUnsignedWord()
00877   {
00878     uint16 val = this->PopUnsignedByte();
00879     return val | (this->PopUnsignedByte() << 8);
00880   }
00881   int16  PopSignedWord()    { return (int32)this->PopUnsignedWord(); }
00882 
00883   uint32 PopUnsignedDWord()
00884   {
00885     uint32 val = this->PopUnsignedWord();
00886     return val | (this->PopUnsignedWord() << 16);
00887   }
00888   int32  PopSignedDWord()   { return (int32)this->PopUnsignedDWord(); }
00889 
00890   uint64 PopUnsignedQWord()
00891   {
00892     uint64 val = this->PopUnsignedDWord();
00893     return val | (((uint64)this->PopUnsignedDWord()) << 32);
00894   }
00895   int64  PopSignedQWord()   { return (int64)this->PopUnsignedQWord(); }
00896 
00898   void RotateTop4Words()
00899   {
00900     byte tmp[2];
00901     for (int i = 0; i  < 2; i++) tmp[i] = this->stack[this->position + i + 6];
00902     for (int i = 5; i >= 0; i--) this->stack[this->position + i + 2] = this->stack[this->position + i];
00903     for (int i = 0; i  < 2; i++) this->stack[this->position + i] = tmp[i];
00904   }
00905 
00906   void PushWord(uint16 word)
00907   {
00908     if (this->position >= 2) {
00909       this->position -= 2;
00910     } else {
00911       for (int i = lengthof(stack) - 1; i >= this->position + 2; i--) {
00912         this->stack[i] = this->stack[i - 2];
00913       }
00914     }
00915     this->stack[this->position]     = GB(word, 0, 8);
00916     this->stack[this->position + 1] = GB(word, 8, 8);
00917   }
00918 
00919   void ResetStack()  { this->position = 0; this->used = true; }
00920   void RewindStack() { this->position = 0; }
00921 };
00922 
00924 static TextRefStack _newgrf_textrefstack;
00925 
00930 bool UsingNewGRFTextStack()
00931 {
00932   return _newgrf_textrefstack.used;
00933 }
00934 
00939 struct TextRefStack *CreateTextRefStackBackup()
00940 {
00941   return new TextRefStack(_newgrf_textrefstack);
00942 }
00943 
00948 void RestoreTextRefStackBackup(struct TextRefStack *backup)
00949 {
00950   _newgrf_textrefstack = *backup;
00951   delete backup;
00952 }
00953 
00971 void StartTextRefStackUsage(byte numEntries, const uint32 *values)
00972 {
00973   extern TemporaryStorageArray<int32, 0x110> _temp_store;
00974 
00975   _newgrf_textrefstack.ResetStack();
00976 
00977   byte *p = _newgrf_textrefstack.stack;
00978   for (uint i = 0; i < numEntries; i++) {
00979     uint32 value = values != NULL ? values[i] : _temp_store.Get(0x100 + i);
00980     for (uint j = 0; j < 32; j += 8) {
00981       *p = GB(value, j, 8);
00982       p++;
00983     }
00984   }
00985 }
00986 
00988 void StopTextRefStackUsage()
00989 {
00990   _newgrf_textrefstack.used = false;
00991 }
00992 
00993 void RewindTextRefStack()
00994 {
00995   _newgrf_textrefstack.RewindStack();
00996 }
00997 
01006 uint RemapNewGRFStringControlCode(uint scc, char *buf_start, char **buff, const char **str, int64 *argv)
01007 {
01008   if (_newgrf_textrefstack.used) {
01009     switch (scc) {
01010       default: NOT_REACHED();
01011       case SCC_NEWGRF_PRINT_SIGNED_BYTE:    *argv = _newgrf_textrefstack.PopSignedByte();    break;
01012       case SCC_NEWGRF_PRINT_SIGNED_WORD:    *argv = _newgrf_textrefstack.PopSignedWord();    break;
01013       case SCC_NEWGRF_PRINT_QWORD_CURRENCY: *argv = _newgrf_textrefstack.PopUnsignedQWord(); break;
01014 
01015       case SCC_NEWGRF_PRINT_DWORD_CURRENCY:
01016       case SCC_NEWGRF_PRINT_DWORD:          *argv = _newgrf_textrefstack.PopSignedDWord();   break;
01017 
01018       case SCC_NEWGRF_PRINT_HEX_BYTE:       *argv = _newgrf_textrefstack.PopUnsignedByte();  break;
01019       case SCC_NEWGRF_PRINT_HEX_DWORD:      *argv = _newgrf_textrefstack.PopUnsignedDWord(); break;
01020       case SCC_NEWGRF_PRINT_HEX_QWORD:      *argv = _newgrf_textrefstack.PopSignedQWord(); break;
01021 
01022       case SCC_NEWGRF_PRINT_HEX_WORD:
01023       case SCC_NEWGRF_PRINT_WORD_SPEED:
01024       case SCC_NEWGRF_PRINT_WORD_VOLUME:
01025       case SCC_NEWGRF_PRINT_WORD_WEIGHT:
01026       case SCC_NEWGRF_PRINT_WORD_STATION_NAME:
01027       case SCC_NEWGRF_PRINT_UNSIGNED_WORD:  *argv = _newgrf_textrefstack.PopUnsignedWord();  break;
01028 
01029       case SCC_NEWGRF_PRINT_DATE:
01030       case SCC_NEWGRF_PRINT_MONTH_YEAR:     *argv = _newgrf_textrefstack.PopSignedWord() + DAYS_TILL_ORIGINAL_BASE_YEAR; break;
01031 
01032       case SCC_NEWGRF_DISCARD_WORD:         _newgrf_textrefstack.PopUnsignedWord(); break;
01033 
01034       case SCC_NEWGRF_ROTATE_TOP_4_WORDS:   _newgrf_textrefstack.RotateTop4Words(); break;
01035       case SCC_NEWGRF_PUSH_WORD:            _newgrf_textrefstack.PushWord(Utf8Consume(str)); break;
01036       case SCC_NEWGRF_UNPRINT:              *buff = max(*buff - Utf8Consume(str), buf_start); break;
01037 
01038       case SCC_NEWGRF_PRINT_STRING_ID:
01039         *argv = TTDPStringIDToOTTDStringIDMapping(_newgrf_textrefstack.PopUnsignedWord());
01040         break;
01041     }
01042   }
01043 
01044   switch (scc) {
01045     default: NOT_REACHED();
01046     case SCC_NEWGRF_PRINT_DWORD:
01047     case SCC_NEWGRF_PRINT_SIGNED_WORD:
01048     case SCC_NEWGRF_PRINT_SIGNED_BYTE:
01049     case SCC_NEWGRF_PRINT_UNSIGNED_WORD:
01050       return SCC_COMMA;
01051 
01052     case SCC_NEWGRF_PRINT_HEX_BYTE:
01053     case SCC_NEWGRF_PRINT_HEX_WORD:
01054     case SCC_NEWGRF_PRINT_HEX_DWORD:
01055     case SCC_NEWGRF_PRINT_HEX_QWORD:
01056       return SCC_HEX;
01057 
01058     case SCC_NEWGRF_PRINT_DWORD_CURRENCY:
01059     case SCC_NEWGRF_PRINT_QWORD_CURRENCY:
01060       return SCC_CURRENCY;
01061 
01062     case SCC_NEWGRF_PRINT_STRING_ID:
01063       return SCC_NEWGRF_PRINT_STRING_ID;
01064 
01065     case SCC_NEWGRF_PRINT_DATE:
01066       return SCC_DATE_LONG;
01067 
01068     case SCC_NEWGRF_PRINT_MONTH_YEAR:
01069       return SCC_DATE_TINY;
01070 
01071     case SCC_NEWGRF_PRINT_WORD_SPEED:
01072       return SCC_VELOCITY;
01073 
01074     case SCC_NEWGRF_PRINT_WORD_VOLUME:
01075       return SCC_VOLUME;
01076 
01077     case SCC_NEWGRF_PRINT_WORD_WEIGHT:
01078       return SCC_WEIGHT;
01079 
01080     case SCC_NEWGRF_PRINT_WORD_STATION_NAME:
01081       return SCC_STATION_NAME;
01082 
01083     case SCC_NEWGRF_DISCARD_WORD:
01084     case SCC_NEWGRF_ROTATE_TOP_4_WORDS:
01085     case SCC_NEWGRF_PUSH_WORD:
01086     case SCC_NEWGRF_UNPRINT:
01087       return 0;
01088   }
01089 }