From 2687704afc028a5e165d0aa21c2074051ff67fc6 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Wed, 21 Jun 2023 06:35:06 +0200 Subject: [PATCH] Codechange: introduce new type and functions for StringParameter backups --- src/strings.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++ src/strings_func.h | 5 +++++ src/strings_type.h | 30 ++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) diff --git a/src/strings.cpp b/src/strings.cpp index e3abf74d5d..fe70915c1c 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -197,6 +197,56 @@ void CopyOutDParam(uint64 *dst, const char **strings, StringID string, int num) } } +/** + * Copy the parameters from the backup into the global string parameter array. + * @param backup The backup to copy from. + */ +void CopyInDParam(const span backup) +{ + for (size_t i = 0; i < backup.size(); i++) { + auto &value = backup[i]; + if (value.string.has_value()) { + _global_string_params.SetParam(i, value.string.value()); + } else { + _global_string_params.SetParam(i, value.data); + } + } +} + +/** + * Copy \a num string parameters from the global string parameter array to the \a backup. + * @param backup The backup to write to. + * @param num Number of string parameters to copy. + */ +void CopyOutDParam(std::vector &backup, size_t num) +{ + backup.resize(num); + for (size_t i = 0; i < backup.size(); i++) { + backup[i] = _global_string_params.GetParam(i); + } +} + +/** + * Copy \a num string parameters from the global string parameter array to the \a backup. + * @param backup The backup to write to. + * @param num Number of string parameters to copy. + * @param string The string used to determine where raw strings are and where there are no raw strings. + */ +void CopyOutDParam(std::vector &backup, size_t num, StringID string) +{ + /* Just get the string to extract the type information. */ + GetString(string); + + backup.resize(num); + for (size_t i = 0; i < backup.size(); i++) { + if (_global_string_params.GetTypeAtOffset(i) == SCC_RAW_STRING_POINTER) { + backup[i] = (const char *)(size_t)_global_string_params.GetParam(i); + } else { + backup[i] = _global_string_params.GetParam(i); + } + } +} + static void StationGetSpecialString(StringBuilder &builder, StationFacility x); static void GetSpecialTownNameString(StringBuilder &builder, int ind, uint32 seed); static void GetSpecialNameString(StringBuilder &builder, int ind, StringParameters &args); diff --git a/src/strings_func.h b/src/strings_func.h index aa726e2435..5e407f9a40 100644 --- a/src/strings_func.h +++ b/src/strings_func.h @@ -14,6 +14,7 @@ #include "string_type.h" #include "gfx_type.h" #include "core/bitmath_func.hpp" +#include "core/span_type.hpp" #include "vehicle_type.h" /** @@ -89,6 +90,10 @@ void CopyInDParam(const uint64 *src, int num); void CopyOutDParam(uint64 *dst, int num); void CopyOutDParam(uint64 *dst, const char **strings, StringID string, int num); +void CopyInDParam(const span backup); +void CopyOutDParam(std::vector &backup, size_t num); +void CopyOutDParam(std::vector &backup, size_t num, StringID string); + uint64_t GetDParam(size_t n); extern TextDirection _current_text_dir; ///< Text direction of the currently selected language diff --git a/src/strings_type.h b/src/strings_type.h index 7ad0c71265..53b2abd48b 100644 --- a/src/strings_type.h +++ b/src/strings_type.h @@ -88,4 +88,34 @@ enum SpecialStrings { SPECSTR_PRESIDENT_NAME = 0x70E7, }; +/** Data that is to be stored when backing up StringParameters. */ +struct StringParameterBackup { + uint64_t data; ///< The data field; valid *when* string has no value. + std::optional string; ///< The string value. + + /** + * Assign the numeric data with the given value, while clearing the stored string. + * @param data The new value of the data field. + * @return This object. + */ + StringParameterBackup &operator=(uint64_t data) + { + this->string.reset(); + this->data = data; + return *this; + } + + /** + * Assign a copy of the given string to the string field, while clearing the data field. + * @param string The new value of the string. + * @return This object. + */ + StringParameterBackup &operator=(const std::string_view string) + { + this->data = 0; + this->string.emplace(string); + return *this; + } +}; + #endif /* STRINGS_TYPE_H */