Add message loading functionality and refactor message data handling

- Introduced yaze_load_messages function to load messages from ROM data, enhancing message management.
- Updated message data structures to use std::string_view for improved performance and clarity.
- Refactored message parsing logic to utilize modern C++ features like ranges and optional, improving readability.
- Adjusted various functions to streamline message handling and ensure consistency across the codebase.
This commit is contained in:
scawful
2025-08-03 17:55:11 -04:00
parent c9921c91bf
commit dacd9551f0
3 changed files with 56 additions and 29 deletions

View File

@@ -36,10 +36,10 @@ std::optional<TextElement> FindMatchingCommand(uint8_t b) {
}
std::optional<TextElement> FindMatchingSpecial(uint8_t value) {
auto it = std::find_if(SpecialChars.begin(), SpecialChars.end(),
[value](const TextElement &text_element) {
return text_element.ID == value;
});
auto it = std::ranges::find_if(SpecialChars,
[value](const TextElement &text_element) {
return text_element.ID == value;
});
if (it != SpecialChars.end()) {
return *it;
}
@@ -83,15 +83,15 @@ std::string ParseTextDataByte(uint8_t value) {
}
// Check for command.
auto text_element = FindMatchingCommand(value);
if (text_element != std::nullopt) {
if (auto text_element = FindMatchingCommand(value);
text_element != std::nullopt) {
return text_element->GenericToken;
}
// Check for special characters.
auto special_element = FindMatchingSpecial(value);
if (special_element != std::nullopt) {
return text_element->GenericToken;
if (auto special_element = FindMatchingSpecial(value);
special_element != std::nullopt) {
return special_element->GenericToken;
}
// Check for dictionary.
@@ -172,10 +172,10 @@ std::vector<DictionaryEntry> BuildDictionaryEntries(Rom *rom) {
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();
});
std::ranges::sort(AllDictionaries,
[](const DictionaryEntry &a, const DictionaryEntry &b) {
return a.Contents.size() > b.Contents.size();
});
return AllDictionaries;
}
@@ -319,6 +319,10 @@ std::vector<std::string> ParseMessageData(
parsed_message.append(text_element->GetParamToken());
}
}
auto special_element = FindMatchingSpecial(byte);
if (special_element != std::nullopt) {
parsed_message.append(special_element->GetParamToken());
}
}
}
pos++;

View File

@@ -12,9 +12,9 @@
namespace yaze {
namespace editor {
const uint8_t kMessageTerminator = 0x7F;
const std::string kBankToken = "BANK";
const std::string DICTIONARYTOKEN = "D";
constexpr uint8_t kMessageTerminator = 0x7F;
constexpr uint8_t DICTOFF = 0x88;
constexpr uint8_t kWidthArraySize = 100;
@@ -50,18 +50,18 @@ struct DictionaryEntry {
std::string Token = "";
DictionaryEntry() = default;
DictionaryEntry(uint8_t i, std::string s)
: Contents(s), ID(i), Length(s.length()) {
DictionaryEntry(uint8_t i, std::string_view s)
: ID(i), Contents(s), Length(s.length()) {
Token = absl::StrFormat("[%s:%02X]", DICTIONARYTOKEN, ID);
Data = ParseMessageToData(Contents);
}
bool ContainedInString(std::string s) const {
return s.find(Contents) != std::string::npos;
bool ContainedInString(std::string_view s) const {
return s.contains(Contents);
}
std::string ReplaceInstancesOfIn(std::string s) const {
std::string replaced_string = s;
std::string ReplaceInstancesOfIn(std::string_view s) const {
auto replaced_string = std::string(s);
size_t pos = replaced_string.find(Contents);
while (pos != std::string::npos) {
replaced_string.replace(pos, Contents.length(), Token);
@@ -105,9 +105,9 @@ struct MessageData {
: ID(id),
Address(address),
RawString(rawString),
ContentsParsed(parsedString),
Data(rawData),
DataParsed(parsedData),
ContentsParsed(parsedString) {}
DataParsed(parsedData) {}
// Copy constructor
MessageData(const MessageData &other) {
@@ -120,7 +120,7 @@ struct MessageData {
}
std::string OptimizeMessageForDictionary(
std::string message_string,
std::string_view message_string,
const std::vector<DictionaryEntry> &dictionary) {
std::stringstream protons;
bool command = false;
@@ -288,11 +288,8 @@ struct ParsedElement {
bool Active = false;
ParsedElement() = default;
ParsedElement(TextElement textElement, uint8_t value) {
Parent = textElement;
Value = value;
Active = true;
}
ParsedElement(const TextElement &textElement, uint8_t value)
: Parent(textElement), Value(value), Active(true) {}
};
ParsedElement FindMatchingElement(const std::string &str);

View File

@@ -6,6 +6,7 @@
#include "app/core/controller.h"
#include "app/core/platform/app_delegate.h"
#include "app/editor/message/message_data.h"
#include "app/rom.h"
#include "app/zelda3/overworld/overworld.h"
#include "util/flag.h"
@@ -94,7 +95,7 @@ zelda3_rom *yaze_load_rom(const char *filename) {
zelda3_rom *rom = new zelda3_rom();
rom->filename = filename;
rom->impl = internal_rom;
rom->data = internal_rom->data();
rom->data = const_cast<uint8_t *>(internal_rom->data());
rom->size = internal_rom->size();
return rom;
}
@@ -181,3 +182,28 @@ zelda3_dungeon_room *yaze_load_all_rooms(const zelda3_rom *rom) {
zelda3_dungeon_room *rooms = new zelda3_dungeon_room[256];
return rooms;
}
yaze_status yaze_load_messages(zelda3_rom *rom, zelda3_message **messages) {
if (rom->impl == nullptr) {
return yaze_status::YAZE_ERROR;
}
// Use LoadAllTextData from message_data.h
std::vector<yaze::editor::MessageData> message_data =
yaze::editor::ReadAllTextData(rom->data, 0);
for (const auto &message : message_data) {
messages[message.ID] = new zelda3_message();
messages[message.ID]->id = message.ID;
messages[message.ID]->address = message.Address;
messages[message.ID]->raw_string = reinterpret_cast<uint8_t *>(
const_cast<char *>(message.RawString.data()));
messages[message.ID]->contents_parsed = reinterpret_cast<uint8_t *>(
const_cast<char *>(message.ContentsParsed.data()));
messages[message.ID]->data =
reinterpret_cast<uint8_t *>(const_cast<uint8_t *>(message.Data.data()));
messages[message.ID]->data_parsed = reinterpret_cast<uint8_t *>(
const_cast<uint8_t *>(message.DataParsed.data()));
}
return yaze_status::YAZE_OK;
}