newgrf_text.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_text.cpp 22995 2011-10-04 20:22:46Z 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)) 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         /* Limit the length of the string we copy to 0xFE. The length is written above
00375          * as a byte and we need room for the final '\0'. */
00376         size_t len = min(0xFE, strlen(str));
00377         memcpy(d, str, len);
00378         d += len;
00379         *d++ = '\0';
00380       }
00381     }
00382     return d;
00383   }
00384 };
00385 
00395 char *TranslateTTDPatchCodes(uint32 grfid, uint8 language_id, bool allow_newlines, const char *str, int *olen)
00396 {
00397   char *tmp = MallocT<char>(strlen(str) * 10 + 1); // Allocate space to allow for expansion
00398   char *d = tmp;
00399   bool unicode = false;
00400   WChar c;
00401   size_t len = Utf8Decode(&c, str);
00402 
00403   /* Helper variable for a possible (string) mapping. */
00404   UnmappedChoiceList *mapping = NULL;
00405 
00406   if (c == NFO_UTF8_IDENTIFIER) {
00407     unicode = true;
00408     str += len;
00409   }
00410 
00411   for (;;) {
00412     if (unicode && Utf8EncodedCharLen(*str) != 0) {
00413       c = Utf8Consume(&str);
00414       /* 'Magic' range of control codes. */
00415       if (GB(c, 8, 8) == 0xE0) {
00416         c = GB(c, 0, 8);
00417       } else if (c >= 0x20) {
00418         if (!IsValidChar(c, CS_ALPHANUMERAL)) c = '?';
00419         d += Utf8Encode(d, c);
00420         continue;
00421       }
00422     } else {
00423       c = (byte)*str++;
00424     }
00425     if (c == '\0') break;
00426 
00427     switch (c) {
00428       case 0x01:
00429         if (str[0] == '\0') goto string_end;
00430         d += Utf8Encode(d, SCC_SETX);
00431         *d++ = *str++;
00432         break;
00433       case 0x0A: break;
00434       case 0x0D:
00435         if (allow_newlines) {
00436           *d++ = 0x0A;
00437         } else {
00438           grfmsg(1, "Detected newline in string that does not allow one");
00439         }
00440         break;
00441       case 0x0E: d += Utf8Encode(d, SCC_TINYFONT); break;
00442       case 0x0F: d += Utf8Encode(d, SCC_BIGFONT); break;
00443       case 0x1F:
00444         if (str[0] == '\0' || str[1] == '\0') goto string_end;
00445         d += Utf8Encode(d, SCC_SETXY);
00446         *d++ = *str++;
00447         *d++ = *str++;
00448         break;
00449       case 0x7B:
00450       case 0x7C:
00451       case 0x7D:
00452       case 0x7E:
00453       case 0x7F:
00454       case 0x80: d += Utf8Encode(d, SCC_NEWGRF_PRINT_DWORD + c - 0x7B); break;
00455       case 0x81: {
00456         if (str[0] == '\0' || str[1] == '\0') goto string_end;
00457         StringID string;
00458         string  = ((uint8)*str++);
00459         string |= ((uint8)*str++) << 8;
00460         d += Utf8Encode(d, SCC_NEWGRF_STRINL);
00461         d += Utf8Encode(d, MapGRFStringID(grfid, string));
00462         break;
00463       }
00464       case 0x82:
00465       case 0x83:
00466       case 0x84: d += Utf8Encode(d, SCC_NEWGRF_PRINT_DATE + c - 0x82); break;
00467       case 0x85: d += Utf8Encode(d, SCC_NEWGRF_DISCARD_WORD);       break;
00468       case 0x86: d += Utf8Encode(d, SCC_NEWGRF_ROTATE_TOP_4_WORDS); break;
00469       case 0x87: d += Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_VOLUME);  break;
00470       case 0x88: d += Utf8Encode(d, SCC_BLUE);    break;
00471       case 0x89: d += Utf8Encode(d, SCC_SILVER);  break;
00472       case 0x8A: d += Utf8Encode(d, SCC_GOLD);    break;
00473       case 0x8B: d += Utf8Encode(d, SCC_RED);     break;
00474       case 0x8C: d += Utf8Encode(d, SCC_PURPLE);  break;
00475       case 0x8D: d += Utf8Encode(d, SCC_LTBROWN); break;
00476       case 0x8E: d += Utf8Encode(d, SCC_ORANGE);  break;
00477       case 0x8F: d += Utf8Encode(d, SCC_GREEN);   break;
00478       case 0x90: d += Utf8Encode(d, SCC_YELLOW);  break;
00479       case 0x91: d += Utf8Encode(d, SCC_DKGREEN); break;
00480       case 0x92: d += Utf8Encode(d, SCC_CREAM);   break;
00481       case 0x93: d += Utf8Encode(d, SCC_BROWN);   break;
00482       case 0x94: d += Utf8Encode(d, SCC_WHITE);   break;
00483       case 0x95: d += Utf8Encode(d, SCC_LTBLUE);  break;
00484       case 0x96: d += Utf8Encode(d, SCC_GRAY);    break;
00485       case 0x97: d += Utf8Encode(d, SCC_DKBLUE);  break;
00486       case 0x98: d += Utf8Encode(d, SCC_BLACK);   break;
00487       case 0x9A: {
00488         int code = *str++;
00489         switch (code) {
00490           case 0x00: goto string_end;
00491           case 0x01: d += Utf8Encode(d, SCC_NEWGRF_PRINT_QWORD_CURRENCY); break;
00492           /* 0x02: ignore next colour byte is not supported. It works on the final
00493            * string and as such hooks into the string drawing routine. At that
00494            * point many things already happened, such as splitting up of strings
00495            * when drawn over multiple lines or right-to-left translations, which
00496            * make the behaviour peculiar, e.g. only happening at specific width
00497            * of windows. Or we need to add another pass over the string to just
00498            * support this. As such it is not implemented in OpenTTD. */
00499           case 0x03: {
00500             if (str[0] == '\0' || str[1] == '\0') goto string_end;
00501             uint16 tmp  = ((uint8)*str++);
00502             tmp        |= ((uint8)*str++) << 8;
00503             d += Utf8Encode(d, SCC_NEWGRF_PUSH_WORD);
00504             d += Utf8Encode(d, tmp);
00505             break;
00506           }
00507           case 0x04:
00508             if (str[0] == '\0') goto string_end;
00509             d += Utf8Encode(d, SCC_NEWGRF_UNPRINT);
00510             d += Utf8Encode(d, *str++);
00511             break;
00512           case 0x06: d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_BYTE);          break;
00513           case 0x07: d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_WORD);          break;
00514           case 0x08: d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_DWORD);         break;
00515           /* 0x09, 0x0A are TTDPatch internal use only string codes. */
00516           case 0x0B: d += Utf8Encode(d, SCC_NEWGRF_PRINT_HEX_QWORD);         break;
00517           case 0x0C: d += Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_STATION_NAME); break;
00518           case 0x0D: d += Utf8Encode(d, SCC_NEWGRF_PRINT_WORD_WEIGHT);       break;
00519           case 0x0E:
00520           case 0x0F: {
00521             if (str[0] == '\0') goto string_end;
00522             const LanguageMap *lm = LanguageMap::GetLanguageMap(grfid, language_id);
00523             int index = *str++;
00524             int mapped = lm != NULL ? lm->GetMapping(index, code == 0x0E) : -1;
00525             if (mapped >= 0) {
00526               d += Utf8Encode(d, code == 0x0E ? SCC_GENDER_INDEX : SCC_SETCASE);
00527               d += Utf8Encode(d, mapped);
00528             }
00529             break;
00530           }
00531 
00532           case 0x10:
00533           case 0x11:
00534             if (str[0] == '\0') goto string_end;
00535             if (mapping == NULL) {
00536               if (code == 0x10) str++; // Skip the index
00537               grfmsg(1, "choice list %s marker found when not expected", code == 0x10 ? "next" : "default");
00538               break;
00539             } else {
00540               /* Terminate the previous string. */
00541               *d = '\0';
00542               int index = (code == 0x10 ? *str++ : 0);
00543               if (mapping->strings.Contains(index)) {
00544                 grfmsg(1, "duplicate choice list string, ignoring");
00545                 d++;
00546               } else {
00547                 d = mapping->strings[index] = MallocT<char>(strlen(str) * 10 + 1);
00548               }
00549             }
00550             break;
00551 
00552           case 0x12:
00553             if (mapping == NULL) {
00554               grfmsg(1, "choice list end marker found when not expected");
00555             } else {
00556               /* Terminate the previous string. */
00557               *d = '\0';
00558 
00559               /* Now we can start flushing everything and clean everything up. */
00560               d = mapping->Flush(LanguageMap::GetLanguageMap(grfid, language_id));
00561               delete mapping;
00562               mapping = NULL;
00563             }
00564             break;
00565 
00566           case 0x13:
00567           case 0x14:
00568           case 0x15:
00569             if (str[0] == '\0') goto string_end;
00570             if (mapping != NULL) {
00571               grfmsg(1, "choice lists can't be stacked, it's going to get messy now...");
00572               if (code != 0x14) str++;
00573             } else {
00574               static const StringControlCode mp[] = { SCC_GENDER_LIST, SCC_SWITCH_CASE, SCC_PLURAL_LIST };
00575               mapping = new UnmappedChoiceList(mp[code - 0x13], d, code == 0x14 ? 0 : *str++);
00576             }
00577             break;
00578 
00579           default:
00580             grfmsg(1, "missing handler for extended format code");
00581             break;
00582         }
00583         break;
00584       }
00585 
00586       case 0x9E: d += Utf8Encode(d, 0x20AC);             break; // Euro
00587       case 0x9F: d += Utf8Encode(d, 0x0178);             break; // Y with diaeresis
00588       case 0xA0: d += Utf8Encode(d, SCC_UPARROW);        break;
00589       case 0xAA: d += Utf8Encode(d, SCC_DOWNARROW);      break;
00590       case 0xAC: d += Utf8Encode(d, SCC_CHECKMARK);      break;
00591       case 0xAD: d += Utf8Encode(d, SCC_CROSS);          break;
00592       case 0xAF: d += Utf8Encode(d, SCC_RIGHTARROW);     break;
00593       case 0xB4: d += Utf8Encode(d, SCC_TRAIN);          break;
00594       case 0xB5: d += Utf8Encode(d, SCC_LORRY);          break;
00595       case 0xB6: d += Utf8Encode(d, SCC_BUS);            break;
00596       case 0xB7: d += Utf8Encode(d, SCC_PLANE);          break;
00597       case 0xB8: d += Utf8Encode(d, SCC_SHIP);           break;
00598       case 0xB9: d += Utf8Encode(d, SCC_SUPERSCRIPT_M1); break;
00599       case 0xBC: d += Utf8Encode(d, SCC_SMALLUPARROW);   break;
00600       case 0xBD: d += Utf8Encode(d, SCC_SMALLDOWNARROW); break;
00601       default:
00602         /* Validate any unhandled character */
00603         if (!IsValidChar(c, CS_ALPHANUMERAL)) c = '?';
00604         d += Utf8Encode(d, c);
00605         break;
00606     }
00607   }
00608 
00609 string_end:
00610   if (mapping != NULL) {
00611     grfmsg(1, "choice list was incomplete, the whole list is ignored");
00612     delete mapping;
00613   }
00614 
00615   *d = '\0';
00616   if (olen != NULL) *olen = d - tmp + 1;
00617   tmp = ReallocT(tmp, d - tmp + 1);
00618   return tmp;
00619 }
00620 
00626 void AddGRFTextToList(GRFText **list, GRFText *text_to_add)
00627 {
00628   GRFText **ptext, *text;
00629 
00630   /* Loop through all languages and see if we can replace a string */
00631   for (ptext = list; (text = *ptext) != NULL; ptext = &text->next) {
00632     if (text->langid == text_to_add->langid) {
00633       text_to_add->next = text->next;
00634       *ptext = text_to_add;
00635       delete text;
00636       return;
00637     }
00638   }
00639 
00640   /* If a string wasn't replaced, then we must append the new string */
00641   *ptext = text_to_add;
00642 }
00643 
00653 void AddGRFTextToList(struct GRFText **list, byte langid, uint32 grfid, bool allow_newlines, const char *text_to_add)
00654 {
00655   int len;
00656   char *translatedtext = TranslateTTDPatchCodes(grfid, langid, allow_newlines, text_to_add, &len);
00657   GRFText *newtext = GRFText::New(langid, translatedtext, len);
00658   free(translatedtext);
00659 
00660   AddGRFTextToList(list, newtext);
00661 }
00662 
00669 void AddGRFTextToList(struct GRFText **list, const char *text_to_add)
00670 {
00671   AddGRFTextToList(list, GRFText::New(0x7F, text_to_add, strlen(text_to_add) + 1));
00672 }
00673 
00679 GRFText *DuplicateGRFText(GRFText *orig)
00680 {
00681   GRFText *newtext = NULL;
00682   GRFText **ptext = &newtext;
00683   for (; orig != NULL; orig = orig->next) {
00684     *ptext = GRFText::Copy(orig);
00685     ptext = &(*ptext)->next;
00686   }
00687   return newtext;
00688 }
00689 
00693 StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid_to_add, bool new_scheme, bool allow_newlines, const char *text_to_add, StringID def_string)
00694 {
00695   char *translatedtext;
00696   uint id;
00697 
00698   /* When working with the old language scheme (grf_version is less than 7) and
00699    * English or American is among the set bits, simply add it as English in
00700    * the new scheme, i.e. as langid = 1.
00701    * If English is set, it is pretty safe to assume the translations are not
00702    * actually translated.
00703    */
00704   if (!new_scheme) {
00705     if (langid_to_add & (GRFLB_AMERICAN | GRFLB_ENGLISH)) {
00706       langid_to_add = GRFLX_ENGLISH;
00707     } else {
00708       StringID ret = STR_EMPTY;
00709       if (langid_to_add & GRFLB_GERMAN)  ret = AddGRFString(grfid, stringid, GRFLX_GERMAN,  true, allow_newlines, text_to_add, def_string);
00710       if (langid_to_add & GRFLB_FRENCH)  ret = AddGRFString(grfid, stringid, GRFLX_FRENCH,  true, allow_newlines, text_to_add, def_string);
00711       if (langid_to_add & GRFLB_SPANISH) ret = AddGRFString(grfid, stringid, GRFLX_SPANISH, true, allow_newlines, text_to_add, def_string);
00712       return ret;
00713     }
00714   }
00715 
00716   for (id = 0; id < _num_grf_texts; id++) {
00717     if (_grf_text[id].grfid == grfid && _grf_text[id].stringid == stringid) {
00718       break;
00719     }
00720   }
00721 
00722   /* Too many strings allocated, return empty */
00723   if (id == lengthof(_grf_text)) return STR_EMPTY;
00724 
00725   int len;
00726   translatedtext = TranslateTTDPatchCodes(grfid, langid_to_add, allow_newlines, text_to_add, &len);
00727 
00728   GRFText *newtext = GRFText::New(langid_to_add, translatedtext, len);
00729 
00730   free(translatedtext);
00731 
00732   /* If we didn't find our stringid and grfid in the list, allocate a new id */
00733   if (id == _num_grf_texts) _num_grf_texts++;
00734 
00735   if (_grf_text[id].textholder == NULL) {
00736     _grf_text[id].grfid      = grfid;
00737     _grf_text[id].stringid   = stringid;
00738     _grf_text[id].def_string = def_string;
00739   }
00740   AddGRFTextToList(&_grf_text[id].textholder, newtext);
00741 
00742   grfmsg(3, "Added 0x%X: grfid %08X string 0x%X lang 0x%X string '%s'", id, grfid, stringid, newtext->langid, newtext->text);
00743 
00744   return (GRFTAB << TABSIZE) + id;
00745 }
00746 
00747 /* Used to remember the grfid that the last retrieved string came from */
00748 static uint32 _last_grfid = 0;
00749 
00753 StringID GetGRFStringID(uint32 grfid, uint16 stringid)
00754 {
00755   uint id;
00756 
00757   /* grfid is zero when we're being called via an include */
00758   if (grfid == 0) grfid = _last_grfid;
00759 
00760   for (id = 0; id < _num_grf_texts; id++) {
00761     if (_grf_text[id].grfid == grfid && _grf_text[id].stringid == stringid) {
00762       return (GRFTAB << TABSIZE) + id;
00763     }
00764   }
00765 
00766   return STR_UNDEFINED;
00767 }
00768 
00769 
00777 const char *GetGRFStringFromGRFText(const GRFText *text)
00778 {
00779   const char *default_text = NULL;
00780 
00781   /* Search the list of lang-strings of this stringid for current lang */
00782   for (; text != NULL; text = text->next) {
00783     if (text->langid == _currentLangID) return text->text;
00784 
00785     /* If the current string is English or American, set it as the
00786      * fallback language if the specific language isn't available. */
00787     if (text->langid == GRFLX_UNSPECIFIED || (default_text == NULL && (text->langid == GRFLX_ENGLISH || text->langid == GRFLX_AMERICAN))) {
00788       default_text = text->text;
00789     }
00790   }
00791 
00792   return default_text;
00793 }
00794 
00798 const char *GetGRFStringPtr(uint16 stringid)
00799 {
00800   assert(_grf_text[stringid].grfid != 0);
00801 
00802   /* Remember this grfid in case the string has included text */
00803   _last_grfid = _grf_text[stringid].grfid;
00804 
00805   const char *str = GetGRFStringFromGRFText(_grf_text[stringid].textholder);
00806   if (str != NULL) return str;
00807 
00808   /* Use the default string ID if the fallback string isn't available */
00809   return GetStringPtr(_grf_text[stringid].def_string);
00810 }
00811 
00820 void SetCurrentGrfLangID(byte language_id)
00821 {
00822   _currentLangID = language_id;
00823 }
00824 
00825 bool CheckGrfLangID(byte lang_id, byte grf_version)
00826 {
00827   if (grf_version < 7) {
00828     switch (_currentLangID) {
00829       case GRFLX_GERMAN:  return (lang_id & GRFLB_GERMAN)  != 0;
00830       case GRFLX_FRENCH:  return (lang_id & GRFLB_FRENCH)  != 0;
00831       case GRFLX_SPANISH: return (lang_id & GRFLB_SPANISH) != 0;
00832       default:            return (lang_id & (GRFLB_ENGLISH | GRFLB_AMERICAN)) != 0;
00833     }
00834   }
00835 
00836   return (lang_id == _currentLangID || lang_id == GRFLX_UNSPECIFIED);
00837 }
00838 
00843 void CleanUpGRFText(GRFText *grftext)
00844 {
00845   while (grftext != NULL) {
00846     GRFText *grftext2 = grftext->next;
00847     delete grftext;
00848     grftext = grftext2;
00849   }
00850 }
00851 
00856 void CleanUpStrings()
00857 {
00858   uint id;
00859 
00860   for (id = 0; id < _num_grf_texts; id++) {
00861     CleanUpGRFText(_grf_text[id].textholder);
00862     _grf_text[id].grfid      = 0;
00863     _grf_text[id].stringid   = 0;
00864     _grf_text[id].textholder = NULL;
00865   }
00866 
00867   _num_grf_texts = 0;
00868 }
00869 
00870 struct TextRefStack {
00871   byte stack[0x30];
00872   byte position;
00873   bool used;
00874 
00875   TextRefStack() : used(false) {}
00876 
00877   TextRefStack(const TextRefStack &stack) :
00878     position(stack.position),
00879     used(stack.used)
00880   {
00881     memcpy(this->stack, stack.stack, sizeof(this->stack));
00882   }
00883 
00884   uint8  PopUnsignedByte()  { assert(this->position < lengthof(this->stack)); return this->stack[this->position++]; }
00885   int8   PopSignedByte()    { return (int8)this->PopUnsignedByte(); }
00886 
00887   uint16 PopUnsignedWord()
00888   {
00889     uint16 val = this->PopUnsignedByte();
00890     return val | (this->PopUnsignedByte() << 8);
00891   }
00892   int16  PopSignedWord()    { return (int32)this->PopUnsignedWord(); }
00893 
00894   uint32 PopUnsignedDWord()
00895   {
00896     uint32 val = this->PopUnsignedWord();
00897     return val | (this->PopUnsignedWord() << 16);
00898   }
00899   int32  PopSignedDWord()   { return (int32)this->PopUnsignedDWord(); }
00900 
00901   uint64 PopUnsignedQWord()
00902   {
00903     uint64 val = this->PopUnsignedDWord();
00904     return val | (((uint64)this->PopUnsignedDWord()) << 32);
00905   }
00906   int64  PopSignedQWord()   { return (int64)this->PopUnsignedQWord(); }
00907 
00909   void RotateTop4Words()
00910   {
00911     byte tmp[2];
00912     for (int i = 0; i  < 2; i++) tmp[i] = this->stack[this->position + i + 6];
00913     for (int i = 5; i >= 0; i--) this->stack[this->position + i + 2] = this->stack[this->position + i];
00914     for (int i = 0; i  < 2; i++) this->stack[this->position + i] = tmp[i];
00915   }
00916 
00917   void PushWord(uint16 word)
00918   {
00919     if (this->position >= 2) {
00920       this->position -= 2;
00921     } else {
00922       for (int i = lengthof(stack) - 1; i >= this->position + 2; i--) {
00923         this->stack[i] = this->stack[i - 2];
00924       }
00925     }
00926     this->stack[this->position]     = GB(word, 0, 8);
00927     this->stack[this->position + 1] = GB(word, 8, 8);
00928   }
00929 
00930   void ResetStack()  { this->position = 0; this->used = true; }
00931   void RewindStack() { this->position = 0; }
00932 };
00933 
00935 static TextRefStack _newgrf_textrefstack;
00936 
00941 bool UsingNewGRFTextStack()
00942 {
00943   return _newgrf_textrefstack.used;
00944 }
00945 
00950 struct TextRefStack *CreateTextRefStackBackup()
00951 {
00952   return new TextRefStack(_newgrf_textrefstack);
00953 }
00954 
00959 void RestoreTextRefStackBackup(struct TextRefStack *backup)
00960 {
00961   _newgrf_textrefstack = *backup;
00962   delete backup;
00963 }
00964 
00982 void StartTextRefStackUsage(byte numEntries, const uint32 *values)
00983 {
00984   extern TemporaryStorageArray<int32, 0x110> _temp_store;
00985 
00986   _newgrf_textrefstack.ResetStack();
00987 
00988   byte *p = _newgrf_textrefstack.stack;
00989   for (uint i = 0; i < numEntries; i++) {
00990     uint32 value = values != NULL ? values[i] : _temp_store.Get(0x100 + i);
00991     for (uint j = 0; j < 32; j += 8) {
00992       *p = GB(value, j, 8);
00993       p++;
00994     }
00995   }
00996 }
00997 
00999 void StopTextRefStackUsage()
01000 {
01001   _newgrf_textrefstack.used = false;
01002 }
01003 
01004 void RewindTextRefStack()
01005 {
01006   _newgrf_textrefstack.RewindStack();
01007 }
01008 
01017 uint RemapNewGRFStringControlCode(uint scc, char *buf_start, char **buff, const char **str, int64 *argv)
01018 {
01019   if (_newgrf_textrefstack.used) {
01020     switch (scc) {
01021       default: NOT_REACHED();
01022       case SCC_NEWGRF_PRINT_SIGNED_BYTE:    *argv = _newgrf_textrefstack.PopSignedByte();    break;
01023       case SCC_NEWGRF_PRINT_SIGNED_WORD:    *argv = _newgrf_textrefstack.PopSignedWord();    break;
01024       case SCC_NEWGRF_PRINT_QWORD_CURRENCY: *argv = _newgrf_textrefstack.PopUnsignedQWord(); break;
01025 
01026       case SCC_NEWGRF_PRINT_DWORD_CURRENCY:
01027       case SCC_NEWGRF_PRINT_DWORD:          *argv = _newgrf_textrefstack.PopSignedDWord();   break;
01028 
01029       case SCC_NEWGRF_PRINT_HEX_BYTE:       *argv = _newgrf_textrefstack.PopUnsignedByte();  break;
01030       case SCC_NEWGRF_PRINT_HEX_DWORD:      *argv = _newgrf_textrefstack.PopUnsignedDWord(); break;
01031       case SCC_NEWGRF_PRINT_HEX_QWORD:      *argv = _newgrf_textrefstack.PopSignedQWord(); break;
01032 
01033       case SCC_NEWGRF_PRINT_HEX_WORD:
01034       case SCC_NEWGRF_PRINT_WORD_SPEED:
01035       case SCC_NEWGRF_PRINT_WORD_VOLUME:
01036       case SCC_NEWGRF_PRINT_WORD_WEIGHT:
01037       case SCC_NEWGRF_PRINT_WORD_STATION_NAME:
01038       case SCC_NEWGRF_PRINT_UNSIGNED_WORD:  *argv = _newgrf_textrefstack.PopUnsignedWord();  break;
01039 
01040       case SCC_NEWGRF_PRINT_DATE:
01041       case SCC_NEWGRF_PRINT_MONTH_YEAR:     *argv = _newgrf_textrefstack.PopUnsignedWord() + DAYS_TILL_ORIGINAL_BASE_YEAR; break;
01042 
01043       case SCC_NEWGRF_DISCARD_WORD:         _newgrf_textrefstack.PopUnsignedWord(); break;
01044 
01045       case SCC_NEWGRF_ROTATE_TOP_4_WORDS:   _newgrf_textrefstack.RotateTop4Words(); break;
01046       case SCC_NEWGRF_PUSH_WORD:            _newgrf_textrefstack.PushWord(Utf8Consume(str)); break;
01047       case SCC_NEWGRF_UNPRINT:              *buff = max(*buff - Utf8Consume(str), buf_start); break;
01048 
01049       case SCC_NEWGRF_PRINT_STRING_ID:
01050         *argv = TTDPStringIDToOTTDStringIDMapping(_newgrf_textrefstack.PopUnsignedWord());
01051         break;
01052     }
01053   }
01054 
01055   switch (scc) {
01056     default: NOT_REACHED();
01057     case SCC_NEWGRF_PRINT_DWORD:
01058     case SCC_NEWGRF_PRINT_SIGNED_WORD:
01059     case SCC_NEWGRF_PRINT_SIGNED_BYTE:
01060     case SCC_NEWGRF_PRINT_UNSIGNED_WORD:
01061       return SCC_COMMA;
01062 
01063     case SCC_NEWGRF_PRINT_HEX_BYTE:
01064     case SCC_NEWGRF_PRINT_HEX_WORD:
01065     case SCC_NEWGRF_PRINT_HEX_DWORD:
01066     case SCC_NEWGRF_PRINT_HEX_QWORD:
01067       return SCC_HEX;
01068 
01069     case SCC_NEWGRF_PRINT_DWORD_CURRENCY:
01070     case SCC_NEWGRF_PRINT_QWORD_CURRENCY:
01071       return SCC_CURRENCY;
01072 
01073     case SCC_NEWGRF_PRINT_STRING_ID:
01074       return SCC_NEWGRF_PRINT_STRING_ID;
01075 
01076     case SCC_NEWGRF_PRINT_DATE:
01077       return SCC_DATE_LONG;
01078 
01079     case SCC_NEWGRF_PRINT_MONTH_YEAR:
01080       return SCC_DATE_TINY;
01081 
01082     case SCC_NEWGRF_PRINT_WORD_SPEED:
01083       return SCC_VELOCITY;
01084 
01085     case SCC_NEWGRF_PRINT_WORD_VOLUME:
01086       return SCC_VOLUME;
01087 
01088     case SCC_NEWGRF_PRINT_WORD_WEIGHT:
01089       return SCC_WEIGHT;
01090 
01091     case SCC_NEWGRF_PRINT_WORD_STATION_NAME:
01092       return SCC_STATION_NAME;
01093 
01094     case SCC_NEWGRF_DISCARD_WORD:
01095     case SCC_NEWGRF_ROTATE_TOP_4_WORDS:
01096     case SCC_NEWGRF_PUSH_WORD:
01097     case SCC_NEWGRF_UNPRINT:
01098       return 0;
01099   }
01100 }