newgrf_text.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_text.cpp 22747 2011-08-14 15:23:10Z 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       /* In case there is no mapping, just ignore everything but the default.
00297        * A probable cause for this happening is when the language file has
00298        * been removed by the user and as such no mapping could be made. */
00299       size_t len = strlen(this->strings[0]);
00300       memcpy(d, this->strings[0], len);
00301       return d + len;
00302     }
00303 
00304     d += Utf8Encode(d, this->type);
00305 
00306     if (this->type == SCC_SWITCH_CASE) {
00307       /*
00308        * Format for case switch:
00309        * <NUM CASES> <CASE1> <LEN1> <STRING1> <CASE2> <LEN2> <STRING2> <CASE3> <LEN3> <STRING3> <STRINGDEFAULT>
00310        * Each LEN is printed using 2 bytes in big endian order.
00311        */
00312 
00313       /* "<NUM CASES>" */
00314       int count = 0;
00315       for (uint8 i = 0; i < _current_language->num_cases; i++) {
00316         /* Count the ones we have a mapped string for. */
00317         if (this->strings.Contains(lm->GetReverseMapping(i, false))) count++;
00318       }
00319       *d++ = count;
00320 
00321       for (uint8 i = 0; i < _current_language->num_cases; i++) {
00322         /* Resolve the string we're looking for. */
00323         int idx = lm->GetReverseMapping(i, false);
00324         if (!this->strings.Contains(idx)) continue;
00325         char *str = this->strings[idx];
00326 
00327         /* "<CASEn>" */
00328         *d++ = i + 1;
00329 
00330         /* "<LENn>" */
00331         size_t len = strlen(str) + 1;
00332         *d++ = GB(len, 8, 8);
00333         *d++ = GB(len, 0, 8);
00334 
00335         /* "<STRINGn>" */
00336         memcpy(d, str, len);
00337         d += len;
00338       }
00339 
00340       /* "<STRINGDEFAULT>" */
00341       size_t len = strlen(this->strings[0]) + 1;
00342       memcpy(d, this->strings[0], len);
00343       d += len;
00344     } else {
00345       if (this->type == SCC_PLURAL_LIST) {
00346         *d++ = lm->plural_form;
00347       }
00348 
00349       /*
00350        * Format for choice list:
00351        * <OFFSET> <NUM CHOICES> <LENs> <STRINGs>
00352        */
00353 
00354       /* "<OFFSET>" */
00355       *d++ = this->offset - 0x80;
00356 
00357       /* "<NUM CHOICES>" */
00358       int count = (this->type == SCC_GENDER_LIST ? _current_language->num_genders : LANGUAGE_MAX_PLURAL_FORMS);
00359       *d++ = count;
00360 
00361       /* "<LENs>" */
00362       for (int i = 0; i < count; i++) {
00363         int idx = (this->type == SCC_GENDER_LIST ? lm->GetReverseMapping(i, true) : i + 1);
00364         const char *str = this->strings[this->strings.Contains(idx) ? idx : 0];
00365         size_t len = strlen(str) + 1;
00366         if (len > 0xFF) grfmsg(1, "choice list string is too long");
00367         *d++ = GB(len, 0, 8);
00368       }
00369 
00370       /* "<STRINGs>" */
00371       for (int i = 0; i < count; i++) {
00372         int idx = (this->type == SCC_GENDER_LIST ? lm->GetReverseMapping(i, true) : i + 1);
00373         const char *str = this->strings[this->strings.Contains(idx) ? idx : 0];
00374         size_t len = strlen(str);
00375         memcpy(d, str, len);
00376         d += len;
00377         *d++ = '\0';
00378       }
00379     }
00380     return d;
00381   }
00382 };
00383 
00392 char *TranslateTTDPatchCodes(uint32 grfid, uint8 language_id, const char *str, int *olen)
00393 {
00394   char *tmp = MallocT<char>(strlen(str) * 10 + 1); // Allocate space to allow for expansion
00395   char *d = tmp;
00396   bool unicode = false;
00397   WChar c;
00398   size_t len = Utf8Decode(&c, str);
00399 
00400   /* Helper variable for a possible (string) mapping. */
00401   UnmappedChoiceList *mapping = NULL;
00402 
00403   if (c == NFO_UTF8_IDENTIFIER) {
00404     unicode = true;
00405     str += len;
00406   }
00407 
00408   for (;;) {
00409     if (unicode && Utf8EncodedCharLen(*str) != 0) {
00410       c = Utf8Consume(&str);
00411       /* 'Magic' range of control codes. */
00412       if (GB(c, 8, 8) == 0xE0) {
00413         c = GB(c, 0, 8);
00414       } else if (c >= 0x20) {
00415         if (!IsValidChar(c, CS_ALPHANUMERAL)) c = '?';
00416         d += Utf8Encode(d, c);
00417         continue;
00418       }
00419     } else {
00420       c = (byte)*str++;
00421     }
00422     if (c == '\0') break;
00423 
00424     switch (c) {
00425       case 0x01:
00426         if (str[0] == '\0') goto string_end;
00427         d += Utf8Encode(d, SCC_SETX);
00428         *d++ = *str++;
00429         break;
00430       case 0x0A: break;
00431       case 0x0D: *d++ = 0x0A; break;
00432       case 0x0E: d += Utf8Encode(d, SCC_TINYFONT); break;
00433       case 0x0F: d += Utf8Encode(d, SCC_BIGFONT); break;
00434       case 0x1F:
00435         if (str[0] == '\0' || str[1] == '\0') goto string_end;
00436         d += Utf8Encode(d, SCC_SETXY);
00437         *d++ = *str++;
00438         *d++ = *str++;
00439         break;
00440       case 0x7B:
00441       case 0x7C:
00442       case 0x7D:
00443       case 0x7E:
00444       case 0x7F:
00445       case 0x80: d += Utf8Encode(d, SCC_NEWGRF_PRINT_DWORD + c - 0x7B); break;
00446       case 0x81: {
00447         if (str[0] == '\0' || str[1] == '\0') goto string_end;
00448         StringID string;
00449         string  = ((uint8)*str++);
00450         string |= ((uint8)*str++) << 8;
00451         d += Utf8Encode(d, SCC_NEWGRF_STRINL);
00452         d += Utf8Encode(d, MapGRFStringID(grfid, string));
00453         break;
00454       }
00455       case 0x82:
00456       case 0x83:
00457       case 0x84: d += Utf8Encode(d, SCC_NEWGRF_PRINT_DATE + c - 0x82); break;
00458       case 0x85: d += Utf8Encode(d, SCC_NEWGRF_DISCARD_WORD);       break;
00459       case 0x86: d += Utf8Encode(d, SCC_NEWGRF_ROTATE_TOP_4_WORDS); break;
00460       case 0x87: d += Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_VOLUME);  break;
00461       case 0x88: d += Utf8Encode(d, SCC_BLUE);    break;
00462       case 0x89: d += Utf8Encode(d, SCC_SILVER);  break;
00463       case 0x8A: d += Utf8Encode(d, SCC_GOLD);    break;
00464       case 0x8B: d += Utf8Encode(d, SCC_RED);     break;
00465       case 0x8C: d += Utf8Encode(d, SCC_PURPLE);  break;
00466       case 0x8D: d += Utf8Encode(d, SCC_LTBROWN); break;
00467       case 0x8E: d += Utf8Encode(d, SCC_ORANGE);  break;
00468       case 0x8F: d += Utf8Encode(d, SCC_GREEN);   break;
00469       case 0x90: d += Utf8Encode(d, SCC_YELLOW);  break;
00470       case 0x91: d += Utf8Encode(d, SCC_DKGREEN); break;
00471       case 0x92: d += Utf8Encode(d, SCC_CREAM);   break;
00472       case 0x93: d += Utf8Encode(d, SCC_BROWN);   break;
00473       case 0x94: d += Utf8Encode(d, SCC_WHITE);   break;
00474       case 0x95: d += Utf8Encode(d, SCC_LTBLUE);  break;
00475       case 0x96: d += Utf8Encode(d, SCC_GRAY);    break;
00476       case 0x97: d += Utf8Encode(d, SCC_DKBLUE);  break;
00477       case 0x98: d += Utf8Encode(d, SCC_BLACK);   break;
00478       case 0x9A: {
00479         int code = *str++;
00480         switch (code) {
00481           case 0x00: goto string_end;
00482           case 0x01: d += Utf8Encode(d, SCC_NEWGRF_PRINT_QWORD_CURRENCY); break;
00483           /* 0x02: ignore next colour byte is not supported. It works on the final
00484            * string and as such hooks into the string drawing routine. At that
00485            * point many things already happened, such as splitting up of strings
00486            * when drawn over multiple lines or right-to-left translations, which
00487            * make the behaviour peculiar, e.g. only happening at specific width
00488            * of windows. Or we need to add another pass over the string to just
00489            * support this. As such it is not implemented in OpenTTD. */
00490           case 0x03: {
00491             if (str[0] == '\0' || str[1] == '\0') goto string_end;
00492             uint16 tmp  = ((uint8)*str++);
00493             tmp        |= ((uint8)*str++) << 8;
00494             d += Utf8Encode(d, SCC_NEWGRF_PUSH_WORD);
00495             d += Utf8Encode(d, tmp);
00496             break;
00497           }
00498           case 0x04:
00499             if (str[0] == '\0') goto string_end;
00500             d += Utf8Encode(d, SCC_NEWGRF_UNPRINT);
00501             d += Utf8Encode(d, *str++);
00502             break;
00503           case 0x06: d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_BYTE);          break;
00504           case 0x07: d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_WORD);          break;
00505           case 0x08: d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_DWORD);         break;
00506           /* 0x09, 0x0A are TTDPatch internal use only string codes. */
00507           case 0x0B: d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_QWORD);         break;
00508           case 0x0C: d += Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_STATION_NAME); break;
00509           case 0x0D: d += Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_WEIGHT);       break;
00510           case 0x0E:
00511           case 0x0F: {
00512             if (str[0] == '\0') goto string_end;
00513             const LanguageMap *lm = LanguageMap::GetLanguageMap(grfid, language_id);
00514             int index = *str++;
00515             int mapped = lm != NULL ? lm->GetMapping(index, code == 0x0E) : -1;
00516             if (mapped >= 0) {
00517               d += Utf8Encode(d, code == 0x0E ? SCC_GENDER_INDEX : SCC_SETCASE);
00518               d += Utf8Encode(d, mapped);
00519             }
00520             break;
00521           }
00522 
00523           case 0x10:
00524           case 0x11:
00525             if (str[0] == '\0') goto string_end;
00526             if (mapping == NULL) {
00527               if (code == 0x10) str++; // Skip the index
00528               grfmsg(1, "choice list %s marker found when not expected", code == 0x10 ? "next" : "default");
00529               break;
00530             } else {
00531               /* Terminate the previous string. */
00532               *d = '\0';
00533               int index = (code == 0x10 ? *str++ : 0);
00534               if (mapping->strings.Contains(index)) {
00535                 grfmsg(1, "duplicate choice list string, ignoring");
00536                 d++;
00537               } else {
00538                 d = mapping->strings[index] = MallocT<char>(strlen(str) * 10 + 1);
00539               }
00540             }
00541             break;
00542 
00543           case 0x12:
00544             if (mapping == NULL) {
00545               grfmsg(1, "choice list end marker found when not expected");
00546             } else {
00547               /* Terminate the previous string. */
00548               *d = '\0';
00549 
00550               /* Now we can start flushing everything and clean everything up. */
00551               d = mapping->Flush(LanguageMap::GetLanguageMap(grfid, language_id));
00552               delete mapping;
00553               mapping = NULL;
00554             }
00555             break;
00556 
00557           case 0x13:
00558           case 0x14:
00559           case 0x15:
00560             if (str[0] == '\0') goto string_end;
00561             if (mapping != NULL) {
00562               grfmsg(1, "choice lists can't be stacked, it's going to get messy now...");
00563               if (code != 0x14) str++;
00564             } else {
00565               static const StringControlCode mp[] = { SCC_GENDER_LIST, SCC_SWITCH_CASE, SCC_PLURAL_LIST };
00566               mapping = new UnmappedChoiceList(mp[code - 0x13], d, code == 0x14 ? 0 : *str++);
00567             }
00568             break;
00569 
00570           default:
00571             grfmsg(1, "missing handler for extended format code");
00572             break;
00573         }
00574         break;
00575       }
00576 
00577       case 0x9E: d += Utf8Encode(d, 0x20AC);             break; // Euro
00578       case 0x9F: d += Utf8Encode(d, 0x0178);             break; // Y with diaeresis
00579       case 0xA0: d += Utf8Encode(d, SCC_UPARROW);        break;
00580       case 0xAA: d += Utf8Encode(d, SCC_DOWNARROW);      break;
00581       case 0xAC: d += Utf8Encode(d, SCC_CHECKMARK);      break;
00582       case 0xAD: d += Utf8Encode(d, SCC_CROSS);          break;
00583       case 0xAF: d += Utf8Encode(d, SCC_RIGHTARROW);     break;
00584       case 0xB4: d += Utf8Encode(d, SCC_TRAIN);          break;
00585       case 0xB5: d += Utf8Encode(d, SCC_LORRY);          break;
00586       case 0xB6: d += Utf8Encode(d, SCC_BUS);            break;
00587       case 0xB7: d += Utf8Encode(d, SCC_PLANE);          break;
00588       case 0xB8: d += Utf8Encode(d, SCC_SHIP);           break;
00589       case 0xB9: d += Utf8Encode(d, SCC_SUPERSCRIPT_M1); break;
00590       case 0xBC: d += Utf8Encode(d, SCC_SMALLUPARROW);   break;
00591       case 0xBD: d += Utf8Encode(d, SCC_SMALLDOWNARROW); break;
00592       default:
00593         /* Validate any unhandled character */
00594         if (!IsValidChar(c, CS_ALPHANUMERAL)) c = '?';
00595         d += Utf8Encode(d, c);
00596         break;
00597     }
00598   }
00599 
00600 string_end:
00601   if (mapping != NULL) {
00602     grfmsg(1, "choice list was incomplete, the whole list is ignored");
00603     delete mapping;
00604   }
00605 
00606   *d = '\0';
00607   if (olen != NULL) *olen = d - tmp + 1;
00608   tmp = ReallocT(tmp, d - tmp + 1);
00609   return tmp;
00610 }
00611 
00617 void AddGRFTextToList(GRFText **list, GRFText *text_to_add)
00618 {
00619   GRFText **ptext, *text;
00620 
00621   /* Loop through all languages and see if we can replace a string */
00622   for (ptext = list; (text = *ptext) != NULL; ptext = &text->next) {
00623     if (text->langid == text_to_add->langid) {
00624       text_to_add->next = text->next;
00625       *ptext = text_to_add;
00626       delete text;
00627       return;
00628     }
00629   }
00630 
00631   /* If a string wasn't replaced, then we must append the new string */
00632   *ptext = text_to_add;
00633 }
00634 
00643 void AddGRFTextToList(struct GRFText **list, byte langid, uint32 grfid, const char *text_to_add)
00644 {
00645   int len;
00646   char *translatedtext = TranslateTTDPatchCodes(grfid, langid, text_to_add, &len);
00647   GRFText *newtext = GRFText::New(langid, translatedtext, len);
00648   free(translatedtext);
00649 
00650   AddGRFTextToList(list, newtext);
00651 }
00652 
00659 void AddGRFTextToList(struct GRFText **list, const char *text_to_add)
00660 {
00661   AddGRFTextToList(list, GRFText::New(0x7F, text_to_add, strlen(text_to_add) + 1));
00662 }
00663 
00669 GRFText *DuplicateGRFText(GRFText *orig)
00670 {
00671   GRFText *newtext = NULL;
00672   GRFText **ptext = &newtext;
00673   for (; orig != NULL; orig = orig->next) {
00674     *ptext = GRFText::Copy(orig);
00675     ptext = &(*ptext)->next;
00676   }
00677   return newtext;
00678 }
00679 
00683 StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid_to_add, bool new_scheme, const char *text_to_add, StringID def_string)
00684 {
00685   char *translatedtext;
00686   uint id;
00687 
00688   /* When working with the old language scheme (grf_version is less than 7) and
00689    * English or American is among the set bits, simply add it as English in
00690    * the new scheme, i.e. as langid = 1.
00691    * If English is set, it is pretty safe to assume the translations are not
00692    * actually translated.
00693    */
00694   if (!new_scheme) {
00695     if (langid_to_add & (GRFLB_AMERICAN | GRFLB_ENGLISH)) {
00696       langid_to_add = GRFLX_ENGLISH;
00697     } else {
00698       StringID ret = STR_EMPTY;
00699       if (langid_to_add & GRFLB_GERMAN)  ret = AddGRFString(grfid, stringid, GRFLX_GERMAN,  true, text_to_add, def_string);
00700       if (langid_to_add & GRFLB_FRENCH)  ret = AddGRFString(grfid, stringid, GRFLX_FRENCH,  true, text_to_add, def_string);
00701       if (langid_to_add & GRFLB_SPANISH) ret = AddGRFString(grfid, stringid, GRFLX_SPANISH, true, text_to_add, def_string);
00702       return ret;
00703     }
00704   }
00705 
00706   for (id = 0; id < _num_grf_texts; id++) {
00707     if (_grf_text[id].grfid == grfid && _grf_text[id].stringid == stringid) {
00708       break;
00709     }
00710   }
00711 
00712   /* Too many strings allocated, return empty */
00713   if (id == lengthof(_grf_text)) return STR_EMPTY;
00714 
00715   int len;
00716   translatedtext = TranslateTTDPatchCodes(grfid, langid_to_add, text_to_add, &len);
00717 
00718   GRFText *newtext = GRFText::New(langid_to_add, translatedtext, len);
00719 
00720   free(translatedtext);
00721 
00722   /* If we didn't find our stringid and grfid in the list, allocate a new id */
00723   if (id == _num_grf_texts) _num_grf_texts++;
00724 
00725   if (_grf_text[id].textholder == NULL) {
00726     _grf_text[id].grfid      = grfid;
00727     _grf_text[id].stringid   = stringid;
00728     _grf_text[id].def_string = def_string;
00729   }
00730   AddGRFTextToList(&_grf_text[id].textholder, newtext);
00731 
00732   grfmsg(3, "Added 0x%X: grfid %08X string 0x%X lang 0x%X string '%s'", id, grfid, stringid, newtext->langid, newtext->text);
00733 
00734   return (GRFTAB << TABSIZE) + id;
00735 }
00736 
00737 /* Used to remember the grfid that the last retrieved string came from */
00738 static uint32 _last_grfid = 0;
00739 
00743 StringID GetGRFStringID(uint32 grfid, uint16 stringid)
00744 {
00745   uint id;
00746 
00747   /* grfid is zero when we're being called via an include */
00748   if (grfid == 0) grfid = _last_grfid;
00749 
00750   for (id = 0; id < _num_grf_texts; id++) {
00751     if (_grf_text[id].grfid == grfid && _grf_text[id].stringid == stringid) {
00752       return (GRFTAB << TABSIZE) + id;
00753     }
00754   }
00755 
00756   return STR_UNDEFINED;
00757 }
00758 
00759 
00767 const char *GetGRFStringFromGRFText(const GRFText *text)
00768 {
00769   const char *default_text = NULL;
00770 
00771   /* Search the list of lang-strings of this stringid for current lang */
00772   for (; text != NULL; text = text->next) {
00773     if (text->langid == _currentLangID) return text->text;
00774 
00775     /* If the current string is English or American, set it as the
00776      * fallback language if the specific language isn't available. */
00777     if (text->langid == GRFLX_UNSPECIFIED || (default_text == NULL && (text->langid == GRFLX_ENGLISH || text->langid == GRFLX_AMERICAN))) {
00778       default_text = text->text;
00779     }
00780   }
00781 
00782   return default_text;
00783 }
00784 
00788 const char *GetGRFStringPtr(uint16 stringid)
00789 {
00790   assert(_grf_text[stringid].grfid != 0);
00791 
00792   /* Remember this grfid in case the string has included text */
00793   _last_grfid = _grf_text[stringid].grfid;
00794 
00795   const char *str = GetGRFStringFromGRFText(_grf_text[stringid].textholder);
00796   if (str != NULL) return str;
00797 
00798   /* Use the default string ID if the fallback string isn't available */
00799   return GetStringPtr(_grf_text[stringid].def_string);
00800 }
00801 
00810 void SetCurrentGrfLangID(byte language_id)
00811 {
00812   _currentLangID = language_id;
00813 }
00814 
00815 bool CheckGrfLangID(byte lang_id, byte grf_version)
00816 {
00817   if (grf_version < 7) {
00818     switch (_currentLangID) {
00819       case GRFLX_GERMAN:  return (lang_id & GRFLB_GERMAN)  != 0;
00820       case GRFLX_FRENCH:  return (lang_id & GRFLB_FRENCH)  != 0;
00821       case GRFLX_SPANISH: return (lang_id & GRFLB_SPANISH) != 0;
00822       default:            return (lang_id & (GRFLB_ENGLISH | GRFLB_AMERICAN)) != 0;
00823     }
00824   }
00825 
00826   return (lang_id == _currentLangID || lang_id == GRFLX_UNSPECIFIED);
00827 }
00828 
00833 void CleanUpGRFText(GRFText *grftext)
00834 {
00835   while (grftext != NULL) {
00836     GRFText *grftext2 = grftext->next;
00837     delete grftext;
00838     grftext = grftext2;
00839   }
00840 }
00841 
00846 void CleanUpStrings()
00847 {
00848   uint id;
00849 
00850   for (id = 0; id < _num_grf_texts; id++) {
00851     CleanUpGRFText(_grf_text[id].textholder);
00852     _grf_text[id].grfid      = 0;
00853     _grf_text[id].stringid   = 0;
00854     _grf_text[id].textholder = NULL;
00855   }
00856 
00857   _num_grf_texts = 0;
00858 }
00859 
00860 struct TextRefStack {
00861   byte stack[0x30];
00862   byte position;
00863   bool used;
00864 
00865   TextRefStack() : used(false) {}
00866 
00867   TextRefStack(const TextRefStack &stack) :
00868     position(stack.position),
00869     used(stack.used)
00870   {
00871     memcpy(this->stack, stack.stack, sizeof(this->stack));
00872   }
00873 
00874   uint8  PopUnsignedByte()  { assert(this->position < lengthof(this->stack)); return this->stack[this->position++]; }
00875   int8   PopSignedByte()    { return (int8)this->PopUnsignedByte(); }
00876 
00877   uint16 PopUnsignedWord()
00878   {
00879     uint16 val = this->PopUnsignedByte();
00880     return val | (this->PopUnsignedByte() << 8);
00881   }
00882   int16  PopSignedWord()    { return (int32)this->PopUnsignedWord(); }
00883 
00884   uint32 PopUnsignedDWord()
00885   {
00886     uint32 val = this->PopUnsignedWord();
00887     return val | (this->PopUnsignedWord() << 16);
00888   }
00889   int32  PopSignedDWord()   { return (int32)this->PopUnsignedDWord(); }
00890 
00891   uint64 PopUnsignedQWord()
00892   {
00893     uint64 val = this->PopUnsignedDWord();
00894     return val | (((uint64)this->PopUnsignedDWord()) << 32);
00895   }
00896   int64  PopSignedQWord()   { return (int64)this->PopUnsignedQWord(); }
00897 
00899   void RotateTop4Words()
00900   {
00901     byte tmp[2];
00902     for (int i = 0; i  < 2; i++) tmp[i] = this->stack[this->position + i + 6];
00903     for (int i = 5; i >= 0; i--) this->stack[this->position + i + 2] = this->stack[this->position + i];
00904     for (int i = 0; i  < 2; i++) this->stack[this->position + i] = tmp[i];
00905   }
00906 
00907   void PushWord(uint16 word)
00908   {
00909     if (this->position >= 2) {
00910       this->position -= 2;
00911     } else {
00912       for (int i = lengthof(stack) - 1; i >= this->position + 2; i--) {
00913         this->stack[i] = this->stack[i - 2];
00914       }
00915     }
00916     this->stack[this->position]     = GB(word, 0, 8);
00917     this->stack[this->position + 1] = GB(word, 8, 8);
00918   }
00919 
00920   void ResetStack()  { this->position = 0; this->used = true; }
00921   void RewindStack() { this->position = 0; }
00922 };
00923 
00925 static TextRefStack _newgrf_textrefstack;
00926 
00931 bool UsingNewGRFTextStack()
00932 {
00933   return _newgrf_textrefstack.used;
00934 }
00935 
00940 struct TextRefStack *CreateTextRefStackBackup()
00941 {
00942   return new TextRefStack(_newgrf_textrefstack);
00943 }
00944 
00949 void RestoreTextRefStackBackup(struct TextRefStack *backup)
00950 {
00951   _newgrf_textrefstack = *backup;
00952   delete backup;
00953 }
00954 
00972 void StartTextRefStackUsage(byte numEntries, const uint32 *values)
00973 {
00974   extern TemporaryStorageArray<int32, 0x110> _temp_store;
00975 
00976   _newgrf_textrefstack.ResetStack();
00977 
00978   byte *p = _newgrf_textrefstack.stack;
00979   for (uint i = 0; i < numEntries; i++) {
00980     uint32 value = values != NULL ? values[i] : _temp_store.Get(0x100 + i);
00981     for (uint j = 0; j < 32; j += 8) {
00982       *p = GB(value, j, 8);
00983       p++;
00984     }
00985   }
00986 }
00987 
00989 void StopTextRefStackUsage()
00990 {
00991   _newgrf_textrefstack.used = false;
00992 }
00993 
00994 void RewindTextRefStack()
00995 {
00996   _newgrf_textrefstack.RewindStack();
00997 }
00998 
01007 uint RemapNewGRFStringControlCode(uint scc, char *buf_start, char **buff, const char **str, int64 *argv)
01008 {
01009   if (_newgrf_textrefstack.used) {
01010     switch (scc) {
01011       default: NOT_REACHED();
01012       case SCC_NEWGRF_PRINT_SIGNED_BYTE:    *argv = _newgrf_textrefstack.PopSignedByte();    break;
01013       case SCC_NEWGRF_PRINT_SIGNED_WORD:    *argv = _newgrf_textrefstack.PopSignedWord();    break;
01014       case SCC_NEWGRF_PRINT_QWORD_CURRENCY: *argv = _newgrf_textrefstack.PopUnsignedQWord(); break;
01015 
01016       case SCC_NEWGRF_PRINT_DWORD_CURRENCY:
01017       case SCC_NEWGRF_PRINT_DWORD:          *argv = _newgrf_textrefstack.PopSignedDWord();   break;
01018 
01019       case SCC_NEWGRF_PRINT_HEX_BYTE:       *argv = _newgrf_textrefstack.PopUnsignedByte();  break;
01020       case SCC_NEWGRF_PRINT_HEX_DWORD:      *argv = _newgrf_textrefstack.PopUnsignedDWord(); break;
01021       case SCC_NEWGRF_PRINT_HEX_QWORD:      *argv = _newgrf_textrefstack.PopSignedQWord(); break;
01022 
01023       case SCC_NEWGRF_PRINT_HEX_WORD:
01024       case SCC_NEWGRF_PRINT_WORD_SPEED:
01025       case SCC_NEWGRF_PRINT_WORD_VOLUME:
01026       case SCC_NEWGRF_PRINT_WORD_WEIGHT:
01027       case SCC_NEWGRF_PRINT_WORD_STATION_NAME:
01028       case SCC_NEWGRF_PRINT_UNSIGNED_WORD:  *argv = _newgrf_textrefstack.PopUnsignedWord();  break;
01029 
01030       case SCC_NEWGRF_PRINT_DATE:
01031       case SCC_NEWGRF_PRINT_MONTH_YEAR:     *argv = _newgrf_textrefstack.PopSignedWord() + DAYS_TILL_ORIGINAL_BASE_YEAR; break;
01032 
01033       case SCC_NEWGRF_DISCARD_WORD:         _newgrf_textrefstack.PopUnsignedWord(); break;
01034 
01035       case SCC_NEWGRF_ROTATE_TOP_4_WORDS:   _newgrf_textrefstack.RotateTop4Words(); break;
01036       case SCC_NEWGRF_PUSH_WORD:            _newgrf_textrefstack.PushWord(Utf8Consume(str)); break;
01037       case SCC_NEWGRF_UNPRINT:              *buff = max(*buff - Utf8Consume(str), buf_start); break;
01038 
01039       case SCC_NEWGRF_PRINT_STRING_ID:
01040         *argv = TTDPStringIDToOTTDStringIDMapping(_newgrf_textrefstack.PopUnsignedWord());
01041         break;
01042     }
01043   }
01044 
01045   switch (scc) {
01046     default: NOT_REACHED();
01047     case SCC_NEWGRF_PRINT_DWORD:
01048     case SCC_NEWGRF_PRINT_SIGNED_WORD:
01049     case SCC_NEWGRF_PRINT_SIGNED_BYTE:
01050     case SCC_NEWGRF_PRINT_UNSIGNED_WORD:
01051       return SCC_COMMA;
01052 
01053     case SCC_NEWGRF_PRINT_HEX_BYTE:
01054     case SCC_NEWGRF_PRINT_HEX_WORD:
01055     case SCC_NEWGRF_PRINT_HEX_DWORD:
01056     case SCC_NEWGRF_PRINT_HEX_QWORD:
01057       return SCC_HEX;
01058 
01059     case SCC_NEWGRF_PRINT_DWORD_CURRENCY:
01060     case SCC_NEWGRF_PRINT_QWORD_CURRENCY:
01061       return SCC_CURRENCY;
01062 
01063     case SCC_NEWGRF_PRINT_STRING_ID:
01064       return SCC_NEWGRF_PRINT_STRING_ID;
01065 
01066     case SCC_NEWGRF_PRINT_DATE:
01067       return SCC_DATE_LONG;
01068 
01069     case SCC_NEWGRF_PRINT_MONTH_YEAR:
01070       return SCC_DATE_TINY;
01071 
01072     case SCC_NEWGRF_PRINT_WORD_SPEED:
01073       return SCC_VELOCITY;
01074 
01075     case SCC_NEWGRF_PRINT_WORD_VOLUME:
01076       return SCC_VOLUME;
01077 
01078     case SCC_NEWGRF_PRINT_WORD_WEIGHT:
01079       return SCC_WEIGHT;
01080 
01081     case SCC_NEWGRF_PRINT_WORD_STATION_NAME:
01082       return SCC_STATION_NAME;
01083 
01084     case SCC_NEWGRF_DISCARD_WORD:
01085     case SCC_NEWGRF_ROTATE_TOP_4_WORDS:
01086     case SCC_NEWGRF_PUSH_WORD:
01087     case SCC_NEWGRF_UNPRINT:
01088       return 0;
01089   }
01090 }