Refactor message data handling and improve variable naming for clarity

This commit is contained in:
scawful
2025-01-17 23:30:41 -05:00
parent 0cbaaccb66
commit 911cac401d
3 changed files with 114 additions and 86 deletions

View File

@@ -1,6 +1,7 @@
#include "message_data.h"
#include "app/core/common.h"
#include <string>
namespace yaze {
namespace editor {
@@ -45,14 +46,14 @@ TextElement FindMatchingSpecial(uint8_t value) {
ParsedElement FindMatchingElement(const std::string &str) {
std::smatch match;
for (auto &textElement : TextCommands) {
match = textElement.MatchMe(str);
for (auto &text_element : TextCommands) {
match = text_element.MatchMe(str);
if (match.size() > 0) {
if (textElement.HasArgument) {
return ParsedElement(textElement,
if (text_element.HasArgument) {
return ParsedElement(text_element,
std::stoi(match[1].str(), nullptr, 16));
} else {
return ParsedElement(textElement, 0);
return ParsedElement(text_element, 0);
}
}
}
@@ -77,15 +78,15 @@ std::string ParseTextDataByte(uint8_t value) {
}
// Check for command.
TextElement textElement = FindMatchingCommand(value);
if (!textElement.Empty()) {
return textElement.GenericToken;
TextElement text_element = FindMatchingCommand(value);
if (!text_element.Empty()) {
return text_element.GenericToken;
}
// Check for special characters.
textElement = FindMatchingSpecial(value);
if (!textElement.Empty()) {
return textElement.GenericToken;
text_element = FindMatchingSpecial(value);
if (!text_element.Empty()) {
return text_element.GenericToken;
}
// Check for dictionary.
@@ -176,9 +177,9 @@ std::vector<DictionaryEntry> BuildDictionaryEntries(Rom *rom) {
return AllDictionaries;
}
std::vector<std::string>
ParseMessageData(std::vector<MessageData> &message_data,
const std::vector<DictionaryEntry> &dictionary_entries) {
std::vector<std::string> ParseMessageData(
std::vector<MessageData> &message_data,
const std::vector<DictionaryEntry> &dictionary_entries) {
std::vector<std::string> parsed_messages;
for (auto &message : message_data) {
@@ -187,14 +188,17 @@ ParseMessageData(std::vector<MessageData> &message_data,
std::cout << " " << message.RawString << std::endl;
std::string parsed_message = "";
for (const uint8_t &byte : message.Data) {
if (CharEncoder.contains(byte)) {
parsed_message.push_back(CharEncoder.at(byte));
} else {
if (byte >= DICTOFF && byte < (DICTOFF + 97)) {
auto dic_entry = dictionary_entries[byte];
parsed_message.append(dic_entry.Contents);
if (byte > 0 && byte <= dictionary_entries.size()) {
auto dic_entry = dictionary_entries[byte];
parsed_message.append(dic_entry.Contents);
} else {
parsed_message.append(dictionary_entries[0].Contents);
}
} else {
auto text_element = FindMatchingCommand(byte);
if (!text_element.Empty()) {
@@ -213,5 +217,26 @@ ParseMessageData(std::vector<MessageData> &message_data,
return parsed_messages;
}
} // namespace editor
} // namespace yaze
std::vector<std::string> ImportMessageData(std::string_view filename) {
std::vector<std::string> messages;
std::ifstream file(filename.data());
if (!file.is_open()) {
core::logf("Error opening file: %s", filename);
return messages;
}
// Parse a file with dialogue IDs and convert
std::string line;
while (std::getline(file, line)) {
if (line.empty()) {
continue;
}
// Get the Dialogue ID and then read until the next header
}
return messages;
}
} // namespace editor
} // namespace yaze

View File

@@ -14,7 +14,7 @@ namespace yaze {
namespace editor {
const uint8_t kMessageTerminator = 0x7F;
const std::string BANKToken = "BANK";
const std::string kBankToken = "BANK";
const std::string DICTIONARYTOKEN = "D";
constexpr uint8_t DICTOFF = 0x88;
@@ -40,7 +40,6 @@ static const std::unordered_map<uint8_t, wchar_t> CharEncoder = {
uint8_t FindMatchingCharacter(char value);
uint8_t FindDictionaryEntry(uint8_t value);
std::vector<uint8_t> ParseMessageToData(std::string str);
struct DictionaryEntry {
@@ -62,13 +61,13 @@ struct DictionaryEntry {
}
std::string ReplaceInstancesOfIn(std::string s) const {
std::string replacedString = s;
size_t pos = replacedString.find(Contents);
std::string replaced_string = s;
size_t pos = replaced_string.find(Contents);
while (pos != std::string::npos) {
replacedString.replace(pos, Contents.length(), Token);
pos = replacedString.find(Contents, pos + Token.length());
replaced_string.replace(pos, Contents.length(), Token);
pos = replaced_string.find(Contents, pos + Token.length());
}
return replacedString;
return replaced_string;
}
};
@@ -82,7 +81,6 @@ constexpr uint8_t kLine2 = 0x75;
constexpr uint8_t kLine3 = 0x76;
std::vector<DictionaryEntry> BuildDictionaryEntries(Rom *rom);
std::string ReplaceAllDictionaryWords(std::string str,
std::vector<DictionaryEntry> dictionary);
@@ -120,11 +118,11 @@ struct MessageData {
}
std::string
OptimizeMessageForDictionary(std::string messageString,
OptimizeMessageForDictionary(std::string message_string,
const std::vector<DictionaryEntry> &dictionary) {
std::stringstream protons;
bool command = false;
for (const auto &c : messageString) {
for (const auto &c : message_string) {
if (c == '[') {
command = true;
} else if (c == ']') {
@@ -137,13 +135,13 @@ struct MessageData {
}
}
std::string protonsString = protons.str();
std::string replacedString =
ReplaceAllDictionaryWords(protonsString, dictionary);
std::string finalString =
absl::StrReplaceAll(replacedString, {{CHEESE, ""}});
std::string protons_string = protons.str();
std::string replaced_string =
ReplaceAllDictionaryWords(protons_string, dictionary);
std::string final_string =
absl::StrReplaceAll(replaced_string, {{CHEESE, ""}});
return finalString;
return final_string;
}
void SetMessage(const std::string &message,
@@ -153,8 +151,6 @@ struct MessageData {
}
};
struct TextElement {
uint8_t ID;
std::string Token;
@@ -183,7 +179,7 @@ struct TextElement {
StrictPattern = "^" + Pattern + "$";
}
std::string GetParameterizedToken(uint8_t value = 0) const {
std::string GetParamToken(uint8_t value = 0) const {
if (HasArgument) {
return absl::StrFormat("[%s:%02X]", Token, value);
} else {
@@ -252,7 +248,7 @@ static const std::vector<TextElement> TextCommands = {
TextElement(0x68, "CH2I", false, kChoose2Indented),
TextElement(0x69, "CHI", false, kChooseItem),
TextElement(0x67, "IMG", false, kNextAttractImage),
TextElement(0x80, BANKToken, false, kBankMarker),
TextElement(0x80, kBankToken, false, kBankMarker),
TextElement(0x70, "NONO", false, kCrash),
};
@@ -305,6 +301,8 @@ std::vector<std::string>
ParseMessageData(std::vector<MessageData> &message_data,
const std::vector<DictionaryEntry> &dictionary_entries);
std::vector<std::string> ImportMessageData(std::string_view filename);
} // namespace editor
} // namespace yaze

View File

@@ -44,21 +44,22 @@ constexpr ImGuiTableFlags kDictTableFlags =
ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable;
absl::Status MessageEditor::Initialize() {
std::copy(rom()->vector().begin() + kCharactersWidth,
rom()->vector().begin() + kCharactersWidth + kWidthArraySize,
width_array.begin());
for (int i = 0; i < kWidthArraySize; i++) {
width_array[i] = rom()->data()[kCharactersWidth + i];
}
all_dictionaries_ = BuildDictionaryEntries(rom());
ReadAllTextDataV2();
font_preview_colors_.AddColor(0x7FFF); // White
font_preview_colors_.AddColor(0x7C00); // Red
font_preview_colors_.AddColor(0x03E0); // Green
font_preview_colors_.AddColor(0x001F); // Blue
font_preview_colors_.AddColor(0x7FFF); // White
font_preview_colors_.AddColor(0x7C00); // Red
font_preview_colors_.AddColor(0x03E0); // Green
font_preview_colors_.AddColor(0x001F); // Blue
std::vector<uint8_t> data(0x4000, 0);
std::copy(rom()->vector().begin() + kGfxFont,
rom()->vector().begin() + kGfxFont + 0x4000, data.begin());
for (int i = 0; i < 0x4000; i++) {
data[i] = rom()->data()[kGfxFont + i];
}
font_gfx16_data_ = gfx::SnesTo8bppSheet(data, /*bpp=*/2, /*num_sheets=*/2);
// 4bpp
@@ -235,23 +236,31 @@ void MessageEditor::ReadAllTextDataV2() {
while (current_byte != 0xFF) {
current_byte = rom()->data()[pos++];
if (current_byte == kMessageTerminator) {
auto message =
list_of_texts_.push_back(
MessageData(message_id++, pos, current_raw_message, raw_message,
current_parsed_message, parsed_message);
list_of_texts_.push_back(message);
current_parsed_message, parsed_message));
std::cout << "Message ID: " << message_id << std::endl;
std::cout << "Raw: " << current_raw_message << std::endl;
std::cout << "Parsed: " << current_parsed_message << std::endl;
std::cout << "Raw Bytes: ";
for (const auto &byte : raw_message) {
std::cout << core::HexByte(byte) << " ";
}
std::cout << std::endl;
std::cout << "Parsed Bytes: ";
for (const auto &byte : parsed_message) {
std::cout << core::HexByte(byte) << " ";
}
std::cout << std::endl;
raw_message.clear();
parsed_message.clear();
current_raw_message.clear();
current_parsed_message.clear();
continue;
}
raw_message.push_back(current_byte);
// Check for command.
TextElement text_element = FindMatchingCommand(current_byte);
if (!text_element.Empty()) {
parsed_message.push_back(current_byte);
@@ -261,12 +270,10 @@ void MessageEditor::ReadAllTextDataV2() {
parsed_message.push_back(current_byte);
}
current_raw_message.append(
text_element.GetParameterizedToken(current_byte));
current_parsed_message.append(
text_element.GetParameterizedToken(current_byte));
current_raw_message.append(text_element.GetParamToken(current_byte));
current_parsed_message.append(text_element.GetParamToken(current_byte));
if (text_element.Token == BANKToken) {
if (text_element.Token == kBankToken) {
pos = kTextData2;
}
@@ -276,8 +283,8 @@ void MessageEditor::ReadAllTextDataV2() {
// Check for special characters.
text_element = FindMatchingSpecial(current_byte);
if (!text_element.Empty()) {
current_raw_message.append(text_element.GetParameterizedToken());
current_parsed_message.append(text_element.GetParameterizedToken());
current_raw_message.append(text_element.GetParamToken());
current_parsed_message.append(text_element.GetParamToken());
parsed_message.push_back(current_byte);
continue;
}
@@ -293,9 +300,9 @@ void MessageEditor::ReadAllTextDataV2() {
uint32_t address = core::Get24LocalFromPC(
rom()->mutable_data(), kPointersDictionaries + (dictionary * 2));
uint32_t address_end = core::Get24LocalFromPC(rom()->mutable_data(),
kPointersDictionaries +
((dictionary + 1) * 2));
uint32_t address_end = core::Get24LocalFromPC(
rom()->mutable_data(),
kPointersDictionaries + ((dictionary + 1) * 2));
for (uint32_t i = address; i < address_end; i++) {
parsed_message.push_back(rom()->data()[i]);
@@ -360,12 +367,10 @@ void MessageEditor::ReadAllTextData() {
temp_bytes_parsed.push_back(current_byte);
}
current_message_raw.append(
text_element.GetParameterizedToken(current_byte));
current_message_parsed.append(
text_element.GetParameterizedToken(current_byte));
current_message_raw.append(text_element.GetParamToken(current_byte));
current_message_parsed.append(text_element.GetParamToken(current_byte));
if (text_element.Token == BANKToken) {
if (text_element.Token == kBankToken) {
pos = kTextData2;
}
@@ -375,8 +380,8 @@ void MessageEditor::ReadAllTextData() {
// Check for special characters.
text_element = FindMatchingSpecial(current_byte);
if (!text_element.Empty()) {
current_message_raw.append(text_element.GetParameterizedToken());
current_message_parsed.append(text_element.GetParameterizedToken());
current_message_raw.append(text_element.GetParamToken());
current_message_parsed.append(text_element.GetParamToken());
temp_bytes_parsed.push_back(current_byte);
continue;
}
@@ -393,9 +398,9 @@ void MessageEditor::ReadAllTextData() {
uint32_t address = core::Get24LocalFromPC(
rom()->mutable_data(), kPointersDictionaries + (dictionary * 2));
uint32_t address_end = core::Get24LocalFromPC(rom()->mutable_data(),
kPointersDictionaries +
((dictionary + 1) * 2));
uint32_t address_end = core::Get24LocalFromPC(
rom()->mutable_data(),
kPointersDictionaries + ((dictionary + 1) * 2));
for (uint32_t i = address; i < address_end; i++) {
temp_bytes_parsed.push_back(rom()->data()[i]);
@@ -437,7 +442,7 @@ DictionaryEntry MessageEditor::GetDictionaryFromID(uint8_t value) {
void MessageEditor::DrawTileToPreview(int x, int y, int srcx, int srcy, int pal,
int sizex, int sizey) {
const int num_x_tiles = 16;
const int img_width = 512; // (imgwidth/2)
const int img_width = 512; // (imgwidth/2)
int draw_id = srcx + (srcy * 32);
for (int yl = 0; yl < sizey * 8; yl++) {
for (int xl = 0; xl < 4; xl++) {
@@ -512,7 +517,7 @@ void MessageEditor::DrawCharacterToPreview(const std::vector<uint8_t> &text) {
skip_next = true;
continue;
} else if (value == 0x6C) // BCD numbers.
} else if (value == 0x6C) // BCD numbers.
{
DrawCharacterToPreview('0');
skip_next = true;
@@ -582,7 +587,7 @@ absl::Status MessageEditor::Undo() {
absl::Status MessageEditor::Save() {
std::vector<uint8_t> backup = rom()->vector();
for (int i = 0; i < 100; i++) {
for (int i = 0; i < kWidthArraySize; i++) {
RETURN_IF_ERROR(rom()->WriteByte(kCharactersWidth + i, width_array[i]));
}
@@ -609,7 +614,7 @@ absl::Status MessageEditor::Save() {
}
RETURN_IF_ERROR(rom()->WriteByte(
pos++, kMessageTerminator)); // , true, "Terminator text"
pos++, kMessageTerminator)); // , true, "Terminator text"
}
// Verify that we didn't go over the space available for the second block.
@@ -619,7 +624,7 @@ absl::Status MessageEditor::Save() {
return absl::InternalError(DisplayTextOverflowError(pos, false));
}
RETURN_IF_ERROR(rom()->WriteByte(pos, 0xFF)); // , true, "End of text"
RETURN_IF_ERROR(rom()->WriteByte(pos, 0xFF)); // , true, "End of text"
return absl::OkStatus();
}
@@ -630,10 +635,10 @@ std::string MessageEditor::DisplayTextOverflowError(int pos, bool bank) {
std::string posSTR =
bank ? absl::StrFormat("%X4", pos & 0xFFFF)
: absl::StrFormat("%X4", (pos - kTextData2) & 0xFFFF);
std::string message =
absl::StrFormat("There is too much text data in the %s block to save.\n"
"Available: %X4 | Used: %s",
bankSTR, space, posSTR);
std::string message = absl::StrFormat(
"There is too much text data in the %s block to save.\n"
"Available: %X4 | Used: %s",
bankSTR, space, posSTR);
return message;
}
@@ -656,5 +661,5 @@ void MessageEditor::SelectAll() {
}
}
} // namespace editor
} // namespace yaze
} // namespace editor
} // namespace yaze