From 94a5311cba0be1af82c8cca6ac46d92c1be2a487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philip=20Dub=C3=A9?= <159546+serprex@users.noreply.github.com> Date: Mon, 20 Apr 2026 18:37:05 +0000 Subject: [PATCH] port text.hpp to custom-message as text.cpp/text.h (#6541) remove unused logic, leaving Text pretty bare bones --- .../custom-message/CustomMessageManager.h | 2 +- soh/soh/Enhancements/custom-message/text.cpp | 97 ++++++++++ soh/soh/Enhancements/custom-message/text.h | 33 ++++ .../Enhancements/randomizer/3drando/hints.hpp | 2 +- .../Enhancements/randomizer/3drando/text.hpp | 166 ------------------ soh/soh/Enhancements/randomizer/SeedContext.h | 2 +- soh/soh/Enhancements/randomizer/Traps.h | 2 +- soh/soh/Enhancements/randomizer/hint.h | 2 +- soh/soh/Enhancements/randomizer/item.h | 2 +- .../Enhancements/randomizer/item_location.h | 2 +- .../Enhancements/randomizer/item_override.h | 2 +- 11 files changed, 138 insertions(+), 174 deletions(-) create mode 100644 soh/soh/Enhancements/custom-message/text.cpp create mode 100644 soh/soh/Enhancements/custom-message/text.h delete mode 100644 soh/soh/Enhancements/randomizer/3drando/text.hpp diff --git a/soh/soh/Enhancements/custom-message/CustomMessageManager.h b/soh/soh/Enhancements/custom-message/CustomMessageManager.h index a762354f6..d69f81ea6 100644 --- a/soh/soh/Enhancements/custom-message/CustomMessageManager.h +++ b/soh/soh/Enhancements/custom-message/CustomMessageManager.h @@ -8,7 +8,7 @@ #include "../../../include/z64item.h" #include "../../../include/z64.h" #include "../../../include/message_data_textbox_types.h" -#include "../randomizer/3drando/text.hpp" +#include "text.h" #undef MESSAGE_END diff --git a/soh/soh/Enhancements/custom-message/text.cpp b/soh/soh/Enhancements/custom-message/text.cpp new file mode 100644 index 000000000..267f3339e --- /dev/null +++ b/soh/soh/Enhancements/custom-message/text.cpp @@ -0,0 +1,97 @@ +#include "text.h" + +Text::Text() = default; + +Text::Text(std::string english_, std::string french_, std::string german_) + : english(std::move(english_)), french(std::move(french_)), german(std::move(german_)), spanish("") { + spanish = english; +} + +Text::Text(std::string english_, std::string french_, std::string german_, std::string spanish_) + : english(std::move(english_)), french(std::move(french_)), german(std::move(german_)), + spanish(std::move(spanish_)) { +} + +Text::Text(std::string english_) : english(std::move(english_)), french(""), german(""), spanish("") { + french = spanish = german = english; +} + +const std::string& Text::GetEnglish() const { + return english; +} + +const std::string& Text::GetFrench() const { + return french.length() > 0 ? french : english; +} + +const std::string& Text::GetGerman() const { + return german.length() > 0 ? german : english; +} + +const std::string& Text::GetSpanish() const { + return spanish.length() > 0 ? spanish : english; +} + +const std::string& Text::GetForLanguage(uint8_t language) const { + switch (language) { + case 0: + return GetEnglish(); + case 2: + return GetFrench(); + case 1: + return GetGerman(); + default: + return GetEnglish(); + } +} + +Text Text::operator+(const Text& right) const { + return Text{ + english + right.GetEnglish(), + french + right.GetFrench(), + german + right.GetGerman(), + spanish + right.GetSpanish(), + }; +} + +Text Text::operator+(const std::string& right) const { + return Text{ + english + right, + french + right, + german + right, + spanish + right, + }; +} + +bool Text::operator==(const Text& right) const { + return english == right.english; +} + +bool Text::operator==(const std::string& right) const { + return english == right || french == right || german == right || spanish == right; +} + +bool Text::operator!=(const Text& right) const { + return !operator==(right); +} + +static void replaceAll(std::string& target, const std::string& oldStr, const std::string& replacement) { + size_t position = target.find(oldStr); + while (position != std::string::npos) { + target.replace(position, oldStr.length(), replacement); + position = target.find(oldStr); + } +} + +void Text::Replace(const std::string& oldStr, const std::string& newStr) { + for (std::string& str : { std::ref(english), std::ref(french), std::ref(german), std::ref(spanish) }) { + replaceAll(str, oldStr, newStr); + } +} + +void Text::Replace(const std::string& oldStr, const Text& newText) { + replaceAll(english, oldStr, newText.GetEnglish()); + replaceAll(french, oldStr, newText.GetFrench()); + replaceAll(german, oldStr, newText.GetGerman()); + replaceAll(spanish, oldStr, newText.GetSpanish()); +} diff --git a/soh/soh/Enhancements/custom-message/text.h b/soh/soh/Enhancements/custom-message/text.h new file mode 100644 index 000000000..3cca37593 --- /dev/null +++ b/soh/soh/Enhancements/custom-message/text.h @@ -0,0 +1,33 @@ +#pragma once + +#include +#include + +class Text { + public: + Text(); + Text(std::string english_, std::string french_, std::string german_); + Text(std::string english_, std::string french_, std::string german_, std::string spanish_); + explicit Text(std::string english_); + + const std::string& GetEnglish() const; + const std::string& GetFrench() const; + const std::string& GetGerman() const; + const std::string& GetSpanish() const; + const std::string& GetForLanguage(uint8_t language) const; + + Text operator+(const Text& right) const; + Text operator+(const std::string& right) const; + + bool operator==(const Text& right) const; + bool operator==(const std::string& right) const; + bool operator!=(const Text& right) const; + + void Replace(const std::string& oldStr, const std::string& newStr); + void Replace(const std::string& oldStr, const Text& newText); + + std::string english = ""; + std::string french = ""; + std::string german = ""; + std::string spanish = ""; +}; diff --git a/soh/soh/Enhancements/randomizer/3drando/hints.hpp b/soh/soh/Enhancements/randomizer/3drando/hints.hpp index 365904274..812441160 100644 --- a/soh/soh/Enhancements/randomizer/3drando/hints.hpp +++ b/soh/soh/Enhancements/randomizer/3drando/hints.hpp @@ -4,7 +4,7 @@ #include #include -#include "text.hpp" +#include "soh/Enhancements/custom-message/text.h" #include "random.hpp" #include #include "../randomizerTypes.h" diff --git a/soh/soh/Enhancements/randomizer/3drando/text.hpp b/soh/soh/Enhancements/randomizer/3drando/text.hpp deleted file mode 100644 index 6e53ac11b..000000000 --- a/soh/soh/Enhancements/randomizer/3drando/text.hpp +++ /dev/null @@ -1,166 +0,0 @@ -#pragma once - -#include -#include - -#define PLURAL 0 -#define SINGULAR 1 - -class Text { - public: - Text() = default; - Text(std::string english_, std::string french_, std::string german_) - : english(std::move(english_)), french(std::move(french_)), german(std::move(german_)), spanish(std::move("")) { - // spanish defaults to english text until a translation is provided. - spanish = english; - } - Text(std::string english_, std::string french_, std::string german_, std::string spanish_) - : english(std::move(english_)), french(std::move(french_)), german(std::move(german_)), spanish(std::move("")) { - } - Text(std::string english_) - : english(std::move(english_)), french(std::move("")), german(std::move("")), spanish(std::move("")) { - // default unprovided languages to english text - french = spanish = german = english; - } - - const std::string& GetEnglish() const { - return english; - } - - const std::string& GetFrench() const { - if (french.length() > 0) { - return french; - } - return english; - } - - const std::string& GetGerman() const { - if (german.length() > 0) { - return german; - } - return english; - } - const std::string& GetSpanish() const { - if (spanish.length() > 0) { - return spanish; - } - return english; - } - - const std::string& GetForLanguage(uint8_t language) const { - switch (language) { - case 0: // LANGUAGE_ENG: changed to resolve #include loops - return GetEnglish(); - case 2: // LANGUAGE_FRA: - return GetFrench(); - case 1: // LANGUAGE_GER: - return GetGerman(); - default: - return GetEnglish(); - } - } - - Text operator+(const Text& right) const { - return Text{ - english + right.GetEnglish(), - french + right.GetFrench(), - german + right.GetGerman(), - spanish + right.GetSpanish(), - }; - } - - Text operator+(const std::string& right) const { - return Text{ - english + right, - french + right, - german + right, - spanish + right, - }; - } - - bool operator==(const Text& right) const { - return english == right.english; - } - - bool operator==(const std::string& right) const { - return english == right || french == right || german == right || spanish == right; - } - - bool operator!=(const Text& right) const { - return !operator==(right); - } - - void Replace(std::string oldStr, std::string newStr) { - - for (std::string* str : { &english, &french, &german, &spanish }) { - size_t position = str->find(oldStr); - while (position != std::string::npos) { - str->replace(position, oldStr.length(), newStr); - position = str->find(oldStr); - } - } - } - - void Replace(std::string oldStr, Text newText) { - size_t position = english.find(oldStr); - while (position != std::string::npos) { - english.replace(position, oldStr.length(), newText.GetEnglish()); - position = english.find(oldStr); - } - position = french.find(oldStr); - while (position != std::string::npos) { - french.replace(position, oldStr.length(), newText.GetFrench()); - position = french.find(oldStr); - } - position = german.find(oldStr); - while (position != std::string::npos) { - german.replace(position, oldStr.length(), newText.GetGerman()); - position = german.find(oldStr); - } - position = spanish.find(oldStr); - while (position != std::string::npos) { - spanish.replace(position, oldStr.length(), newText.GetSpanish()); - position = spanish.find(oldStr); - } - } - - // Convert first char to upper case - Text Capitalize(void) const { - Text cap = *this + ""; - for (std::string* str : { &cap.english, &cap.french, &cap.german, &cap.spanish }) { - (*str)[0] = std::toupper((*str)[0]); - } - return cap; - } - - // find the appropriate bars that separate singular from plural - void SetForm(int form) { - for (std::string* str : { &english, &french, &german, &spanish }) { - - size_t firstBar = str->find('|'); - if (firstBar != std::string::npos) { - - size_t secondBar = str->find('|', firstBar + 1); - if (secondBar != std::string::npos) { - - size_t thirdBar = str->find('|', secondBar + 1); - if (thirdBar != std::string::npos) { - - if (form == SINGULAR) { - str->erase(secondBar, thirdBar - secondBar); - } else { - str->erase(firstBar, secondBar - firstBar); - } - } - } - } - } - // remove the remaining bar - this->Replace("|", ""); - } - - std::string english = ""; - std::string french = ""; - std::string german = ""; - std::string spanish = ""; -}; diff --git a/soh/soh/Enhancements/randomizer/SeedContext.h b/soh/soh/Enhancements/randomizer/SeedContext.h index 9ca42751a..46b117616 100644 --- a/soh/soh/Enhancements/randomizer/SeedContext.h +++ b/soh/soh/Enhancements/randomizer/SeedContext.h @@ -4,7 +4,7 @@ #include "z64save.h" #include "item_location.h" #include "item_override.h" -#include "3drando/text.hpp" +#include "soh/Enhancements/custom-message/text.h" #include "hint.h" #include "fishsanity.h" #include "trial.h" diff --git a/soh/soh/Enhancements/randomizer/Traps.h b/soh/soh/Enhancements/randomizer/Traps.h index d8f29093d..cf8f8905c 100644 --- a/soh/soh/Enhancements/randomizer/Traps.h +++ b/soh/soh/Enhancements/randomizer/Traps.h @@ -6,7 +6,7 @@ #include "soh/Enhancements/custom-message/CustomMessageManager.h" #include "soh/Enhancements/randomizer/randomizerTypes.h" -#include "soh/Enhancements/randomizer/3drando/text.hpp" +#include "soh/Enhancements/custom-message/text.h" #include "libultraship/libultra/types.h" namespace Rando { diff --git a/soh/soh/Enhancements/randomizer/hint.h b/soh/soh/Enhancements/randomizer/hint.h index 7ff46a104..3c820bcdb 100644 --- a/soh/soh/Enhancements/randomizer/hint.h +++ b/soh/soh/Enhancements/randomizer/hint.h @@ -1,6 +1,6 @@ #pragma once -#include "3drando/text.hpp" +#include "soh/Enhancements/custom-message/text.h" #include "3drando/hints.hpp" #include "../custom-message/CustomMessageManager.h" #include "randomizerTypes.h" diff --git a/soh/soh/Enhancements/randomizer/item.h b/soh/soh/Enhancements/randomizer/item.h index ab4b3c119..3c8e9744a 100644 --- a/soh/soh/Enhancements/randomizer/item.h +++ b/soh/soh/Enhancements/randomizer/item.h @@ -4,7 +4,7 @@ #include #include -#include "3drando/text.hpp" +#include "soh/Enhancements/custom-message/text.h" #include "randomizerTypes.h" #include "soh/Enhancements/item-tables/ItemTableTypes.h" #include "3drando/hints.hpp" diff --git a/soh/soh/Enhancements/randomizer/item_location.h b/soh/soh/Enhancements/randomizer/item_location.h index a04f5c2cf..54e3ea55c 100644 --- a/soh/soh/Enhancements/randomizer/item_location.h +++ b/soh/soh/Enhancements/randomizer/item_location.h @@ -1,7 +1,7 @@ #pragma once #include "randomizerTypes.h" -#include "3drando/text.hpp" +#include "soh/Enhancements/custom-message/text.h" #include "static_data.h" #include "option.h" diff --git a/soh/soh/Enhancements/randomizer/item_override.h b/soh/soh/Enhancements/randomizer/item_override.h index 355d1e51e..8ab34c0ad 100644 --- a/soh/soh/Enhancements/randomizer/item_override.h +++ b/soh/soh/Enhancements/randomizer/item_override.h @@ -1,7 +1,7 @@ #pragma once #include "randomizerTypes.h" -#include "3drando/text.hpp" +#include "soh/Enhancements/custom-message/text.h" namespace Rando { /// @brief Class representing overrides of individual items. Used for trick names and models for ice traps.