From 7de22e25da5b5c12599d9bad2e12aa9abb3bd7bf Mon Sep 17 00:00:00 2001 From: rubidium Date: Sat, 1 Oct 2011 20:31:21 +0000 Subject: (svn r22970) -Fix [FS#4769]: strip newlines from NewGRF strings that should not have newlines, e.g. the NewGRF's name --- src/newgrf.cpp | 44 ++++++++++++++++++++++---------------------- src/newgrf_text.cpp | 34 +++++++++++++++++++++------------- src/newgrf_text.h | 6 +++--- 3 files changed, 46 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/newgrf.cpp b/src/newgrf.cpp index cfe1c91f1..3266a2c25 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -5153,15 +5153,15 @@ static void FeatureNewName(ByteReader *buf) if (!generic) { Engine *e = GetNewEngine(_cur.grffile, (VehicleType)feature, id, HasBit(_cur.grfconfig->flags, GCF_STATIC)); if (e == NULL) break; - StringID string = AddGRFString(_cur.grffile->grfid, e->index, lang, new_scheme, name, e->info.string_id); + StringID string = AddGRFString(_cur.grffile->grfid, e->index, lang, false, new_scheme, name, e->info.string_id); e->info.string_id = string; } else { - AddGRFString(_cur.grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED); + AddGRFString(_cur.grffile->grfid, id, lang, new_scheme, true, name, STR_UNDEFINED); } break; case GSF_INDUSTRIES: { - AddGRFString(_cur.grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED); + AddGRFString(_cur.grffile->grfid, id, lang, new_scheme, true, name, STR_UNDEFINED); break; } @@ -5173,7 +5173,7 @@ static void FeatureNewName(ByteReader *buf) grfmsg(1, "FeatureNewName: Attempt to name undefined station 0x%X, ignoring", GB(id, 0, 8)); } else { StationClassID cls_id = _cur.grffile->stations[GB(id, 0, 8)]->cls_id; - StationClass::SetName(cls_id, AddGRFString(_cur.grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED)); + StationClass::SetName(cls_id, AddGRFString(_cur.grffile->grfid, id, lang, new_scheme, false, name, STR_UNDEFINED)); } break; @@ -5181,7 +5181,7 @@ static void FeatureNewName(ByteReader *buf) if (_cur.grffile->stations == NULL || _cur.grffile->stations[GB(id, 0, 8)] == NULL) { grfmsg(1, "FeatureNewName: Attempt to name undefined station 0x%X, ignoring", GB(id, 0, 8)); } else { - _cur.grffile->stations[GB(id, 0, 8)]->name = AddGRFString(_cur.grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED); + _cur.grffile->stations[GB(id, 0, 8)]->name = AddGRFString(_cur.grffile->grfid, id, lang, new_scheme, false, name, STR_UNDEFINED); } break; @@ -5189,7 +5189,7 @@ static void FeatureNewName(ByteReader *buf) if (_cur.grffile->airtspec == NULL || _cur.grffile->airtspec[GB(id, 0, 8)] == NULL) { grfmsg(1, "FeatureNewName: Attempt to name undefined airport tile 0x%X, ignoring", GB(id, 0, 8)); } else { - _cur.grffile->airtspec[GB(id, 0, 8)]->name = AddGRFString(_cur.grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED); + _cur.grffile->airtspec[GB(id, 0, 8)]->name = AddGRFString(_cur.grffile->grfid, id, lang, new_scheme, false, name, STR_UNDEFINED); } break; @@ -5197,7 +5197,7 @@ static void FeatureNewName(ByteReader *buf) if (_cur.grffile->housespec == NULL || _cur.grffile->housespec[GB(id, 0, 8)] == NULL) { grfmsg(1, "FeatureNewName: Attempt to name undefined house 0x%X, ignoring.", GB(id, 0, 8)); } else { - _cur.grffile->housespec[GB(id, 0, 8)]->building_name = AddGRFString(_cur.grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED); + _cur.grffile->housespec[GB(id, 0, 8)]->building_name = AddGRFString(_cur.grffile->grfid, id, lang, new_scheme, false, name, STR_UNDEFINED); } break; @@ -5206,7 +5206,7 @@ static void FeatureNewName(ByteReader *buf) case 0xD2: case 0xD3: case 0xDC: - AddGRFString(_cur.grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED); + AddGRFString(_cur.grffile->grfid, id, lang, new_scheme, true, name, STR_UNDEFINED); break; default: @@ -5875,11 +5875,11 @@ static void ScanInfo(ByteReader *buf) /* GRF IDs starting with 0xFF are reserved for internal TTDPatch use */ if (GB(grfid, 24, 8) == 0xFF) SetBit(_cur.grfconfig->flags, GCF_SYSTEM); - AddGRFTextToList(&_cur.grfconfig->name->text, 0x7F, grfid, name); + AddGRFTextToList(&_cur.grfconfig->name->text, 0x7F, grfid, false, name); if (buf->HasData()) { const char *info = buf->ReadString(); - AddGRFTextToList(&_cur.grfconfig->info->text, 0x7F, grfid, info); + AddGRFTextToList(&_cur.grfconfig->info->text, 0x7F, grfid, true, info); } /* GLS_INFOSCAN only looks for the action 8, so we can skip the rest of the file */ @@ -6046,7 +6046,7 @@ static void GRFLoadError(ByteReader *buf) if (buf->HasData()) { const char *message = buf->ReadString(); - error->custom_message = TranslateTTDPatchCodes(_cur.grffile->grfid, lang, message); + error->custom_message = TranslateTTDPatchCodes(_cur.grffile->grfid, lang, true, message); } else { grfmsg(7, "GRFLoadError: No custom message supplied."); error->custom_message = strdup(""); @@ -6058,7 +6058,7 @@ static void GRFLoadError(ByteReader *buf) if (buf->HasData()) { const char *data = buf->ReadString(); - error->data = TranslateTTDPatchCodes(_cur.grffile->grfid, lang, data); + error->data = TranslateTTDPatchCodes(_cur.grffile->grfid, lang, true, data); } else { grfmsg(7, "GRFLoadError: No message data supplied."); error->data = strdup(""); @@ -6596,11 +6596,11 @@ static void FeatureTownName(ByteReader *buf) const char *name = buf->ReadString(); - char *lang_name = TranslateTTDPatchCodes(grfid, lang, name); + char *lang_name = TranslateTTDPatchCodes(grfid, lang, false, name); grfmsg(6, "FeatureTownName: lang 0x%X -> '%s'", lang, lang_name); free(lang_name); - townname->name[nb_gen] = AddGRFString(grfid, id, lang, new_scheme, name, STR_UNDEFINED); + townname->name[nb_gen] = AddGRFString(grfid, id, lang, new_scheme, false, name, STR_UNDEFINED); lang = buf->ReadByte(); } while (lang != 0); @@ -6640,7 +6640,7 @@ static void FeatureTownName(ByteReader *buf) townname->partlist[id][i].parts[j].data.id = ref_id; } else { const char *text = buf->ReadString(); - townname->partlist[id][i].parts[j].data.text = TranslateTTDPatchCodes(grfid, 0, text); + townname->partlist[id][i].parts[j].data.text = TranslateTTDPatchCodes(grfid, 0, false, text); grfmsg(6, "FeatureTownName: part %d, text %d, '%s' (with probability %d)", i, j, townname->partlist[id][i].parts[j].data.text, prob); } townname->partlist[id][i].parts[j].prob = prob; @@ -6937,21 +6937,21 @@ static void TranslateGRFStrings(ByteReader *buf) * new_scheme has to be true as well. A language id of 0x7F will be * overridden by a non-generic id, so this will not change anything if * a string has been provided specifically for this language. */ - AddGRFString(grfid, first_id + i, 0x7F, true, string, STR_UNDEFINED); + AddGRFString(grfid, first_id + i, 0x7F, true, true, string, STR_UNDEFINED); } } /** Callback function for 'INFO'->'NAME' to add a translation to the newgrf name. */ static bool ChangeGRFName(byte langid, const char *str) { - AddGRFTextToList(&_cur.grfconfig->name->text, langid, _cur.grfconfig->ident.grfid, str); + AddGRFTextToList(&_cur.grfconfig->name->text, langid, _cur.grfconfig->ident.grfid, false, str); return true; } /** Callback function for 'INFO'->'DESC' to add a translation to the newgrf description. */ static bool ChangeGRFDescription(byte langid, const char *str) { - AddGRFTextToList(&_cur.grfconfig->info->text, langid, _cur.grfconfig->ident.grfid, str); + AddGRFTextToList(&_cur.grfconfig->info->text, langid, _cur.grfconfig->ident.grfid, true, str); return true; } @@ -7031,14 +7031,14 @@ static GRFParameterInfo *_cur_parameter; ///< The parameter which info is curren /** Callback function for 'INFO'->'PARAM'->param_num->'NAME' to set the name of a parameter. */ static bool ChangeGRFParamName(byte langid, const char *str) { - AddGRFTextToList(&_cur_parameter->name, langid, _cur.grfconfig->ident.grfid, str); + AddGRFTextToList(&_cur_parameter->name, langid, _cur.grfconfig->ident.grfid, false, str); return true; } /** Callback function for 'INFO'->'PARAM'->param_num->'DESC' to set the description of a parameter. */ static bool ChangeGRFParamDescription(byte langid, const char *str) { - AddGRFTextToList(&_cur_parameter->desc, langid, _cur.grfconfig->ident.grfid, str); + AddGRFTextToList(&_cur_parameter->desc, langid, _cur.grfconfig->ident.grfid, true, str); return true; } @@ -7218,10 +7218,10 @@ static bool ChangeGRFParamValueNames(ByteReader *buf) SmallPair *val_name = _cur_parameter->value_names.Find(id); if (val_name != _cur_parameter->value_names.End()) { - AddGRFTextToList(&val_name->second, langid, _cur.grfconfig->ident.grfid, name_string); + AddGRFTextToList(&val_name->second, langid, _cur.grfconfig->ident.grfid, false, name_string); } else { GRFText *list = NULL; - AddGRFTextToList(&list, langid, _cur.grfconfig->ident.grfid, name_string); + AddGRFTextToList(&list, langid, _cur.grfconfig->ident.grfid, false, name_string); _cur_parameter->value_names.Insert(id, list); } diff --git a/src/newgrf_text.cpp b/src/newgrf_text.cpp index d3b2e1372..711d68d15 100644 --- a/src/newgrf_text.cpp +++ b/src/newgrf_text.cpp @@ -389,13 +389,14 @@ struct UnmappedChoiceList : ZeroedMemoryAllocator { /** * Translate TTDPatch string codes into something OpenTTD can handle (better). - * @param grfid The (NewGRF) ID associated with this string - * @param language_id The (NewGRF) language ID associated with this string. - * @param str The string to translate. - * @param [out] olen The length of the final string. + * @param grfid The (NewGRF) ID associated with this string + * @param language_id The (NewGRF) language ID associated with this string. + * @param allow_newlines Whether newlines are allowed in the string or not. + * @param str The string to translate. + * @param [out] olen The length of the final string. * @return The translated string. */ -char *TranslateTTDPatchCodes(uint32 grfid, uint8 language_id, const char *str, int *olen) +char *TranslateTTDPatchCodes(uint32 grfid, uint8 language_id, bool allow_newlines, const char *str, int *olen) { char *tmp = MallocT(strlen(str) * 10 + 1); // Allocate space to allow for expansion char *d = tmp; @@ -434,7 +435,13 @@ char *TranslateTTDPatchCodes(uint32 grfid, uint8 language_id, const char *str, i *d++ = *str++; break; case 0x0A: break; - case 0x0D: *d++ = 0x0A; break; + case 0x0D: + if (allow_newlines) { + *d++ = 0x0A; + } else { + grfmsg(1, "Detected newline in string that does not allow one"); + } + break; case 0x0E: d += Utf8Encode(d, SCC_TINYFONT); break; case 0x0F: d += Utf8Encode(d, SCC_BIGFONT); break; case 0x1F: @@ -647,13 +654,14 @@ void AddGRFTextToList(GRFText **list, GRFText *text_to_add) * @param list The list where the text should be added to. * @param langid The language of the new text. * @param grfid The grfid where this string is defined. + * @param allow_newlines Whether newlines are allowed in this string. * @param text_to_add The text to add to the list. * @note All text-codes will be translated. */ -void AddGRFTextToList(struct GRFText **list, byte langid, uint32 grfid, const char *text_to_add) +void AddGRFTextToList(struct GRFText **list, byte langid, uint32 grfid, bool allow_newlines, const char *text_to_add) { int len; - char *translatedtext = TranslateTTDPatchCodes(grfid, langid, text_to_add, &len); + char *translatedtext = TranslateTTDPatchCodes(grfid, langid, allow_newlines, text_to_add, &len); GRFText *newtext = GRFText::New(langid, translatedtext, len); free(translatedtext); @@ -690,7 +698,7 @@ GRFText *DuplicateGRFText(GRFText *orig) /** * Add the new read string into our structure. */ -StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid_to_add, bool new_scheme, const char *text_to_add, StringID def_string) +StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid_to_add, bool new_scheme, bool allow_newlines, const char *text_to_add, StringID def_string) { char *translatedtext; uint id; @@ -706,9 +714,9 @@ StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid_to_add, bool ne langid_to_add = GRFLX_ENGLISH; } else { StringID ret = STR_EMPTY; - if (langid_to_add & GRFLB_GERMAN) ret = AddGRFString(grfid, stringid, GRFLX_GERMAN, true, text_to_add, def_string); - if (langid_to_add & GRFLB_FRENCH) ret = AddGRFString(grfid, stringid, GRFLX_FRENCH, true, text_to_add, def_string); - if (langid_to_add & GRFLB_SPANISH) ret = AddGRFString(grfid, stringid, GRFLX_SPANISH, true, text_to_add, def_string); + if (langid_to_add & GRFLB_GERMAN) ret = AddGRFString(grfid, stringid, GRFLX_GERMAN, true, allow_newlines, text_to_add, def_string); + if (langid_to_add & GRFLB_FRENCH) ret = AddGRFString(grfid, stringid, GRFLX_FRENCH, true, allow_newlines, text_to_add, def_string); + if (langid_to_add & GRFLB_SPANISH) ret = AddGRFString(grfid, stringid, GRFLX_SPANISH, true, allow_newlines, text_to_add, def_string); return ret; } } @@ -723,7 +731,7 @@ StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid_to_add, bool ne if (id == lengthof(_grf_text)) return STR_EMPTY; int len; - translatedtext = TranslateTTDPatchCodes(grfid, langid_to_add, text_to_add, &len); + translatedtext = TranslateTTDPatchCodes(grfid, langid_to_add, allow_newlines, text_to_add, &len); GRFText *newtext = GRFText::New(langid_to_add, translatedtext, len); diff --git a/src/newgrf_text.h b/src/newgrf_text.h index 3531f5b94..4a9d96af1 100644 --- a/src/newgrf_text.h +++ b/src/newgrf_text.h @@ -19,16 +19,16 @@ /** This character, the thorn ('รพ'), indicates a unicode string to NFO. */ static const WChar NFO_UTF8_IDENTIFIER = 0x00DE; -StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid, bool new_scheme, const char *text_to_add, StringID def_string); +StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid, bool new_scheme, bool allow_newlines, const char *text_to_add, StringID def_string); StringID GetGRFStringID(uint32 grfid, uint16 stringid); const char *GetGRFStringFromGRFText(const struct GRFText *text); const char *GetGRFStringPtr(uint16 stringid); void CleanUpStrings(); void SetCurrentGrfLangID(byte language_id); -char *TranslateTTDPatchCodes(uint32 grfid, uint8 language_id, const char *str, int *olen = NULL); +char *TranslateTTDPatchCodes(uint32 grfid, uint8 language_id, bool allow_newlines, const char *str, int *olen = NULL); struct GRFText *DuplicateGRFText(struct GRFText *orig); void AddGRFTextToList(struct GRFText **list, struct GRFText *text_to_add); -void AddGRFTextToList(struct GRFText **list, byte langid, uint32 grfid, const char *text_to_add); +void AddGRFTextToList(struct GRFText **list, byte langid, uint32 grfid, bool allow_newlines, const char *text_to_add); void AddGRFTextToList(struct GRFText **list, const char *text_to_add); void CleanUpGRFText(struct GRFText *grftext); -- cgit v1.2.3-70-g09d2