From 2857eca92d021d60cb23f5db483ab0887cccaf3a Mon Sep 17 00:00:00 2001 From: scawful Date: Tue, 17 Sep 2024 08:42:36 -0400 Subject: [PATCH] move DictionaryEntry struct, make all_dictionaries_ local to MessageEditor insteaad of static --- src/app/editor/message/message_data.h | 56 +++++++++++++++++++++--- src/app/editor/message/message_editor.cc | 48 +++++--------------- src/app/editor/message/message_editor.h | 37 +--------------- 3 files changed, 61 insertions(+), 80 deletions(-) diff --git a/src/app/editor/message/message_data.h b/src/app/editor/message/message_data.h index f6f80c5e..4934d057 100644 --- a/src/app/editor/message/message_data.h +++ b/src/app/editor/message/message_data.h @@ -8,6 +8,7 @@ #include "absl/strings/str_cat.h" #include "absl/strings/str_format.h" #include "absl/strings/str_replace.h" +#include "app/rom.h" namespace yaze { namespace app { @@ -41,7 +42,46 @@ static const std::unordered_map CharEncoder = { uint8_t FindMatchingCharacter(char value); uint8_t FindDictionaryEntry(uint8_t value); -std::string ReplaceAllDictionaryWords(std::string str); +std::vector ParseMessageToData(std::string str); + +struct DictionaryEntry { + uint8_t ID; + std::string Contents; + std::vector Data; + int Length; + std::string Token; + + DictionaryEntry() = default; + DictionaryEntry(uint8_t i, std::string s) + : Contents(s), ID(i), Length(s.length()) { + Token = absl::StrFormat("[%s:%00X]", DICTIONARYTOKEN, ID); + Data = ParseMessageToData(Contents); + } + + bool ContainedInString(std::string s) { + return s.find(Contents) != std::string::npos; + } + + std::string ReplaceInstancesOfIn(std::string s) { + std::string replacedString = s; + size_t pos = replacedString.find(Contents); + while (pos != std::string::npos) { + replacedString.replace(pos, Contents.length(), Token); + pos = replacedString.find(Contents, pos + Token.length()); + } + return replacedString; + } +}; + +constexpr int kTextData = 0xE0000; +constexpr int kTextDataEnd = 0xE7FFF; +constexpr int kNumDictionaryEntries = 97; +constexpr int kPointersDictionaries = 0x74703; + +std::vector BuildDictionaryEntries(app::Rom* rom); + +std::string ReplaceAllDictionaryWords(std::string str, + std::vector dictionary); // Inserted into commands to protect them from dictionary replacements. const std::string CHEESE = "\uBEBE"; @@ -80,7 +120,9 @@ struct MessageData { return absl::StrFormat("%0X - %s", ID, ContentsParsed); } - std::string OptimizeMessageForDictionary(std::string messageString) { + std::string OptimizeMessageForDictionary( + std::string messageString, + const std::vector& dictionary) { std::stringstream protons; bool command = false; for (const auto& c : messageString) { @@ -97,16 +139,18 @@ struct MessageData { } std::string protonsString = protons.str(); - std::string replacedString = ReplaceAllDictionaryWords(protonsString); + std::string replacedString = + ReplaceAllDictionaryWords(protonsString, dictionary); std::string finalString = absl::StrReplaceAll(replacedString, {{CHEESE, ""}}); return finalString; } - void SetMessage(const std::string& message) { + void SetMessage(const std::string& message, + const std::vector& dictionary) { RawString = message; - ContentsParsed = OptimizeMessageForDictionary(message); + ContentsParsed = OptimizeMessageForDictionary(message, dictionary); } }; @@ -233,8 +277,6 @@ ParsedElement FindMatchingElement(const std::string& str); std::string ParseTextDataByte(uint8_t value); -std::vector ParseMessageToData(std::string str); - } // namespace editor } // namespace app } // namespace yaze diff --git a/src/app/editor/message/message_editor.cc b/src/app/editor/message/message_editor.cc index 3093ba15..b5b4cb87 100644 --- a/src/app/editor/message/message_editor.cc +++ b/src/app/editor/message/message_editor.cc @@ -11,7 +11,6 @@ #include "absl/strings/str_format.h" #include "absl/strings/str_replace.h" #include "absl/strings/str_split.h" - #include "app/core/platform/renderer.h" #include "app/editor/utils/editor.h" #include "app/gfx/bitmap.h" @@ -48,7 +47,7 @@ absl::Status MessageEditor::Initialize() { width_array[i] = rom()->data()[kCharactersWidth + i]; } - BuildDictionaryEntries(); + all_dictionaries_ = BuildDictionaryEntries(rom()); ReadAllTextDataV2(); font_preview_colors_.AddColor(0x7FFF); // White @@ -66,7 +65,8 @@ absl::Status MessageEditor::Initialize() { RETURN_IF_ERROR(Renderer::GetInstance().CreateAndRenderBitmap( 128, 128, 8, font_gfx16_data_, font_gfx_bitmap_, font_preview_colors_)) - current_font_gfx16_data_.reserve(kCurrentMessageWidth * kCurrentMessageHeight); + current_font_gfx16_data_.reserve(kCurrentMessageWidth * + kCurrentMessageHeight); for (int i = 0; i < kCurrentMessageWidth * kCurrentMessageHeight; i++) { current_font_gfx16_data_.push_back(0); } @@ -153,7 +153,7 @@ absl::Status MessageEditor::Update() { TableSetupColumn("ID"); TableSetupColumn("Contents"); - for (const auto& dictionary : AllDictionaries) { + for (const auto& dictionary : all_dictionaries_) { TableNextColumn(); Text("%s", core::UppercaseHexWord(dictionary.ID).c_str()); TableNextColumn(); @@ -259,34 +259,6 @@ void MessageEditor::DrawTextCommands() { } } -void MessageEditor::BuildDictionaryEntries() { - for (int i = 0; i < kNumDictionaryEntries; i++) { - std::vector bytes; - std::stringstream stringBuilder; - - int address = core::SnesToPc( - kTextData + (rom()->data()[kPointersDictionaries + (i * 2) + 1] << 8) + - rom()->data()[kPointersDictionaries + (i * 2)]); - - int temppush_backress = core::SnesToPc( - kTextData + - (rom()->data()[kPointersDictionaries + ((i + 1) * 2) + 1] << 8) + - rom()->data()[kPointersDictionaries + ((i + 1) * 2)]); - - while (address < temppush_backress) { - uint8_t uint8_tDictionary = rom()->data()[address++]; - bytes.push_back(uint8_tDictionary); - stringBuilder << ParseTextDataByte(uint8_tDictionary); - } - - AllDictionaries.push_back(DictionaryEntry{(uint8_t)i, stringBuilder.str()}); - } - - std::sort(AllDictionaries.begin(), AllDictionaries.end(), - [](const DictionaryEntry& a, const DictionaryEntry& b) { - return a.Contents.size() > b.Contents.size(); - }); -} // TODO: Fix the command parsing. void MessageEditor::ReadAllTextDataV2() { @@ -484,9 +456,10 @@ void MessageEditor::ReadAllTextData() { } } -std::string ReplaceAllDictionaryWords(std::string str) { +std::string ReplaceAllDictionaryWords(std::string str, + std::vector dictionary) { std::string temp = str; - for (const auto& entry : AllDictionaries) { + for (const auto& entry : dictionary) { if (absl::StrContains(temp, entry.Contents)) { temp = absl::StrReplaceAll(temp, {{entry.Contents, entry.Contents}}); } @@ -494,12 +467,11 @@ std::string ReplaceAllDictionaryWords(std::string str) { return temp; } -MessageEditor::DictionaryEntry MessageEditor::GetDictionaryFromID( - uint8_t value) { - if (value < 0 || value >= AllDictionaries.size()) { +DictionaryEntry MessageEditor::GetDictionaryFromID(uint8_t value) { + if (value < 0 || value >= all_dictionaries_.size()) { return DictionaryEntry(); } - return AllDictionaries[value]; + return all_dictionaries_[value]; } void MessageEditor::DrawTileToPreview(int x, int y, int srcx, int srcy, int pal, diff --git a/src/app/editor/message/message_editor.h b/src/app/editor/message/message_editor.h index 0e12f2be..df611fd1 100644 --- a/src/app/editor/message/message_editor.h +++ b/src/app/editor/message/message_editor.h @@ -17,13 +17,9 @@ namespace app { namespace editor { constexpr int kGfxFont = 0x70000; // 2bpp format -constexpr int kTextData = 0xE0000; -constexpr int kTextDataEnd = 0xE7FFF; constexpr int kTextData2 = 0x75F40; constexpr int kTextData2End = 0x773FF; -constexpr int kPointersDictionaries = 0x74703; constexpr int kCharactersWidth = 0x74ADF; -constexpr int kNumDictionaryEntries = 97; constexpr int kNumMessages = 396; constexpr int kCurrentMessageWidth = 172; constexpr int kCurrentMessageHeight = 4096; @@ -51,7 +47,6 @@ class MessageEditor : public Editor, public SharedRom { void ReadAllTextDataV2(); [[deprecated]] void ReadAllTextData(); - void BuildDictionaryEntries(); absl::Status Cut() override; absl::Status Copy() override; @@ -67,34 +62,6 @@ class MessageEditor : public Editor, public SharedRom { void Delete(); void SelectAll(); - struct DictionaryEntry { - uint8_t ID; - std::string Contents; - std::vector Data; - int Length; - std::string Token; - - DictionaryEntry() = default; - DictionaryEntry(uint8_t i, std::string s) - : Contents(s), ID(i), Length(s.length()) { - Token = absl::StrFormat("[%s:%00X]", DICTIONARYTOKEN, ID); - Data = ParseMessageToData(Contents); - } - - bool ContainedInString(std::string s) { - return s.find(Contents) != std::string::npos; - } - - std::string ReplaceInstancesOfIn(std::string s) { - std::string replacedString = s; - size_t pos = replacedString.find(Contents); - while (pos != std::string::npos) { - replacedString.replace(pos, Contents.length(), Token); - pos = replacedString.find(Contents, pos + Token.length()); - } - return replacedString; - } - }; DictionaryEntry GetDictionaryFromID(uint8_t value); void DrawTileToPreview(int x, int y, int srcx, int srcy, int pal, int sizex = 1, int sizey = 1); @@ -123,6 +90,8 @@ class MessageEditor : public Editor, public SharedRom { std::vector list_of_texts_; + std::vector all_dictionaries_; + MessageData current_message_; gfx::Bitmap font_gfx_bitmap_; @@ -191,8 +160,6 @@ class MessageEditor : public Editor, public SharedRom { TextBox message_text_box_; }; -static std::vector AllDictionaries; - } // namespace editor } // namespace app } // namespace yaze