Codechange: use StringParameters for remapping the NewGRF string control codes

This commit is contained in:
Rubidium
2023-06-13 15:55:46 +02:00
committed by rubidium42
parent 1146904d45
commit aba0d27a28
5 changed files with 38 additions and 24 deletions

View File

@@ -842,12 +842,11 @@ void RewindTextRefStack()
* FormatString for NewGRF specific "magic" string control codes
* @param scc the string control code that has been read
* @param str the string that we need to write
* @param argv the OpenTTD stack of values
* @param argv_size space on the stack \a argv
* @param modify_argv When true, modify the OpenTTD stack.
* @param parameters the OpenTTD string formatting parameters
* @param modify_parameters When true, modify the OpenTTD string formatting parameters.
* @return the string control code to "execute" now
*/
uint RemapNewGRFStringControlCode(uint scc, const char **str, int64 *argv, uint argv_size, bool modify_argv)
uint RemapNewGRFStringControlCode(uint scc, const char **str, StringParameters &parameters, bool modify_parameters)
{
switch (scc) {
default: break;
@@ -877,7 +876,7 @@ uint RemapNewGRFStringControlCode(uint scc, const char **str, int64 *argv, uint
case SCC_NEWGRF_PRINT_DWORD_FORCE:
case SCC_NEWGRF_PRINT_WORD_STATION_NAME:
case SCC_NEWGRF_PRINT_WORD_CARGO_NAME:
if (argv_size < 1) {
if (parameters.GetDataLeft() < 1) {
Debug(misc, 0, "Too many NewGRF string parameters.");
return 0;
}
@@ -887,47 +886,47 @@ uint RemapNewGRFStringControlCode(uint scc, const char **str, int64 *argv, uint
case SCC_NEWGRF_PRINT_WORD_CARGO_LONG:
case SCC_NEWGRF_PRINT_WORD_CARGO_SHORT:
case SCC_NEWGRF_PRINT_WORD_CARGO_TINY:
if (argv_size < 2) {
if (parameters.GetDataLeft() < 2) {
Debug(misc, 0, "Too many NewGRF string parameters.");
return 0;
}
break;
}
if (_newgrf_textrefstack.used && modify_argv) {
if (_newgrf_textrefstack.used && modify_parameters) {
/* There is data on the NewGRF text stack, and we want to move them to OpenTTD's string stack.
* After this call, a new call is made with `modify_argv` set to false when the string is finally formatted. */
* After this call, a new call is made with `modify_parameters` set to false when the string is finally formatted. */
switch (scc) {
default: NOT_REACHED();
case SCC_NEWGRF_PRINT_BYTE_SIGNED: *argv = _newgrf_textrefstack.PopSignedByte(); break;
case SCC_NEWGRF_PRINT_QWORD_CURRENCY: *argv = _newgrf_textrefstack.PopSignedQWord(); break;
case SCC_NEWGRF_PRINT_BYTE_SIGNED: parameters.SetParam(0, _newgrf_textrefstack.PopSignedByte()); break;
case SCC_NEWGRF_PRINT_QWORD_CURRENCY: parameters.SetParam(0, _newgrf_textrefstack.PopSignedQWord()); break;
case SCC_NEWGRF_PRINT_DWORD_CURRENCY:
case SCC_NEWGRF_PRINT_DWORD_SIGNED: *argv = _newgrf_textrefstack.PopSignedDWord(); break;
case SCC_NEWGRF_PRINT_DWORD_SIGNED: parameters.SetParam(0, _newgrf_textrefstack.PopSignedDWord()); break;
case SCC_NEWGRF_PRINT_BYTE_HEX: *argv = _newgrf_textrefstack.PopUnsignedByte(); break;
case SCC_NEWGRF_PRINT_QWORD_HEX: *argv = _newgrf_textrefstack.PopUnsignedQWord(); break;
case SCC_NEWGRF_PRINT_BYTE_HEX: parameters.SetParam(0, _newgrf_textrefstack.PopUnsignedByte()); break;
case SCC_NEWGRF_PRINT_QWORD_HEX: parameters.SetParam(0, _newgrf_textrefstack.PopUnsignedQWord()); break;
case SCC_NEWGRF_PRINT_WORD_SPEED:
case SCC_NEWGRF_PRINT_WORD_VOLUME_LONG:
case SCC_NEWGRF_PRINT_WORD_VOLUME_SHORT:
case SCC_NEWGRF_PRINT_WORD_SIGNED: *argv = _newgrf_textrefstack.PopSignedWord(); break;
case SCC_NEWGRF_PRINT_WORD_SIGNED: parameters.SetParam(0, _newgrf_textrefstack.PopSignedWord()); break;
case SCC_NEWGRF_PRINT_WORD_HEX:
case SCC_NEWGRF_PRINT_WORD_WEIGHT_LONG:
case SCC_NEWGRF_PRINT_WORD_WEIGHT_SHORT:
case SCC_NEWGRF_PRINT_WORD_POWER:
case SCC_NEWGRF_PRINT_WORD_STATION_NAME:
case SCC_NEWGRF_PRINT_WORD_UNSIGNED: *argv = _newgrf_textrefstack.PopUnsignedWord(); break;
case SCC_NEWGRF_PRINT_WORD_UNSIGNED: parameters.SetParam(0, _newgrf_textrefstack.PopUnsignedWord()); break;
case SCC_NEWGRF_PRINT_DWORD_FORCE:
case SCC_NEWGRF_PRINT_DWORD_DATE_LONG:
case SCC_NEWGRF_PRINT_DWORD_DATE_SHORT:
case SCC_NEWGRF_PRINT_DWORD_HEX: *argv = _newgrf_textrefstack.PopUnsignedDWord(); break;
case SCC_NEWGRF_PRINT_DWORD_HEX: parameters.SetParam(0, _newgrf_textrefstack.PopUnsignedDWord()); break;
/* Dates from NewGRFs have 1920-01-01 as their zero point, convert it to OpenTTD's epoch. */
case SCC_NEWGRF_PRINT_WORD_DATE_LONG:
case SCC_NEWGRF_PRINT_WORD_DATE_SHORT: *argv = _newgrf_textrefstack.PopUnsignedWord() + DAYS_TILL_ORIGINAL_BASE_YEAR; break;
case SCC_NEWGRF_PRINT_WORD_DATE_SHORT: parameters.SetParam(0, _newgrf_textrefstack.PopUnsignedWord() + DAYS_TILL_ORIGINAL_BASE_YEAR); break;
case SCC_NEWGRF_DISCARD_WORD: _newgrf_textrefstack.PopUnsignedWord(); break;
@@ -937,17 +936,17 @@ uint RemapNewGRFStringControlCode(uint scc, const char **str, int64 *argv, uint
case SCC_NEWGRF_PRINT_WORD_CARGO_LONG:
case SCC_NEWGRF_PRINT_WORD_CARGO_SHORT:
case SCC_NEWGRF_PRINT_WORD_CARGO_TINY:
argv[0] = GetCargoTranslation(_newgrf_textrefstack.PopUnsignedWord(), _newgrf_textrefstack.grffile);
argv[1] = _newgrf_textrefstack.PopUnsignedWord();
parameters.SetParam(0, GetCargoTranslation(_newgrf_textrefstack.PopUnsignedWord(), _newgrf_textrefstack.grffile));
parameters.SetParam(1, _newgrf_textrefstack.PopUnsignedWord());
break;
case SCC_NEWGRF_PRINT_WORD_STRING_ID:
*argv = MapGRFStringID(_newgrf_textrefstack.grffile->grfid, _newgrf_textrefstack.PopUnsignedWord());
parameters.SetParam(0, MapGRFStringID(_newgrf_textrefstack.grffile->grfid, _newgrf_textrefstack.PopUnsignedWord()));
break;
case SCC_NEWGRF_PRINT_WORD_CARGO_NAME: {
CargoID cargo = GetCargoTranslation(_newgrf_textrefstack.PopUnsignedWord(), _newgrf_textrefstack.grffile);
*argv = cargo < NUM_CARGO ? 1ULL << cargo : 0;
parameters.SetParam(0, cargo < NUM_CARGO ? 1ULL << cargo : 0);
break;
}
}