From 4654b2b0aae35bf870e943d4a9be4a572d672f99 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Thu, 22 Jun 2023 18:47:32 +0200 Subject: [PATCH] Codechange: separate integer and string usage in StringParameters --- src/strings.cpp | 21 ++++++++------------- src/strings_internal.h | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/src/strings.cpp b/src/strings.cpp index 51f5eb2db2..c4e095cf72 100644 --- a/src/strings.cpp +++ b/src/strings.cpp @@ -178,7 +178,12 @@ 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); + const char *str = _global_string_params.GetParamStr(i); + if (str != nullptr) { + backup[i] = str; + } else { + backup[i] = _global_string_params.GetParam(i); + } } } @@ -190,17 +195,7 @@ void CopyOutDParam(std::vector &backup, size_t num) */ 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); - } - } + CopyOutDParam(backup, num); } /** @@ -1124,7 +1119,7 @@ static void FormatString(StringBuilder &builder, const char *str_arg, StringPara break; case SCC_RAW_STRING_POINTER: { // {RAW_STRING} - const char *raw_string = (const char *)(size_t)args.GetNextParameter(); + const char *raw_string = args.GetNextParameterString(); /* raw_string can be(come) nullptr when the parameter is out of range and 0 is returned instead. */ if (raw_string == nullptr || (game_script && std::find(_game_script_raw_strings.begin(), _game_script_raw_strings.end(), raw_string) == _game_script_raw_strings.end())) { diff --git a/src/strings_internal.h b/src/strings_internal.h index e214a3c1a1..3759c9bfab 100644 --- a/src/strings_internal.h +++ b/src/strings_internal.h @@ -17,6 +17,7 @@ /** The data required to format and validate a single parameter of a string. */ struct StringParameter { uint64_t data; ///< The data of the parameter. + const char *string_view; ///< The string value, if it has any. WChar type; ///< The #StringControlCode to interpret this data with when it's the first parameter, otherwise '\0'. }; @@ -92,6 +93,18 @@ public: return static_cast(ptr == nullptr ? 0 : ptr->data); } + /** + * Get the next string parameter from our parameters. + * This updates the offset, so the next time this is called the next parameter + * will be read. + * @return The next parameter's value. + */ + const char *GetNextParameterString() + { + auto ptr = GetNextParameterPointer(); + return ptr == nullptr ? nullptr : ptr->string_view; + } + /** * Get a new instance of StringParameters that is a "range" into the * remaining existing parameters. Upon destruction the offset in the parent @@ -134,17 +147,36 @@ public: { assert(n < this->parameters.size()); this->parameters[n].data = v; + this->parameters[n].string_view = nullptr; + } + + void SetParam(size_t n, const char *str) + { + assert(n < this->parameters.size()); + this->parameters[n].data = 0; + this->parameters[n].string_view = str; } - void SetParam(size_t n, const char *str) { this->SetParam(n, (uint64_t)(size_t)str); } void SetParam(size_t n, const std::string &str) { this->SetParam(n, str.c_str()); } void SetParam(size_t n, std::string &&str) = delete; // block passing temporaries to SetDParam uint64 GetParam(size_t n) const { assert(n < this->parameters.size()); + assert(this->parameters[n].string_view == nullptr); return this->parameters[n].data; } + + /** + * Get the stored string of the parameter, or \c nullptr when there is none. + * @param n The index into the parameters. + * @return The stored string. + */ + const char *GetParamStr(size_t n) const + { + assert(n < this->parameters.size()); + return this->parameters[n].string_view; + } }; /**