(svn r24278) -Codechange: Deduplicate code to read translation tables.

This commit is contained in:
frosch 2012-05-25 21:00:00 +00:00
parent 522e5ec86b
commit 5e8151e245
1 changed files with 52 additions and 66 deletions

View File

@ -2416,6 +2416,32 @@ static ChangeInfoResult TownHouseChangeInfo(uint hid, int numinfo, int prop, Byt
return (grffile != NULL && grffile->language_map != NULL && language_id < MAX_LANG) ? &grffile->language_map[language_id] : NULL;
}
/**
* Load a cargo- or railtype-translation table.
* @param gvid ID of the global variable. This is basically only checked for zerones.
* @param numinfo Number of subsequent IDs to change the property for.
* @param buf The property value.
* @param [in,out] translation_table Storage location for the translation table.
* @param name Name of the table for debug output.
* @return ChangeInfoResult.
*/
template <typename T>
static ChangeInfoResult LoadTranslationTable(uint gvid, int numinfo, ByteReader *buf, T &translation_table, const char *name)
{
if (gvid != 0) {
grfmsg(1, "LoadTranslationTable: %s translation table must start at zero", name);
return CIR_INVALID_ID;
}
translation_table.Clear();
for (int i = 0; i < numinfo; i++) {
uint32 item = buf->ReadDWord();
*translation_table.Append() = BSWAP32(item);
}
return CIR_SUCCESS;
}
/**
* Define properties for global variables
* @param gvid ID of the global variable.
@ -2426,8 +2452,20 @@ static ChangeInfoResult TownHouseChangeInfo(uint hid, int numinfo, int prop, Byt
*/
static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, ByteReader *buf)
{
ChangeInfoResult ret = CIR_SUCCESS;
/* Properties which are handled as a whole */
switch (prop) {
case 0x09: // Cargo Translation Table; loading during both reservation and activation stage (in case it is selected depending on defined cargos)
return LoadTranslationTable(gvid, numinfo, buf, _cur.grffile->cargo_list, "Cargo");
case 0x12: // Rail type translation table; loading during both reservation and activation stage (in case it is selected depending on defined railtypes)
return LoadTranslationTable(gvid, numinfo, buf, _cur.grffile->railtype_list, "Rail type");
default:
break;
}
/* Properties which are handled per item */
ChangeInfoResult ret = CIR_SUCCESS;
for (int i = 0; i < numinfo; i++) {
switch (prop) {
case 0x08: { // Cost base factor
@ -2442,22 +2480,6 @@ static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, By
break;
}
case 0x09: { // Cargo Translation Table; loading during both reservation and activation stage (in case it is selected depending on defined cargos)
if (i == 0) {
if (gvid != 0) {
grfmsg(1, "GlobalVarChangeInfo: Cargo translation table must start at zero");
return CIR_INVALID_ID;
}
_cur.grffile->cargo_list.Clear();
_cur.grffile->cargo_list.Append(numinfo);
}
CargoLabel cl = buf->ReadDWord();
_cur.grffile->cargo_list[i] = BSWAP32(cl);
break;
}
case 0x0A: { // Currency display names
uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
StringID newone = GetGRFStringID(_cur.grffile->grfid, buf->ReadWord());
@ -2570,22 +2592,6 @@ static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, By
buf->Skip(8);
break;
case 0x12: { // Rail type translation table; loading during both reservation and activation stage (in case it is selected depending on defined railtypes)
if (i == 0) {
if (gvid != 0) {
grfmsg(1, "GlobalVarChangeInfo: Rail type translation table must start at zero");
return CIR_INVALID_ID;
}
_cur.grffile->railtype_list.Clear();
_cur.grffile->railtype_list.Append(numinfo);
}
RailTypeLabel rtl = buf->ReadDWord();
_cur.grffile->railtype_list[i] = BSWAP32(rtl);
break;
}
case 0x13: // Gender translation table
case 0x14: // Case translation table
case 0x15: { // Plural form translation
@ -2661,8 +2667,20 @@ static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, By
static ChangeInfoResult GlobalVarReserveInfo(uint gvid, int numinfo, int prop, ByteReader *buf)
{
ChangeInfoResult ret = CIR_SUCCESS;
/* Properties which are handled as a whole */
switch (prop) {
case 0x09: // Cargo Translation Table; loading during both reservation and activation stage (in case it is selected depending on defined cargos)
return LoadTranslationTable(gvid, numinfo, buf, _cur.grffile->cargo_list, "Cargo");
case 0x12: // Rail type translation table; loading during both reservation and activation stage (in case it is selected depending on defined railtypes)
return LoadTranslationTable(gvid, numinfo, buf, _cur.grffile->railtype_list, "Rail type");
default:
break;
}
/* Properties which are handled per item */
ChangeInfoResult ret = CIR_SUCCESS;
for (int i = 0; i < numinfo; i++) {
switch (prop) {
case 0x08: // Cost base factor
@ -2670,22 +2688,6 @@ static ChangeInfoResult GlobalVarReserveInfo(uint gvid, int numinfo, int prop, B
buf->ReadByte();
break;
case 0x09: { // Cargo Translation Table
if (i == 0) {
if (gvid != 0) {
grfmsg(1, "ReserveChangeInfo: Cargo translation table must start at zero");
return CIR_INVALID_ID;
}
_cur.grffile->cargo_list.Clear();
_cur.grffile->cargo_list.Append(numinfo);
}
CargoLabel cl = buf->ReadDWord();
_cur.grffile->cargo_list[i] = BSWAP32(cl);
break;
}
case 0x0A: // Currency display names
case 0x0C: // Currency options
case 0x0F: // Euro introduction dates
@ -2709,22 +2711,6 @@ static ChangeInfoResult GlobalVarReserveInfo(uint gvid, int numinfo, int prop, B
break;
}
case 0x12: { // Rail type translation table
if (i == 0) {
if (gvid != 0) {
grfmsg(1, "ReserveChangeInfo: Rail type translation table must start at zero");
return CIR_INVALID_ID;
}
_cur.grffile->railtype_list.Clear();
_cur.grffile->railtype_list.Append(numinfo);
}
RailTypeLabel rtl = buf->ReadDWord();
_cur.grffile->railtype_list[i] = BSWAP32(rtl);
break;
}
case 0x13: // Gender translation table
case 0x14: // Case translation table
while (buf->ReadByte() != 0) {