port text.hpp to custom-message as text.cpp/text.h (#6541)

remove unused logic, leaving Text pretty bare bones
This commit is contained in:
Philip Dubé 2026-04-20 18:37:05 +00:00 committed by GitHub
parent 461cc0930f
commit 94a5311cba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 138 additions and 174 deletions

View file

@ -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

View file

@ -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());
}

View file

@ -0,0 +1,33 @@
#pragma once
#include <string>
#include <stdint.h>
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 = "";
};

View file

@ -4,7 +4,7 @@
#include <vector>
#include <variant>
#include "text.hpp"
#include "soh/Enhancements/custom-message/text.h"
#include "random.hpp"
#include <functional>
#include "../randomizerTypes.h"

View file

@ -1,166 +0,0 @@
#pragma once
#include <string>
#include <stdint.h>
#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 = "";
};

View file

@ -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"

View file

@ -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 {

View file

@ -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"

View file

@ -4,7 +4,7 @@
#include <variant>
#include <memory>
#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"

View file

@ -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"

View file

@ -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.