From 7628289eb3eee1c83a66ce95e4d956f31b73dfff Mon Sep 17 00:00:00 2001 From: Ted John Date: Thu, 27 Feb 2020 22:25:26 +0000 Subject: [PATCH] Implement reverse of string processing --- src/openrct2-ui/scripting/ScWidget.hpp | 2 +- src/openrct2-ui/scripting/ScWindow.hpp | 2 +- src/openrct2/localisation/Language.cpp | 35 ++++++++++++++++++++++++++ src/openrct2/localisation/Language.h | 1 + 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/openrct2-ui/scripting/ScWidget.hpp b/src/openrct2-ui/scripting/ScWidget.hpp index b6aff6d54d..7d046a8a79 100644 --- a/src/openrct2-ui/scripting/ScWidget.hpp +++ b/src/openrct2-ui/scripting/ScWidget.hpp @@ -234,7 +234,7 @@ namespace OpenRCT2::Scripting auto widget = GetWidget(); if (widget != nullptr && (widget->flags & WIDGET_FLAGS::TEXT_IS_STRING) && widget->string != nullptr) { - return widget->string; + return language_convert_string_to_tokens(widget->string); } } return ""; diff --git a/src/openrct2-ui/scripting/ScWindow.hpp b/src/openrct2-ui/scripting/ScWindow.hpp index 33bffc2b7e..ee42b5532b 100644 --- a/src/openrct2-ui/scripting/ScWindow.hpp +++ b/src/openrct2-ui/scripting/ScWindow.hpp @@ -131,7 +131,7 @@ namespace OpenRCT2::Scripting auto w = GetWindow(); if (w != nullptr && w->classification == WC_CUSTOM) { - return GetWindowTitle(w); + return language_convert_string_to_tokens(GetWindowTitle(w)); } return {}; } diff --git a/src/openrct2/localisation/Language.cpp b/src/openrct2/localisation/Language.cpp index adde094cf3..9fa7b04f09 100644 --- a/src/openrct2/localisation/Language.cpp +++ b/src/openrct2/localisation/Language.cpp @@ -137,6 +137,41 @@ rct_string_id language_allocate_object_string(const std::string& target) return localisationService.AllocateObjectString(target); } +std::string language_convert_string_to_tokens(const std::string_view& s) +{ + std::string result; + result.reserve(s.size() * 4); + std::string input = std::string(s); + auto readPtr = input.c_str(); + while (true) + { + char32_t code = utf8_get_next(readPtr, (const utf8**)&readPtr); + if (code == 0) + { + break; + } + else if (code == '\n') + { + result.push_back('\n'); + } + else if (utf8_is_format_code(code)) + { + auto token = format_get_token(code); + result.push_back('{'); + result.append(token); + result.push_back('}'); + } + else + { + char buffer[8]{}; + utf8_write_codepoint(buffer, code); + result.append(buffer); + } + } + result.shrink_to_fit(); + return result; +} + std::string language_convert_string(const std::string_view& s) { enum class PARSE_STATE diff --git a/src/openrct2/localisation/Language.h b/src/openrct2/localisation/Language.h index ada81173cc..9a0d40886d 100644 --- a/src/openrct2/localisation/Language.h +++ b/src/openrct2/localisation/Language.h @@ -107,6 +107,7 @@ bool language_get_localised_scenario_strings(const utf8* scenarioFilename, rct_s void language_free_object_string(rct_string_id stringId); rct_string_id language_get_object_override_string_id(const char* identifier, uint8_t index); rct_string_id language_allocate_object_string(const std::string& target); +std::string language_convert_string_to_tokens(const std::string_view& s); std::string language_convert_string(const std::string_view& s); constexpr utf8* utf8_write_codepoint(utf8* dst, uint32_t codepoint)